Keycap Press
Two 3D keycaps press Cmd and K under the line Everything, one shortcut away.
Source
components/kuchi/keycap-press.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/BricolageGrotesque";4import type React from "react";5import {6 AbsoluteFill,7 Easing,8 interpolate,9 spring,10 useCurrentFrame,11 useVideoConfig,12} from "remotion";13import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";14import { GshGround } from "@/lib/kuchi/gsh/gsh-ground";15import type { GshPresetId } from "@/lib/kuchi/gsh/presets";1617const { fontFamily: sans } = loadFont("normal", {18 weights: ["300", "400"],19 subsets: ["latin"],20});2122export interface KeycapKey {23 label: string;24 sub?: string;25 width?: number;26}2728export interface KeycapPressProps {29 keys?: (string | KeycapKey)[];30 label?: string;31 material?: "dark" | "light";32 theme?: "dark" | "light";33 accent?: string;34 ground?: GshPresetId;35 groundParams?: Partial<GradientParams>;36 /** Frames between words as the label writes in. */37 stagger?: number;38 /** Strength of the accent underglow beneath the keys, 0 to 1. */39 glow?: number;40 /** Text color of the label; overrides the theme ink. */41 ink?: string;42 /** Solid fill in place of the gradient ground; "transparent" drops it. */43 background?: string;44 /** Typeface for the label and legends. Load it yourself; Bricolage Grotesque by default. */45 fontFamily?: string;46 /** Size of the keys and label, around the frame's center. */47 scale?: number;48 /** Frames to hold the first frame before anything moves. */49 delay?: number;50 speed?: number;51}5253const DEFAULT_KEYS: KeycapKey[] = [54 { label: "⌘", sub: "command", width: 1.25 },55 { label: "K" },56];5758const LIGHT_GLASS: Partial<GradientParams> = {59 colors: ["#a9bbff", "#3b5bfd", "#1a2a9e", "#8fa6ff"],60};61const EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);62const CAMERA = Easing.bezier(0.87, 0, 0.13, 1);63const HEAVY = { mass: 1, damping: 26, stiffness: 120 };64const CARD = { mass: 0.8, damping: 18, stiffness: 160 };65const CHIP = { mass: 0.5, damping: 12, stiffness: 200 };66const EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);67const CLAMP = {68 extrapolateLeft: "clamp",69 extrapolateRight: "clamp",70} as const;7172const MATERIALS = {73 dark: {74 top: [0x36, 0x36, 0x3c],75 sideTop: [0x2e, 0x2e, 0x34],76 sideBottom: [0x10, 0x10, 0x13],77 legend: "#ECEBE7",78 sub: "#8F8E95",79 rim: 0.3,80 dish: 0.34,81 sheen: 0.11,82 front: 0.4,83 left: 0.07,84 right: 0.45,85 shadow: "rgba(0,0,0,0.95)",86 },87 light: {88 top: [0xf7, 0xf6, 0xf2],89 sideTop: [0xe9, 0xe7, 0xe1],90 sideBottom: [0xa9, 0xa5, 0x9b],91 legend: "#2A2A2E",92 sub: "#8C8981",93 rim: 0.9,94 dish: 0.07,95 sheen: 0.5,96 front: 0.14,97 left: 0.35,98 right: 0.16,99 shadow: "rgba(58,48,32,0.5)",100 },101} as const;102103const STAGES = {104 dark: {105 base: "#04050A",106 fg: "#F3F2EF",107 muted: "rgba(243,242,239,0.5)",108 ripple: "255,255,255",109 scrim: "4,5,10",110 scrimA: 0.42,111 glowA: 0.34,112 glowBlend: "screen",113 },114 light: {115 base: "#EEF0F6",116 fg: "#121214",117 muted: "rgba(18,18,20,0.5)",118 ripple: "96,88,74",119 scrim: "244,244,247",120 scrimA: 0.4,121 glowA: 0.16,122 glowBlend: "multiply",123 },124} as const;125126const SYMBOLS: Record<string, string> = {127 "⌘": "M15 6v12a3 3 0 1 0 3-3H6a3 3 0 1 0 3 3V6a3 3 0 1 0-3 3h12a3 3 0 1 0-3-3",128 "⌥": "M3 5h6l6 14h6M14 5h7",129 "⇧": "M9 19v-6H5l7-8 7 8h-4v6z",130};131132type Material = (typeof MATERIALS)[keyof typeof MATERIALS];133134function rgb(c: readonly number[]) {135 return `rgb(${c[0]},${c[1]},${c[2]})`;136}137138function mix(a: readonly number[], b: readonly number[], t: number) {139 return a.map((v, i) => Math.round(v + (b[i] - v) * t));140}141142function hexToRgb(hex: string) {143 const h = hex.replace("#", "");144 const n = Number.parseInt(145 h.length === 3146 ? h147 .split("")148 .map((c) => c + c)149 .join("")150 : h,151 16,152 );153 return `${(n >> 16) & 255},${(n >> 8) & 255},${n & 255}`;154}155156const SLICES = 18;157158function profile(tt: number) {159 const fillet = Math.max(0, (tt - 0.72) / 0.28) ** 2;160 return tt * 0.68 + fillet * 0.32;161}162163function Keycap({164 w,165 d,166 h,167 keyDef,168 m,169 unit,170 fontFamily,171}: {172 w: number;173 d: number;174 h: number;175 keyDef: KeycapKey;176 m: Material;177 unit: number;178 fontFamily: string;179}) {180 const side = unit * 0.13;181 const front = unit * 0.17;182 const back = unit * 0.08;183 const r0 = unit * 0.1;184 const r1 = unit * 0.085;185 const slices: React.ReactNode[] = [];186 for (let k = 0; k < SLICES; k++) {187 const tt = k / (SLICES - 1);188 const p = profile(tt);189 const base = mix(m.sideBottom, m.sideTop, Math.min(1, tt * 1.15));190 const rim = Math.max(0, (tt - 0.7) / 0.3);191 slices.push(192 <div193 key={k}194 style={{195 position: "absolute",196 left: side * p,197 right: side * p,198 top: back * p,199 bottom: front * p,200 borderRadius: r0 + (r1 - r0) * tt,201 transform: `translateZ(${tt * h}px)`,202 background: [203 `linear-gradient(180deg, rgba(255,255,255,${m.rim * rim * 0.5}) 0%, transparent 40%, rgba(0,0,0,${m.front * (1 - rim * 0.5)}) 100%)`,204 `linear-gradient(90deg, rgba(255,255,255,${m.left + rim * m.rim * 0.25}) 0%, transparent 30%, transparent 62%, rgba(0,0,0,${m.right}) 100%)`,205 rgb(base),206 ].join(", "),207 }}208 />,209 );210 }211 const topInset = profile(1);212 const legendSize = unit * (keyDef.label.length > 1 ? 0.2 : 0.36);213 return (214 <div215 style={{216 position: "absolute",217 left: -w / 2,218 top: -d / 2,219 width: w,220 height: d,221 transformStyle: "preserve-3d",222 }}223 >224 {slices}225 <div226 style={{227 position: "absolute",228 left: side * topInset,229 right: side * topInset,230 top: back * topInset,231 bottom: front * topInset,232 borderRadius: r1,233 transform: `translateZ(${h + 0.5}px)`,234 background: [235 `radial-gradient(ellipse 72% 64% at 50% 54%, rgba(0,0,0,${m.dish * 0.5}) 0%, rgba(0,0,0,${m.dish * 0.18}) 55%, transparent 100%)`,236 `linear-gradient(160deg, rgba(255,255,255,${m.sheen}) 0%, transparent 45%)`,237 rgb(m.top),238 ].join(", "),239 boxShadow: `inset 0 ${unit * 0.012}px 0 rgba(255,255,255,${m.rim * 0.5}), inset 0 -${unit * 0.02}px ${unit * 0.03}px rgba(0,0,0,${m.dish * 0.5})`,240 }}241 >242 <div243 style={{244 position: "absolute",245 inset: 0,246 display: "flex",247 alignItems: "center",248 justifyContent: "center",249 fontFamily,250 fontWeight: 400,251 fontSize: legendSize,252 lineHeight: 1,253 letterSpacing: "-0.02em",254 color: m.legend,255 paddingBottom: keyDef.sub ? unit * 0.06 : 0,256 }}257 >258 {SYMBOLS[keyDef.label] ? (259 <svg260 width={legendSize * 0.86}261 height={legendSize * 0.86}262 viewBox="0 0 24 24"263 fill="none"264 stroke={m.legend}265 strokeWidth={1.7}266 strokeLinecap="round"267 strokeLinejoin="round"268 aria-hidden269 >270 <path d={SYMBOLS[keyDef.label]} />271 </svg>272 ) : (273 keyDef.label274 )}275 </div>276 {keyDef.sub ? (277 <div278 style={{279 position: "absolute",280 left: unit * 0.09,281 bottom: unit * 0.07,282 fontFamily,283 fontWeight: 400,284 fontSize: unit * 0.085,285 letterSpacing: "0.01em",286 color: m.sub,287 }}288 >289 {keyDef.sub}290 </div>291 ) : null}292 </div>293 </div>294 );295}296297/** The foreground at `scale`, around the frame's center; nothing extra at 1. */298function ScaleLayer({299 scale,300 children,301}: {302 scale: number;303 children: React.ReactNode;304}) {305 if (scale === 1) return children;306 return (307 <AbsoluteFill style={{ transform: `scale(${scale})` }}>308 {children}309 </AbsoluteFill>310 );311}312313export function KeycapPress({314 keys = DEFAULT_KEYS,315 label = "Everything, one shortcut away",316 material = "dark",317 theme,318 accent = "#3B5BFD",319 ground,320 groundParams,321 stagger = 3,322 glow: glowK = 1,323 ink,324 background,325 fontFamily = sans,326 scale = 1,327 delay = 0,328 speed = 1,329}: KeycapPressProps) {330 const t = Math.max(0, useCurrentFrame() - delay) * speed;331 const { width, height, fps } = useVideoConfig();332 const tall = height > width;333 const u = tall ? width / 760 : Math.min(width / 1280, height / 720);334 const m = MATERIALS[material];335 const look = theme ?? material;336 const stage = STAGES[look];337 const resolvedGround: GshPresetId | undefined =338 ground ??339 (groundParams ? undefined : look === "dark" ? "spotlight" : "glasswork");340 const acc = hexToRgb(accent);341342 const defs: KeycapKey[] = keys.map((k) =>343 typeof k === "string" ? { label: k } : k,344 );345 const unit = 205 * u * (tall ? 1.22 : 1) * (defs.length > 2 ? 0.82 : 1);346 const gap = unit * 0.1;347 const widths = defs.map((k) => (k.width ?? 1) * unit);348 const total = widths.reduce((s, x) => s + x, 0) + gap * (defs.length - 1);349 const centers: number[] = [];350 let cursor = -total / 2;351 for (const w of widths) {352 centers.push(cursor + w / 2);353 cursor += w + gap;354 }355 const depth = unit;356 const capH = unit * 0.44;357 const travel = unit * 0.13;358 const n = defs.length;359 const pressGap = n > 2 ? 10 : 12;360 const pressAt = defs.map((_, i) => 36 + i * pressGap);361 const chord = pressAt[n - 1];362 const releaseAt = defs.map((_, i) => chord + 22 + (n - 1 - i) * 4);363 const settle = spring({364 frame: t,365 fps,366 config: HEAVY,367 durationInFrames: 54,368 });369 const lift = interpolate(t, [chord + 2, chord + 34], [0, 1], {370 ...CLAMP,371 easing: CAMERA,372 });373 const rotX = 50 - settle * 8 - lift * 3;374 const rotZ = interpolate(t, [0, 135], [-9, -4], {375 ...CLAMP,376 easing: EASE_IO,377 });378 const camScale = 1.04 - settle * 0.04 - lift * 0.06;379 const kick = interpolate(t, [chord, chord + 3, chord + 14], [0, 1, 0], {380 ...CLAMP,381 easing: EASE_IO,382 });383 const keysY =384 height * ((tall ? 0.42 : 0.46) - lift * (tall ? 0.05 : 0.08)) +385 kick * 2 * u;386 const perspective = 1800 * u;387388 const keyState = defs.map((_, i) => {389 const rise = spring({390 frame: t - (-8 + i * 5),391 fps,392 config: CARD,393 });394 const down = Math.min(395 1.06,396 spring({397 frame: t - pressAt[i],398 fps,399 config: { mass: 0.4, damping: 14, stiffness: 320 },400 }),401 );402 const up = spring({403 frame: t - releaseAt[i],404 fps,405 config: CHIP,406 });407 const press = down * (1 - up);408 return { rise, press };409 });410411 const camera = (children: React.ReactNode) => (412 <AbsoluteFill413 style={{414 perspective,415 perspectiveOrigin: `50% ${(keysY / height) * 100}%`,416 }}417 >418 <div419 style={{420 position: "absolute",421 left: width / 2,422 top: keysY,423 width: 0,424 height: 0,425 transformStyle: "preserve-3d",426 transform: `scale(${camScale}) rotateX(${rotX}deg) rotateZ(${rotZ}deg)`,427 }}428 >429 {children}430 </div>431 </AbsoluteFill>432 );433 const ripples: React.ReactNode[] = [];434 defs.forEach((_, i) => {435 const age = t - pressAt[i];436 if (age < 0 || age > 34) return;437 const p = interpolate(age, [0, 34], [0, 1], {438 ...CLAMP,439 easing: EASE_OUT,440 });441 const size = widths[i] * (1.05 + p * 1.5);442 const sizeY = depth * (1.05 + p * 1.5);443 ripples.push(444 <div445 key={`r${i}`}446 style={{447 position: "absolute",448 left: centers[i] - size / 2,449 top: -sizeY / 2 + depth * 0.04,450 width: size,451 height: sizeY,452 borderRadius: unit * (0.2 + p * 0.5),453 border: `${unit * 0.035}px solid rgba(${stage.ripple},${(1 - p) * 0.12})`,454 boxShadow: `0 0 ${unit * 0.1}px rgba(${acc},${(1 - p) * 0.22})`,455 filter: `blur(${unit * (0.012 + p * 0.03)}px)`,456 }}457 />,458 );459 });460 const chordAge = t - chord;461 if (chordAge >= 0 && chordAge < 50) {462 for (const [lag, strength] of [463 [0, 1],464 [7, 0.55],465 ] as const) {466 const a = chordAge - lag;467 if (a < 0) continue;468 const p = interpolate(a, [0, 44], [0, 1], { ...CLAMP, easing: EASE_OUT });469 const sx = total * (1.1 + p * 1.6);470 const sy = depth * (1.1 + p * 2.6);471 ripples.push(472 <div473 key={`c${lag}`}474 style={{475 position: "absolute",476 left: -sx / 2,477 top: -sy / 2 + depth * 0.05,478 width: sx,479 height: sy,480 borderRadius: "50%",481 border: `${unit * 0.04}px solid rgba(${stage.ripple},${(1 - p) * 0.1 * strength})`,482 boxShadow: `0 0 ${unit * 0.14}px rgba(${acc},${(1 - p) * 0.22 * strength}), inset 0 0 ${unit * 0.14}px rgba(${acc},${(1 - p) * 0.14 * strength})`,483 filter: `blur(${unit * (0.015 + p * 0.035)}px)`,484 }}485 />,486 );487 }488 }489 const poolFlash = interpolate(chordAge, [0, 3, 30], [0, 1, 0], CLAMP);490 const glowRest = interpolate(t, [6, 40], [0, 0.35], {491 ...CLAMP,492 easing: EASE_IO,493 });494 const glowHold = interpolate(chordAge, [0, 4, 40], [0, 1, 0.7], {495 ...CLAMP,496 easing: EASE_OUT,497 });498 const glow = Math.max(glowRest, glowHold);499 const glowSpread =500 1 +501 0.12 *502 interpolate(chordAge, [0, 40], [0, 1], {503 ...CLAMP,504 easing: EASE_OUT,505 });506 const gx = 50 + Math.sin(t / 47) * 1.5;507 const underglow = `radial-gradient(closest-side at ${gx}% 50%, rgba(${acc},${stage.glowA}) 0%, rgba(${acc},${stage.glowA * 0.45}) 55%, transparent 100%)`;508509 const floor = camera(510 <>511 <div512 style={{513 position: "absolute",514 left: -total,515 top: -depth * 1.4,516 width: total * 2,517 height: depth * 2.8,518 borderRadius: "50%",519 background: `radial-gradient(closest-side, rgba(${acc},${0.16 * poolFlash}), transparent)`,520 }}521 />522 <div523 style={{524 position: "absolute",525 left: -total * 0.95 * glowSpread,526 top: -depth * 1.1 * glowSpread + depth * 0.05,527 width: total * 1.9 * glowSpread,528 height: depth * 2.2 * glowSpread,529 background: underglow,530 opacity: glow * glowK,531 mixBlendMode: stage.glowBlend,532 filter: `blur(${unit * 0.12}px)`,533 }}534 />535 {ripples}536 {defs.map((_, i) => {537 const { rise, press } = keyState[i];538 const w = widths[i];539 const tight = press;540 const spread = (1 - tight) * 0.5 + 0.5;541 return (542 <div key={`s${i}`}>543 <div544 style={{545 position: "absolute",546 left: centers[i] - w * 0.55 + unit * 0.04,547 top: -depth * 0.5 + depth * 0.14,548 width: w * 1.1,549 height: depth * 1.1,550 borderRadius: unit * 0.24,551 background: m.shadow,552 opacity: 0.55 * rise * (0.85 + tight * 0.15),553 filter: `blur(${unit * (0.16 + spread * 0.12)}px)`,554 }}555 />556 <div557 style={{558 position: "absolute",559 left: centers[i] - w * 0.48,560 top: -depth * 0.48 + depth * 0.03,561 width: w * 0.96,562 height: depth * 0.96,563 borderRadius: unit * 0.12,564 background: m.shadow,565 opacity: (0.6 + tight * 0.3) * rise,566 filter: `blur(${unit * (0.045 + (1 - tight) * 0.04)}px)`,567 }}568 />569 </div>570 );571 })}572 </>,573 );574575 const caps = camera(576 defs.map((k, i) => {577 const { rise, press } = keyState[i];578 const off = 1 - rise;579 const z = unit * 0.035 - press * travel - off * unit * 0.6;580 const y = off * unit * 2.6;581 const tilt = off * -24;582 return (583 <div584 key={i}585 style={{586 position: "absolute",587 left: centers[i],588 top: 0,589 width: 0,590 height: 0,591 transformStyle: "preserve-3d",592 transform: `translate3d(0, ${y}px, ${z}px) rotateX(${tilt}deg)`,593 }}594 >595 <Keycap596 w={widths[i] - unit * 0.02}597 d={depth}598 h={capH}599 keyDef={k}600 m={m}601 unit={unit}602 fontFamily={fontFamily}603 />604 </div>605 );606 }),607 );608 const words = label.split(" ");609 const word = (i: number) =>610 interpolate(611 t,612 [chord + 6 + i * stagger, chord + 26 + i * stagger],613 [0, 1],614 {615 ...CLAMP,616 easing: EASE_OUT,617 },618 );619620 return (621 <AbsoluteFill622 style={{623 background:624 background === undefined625 ? stage.base626 : background === "transparent"627 ? undefined628 : background,629 overflow: "hidden",630 }}631 >632 {background === undefined ? (633 <>634 <GshGround635 preset={resolvedGround}636 params={637 groundParams ?? (look === "light" ? LIGHT_GLASS : undefined)638 }639 timeOffset={look === "dark" ? 2 : 5}640 timeScale={0.45}641 />642 <AbsoluteFill643 style={{644 background: `radial-gradient(60% 55% at 50% ${tall ? 42 : 46}%, rgba(${stage.scrim},${stage.scrimA}) 0%, rgba(${stage.scrim},${stage.scrimA * 0.4}) 100%)`,645 }}646 />647 </>648 ) : null}649 <ScaleLayer scale={scale}>650 {floor}651 {caps}652 <div653 style={{654 position: "absolute",655 left: "10%",656 right: "10%",657 top: height * (tall ? 0.6 : 0.7),658 textAlign: "center",659 fontFamily,660 fontWeight: 300,661 fontSize: (tall ? 72 : 60) * u,662 lineHeight: 1.08,663 letterSpacing: "-0.04em",664 color: ink ?? stage.fg,665 textWrap: "balance",666 }}667 >668 {words.map((w, i) => (669 <span670 key={i}671 style={{672 display: "inline-block",673 whiteSpace: "pre",674 opacity: word(i),675 transform: `translateY(${(1 - word(i)) * 14 * u}px)`,676 filter: `blur(${(1 - word(i)) * 5 * u}px)`,677 }}678 >679 {i < words.length - 1 ? `${w} ` : w}680 </span>681 ))}682 </div>683 </ScaleLayer>684 </AbsoluteFill>685 );686}









