Ghost Complete
Grey ghost suggestions autocomplete a sentence, each accepted with a Tab keypress.
Source
components/kuchi/ghost-complete.tsx
1"use client";23import { loadFont as loadGeist } from "@remotion/google-fonts/Geist";4import { loadFont as loadGeistMono } from "@remotion/google-fonts/GeistMono";5import { loadFont as loadManrope } from "@remotion/google-fonts/Manrope";6import type React from "react";7import { useMemo } from "react";8import {9 AbsoluteFill,10 Easing,11 interpolate,12 interpolateColors,13 spring,14 useCurrentFrame,15 useVideoConfig,16} from "remotion";17import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";18import { GshGround } from "@/lib/kuchi/gsh/gsh-ground";19import type { GshPresetId } from "@/lib/kuchi/gsh/presets";2021const { fontFamily: geist } = loadGeist("normal", {22 weights: ["300", "400"],23 subsets: ["latin"],24});25const { fontFamily: mono } = loadGeistMono("normal", {26 weights: ["400"],27 subsets: ["latin"],28});29const manrope = loadManrope("normal", {30 weights: ["600"],31 subsets: ["latin"],32});3334export interface GhostCompleteProps {35 title?: string;36 typed?: string;37 suggestions?: string[];38 keepLastFill?: boolean;39 showLogo?: boolean;40 accent?: string;41 theme?: "dark" | "light";42 ground?: GshPresetId;43 groundParams?: Partial<GradientParams>;44 /** Label of the key pressed to accept a suggestion. */45 keyLabel?: string;46 /** Text next to the key. */47 hintLabel?: string;48 /** Frames a ghost suggestion waits before it is accepted. */49 hold?: number;50 /** Replaces the acme mark. */51 logo?: React.ReactNode;52 /** Text color; overrides the theme ink. */53 ink?: string;54 /** Solid fill in place of the gradient ground; "transparent" drops it. */55 background?: string;56 /** Typeface for the copy. Load it yourself; Geist by default. */57 fontFamily?: string;58 /** Size of the card and key, around the frame's center. */59 scale?: number;60 /** Frames to hold the first frame before anything moves. */61 delay?: number;62 speed?: number;63}646566function KuchiLogo({ fontSize, color }: { fontSize: number; color: string }) {67 return (68 <div69 style={{70 display: "flex",71 alignItems: "baseline",72 color,73 fontFamily: manrope.fontFamily,74 fontWeight: 600,75 fontSize,76 letterSpacing: "-0.025em",77 lineHeight: 1,78 }}79 >80 <span>Acme</span>81 </div>82 );83}8485const ADV = [86 243, 228, 361, 515, 643, 810, 649, 186, 290, 290, 427, 562, 213, 418, 213,87 494, 673, 406, 630, 625, 629, 641, 604, 531, 624, 606, 302, 302, 546, 544,88 546, 570, 925, 689, 688, 713, 701, 609, 595, 713, 716, 280, 607, 656, 583,89 890, 745, 751, 657, 745, 680, 654, 568, 694, 688, 968, 633, 594, 561, 361,90 470, 361, 438, 558, 258, 565, 608, 563, 608, 576, 412, 607, 591, 256, 284,91 609, 282, 885, 591, 588, 608, 608, 394, 537, 410, 586, 560, 829, 607, 553,92 552, 395, 274, 395, 523,93];94const TRACK = -0.035;95const LEADING = 1.08;9697const LIGHT_GROUND: Partial<GradientParams> = {98 kind: "mist",99 colors: ["#F2955A", "#F7CBA8", "#F3E6D8", "#6B2A12"],100 background: "#F2EEE8",101 speed: 0.8,102 loopSeconds: 14,103 scale: 0.8,104 warp: 0.4,105 blur: 0.65,106 grain: 0.3,107 seed: 9,108};109110const THEMES = {111 dark: {112 base: "#050303",113 scrim:114 "linear-gradient(180deg, rgba(5,3,3,0.55) 0%, rgba(5,3,3,0.2) 70%, rgba(5,3,3,0) 100%)",115 card: "rgba(17,14,13,0.9)",116 depth:117 "inset 0 1px 0 rgba(255,255,255,0.06), 0 0 0 1px rgba(255,255,255,0.08), 0 30px 60px -20px rgba(0,0,0,0.75)",118 divider: "rgba(255,255,255,0.07)",119 fg: "#F4F1EC",120 ghost: "#5C5650",121 fill: "#FFB27A",122 dim: "#8F8780",123 key: "rgba(255,255,255,0.14)",124 keyBg: "rgba(255,255,255,0.04)",125 logo: "#F4F1EC",126 },127 light: {128 base: "#F2EEE8",129 scrim:130 "linear-gradient(180deg, rgba(246,243,238,0.5) 0%, rgba(246,243,238,0.1) 100%)",131 card: "rgba(255,255,255,0.94)",132 depth:133 "0 0 0 1px rgba(40,24,12,0.06), 0 1px 3px rgba(40,24,12,0.05), 0 30px 60px -24px rgba(90,40,10,0.25)",134 divider: "rgba(40,24,12,0.07)",135 fg: "#17120F",136 ghost: "#C2BAB1",137 fill: "#C2410C",138 dim: "#857B73",139 key: "rgba(40,24,12,0.16)",140 keyBg: "#F7F4F0",141 logo: "#17120F",142 },143} as const;144145const ENTER = Easing.bezier(0.32, 0.72, 0, 1);146const MOVE = Easing.bezier(0.65, 0, 0.35, 1);147const EXIT = Easing.bezier(0.55, 0, 1, 0.45);148const CLAMP = {149 extrapolateLeft: "clamp",150 extrapolateRight: "clamp",151} as const;152153const TYPE_START = 6;154const PER_CHAR = 2;155const GHOST_LEAD = 4;156const HOLD_GHOST = 16;157const AFTER_FILL = 8;158const SETTLE = 12;159160function advance(ch: string) {161 const code = ch.charCodeAt(0);162 return (code >= 32 && code < 127 ? ADV[code - 32] : 600) / 1000;163}164165interface Glyph {166 ch: string;167 seg: number;168 i: number;169}170171interface Segment {172 start: number;173 length: number;174 breaks: number;175 ghostIn: number;176 accept: number;177 fillDur: number;178}179180/** The foreground at `scale`, around the frame's center; nothing extra at 1. */181function ScaleLayer({182 scale,183 children,184}: {185 scale: number;186 children: React.ReactNode;187}) {188 if (scale === 1) return children;189 return (190 <AbsoluteFill style={{ transform: `scale(${scale})` }}>191 {children}192 </AbsoluteFill>193 );194}195196export function GhostComplete({197 title = "Launch notes",198 typed = "Ship the",199 suggestions = ["launch video", "\nbefore lunch."],200 keepLastFill = true,201 showLogo = true,202 theme = "dark",203 ground,204 groundParams,205 keyLabel = "Tab",206 hintLabel = "to accept",207 hold = HOLD_GHOST,208 logo,209 ink,210 background,211 fontFamily = geist,212 scale = 1,213 delay = 0,214 speed = 1,215}: GhostCompleteProps) {216 const t = Math.max(0, useCurrentFrame() - delay) * speed;217 const { width: W, height: H, fps } = useVideoConfig();218 const tall = H > W;219 const u = tall ? W / 1000 : H / 720;220 const c = THEMES[theme];221 const dark = theme === "dark";222 const fg = ink ?? c.fg;223 const resolvedGround: GshPresetId | undefined =224 ground ?? (groundParams ? undefined : dark ? "hearth" : undefined);225 const resolvedParams = groundParams ?? (dark ? undefined : LIGHT_GROUND);226227 const plan = useMemo(() => {228 const glyphs: Glyph[] = Array.from(typed).map((ch, i) => ({229 ch,230 seg: -1,231 i,232 }));233 const segs: Segment[] = [];234 let clock = TYPE_START + typed.length * PER_CHAR + GHOST_LEAD;235 suggestions.forEach((raw, k) => {236 const text =237 raw.startsWith("\n") || raw.startsWith(" ") ? raw : ` ${raw}`;238 const chars = Array.from(text);239 const start = glyphs.length;240 chars.forEach((ch, i) => {241 glyphs.push({ ch, seg: k, i });242 });243 const fillDur = Math.max(7, Math.min(12, chars.length * 0.6));244 const ghostIn = clock;245 const accept = ghostIn + hold + (k === 0 ? 6 : 0);246 segs.push({247 start,248 length: chars.length,249 breaks: chars.filter((ch) => ch === "\n").length,250 ghostIn,251 accept,252 fillDur,253 });254 clock = accept + fillDur + AFTER_FILL;255 });256 const lines: number[] = [0];257 for (const g of glyphs) {258 if (g.ch === "\n") lines.push(0);259 else lines[lines.length - 1] += advance(g.ch) + TRACK;260 }261 return {262 glyphs,263 segs,264 widest: Math.max(...lines) - TRACK,265 lineCount: lines.length,266 end: clock,267 };268 }, [typed, suggestions, hold]);269 const { glyphs, segs } = plan;270 const last = segs[segs.length - 1];271272 const F = Math.min(273 (tall ? 104 : 112) * u,274 (W * (tall ? 0.72 : 0.62)) / plan.widest,275 (H * 0.4) / (plan.lineCount * LEADING),276 );277 const lineH = F * LEADING;278 const blockW = plan.widest * F;279280 const padX = 44 * u;281 const headH = 66 * u;282 const padTop = headH + 14 * u;283 const footH = tall ? 16 * u : 72 * u;284 const cardW = blockW + padX * 2 + F * 0.2;285286 const visLines =287 1 +288 segs.reduce(289 (n, s) =>290 n +291 s.breaks *292 spring({293 frame: t - s.ghostIn + 2,294 fps,295 config: { damping: 200 },296 durationInFrames: 14,297 }),298 0,299 );300 const cardH = padTop + visLines * lineH + 28 * u + footH;301 const finalH = padTop + plan.lineCount * lineH + 28 * u + footH;302 const cardLeft = (W - cardW) / 2;303 const cardTop = (tall ? 0.38 : 0.47) * H - finalH / 2;304 const bigKeyY = cardTop + finalH + 0.13 * H;305306 const cardIn = spring({307 frame: t,308 fps,309 config: { mass: 0.8, damping: 18, stiffness: 160 },310 });311312 const typedCount = Math.max(313 0,314 Math.min(typed.length, Math.floor((t - TYPE_START) / PER_CHAR) + 1),315 );316 let caret = typedCount;317 let busy = t < TYPE_START + typed.length * PER_CHAR + 2;318 const fronts = segs.map((s) => {319 const k = interpolate(t, [s.accept, s.accept + s.fillDur], [0, 1], {320 ...CLAMP,321 easing: MOVE,322 });323 const front = k * s.length;324 if (t >= s.accept) caret = s.start + Math.min(s.length, Math.ceil(front));325 if (t >= s.accept - 2 && t <= s.accept + s.fillDur + 3) busy = true;326 return front;327 });328 const phase = (t % 30) / 30;329 const blink = busy330 ? 1331 : interpolate(phase, [0, 0.45, 0.55, 0.95, 1], [1, 1, 0, 0, 1], CLAMP);332333 const styleOf = (g: Glyph): React.CSSProperties => {334 if (g.seg < 0) return { color: fg };335 const s = segs[g.seg];336 const appear = interpolate(337 t,338 [s.ghostIn + g.i * 0.35, s.ghostIn + g.i * 0.35 + 6],339 [0, 1],340 { ...CLAMP, easing: ENTER },341 );342 const filled = Math.min(1, Math.max(0, fronts[g.seg] - g.i));343 const settleAt = s.accept + s.fillDur + AFTER_FILL;344 const settle =345 keepLastFill && g.seg === segs.length - 1346 ? 0347 : interpolate(t, [settleAt, settleAt + SETTLE], [0, 1], {348 ...CLAMP,349 easing: MOVE,350 });351 if (filled <= 0) return { color: c.ghost, opacity: appear };352 if (settle > 0) {353 return { color: interpolateColors(settle, [0, 1], [c.fill, fg]) };354 }355 return { color: interpolateColors(filled, [0, 1], [c.ghost, c.fill]) };356 };357358 const hintOut = interpolate(359 t,360 [last.accept + last.fillDur + 4, last.accept + last.fillDur + 12],361 [0, 1],362 { ...CLAMP, easing: EXIT },363 );364 const hintArmed = interpolate(365 t,366 [segs[0].ghostIn + 2, segs[0].ghostIn + 12],367 [0, 1],368 { ...CLAMP, easing: ENTER },369 );370 const press = segs.reduce((p, s) => {371 const down = spring({372 frame: t - s.accept + 3,373 fps,374 config: { mass: 0.5, damping: 12, stiffness: 200 },375 });376 const up = spring({377 frame: t - s.accept - 2,378 fps,379 config: { mass: 0.5, damping: 12, stiffness: 200 },380 });381 return Math.max(p, Math.max(0, down - up));382 }, 0);383 const logoIn = interpolate(t, [plan.end - 2, plan.end + 12], [0, 1], {384 ...CLAMP,385 easing: ENTER,386 });387 const push = interpolate(t, [0, plan.end + 40], [1, 1.03], {388 ...CLAMP,389 easing: MOVE,390 });391392 const caretEl = (393 <span394 key="caret"395 style={{396 display: "inline-block",397 position: "relative",398 width: 0,399 height: "0.86em",400 verticalAlign: "-0.1em",401 }}402 >403 <span404 style={{405 position: "absolute",406 left: "0.035em",407 top: 0,408 bottom: 0,409 width: Math.max(2, 0.04 * F),410 borderRadius: 2,411 background: c.fill,412 opacity: blink,413 }}414 />415 </span>416 );417418 const nodes: React.ReactNode[] = [];419 glyphs.forEach((g, idx) => {420 if (idx === caret) nodes.push(caretEl);421 const visible =422 g.seg < 0 ? idx < typedCount : t >= segs[g.seg].ghostIn - 0.5;423 if (!visible) return;424 if (g.ch === "\n") {425 nodes.push(<br key={idx} />);426 return;427 }428 nodes.push(429 <span key={idx} style={styleOf(g)}>430 {g.ch}431 </span>,432 );433 });434 if (caret >= glyphs.length) nodes.push(caretEl);435436 const label = (size: number): React.CSSProperties => ({437 fontFamily,438 fontWeight: 400,439 fontSize: size,440 letterSpacing: "-0.01em",441 color: c.dim,442 });443444 const keyCap = (k: number, raised: boolean) => (445 <div446 style={{447 height: 34 * k,448 padding: `0 ${12 * k}px`,449 display: "flex",450 alignItems: "center",451 borderRadius: 8 * k,452 background: interpolateColors(453 press,454 [0, 1],455 [raised ? c.card : c.keyBg, c.fill],456 ),457 boxShadow: `inset 0 0 0 1px ${c.key}, 0 ${(1 - press) * 2 * k}px 0 ${c.key}${raised ? `, ${c.depth}` : ""}`,458 transform: `translateY(${press * 2 * k}px) scale(${1 - press * 0.04})`,459 color: interpolateColors(460 press,461 [0, 1],462 [fg, dark ? "#1A0E06" : "#FFFFFF"],463 ),464 fontFamily: mono,465 fontWeight: 400,466 fontSize: 17 * k,467 letterSpacing: "0.02em",468 }}469 >470 {keyLabel}471 </div>472 );473474 const keyIn = spring({475 frame: t - segs[0].ghostIn + 4,476 fps,477 config: { mass: 0.8, damping: 18, stiffness: 160 },478 });479480 return (481 <AbsoluteFill482 style={{483 background:484 background === undefined485 ? c.base486 : background === "transparent"487 ? undefined488 : background,489 overflow: "hidden",490 }}491 >492 {background === undefined ? (493 <>494 <GshGround495 preset={resolvedGround}496 params={resolvedParams}497 timeOffset={dark ? 6 : 2}498 timeScale={0.45}499 />500 <AbsoluteFill style={{ background: c.scrim }} />501 </>502 ) : null}503 <ScaleLayer scale={scale}>504 <AbsoluteFill style={{ transform: `scale(${push})` }}>505 <div506 style={{507 position: "absolute",508 left: cardLeft,509 top: cardTop,510 width: cardW,511 height: cardH,512 borderRadius: 28 * u,513 background: c.card,514 boxShadow: c.depth,515 overflow: "hidden",516 opacity: interpolate(cardIn, [0, 0.4], [0, 1], CLAMP),517 transform: `translateY(${(1 - cardIn) * 40 * u}px) scale(${0.96 + 0.04 * cardIn})`,518 }}519 >520 <div521 style={{522 position: "absolute",523 left: padX,524 top: 0,525 height: headH,526 display: "flex",527 alignItems: "flex-end",528 gap: 10 * u,529 }}530 >531 <svg532 viewBox="0 0 24 24"533 width={(tall ? 28 : 22) * u}534 height={(tall ? 28 : 22) * u}535 style={{ marginBottom: 1 * u }}536 aria-hidden537 >538 <g539 fill="none"540 stroke={c.dim}541 strokeWidth={1.5}542 strokeLinecap="round"543 strokeLinejoin="round"544 >545 <path d="M6.5 3.5h7l4 4v13h-11z" />546 <path d="M13.5 3.5v4h4M9.5 12.5h5M9.5 16h5" />547 </g>548 </svg>549 <div style={label((tall ? 26 : 19) * u)}>{title}</div>550 </div>551 <div552 style={{553 position: "absolute",554 left: padX,555 top: padTop,556 width: blockW + F,557 fontFamily,558 fontWeight: 400,559 fontSize: F,560 lineHeight: LEADING,561 letterSpacing: `${TRACK}em`,562 whiteSpace: "pre",563 fontKerning: "normal",564 }}565 >566 {nodes}567 </div>568 {tall ? null : (569 <div570 style={{571 position: "absolute",572 left: 0,573 right: 0,574 bottom: 0,575 height: footH,576 borderTop: `1px solid ${c.divider}`,577 display: "flex",578 alignItems: "center",579 justifyContent: "space-between",580 padding: `0 ${padX - 10 * u}px 0 ${padX}px`,581 }}582 >583 <div584 style={{585 display: "flex",586 alignItems: "center",587 gap: 12 * u,588 opacity: (0.35 + 0.65 * hintArmed) * (1 - hintOut),589 }}590 >591 {keyCap(u, false)}592 <div style={label(19 * u)}>{hintLabel}</div>593 </div>594 {showLogo ? (595 <div596 style={{597 opacity: logoIn,598 transform: `translateY(${(1 - logoIn) * 8 * u}px)`,599 }}600 >601 {logo ?? <KuchiLogo fontSize={24 * u} color={c.logo} />}602 </div>603 ) : null}604 </div>605 )}606 </div>607 {tall ? (608 <div609 style={{610 position: "absolute",611 left: 0,612 right: 0,613 top: bigKeyY,614 display: "flex",615 flexDirection: "column",616 alignItems: "center",617 gap: 30 * u,618 opacity:619 Math.min(1, keyIn * 1.4) *620 (0.35 + 0.65 * hintArmed) *621 (1 - 0.5 * hintOut),622 transform: `translateY(${(1 - keyIn) * 40 * u}px)`,623 }}624 >625 {keyCap(3.4 * u, true)}626 <div style={label(34 * u)}>{hintLabel}</div>627 </div>628 ) : null}629 {tall && showLogo ? (630 <div631 style={{632 position: "absolute",633 left: 0,634 right: 0,635 top: bigKeyY + 290 * u,636 display: "flex",637 justifyContent: "center",638 opacity: logoIn,639 transform: `translateY(${(1 - logoIn) * 12 * u}px)`,640 }}641 >642 {logo ?? <KuchiLogo fontSize={40 * u} color={c.logo} />}643 </div>644 ) : null}645 </AbsoluteFill>646 </ScaleLayer>647 </AbsoluteFill>648 );649}









