Card Push Line
A prompt card with a slash command pushes forward as its text line types.
Source
components/kuchi/card-push-line.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/Geist";4import { loadFont as loadMono } from "@remotion/google-fonts/GeistMono";5import {6 type CSSProperties,7 type ReactNode,8 useEffect,9 useId,10 useMemo,11 useRef,12 useState,13} from "react";14import {15 AbsoluteFill,16 continueRender,17 delayRender,18 Easing,19 interpolate,20 spring,21 useCurrentFrame,22 useVideoConfig,23} from "remotion";24import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";25import { GshGround, gshParams } from "@/lib/kuchi/gsh/gsh-ground";26import type { GshPresetId } from "@/lib/kuchi/gsh/presets";2728const sans = loadFont("normal", {29 weights: ["300", "400"],30 subsets: ["latin"],31});32const mono = loadMono("normal", { weights: ["400"], subsets: ["latin"] });3334export interface CardPushLineProps {35 line?: string;36 command?: string;37 prompt?: string;38 placeholder?: string;39 modelLabel?: string;40 chipLabel?: string;41 attachName?: string;42 attachMeta?: string;43 accent?: string;44 ink?: string;45 background?: string;46 theme?: "dark" | "light";47 ground?: GshPresetId;48 groundParams?: Partial<GradientParams>;49 /** Typeface for the line and the card copy. Load it yourself; Geist by default. */50 fontFamily?: string;51 /** Frames between words as the line rises in. */52 stagger?: number;53 /** Size of the line and card, around the frame's center. */54 scale?: number;55 /** Frames to hold the first frame before anything moves. */56 delay?: number;57 speed?: number;58}5960const THEMES = {61 dark: {62 ink: "#eef3ff",63 muted: "rgba(238,243,255,0.46)",64 faint: "rgba(238,243,255,0.1)",65 accent: "#4f7dff",66 cmdInk: "#a9c4ff",67 cmdBg: "rgba(79,125,255,0.2)",68 rim: "168,198,255",69 ground: "film-blue" as GshPresetId,70 groundParams: undefined as Partial<GradientParams> | undefined,71 groundOffset: 3,72 scrim: "2,5,12",73 scrimAlpha: 0.5,74 card: "linear-gradient(180deg, #121722 0%, #0c1018 100%)",75 border: "rgba(255,255,255,0.08)",76 shadow: "0 40px 90px -24px rgba(0,3,12,0.7)",77 send: "rgba(238,243,255,0.12)",78 tile: "rgba(238,243,255,0.035)",79 },80 light: {81 ink: "#14161c",82 muted: "rgba(20,22,28,0.45)",83 faint: "rgba(20,22,28,0.09)",84 accent: "#3B5BFD",85 cmdInk: "#2f4fe0",86 cmdBg: "rgba(59,91,253,0.1)",87 rim: "59,91,253",88 ground: "glasswork" as GshPresetId,89 groundParams: {90 colors: ["#a9c9ec", "#4f8fd6", "#2b5fbe", "#cfe0f2"],91 background: "#eef1f5",92 } as Partial<GradientParams> | undefined,93 groundOffset: 2,94 scrim: "238,241,245",95 scrimAlpha: 0.5,96 card: "#ffffff",97 border: "rgba(17,24,40,0.07)",98 shadow:99 "0 1px 3px rgba(17,24,40,0.06), 0 36px 80px -24px rgba(17,30,60,0.24)",100 send: "rgba(20,22,28,0.08)",101 tile: "rgba(20,22,28,0.025)",102 },103};104105const BEAT = {106 wordStep: 3,107 enter: 12,108 card: 36,109 cmd: 60,110 chip: 72,111 prompt: 76,112 arm: 108,113};114115const TRACK = -0.03;116const FONT_PX = 200;117const ENTER = Easing.bezier(0.22, 1, 0.36, 1);118const CLAMP = {119 extrapolateLeft: "clamp",120 extrapolateRight: "clamp",121} as const;122123type Measure = (s: string, font: string, track: number) => number;124125function buildMeasure(_version: number): Measure {126 const ctx = document.createElement("canvas").getContext("2d");127 const cache: Record<string, number> = {};128 return (s: string, font: string, track: number) => {129 const key = `${font}|${track}|${s}`;130 if (cache[key] === undefined) {131 if (!ctx) return s.length * 0.55;132 ctx.font = font.replace("@", `${FONT_PX}px`);133 cache[key] =134 ctx.measureText(s).width / FONT_PX + track * Array.from(s).length;135 }136 return cache[key];137 };138}139140function useMeasure(fontFamily: string) {141 const [version, setVersion] = useState(0);142 const [handle] = useState(() => delayRender("card-push-line: measuring"));143 const released = useRef(false);144 useEffect(() => {145 let live = true;146 const bump = () => {147 if (live) setVersion((v) => v + 1);148 };149 document.fonts.addEventListener("loadingdone", bump);150 Promise.all([151 sans.waitUntilDone(),152 mono.waitUntilDone(),153 document.fonts.load(`300 100px "${fontFamily}"`),154 document.fonts.load(`400 100px "${fontFamily}"`),155 document.fonts.load(`400 100px "${mono.fontFamily}"`),156 ])157 .catch(() => undefined)158 .then(() => {159 bump();160 if (!released.current) {161 released.current = true;162 continueRender(handle);163 }164 });165 return () => {166 live = false;167 document.fonts.removeEventListener("loadingdone", bump);168 };169 }, [handle, fontFamily]);170 return useMemo(() => buildMeasure(version), [version]);171}172173function Icon({174 size,175 color,176 children,177}: {178 size: number;179 color: string;180 children: ReactNode;181}) {182 return (183 <svg184 viewBox="0 0 24 24"185 width={size}186 height={size}187 fill="none"188 stroke={color}189 strokeWidth={1.6}190 strokeLinecap="round"191 strokeLinejoin="round"192 style={{ display: "block", flexShrink: 0 }}193 aria-hidden194 >195 {children}196 </svg>197 );198}199200function wrap(words: string[], room: number, w: (s: string) => number) {201 const lines: string[] = [];202 let cur = "";203 for (const word of words) {204 const next = cur.length > 0 ? `${cur} ${word}` : word;205 if (cur.length > 0 && w(next) > room) {206 lines.push(cur);207 cur = word;208 } else {209 cur = next;210 }211 }212 if (cur.length > 0) lines.push(cur);213 return lines;214}215216/** The foreground at `scale`, around the frame's center; nothing extra at 1. */217function ScaleLayer({218 scale,219 children,220}: {221 scale: number;222 children: React.ReactNode;223}) {224 if (scale === 1) return children;225 return (226 <AbsoluteFill style={{ transform: `scale(${scale})` }}>227 {children}228 </AbsoluteFill>229 );230}231232export function CardPushLine({233 line = "You can just write it",234 command = "/launch-video",235 prompt = "a teaser for the 2.0 release",236 placeholder = "Describe your launch…",237 modelLabel = "Acme 1",238 chipLabel = "Brand kit",239 attachName = "release-notes-2.0.md",240 attachMeta = "Markdown · 18 KB",241 accent,242 ink,243 background,244 theme = "dark",245 ground,246 groundParams,247 fontFamily = sans.fontFamily,248 stagger = BEAT.wordStep,249 scale = 1,250 delay = 0,251 speed = 1,252}: CardPushLineProps) {253 const frame = Math.max(0, useCurrentFrame() - delay) * speed;254 const { width: W, height: H, fps, durationInFrames } = useVideoConfig();255 const c = THEMES[theme];256 const inkC = ink ?? c.ink;257 const hi = accent ?? c.accent;258 const preset = ground ?? c.ground;259 const gParams = groundParams ?? (ground ? undefined : c.groundParams);260 const bg = background ?? gshParams(preset, gParams).background;261 const m = useMeasure(fontFamily);262 const portrait = H > W * 1.1;263 const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "");264 const f = frame;265266 let content: ReactNode = <div />;267 if (m) {268 const lineFont = `300 @ "${fontFamily}"`;269 const uiFont = `400 @ "${fontFamily}"`;270 const monoFont = `400 @ "${mono.fontFamily}"`;271 const room = W * (portrait ? 0.84 : 0.7);272 const words = line.split(" ").filter(Boolean);273 const wl = (s: string) => m(s, lineFont, TRACK);274 let L = Math.min(portrait ? W * 0.12 : H * 0.15, room / wl(line));275 let sLines = [line];276 if (portrait && L < W * 0.095) {277 L = Math.min(W * 0.12, room / Math.max(...words.map(wl)));278 sLines = wrap(words, room / L, wl);279 }280 const LLH = L * 1.08;281 const blockW = Math.max(...sLines.map((s) => wl(s) * L));282 const restRight = W / 2 + blockW / 2;283284 const CW = portrait ? W * 0.78 : W * 0.62;285 const T = portrait ? W * 0.046 : Math.max(H * 0.048, CW * 0.042);286 const u = T / 24;287 const pad = 24 * u;288 const PLH = T * 1.45;289 const cmdT = T * 0.86;290 const chipPadX = 8 * u;291 const chipW = m(command, monoFont, 0) * cmdT + chipPadX * 2;292 const promptRoom = CW - pad * 2;293 const firstIndent = chipW + 8 * u;294 const pw = (s: string) => m(s, uiFont, -0.01) * T;295 const pWords = prompt.split(" ").filter(Boolean);296 const pLines: { text: string; x: number }[] = [];297 {298 let cur = "";299 let indent = firstIndent;300 for (const word of pWords) {301 const next = cur.length > 0 ? `${cur} ${word}` : word;302 if (cur.length > 0 && indent + pw(next) > promptRoom) {303 pLines.push({ text: cur, x: indent });304 cur = word;305 indent = 0;306 } else {307 cur = next;308 }309 }310 pLines.push({ text: cur, x: indent });311 }312 const barH = 36 * u;313 const attH = 52 * u;314 const top0 = pad + attH + 14 * u;315 const CH = Math.max(316 top0 + pLines.length * PLH + 24 * u + barH + pad * 0.8,317 portrait ? H * 0.2 : H * 0.32,318 );319 const restLeft = (W - CW) / 2;320 const startLeft = W * 1.04;321 const cardSp = (ff: number) =>322 spring({323 frame: ff - BEAT.card,324 fps,325 config: { mass: 0.8, damping: 18, stiffness: 110 },326 });327 const cardLeft = (ff: number) =>328 restLeft + (startLeft - restLeft) * (1 - cardSp(ff));329 const gap = W * 0.03;330331 let off = 0;332 let vel = 0;333 let lastV = 0;334 for (let k = BEAT.card; k <= f; k++) {335 const limit = cardLeft(k) - gap - restRight;336 const free = off + vel;337 const next = Math.min(free, limit);338 lastV = next - off;339 vel = limit < free ? lastV : lastV * 0.93;340 off = next;341 }342 const lineSpeed = Math.abs(lastV);343 const cardV = Math.abs(cardLeft(f) - cardLeft(f - 1));344 const lineBlur = Math.min(10, lineSpeed * 0.12);345 const cardBlur = Math.min(10, cardV * 0.1);346 const rim = Math.min(1, cardV / 18);347348 const sentence = (349 <div350 style={{351 position: "absolute",352 inset: 0,353 WebkitMaskImage: `linear-gradient(90deg, transparent 0px, #000 ${(W * 0.1).toFixed(0)}px)`,354 maskImage: `linear-gradient(90deg, transparent 0px, #000 ${(W * 0.1).toFixed(0)}px)`,355 }}356 >357 <div358 style={{359 position: "absolute",360 left: 0,361 top: 0,362 width: W,363 height: H,364 transform: `translateX(${off.toFixed(2)}px)`,365 filter: lineBlur > 0.2 ? `url(#lb${uid})` : undefined,366 }}367 >368 {sLines.map((s, li) => {369 const lw = wl(s) * L;370 const y = H / 2 + (li - (sLines.length - 1) / 2) * LLH;371 const before = sLines372 .slice(0, li)373 .reduce((a, q) => a + q.split(" ").length, 0);374 return (375 <div376 key={`sl${li}`}377 style={{378 position: "absolute",379 left: W / 2 - lw / 2,380 top: y - L / 2,381 display: "flex",382 whiteSpace: "pre",383 fontFamily,384 fontWeight: 300,385 fontSize: L,386 lineHeight: 1,387 letterSpacing: `${TRACK}em`,388 color: inkC,389 }}390 >391 {s.split(" ").map((w, wi) => {392 const at = (before + wi) * stagger - 4;393 const e = interpolate(f, [at, at + BEAT.enter], [0, 1], {394 ...CLAMP,395 easing: ENTER,396 });397 return (398 <span399 key={`w${wi}`}400 style={{401 display: "inline-block",402 clipPath: "inset(-40% -20% -0.24em -20%)",403 marginRight:404 wi < s.split(" ").length - 1 ? "0.25em" : 0,405 }}406 >407 <span408 style={{409 display: "inline-block",410 transform: `translateY(${((1 - e) * 0.7).toFixed(4)}em)`,411 opacity: e,412 filter:413 e < 1414 ? `blur(${((1 - e) * 6).toFixed(2)}px)`415 : undefined,416 }}417 >418 {w}419 </span>420 </span>421 );422 })}423 </div>424 );425 })}426 </div>427 </div>428 );429430 const cmdRate = Math.max(1.2, command.length / 11);431 const promptRate = Math.max(1.2, prompt.length / 28);432 const cmdTyped = Math.max(433 0,434 Math.min(command.length, Math.floor((f - BEAT.cmd) * cmdRate)),435 );436 const pTyped = Math.max(437 0,438 Math.min(prompt.length, Math.floor((f - BEAT.prompt) * promptRate)),439 );440 const promptEnd = BEAT.prompt + prompt.length / promptRate;441 const chipPop = spring({442 frame: f - BEAT.chip,443 fps,444 config: { mass: 0.5, damping: 12, stiffness: 200 },445 });446 const chipOn = f >= BEAT.chip;447 const phOp = interpolate(f, [BEAT.cmd - 4, BEAT.cmd], [1, 0], CLAMP);448 const armP = spring({449 frame: f - BEAT.arm,450 fps,451 config: { mass: 0.5, damping: 12, stiffness: 200 },452 });453 const armed = f >= BEAT.arm;454 const attIn = spring({455 frame: f - (BEAT.card + 8),456 fps,457 config: { mass: 0.5, damping: 12, stiffness: 200 },458 });459 const toolIn = [0, 1, 2, 3, 4].map((i) =>460 spring({461 frame: f - (BEAT.card + 12 + i * 2),462 fps,463 config: { mass: 0.5, damping: 12, stiffness: 200 },464 }),465 );466467 let left = pTyped;468 const promptNodes = pLines.map((pl, li) => {469 const take = Math.max(0, Math.min(pl.text.length, left));470 left -= pl.text.length + 1;471 if (take <= 0) return null;472 return (473 <div474 key={`p${li}`}475 style={{476 position: "absolute",477 left: pad + pl.x,478 top: top0 + li * PLH + (PLH - T) / 2,479 fontFamily,480 fontWeight: 400,481 fontSize: T,482 lineHeight: 1,483 letterSpacing: "-0.01em",484 whiteSpace: "pre",485 color: inkC,486 }}487 >488 {pl.text.slice(0, take)}489 </div>490 );491 });492 let caretX =493 pad + chipPadX + m(command.slice(0, cmdTyped), monoFont, 0) * cmdT;494 let caretLi = 0;495 if (f >= BEAT.prompt && pTyped > 0) {496 let rem = pTyped;497 for (let li = 0; li < pLines.length; li++) {498 const len = pLines[li].text.length;499 if (rem <= len || li === pLines.length - 1) {500 caretX =501 pad +502 pLines[li].x +503 pw(pLines[li].text.slice(0, Math.min(rem, len)));504 caretLi = li;505 break;506 }507 rem -= len + 1;508 }509 } else if (f >= BEAT.chip) {510 caretX = pad + firstIndent;511 }512 const typing =513 (f >= BEAT.cmd && f < BEAT.cmd + command.length / cmdRate + 2) ||514 (f >= BEAT.prompt && f < promptEnd + 2);515 const caretOn =516 f >= BEAT.card + 18 && (typing || Math.floor(f / 14) % 2 === 0);517 const caretShow = f < BEAT.arm ? 1 : 0;518519 const chipStyle: CSSProperties = {520 position: "absolute",521 left: pad,522 top: top0 + (PLH - (cmdT + 10 * u)) / 2,523 height: cmdT + 10 * u,524 width: chipW,525 borderRadius: 8 * u,526 background: chipOn ? c.cmdBg : "transparent",527 transform: `scale(${(chipOn ? 0.9 + 0.1 * chipPop : 1).toFixed(4)})`,528 transformOrigin: "0% 50%",529 display: "flex",530 alignItems: "center",531 paddingLeft: chipPadX,532 fontFamily: mono.fontFamily,533 fontWeight: 400,534 fontSize: cmdT,535 lineHeight: 1,536 whiteSpace: "pre",537 color: c.cmdInk,538 };539 const ctrlD = 32 * u;540 const labelF = 15 * u;541 const push = interpolate(f, [0, durationInFrames], [1, 1.03], CLAMP);542 const tiltY = -9 * (1 - Math.min(1, cardSp(f)));543544 const card = (545 <div546 style={{547 position: "absolute",548 left: 0,549 top: 0,550 width: W,551 height: H,552 filter: cardBlur > 0.2 ? `url(#cb${uid})` : undefined,553 }}554 >555 <div556 style={{557 position: "absolute",558 left: cardLeft(f),559 top: H / 2 - CH / 2,560 width: CW,561 height: CH,562 borderRadius: 24 * u,563 background: c.card,564 boxShadow: `inset 0 0 0 1px ${c.border}, inset ${(1.5 * u).toFixed(2)}px 0 0 rgba(${c.rim},${(0.9 * rim).toFixed(3)}), ${c.shadow}`,565 transform: `perspective(${(W * 1.6).toFixed(0)}px) rotateY(${tiltY.toFixed(3)}deg)`,566 transformOrigin: "0% 50%",567 }}568 >569 <div570 style={{571 position: "absolute",572 left: pad,573 top: pad,574 height: attH,575 display: "flex",576 alignItems: "center",577 gap: 10 * u,578 padding: `0 ${16 * u}px 0 ${8 * u}px`,579 borderRadius: 12 * u,580 background: c.tile,581 boxShadow: `inset 0 0 0 1px ${c.faint}`,582 whiteSpace: "pre",583 opacity: Math.min(1, attIn * 1.5),584 transform: `scale(${(0.92 + 0.08 * attIn).toFixed(4)})`,585 transformOrigin: "0% 50%",586 }}587 >588 <div589 style={{590 width: 36 * u,591 height: 36 * u,592 borderRadius: 8 * u,593 background: c.cmdBg,594 display: "flex",595 alignItems: "center",596 justifyContent: "center",597 }}598 >599 <Icon size={20 * u} color={c.cmdInk}>600 <path d="M14 3.5H7.5a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2V8z" />601 <path d="M14 3.5V8h4.5M9 13h6M9 16.5h4" />602 </Icon>603 </div>604 <div605 style={{606 display: "flex",607 flexDirection: "column",608 gap: 3 * u,609 fontFamily,610 fontWeight: 400,611 lineHeight: 1.1,612 }}613 >614 <div615 style={{616 fontSize: labelF,617 color: inkC,618 letterSpacing: "-0.005em",619 }}620 >621 {attachName}622 </div>623 <div style={{ fontSize: 13 * u, color: c.muted }}>624 {attachMeta}625 </div>626 </div>627 </div>628 <div629 style={{630 position: "absolute",631 left: pad,632 top: top0 + (PLH - T) / 2,633 fontFamily,634 fontWeight: 400,635 fontSize: T,636 lineHeight: 1,637 letterSpacing: "-0.01em",638 whiteSpace: "pre",639 color: c.muted,640 opacity: phOp,641 }}642 >643 {placeholder}644 </div>645 {cmdTyped > 0 ? (646 <div style={chipStyle}>{command.slice(0, cmdTyped)}</div>647 ) : null}648 {promptNodes}649 <div650 style={{651 position: "absolute",652 left: caretX + T * 0.04,653 top: top0 + caretLi * PLH + (PLH - T * 1.2) / 2,654 width: Math.max(1.5, T * 0.06),655 height: T * 1.2,656 borderRadius: T,657 background: inkC,658 opacity: caretOn ? caretShow : 0,659 }}660 />661 <div662 style={{663 position: "absolute",664 left: pad * 0.8,665 right: pad * 0.8,666 bottom: pad * 0.8,667 height: barH,668 display: "flex",669 alignItems: "center",670 gap: 8 * u,671 fontFamily,672 fontWeight: 400,673 fontSize: labelF,674 color: c.muted,675 }}676 >677 <div678 style={{679 width: ctrlD,680 height: ctrlD,681 borderRadius: ctrlD,682 boxShadow: `inset 0 0 0 1px ${c.faint}`,683 display: "flex",684 alignItems: "center",685 justifyContent: "center",686 transform: `scale(${toolIn[0].toFixed(4)})`,687 }}688 >689 <Icon size={18 * u} color={c.muted}>690 <path d="M12 5v14M5 12h14" />691 </Icon>692 </div>693 <div694 style={{695 height: ctrlD,696 padding: `0 ${12 * u}px 0 ${10 * u}px`,697 borderRadius: ctrlD,698 boxShadow: `inset 0 0 0 1px ${c.faint}`,699 display: "flex",700 alignItems: "center",701 gap: 6 * u,702 whiteSpace: "pre",703 transform: `scale(${toolIn[1].toFixed(4)})`,704 transformOrigin: "0% 50%",705 }}706 >707 <Icon size={16 * u} color={c.muted}>708 <circle cx="12" cy="12" r="8.5" />709 <path d="M12 3.5v17M3.5 12h8.5" />710 </Icon>711 {chipLabel}712 </div>713 <div style={{ flex: 1 }} />714 <div715 style={{716 display: "flex",717 alignItems: "center",718 gap: 4 * u,719 whiteSpace: "pre",720 opacity: Math.min(1, toolIn[2]),721 }}722 >723 {modelLabel}724 <Icon size={14 * u} color={c.muted}>725 <path d="M7 10l5 5 5-5" />726 </Icon>727 </div>728 <div729 style={{730 width: 34 * u,731 height: 34 * u,732 borderRadius: 34 * u,733 flexShrink: 0,734 background: armed ? hi : c.send,735 boxShadow: armed736 ? "inset 0 1px 0 rgba(255,255,255,0.22)"737 : "none",738 display: "flex",739 alignItems: "center",740 justifyContent: "center",741 transform: `scale(${(toolIn[4] * (armed ? 0.9 + 0.1 * armP : 1)).toFixed(4)})`,742 }}743 >744 <Icon size={18 * u} color={armed ? "#ffffff" : c.muted}>745 <path d="M12 19V5M6 11l6-6 6 6" />746 </Icon>747 </div>748 </div>749 </div>750 </div>751 );752753 content = (754 <>755 <svg width="0" height="0" style={{ position: "absolute" }} aria-hidden>756 <filter id={`lb${uid}`} x="-5%" y="-5%" width="110%" height="110%">757 <feGaussianBlur stdDeviation={`${lineBlur.toFixed(2)} 0`} />758 </filter>759 <filter id={`cb${uid}`} x="-5%" y="-5%" width="110%" height="110%">760 <feGaussianBlur stdDeviation={`${cardBlur.toFixed(2)} 0`} />761 </filter>762 </svg>763 <AbsoluteFill style={{ transform: `scale(${push.toFixed(5)})` }}>764 {sentence}765 {card}766 </AbsoluteFill>767 </>768 );769 }770771 return (772 <AbsoluteFill style={{ background: bg, overflow: "hidden" }}>773 {background === undefined ? (774 <>775 <GshGround776 preset={preset}777 params={gParams}778 timeOffset={c.groundOffset}779 timeScale={0.6}780 />781 <AbsoluteFill782 style={{783 background: `radial-gradient(${portrait ? "90% 50%" : "70% 64%"} at 50% 50%, rgba(${c.scrim},${c.scrimAlpha}) 0%, rgba(${c.scrim},0) 100%)`,784 }}785 />786 </>787 ) : null}788 <ScaleLayer scale={scale}>{content}</ScaleLayer>789 </AbsoluteFill>790 );791}792793export default CardPushLine;









