{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"answer-highlight","type":"registry:component","title":"Answer Highlight","description":"AI answer highlight animation, ChatGPT-style chat response with a text selection. A question in a pill, an answer that writes itself a word at a time, and then the beat the whole shot is for: a caret lands mid-sentence and a selection band drags across one statement, wrapping the line the way a real one does — and settles onto the single word that matters.","dependencies":["remotion","@remotion/google-fonts"],"docs":"Needs a Remotion project. If a frame renders black, the component is drawing on a transparent layer: put a background behind it, e.g. <AbsoluteFill style={{ background: \"#faf9f6\" }}><Component /></AbsoluteFill>. Pass mode=\"dark\" for dark frames.","registryDependencies":["https://kuchi.kalarislabs.com/r/kuchi-ui.json"],"files":[{"path":"components/kuchi/answer-highlight.tsx","type":"registry:component","target":"components/kuchi/answer-highlight.tsx","content":"/*! Portions Copyright (c) 2026 Sri Nath (snap-cn). MIT License. See THIRD_PARTY_LICENSES. */\n\"use client\";\r\n\r\nimport { loadFont as loadSans } from \"@remotion/google-fonts/Inter\";\r\nimport {\r\n  AbsoluteFill,\r\n  Easing,\r\n  interpolate,\r\n  useCurrentFrame,\r\n  useVideoConfig,\r\n} from \"remotion\";\r\nimport {\r\n  mixOklch,\r\n  resolveFont,\r\n  type KuchiTheme,\r\n  useKuchiTheme,\r\n  withAlpha,\r\n} from \"@/lib/kuchi-ui\";\r\n\r\n// Loaded through @remotion/google-fonts, never a CSS variable — a Remotion\r\n// bundle has none of the app's CSS, so a `var(--font-…)` gets you the right face\r\n// in the Player and a fallback in the mp4 (design-system rule 4).\r\nconst { fontFamily: SANS } = loadSans(\"normal\", {\r\n  weights: [\"400\", \"500\"],\r\n  subsets: [\"latin\"],\r\n});\r\n\r\n// ---------------------------------------------------------------------------\r\n// Proportions\r\n//\r\n// Fractions of the composition height, never pixels, so the scene holds its\r\n// shape at 720p, 1080p or a vertical crop.\r\n//\r\n// The question pill's numbers are the same ones `agent-steps` carries, measured\r\n// off the same 718x398 recording by the mass of the pill's fill and by\r\n// cross-correlating its column ink under a scale sweep. They are repeated here\r\n// rather than imported because a registry file is copied whole into somebody's\r\n// project — a shared constants module would be a dependency they did not ask\r\n// for. If you change one, change the other.\r\n// ---------------------------------------------------------------------------\r\n\r\n/** Question pill height. 22.0px of 398. */\r\nconst PILL_H = 0.0553;\r\n/** Corner radius as a fraction of pill height. Not a stadium — that would be 0.5. */\r\nconst PILL_R = 0.28;\r\n/** Pill padding either side of the label, as a fraction of pill height. */\r\nconst PILL_PAD = 0.42;\r\n/** Question type size, as a fraction of composition height. */\r\nconst QUESTION_FONT = 0.0223;\r\n/** Answer type size. Bigger than the question: the question is a caption on the\r\n *  thing you are here to read. */\r\nconst ANSWER_FONT = 0.0385;\r\n/**\r\n * Answer line height, as a multiple of its own type size.\r\n *\r\n * Looser than a paragraph would normally want. The selection band fills its\r\n * line box the way a browser's does, so at 1.5 the band on one line sits\r\n * against the type on the next and the whole thing reads as a solid block of\r\n * colour instead of as two selected lines.\r\n */\r\nconst ANSWER_LEADING = 1.64;\r\n/** Answer measure, as a fraction of frame width. Long enough for three or four\r\n *  lines of a real answer, short enough that the eye does not lose the line. */\r\nconst MEASURE = 0.6;\r\n/** Gap between the pill's bottom and the answer's first baseline box. */\r\nconst PILL_GAP = 0.075;\r\n\r\n/** Selection band padding above and below the type, as a fraction of the answer\r\n *  type size — a browser's own selection sits a little proud of the leading. */\r\nconst BAND_PAD_Y = 0.18;\r\n/** Band corner radius, as a fraction of the answer type size. */\r\nconst BAND_R = 0.1;\r\n/** The caret at the leading edge of the drag, as a fraction of the type size. */\r\nconst CARET_W = 0.06;\r\n\r\n// ---------------------------------------------------------------------------\r\n// Motion\r\n//\r\n// The pill's entrance is measured (see `agent-steps`). Everything below it is\r\n// *chosen*, not fitted — there is no reference recording for this scene — so the\r\n// numbers say what they are for rather than quoting an rms.\r\n// ---------------------------------------------------------------------------\r\n\r\n/** The question's entrance: scale, blur and opacity, in seconds. */\r\nconst INTRO_S = 0.375;\r\nconst INTRO_BLUR_S = 0.2;\r\nconst INTRO_SCALE = 1.31;\r\n/** Blur at t=0, as a fraction of composition height. */\r\nconst INTRO_BLUR = 0.0141;\r\n\r\n/**\r\n * One word of the answer's arrival, in seconds, and the gap between two of them.\r\n *\r\n * The stagger is the read: at 0.055s a 30-word answer takes 1.65s, which is fast\r\n * enough to feel generated and slow enough that the eye can start reading before\r\n * it finishes. Each word takes longer to arrive (0.22s) than the gap between two\r\n * — so four or five words are always mid-arrival, which is what stops it looking\r\n * like a ticker.\r\n */\r\nconst WORD_IN_S = 0.22;\r\nconst WORD_STEP_S = 0.055;\r\n/** How far a word rises as it arrives, as a fraction of its own type size. */\r\nconst WORD_RISE = 0.34;\r\n\r\n/**\r\n * Seconds the drag spends on each word of the statement.\r\n *\r\n * A selection is not a fade. The band is painted as a gradient with a hard stop\r\n * inside the word the drag is currently crossing, so the leading edge moves\r\n * *through* a word rather than jumping over it — at 30fps a per-word step would\r\n * advance the edge three whole words a second and read as four chips appearing,\r\n * not as a hand pulling across the line.\r\n *\r\n * 0.11s is a real drag: four words in a little under half a second.\r\n */\r\nconst BAND_STEP_S = 0.11;\r\n\r\n/** The word inside the statement, once the statement is selected. */\r\nconst WORD_BAND_S = 0.2;\r\n/** Word gap, as an em of the answer type. Padding, not margin — see the span. */\r\nconst WORD_GAP = 0.28;\r\n\r\n// ---------------------------------------------------------------------------\r\n\r\nexport interface AnswerHighlightProps {\r\n  /** The prompt, in the pill at the top. */\r\n  question?: string;\r\n  /** The answer. It writes itself a word at a time; line breaks are the\r\n   *  browser's, so write a paragraph and let `measure` set the rag. */\r\n  answer?: string;\r\n  /**\r\n   * The run inside `answer` that gets selected — word for word, as written.\r\n   * A run that is not found simply never highlights, which is the signal that\r\n   * the two strings have drifted apart.\r\n   */\r\n  statement?: string;\r\n  /** The one word inside `statement` that takes the second, stronger band. */\r\n  word?: string;\r\n  /** Seconds the question holds alone before the answer starts. */\r\n  questionHold?: number;\r\n  /** Seconds between the answer finishing and the drag starting. */\r\n  beforeDrag?: number;\r\n  /** Seconds the selected statement holds before the word is picked out. */\r\n  beforeWord?: number;\r\n  /** Seconds per word of the answer's arrival, and of the drag. */\r\n  wordStep?: number;\r\n  dragStep?: number;\r\n  /** Page behind the scene. Defaults to `theme.background`. */\r\n  paperColor?: string;\r\n  /** Pill fill. Defaults to `theme.card`. */\r\n  pillColor?: string;\r\n  /** Question ink. Defaults to `theme.mutedForeground`. */\r\n  questionColor?: string;\r\n  /** Answer ink. Defaults to `theme.foreground`. */\r\n  answerColor?: string;\r\n  /** The selection, the caret and the picked word. Defaults to `theme.primary`. */\r\n  accentColor?: string;\r\n  /** Where the lockup sits, as a fraction of frame height. */\r\n  centerY?: number;\r\n  fontFamily?: string;\r\n  theme?: Partial<KuchiTheme>;\r\n  mode?: \"light\" | \"dark\";\r\n  speed?: number;\r\n}\r\n\r\n// --- Pure helpers (unit-tested) -------------------------------------------\r\n\r\n/**\r\n * The answer, as the tokens the scene actually animates.\r\n *\r\n * Split on whitespace and *keep the punctuation attached to its word*, because\r\n * the selection band is painted per token: a comma that is its own token gets\r\n * its own band and its own arrival, and the band comes apart at every clause.\r\n */\r\nexport function splitWords(text: string): string[] {\r\n  return text.split(/\\s+/).filter(Boolean);\r\n}\r\n\r\n/**\r\n * A token with its outer punctuation taken off, for matching only.\r\n *\r\n * So that `statement=\"keep the timeline declarative\"` finds the answer's\r\n * `declarative,` — writing the comma into the prop to make the match land is\r\n * the kind of thing nobody remembers and everybody debugs twice.\r\n */\r\nexport function bare(wordToken: string): string {\r\n  return wordToken.replace(/^[^\\p{L}\\p{N}]+|[^\\p{L}\\p{N}]+$/gu, \"\");\r\n}\r\n\r\n/**\r\n * Where `phrase` sits in `words`, as a half-open [start, end) word range.\r\n *\r\n * Matched on the joined text rather than word by word so that a phrase written\r\n * with its own internal spacing still lands. Returns null when it is not there —\r\n * the caller draws no band, which makes a drifted prop visible instead of\r\n * highlighting some near-miss run of words.\r\n */\r\nexport function findRun(\r\n  words: string[],\r\n  phrase: string,\r\n): [number, number] | null {\r\n  const target = splitWords(phrase).map(bare);\r\n  if (target.length === 0) return null;\r\n  const wanted = target.join(\" \");\r\n  const haystack = words.map(bare);\r\n  for (let i = 0; i + target.length <= words.length; i += 1) {\r\n    if (haystack.slice(i, i + target.length).join(\" \") === wanted) {\r\n      return [i, i + target.length];\r\n    }\r\n  }\r\n  return null;\r\n}\r\n\r\n/**\r\n * The selection band on one word, as a CSS background.\r\n *\r\n * `across` is how far the drag has crossed this word: nothing at 0, a solid\r\n * band at 1, and in between a hard-edged gradient with the caret sitting just\r\n * past the stop. Returned as `undefined` when there is nothing to paint so the\r\n * span keeps its cheap \"no background\" path.\r\n */\r\nexport function paintBand(\r\n  across: number,\r\n  fill: string,\r\n  caret: string,\r\n  caretWidth: number,\r\n): string | undefined {\r\n  if (across <= 0) return undefined;\r\n  if (across >= 1) return fill;\r\n  const stop = `${(across * 100).toFixed(2)}%`;\r\n  return (\r\n    `linear-gradient(to right, ${fill} 0 ${stop}, ` +\r\n    `${caret} ${stop}, ${caret} calc(${stop} + ${caretWidth.toFixed(2)}px), ` +\r\n    `transparent calc(${stop} + ${caretWidth.toFixed(2)}px))`\r\n  );\r\n}\r\n\r\n/** Progress of a value that starts at `at` and takes `dur` frames. */\r\nexport function ramp(fc: number, at: number, dur: number): number {\r\n  if (dur <= 0) return fc >= at ? 1 : 0;\r\n  return Math.max(0, Math.min(1, (fc - at) / dur));\r\n}\r\n\r\n/**\r\n * The frame the drag's leading edge is on, as a fractional word index.\r\n *\r\n * Fractional on purpose: the caret is drawn on `floor(edge)` and the band on\r\n * everything behind it, so the edge can sit between two words and the bar does\r\n * not jump a whole word at a time on a 30fps clock.\r\n */\r\nexport function dragEdge(\r\n  fc: number,\r\n  at: number,\r\n  stepF: number,\r\n  count: number,\r\n): number {\r\n  if (stepF <= 0) return fc >= at ? count : 0;\r\n  return Math.max(0, Math.min(count, (fc - at) / stepF));\r\n}\r\n\r\n// --- Defaults --------------------------------------------------------------\r\n\r\nconst DEFAULT_ANSWER =\r\n  \"Build one composition per scene and keep the timeline declarative, \" +\r\n  \"so the same props always render the same frames. Anything that reads \" +\r\n  \"the clock at render time is a bug you will find in the export.\";\r\n\r\n// ---------------------------------------------------------------------------\r\n\r\nexport function AnswerHighlight({\r\n  question = \"How should we structure the demo video?\",\r\n  answer = DEFAULT_ANSWER,\r\n  statement = \"keep the timeline declarative\",\r\n  word = \"declarative\",\r\n  questionHold = 0.6,\r\n  beforeDrag = 0.45,\r\n  beforeWord = 0.6,\r\n  wordStep = WORD_STEP_S,\r\n  dragStep = BAND_STEP_S,\r\n  paperColor,\r\n  pillColor,\r\n  questionColor,\r\n  answerColor,\r\n  accentColor,\r\n  centerY = 0.5,\r\n  fontFamily,\r\n  theme: themeOverride,\r\n  mode,\r\n  speed = 1,\r\n}: AnswerHighlightProps) {\r\n  const frame = useCurrentFrame();\r\n  const { fps, height, width } = useVideoConfig();\r\n  const t = useKuchiTheme(themeOverride, mode);\r\n  const face = resolveFont(fontFamily ?? t.fontFamily) ?? SANS;\r\n\r\n  const fc = frame * speed;\r\n  const paper = paperColor ?? t.background;\r\n  const pill = pillColor ?? t.card;\r\n  const askInk = questionColor ?? t.mutedForeground;\r\n  const ink = answerColor ?? t.foreground;\r\n  const accent = accentColor ?? t.primary;\r\n\r\n  // A selection is a *tint of the page*, never the accent at full strength.\r\n  // Primary behind body type at any real opacity puts white-on-blue in the\r\n  // middle of a paragraph and the line stops being readable — which is exactly\r\n  // why a browser's own selection is pale until the window loses focus.\r\n  //\r\n  // Alpha, not `mixOklch`. Mixing a warm off-white (#faf9f6) toward a blue in\r\n  // oklch takes the short way round the hue wheel and passes through green: the\r\n  // first cut of this scene highlighted the answer in pale mint. A tint of a\r\n  // colour over paper is an alpha composite, not a hue interpolation.\r\n  const band = withAlpha(accent, 0.16);\r\n\r\n  const font = ANSWER_FONT * height;\r\n  const words = splitWords(answer);\r\n  const run = findRun(words, statement);\r\n  const wordRun = run ? findRun(words, word) : null;\r\n  // Only a word *inside* the selected statement is picked out. One that happens\r\n  // to appear earlier in the answer is not the one meant.\r\n  const picked =\r\n    wordRun && run && wordRun[0] >= run[0] && wordRun[1] <= run[1]\r\n      ? wordRun\r\n      : null;\r\n\r\n  // --- the clock -----------------------------------------------------------\r\n\r\n  const answerAt = questionHold * fps;\r\n  const stepF = wordStep * fps;\r\n  const answerDone = answerAt + (words.length - 1) * stepF + WORD_IN_S * fps;\r\n  const dragAt = answerDone + beforeDrag * fps;\r\n  const dragStepF = dragStep * fps;\r\n  const runLen = run ? run[1] - run[0] : 0;\r\n  const dragDone = dragAt + runLen * dragStepF;\r\n  const wordAt = dragDone + beforeWord * fps;\r\n\r\n  const edge = run ? dragEdge(fc, dragAt, dragStepF, runLen) : 0;\r\n  const wordIn = Easing.out(Easing.cubic)(ramp(fc, wordAt, WORD_BAND_S * fps));\r\n\r\n  const intro = Easing.out(Easing.cubic)(ramp(fc, 0, INTRO_S * fps));\r\n  const introBlur =\r\n    INTRO_BLUR *\r\n    height *\r\n    (1 - Easing.out(Easing.cubic)(ramp(fc, 0, INTRO_BLUR_S * fps)));\r\n\r\n  return (\r\n    <AbsoluteFill\r\n      style={{\r\n        backgroundColor: paper,\r\n        alignItems: \"center\",\r\n        justifyContent: \"center\",\r\n        overflow: \"hidden\",\r\n      }}\r\n    >\r\n      <div\r\n        style={{\r\n          position: \"absolute\",\r\n          top: `${centerY * 100}%`,\r\n          transform: \"translateY(-50%)\",\r\n          width: MEASURE * width,\r\n          display: \"flex\",\r\n          flexDirection: \"column\",\r\n          alignItems: \"flex-start\",\r\n        }}\r\n      >\r\n        {/* The question. Same pill, same entrance, as `agent-steps`. */}\r\n        <div\r\n          style={{\r\n            opacity: intro,\r\n            filter: introBlur > 0.01 ? `blur(${introBlur}px)` : undefined,\r\n            transform: `scale(${interpolate(intro, [0, 1], [INTRO_SCALE, 1])})`,\r\n            transformOrigin: \"0% 50%\",\r\n            marginBottom: PILL_GAP * height,\r\n          }}\r\n        >\r\n          <div\r\n            style={{\r\n              fontFamily: face,\r\n              fontSize: QUESTION_FONT * height,\r\n              fontWeight: 500,\r\n              lineHeight: 1,\r\n              color: askInk,\r\n              background: pill,\r\n              height: PILL_H * height,\r\n              borderRadius: PILL_R * PILL_H * height,\r\n              padding: `0 ${PILL_PAD * PILL_H * height}px`,\r\n              display: \"flex\",\r\n              alignItems: \"center\",\r\n              whiteSpace: \"nowrap\",\r\n            }}\r\n          >\r\n            {question}\r\n          </div>\r\n        </div>\r\n\r\n        {/* The answer. Every word is its own inline span, which is what lets the\r\n            selection band be a background rather than a measured rectangle: the\r\n            browser wraps the spans, and a band that is painted per word wraps\r\n            with them and breaks square at the line end, exactly like a real one.\r\n            No DOM measurement, and it survives any copy you put in it. */}\r\n        <div\r\n          style={{\r\n            fontFamily: face,\r\n            fontSize: font,\r\n            fontWeight: 400,\r\n            lineHeight: ANSWER_LEADING,\r\n            color: ink,\r\n          }}\r\n        >\r\n          {words.map((w, i) => {\r\n            const arrive = Easing.out(Easing.cubic)(\r\n              ramp(fc, answerAt + i * stepF, WORD_IN_S * fps),\r\n            );\r\n            const inRun = run ? i >= run[0] && i < run[1] : false;\r\n            const isPicked = picked ? i >= picked[0] && i < picked[1] : false;\r\n            // How far the drag has crossed *this* word: 0 not yet, 1 fully past,\r\n            // and everything between is where the leading edge actually is.\r\n            const across =\r\n              inRun && run ? Math.max(0, Math.min(1, edge - (i - run[0]))) : 0;\r\n            const fill = isPicked\r\n              ? withAlpha(accent, 0.16 + 0.14 * wordIn)\r\n              : band;\r\n\r\n            return (\r\n              <span\r\n                // biome-ignore lint/suspicious/noArrayIndexKey: the answer is a fixed ordered token list, so the index is the identity\r\n                key={i}\r\n                style={{\r\n                  display: \"inline-block\",\r\n                  opacity: arrive,\r\n                  transform: `translateY(${(1 - arrive) * WORD_RISE * font}px)`,\r\n                  color: isPicked ? mixOklch(ink, accent, wordIn) : ink,\r\n                  // One background does the band, its leading edge and the\r\n                  // caret: solid behind everything the drag has passed, a hard\r\n                  // stop where it has got to, and a sliver of the accent just\r\n                  // ahead of that stop while it is still moving. A caret drawn\r\n                  // as a separate element would have to be positioned, and\r\n                  // positioning it means measuring the text.\r\n                  background: paintBand(across, fill, accent, CARET_W * font),\r\n                  // The word gap is padding, not margin, so the band is drawn\r\n                  // behind the space as well: a selection over four words comes\r\n                  // out as one unbroken run rather than four chips with holes\r\n                  // where the spaces were.\r\n                  padding: `${BAND_PAD_Y * font}px ${WORD_GAP}em ${BAND_PAD_Y * font}px 0`,\r\n                  borderTopLeftRadius:\r\n                    run && i === run[0] ? BAND_R * font : undefined,\r\n                  borderBottomLeftRadius:\r\n                    run && i === run[0] ? BAND_R * font : undefined,\r\n                  borderTopRightRadius:\r\n                    run && i === run[1] - 1 ? BAND_R * font : undefined,\r\n                  borderBottomRightRadius:\r\n                    run && i === run[1] - 1 ? BAND_R * font : undefined,\r\n                }}\r\n              >\r\n                {w}\r\n              </span>\r\n            );\r\n          })}\r\n        </div>\r\n      </div>\r\n    </AbsoluteFill>\r\n  );\r\n}\r\n"}]}