{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "word-gather",
  "title": "Word Gather",
  "description": "Word-by-word sentence animation: words fly in out of order and settle into the line. A sentence that arrives one word at a time, out of order, and gathers. The first word holds the middle of the frame on its own; then the rest drop in around it — not in reading order and not where they belong — each landing near its slot in the accent colour and sliding the last of the way home, turning to ink as it settles. The line makes room for them as they come, and when the last one is down the whole block eases back to its real size. The layout is solved here rather than left to the browser, because a word has to be transformed from somewhere else to its slot and knowing where its slot is means having laid the line out yourself — which also means the line break cannot move while the block's scale is changing under it.",
  "dependencies": [
    "remotion",
    "@remotion/google-fonts"
  ],
  "registryDependencies": [
    "https://snapcn.dev/r/snap-cn-ui.json"
  ],
  "files": [
    {
      "path": "registry/snap-cn/word-gather/index.tsx",
      "content": "// Word Gather · snapcn — https://snapcn.dev/docs/text/word-gather\n\"use client\";\n\nimport { loadFont as loadSans } from \"@remotion/google-fonts/Figtree\";\nimport { useEffect, useMemo, useState } from \"react\";\nimport { AbsoluteFill, useCurrentFrame, useVideoConfig } from \"remotion\";\nimport {\n  resolveFont,\n  type SnapCnTheme,\n  useSnapCnTheme,\n} from \"@/lib/snap-cn-ui\";\n\n/**\n * A sentence that arrives one word at a time, out of order, and gathers.\n *\n * The first word holds the middle of the frame on its own. Then the rest of the\n * sentence drops in around it — not in reading order, and not where it belongs\n * — each word landing somewhere near its slot in the accent colour and sliding\n * the last of the way home, turning to ink as it settles. The line makes room\n * for them as they come, and when the last one is down the whole block eases\n * back to its real size.\n *\n * ## Two layouts, and one word that moves between them\n *\n * The scene is only ever in one of two arrangements: the lead word alone,\n * centred on the card, or the whole sentence wrapped and centred. Every other\n * word appears in the second one and never sees the first. The lead word is the\n * only thing that has to travel between them — and it does so on two separate\n * clocks, which is measured, not invented: it drops onto its line in seven\n * frames and slides left across the line over twenty-one, because one of those\n * is a line changing and the other is a line reflowing.\n *\n * ## The layout is arithmetic, not the browser's\n *\n * Word advances come from `measureText` and the wrap, the centring and every\n * slot are computed here. That is not a preference either: a word has to be\n * transformed from somewhere else to its slot, and to know where its slot *is*\n * you have to have laid the line out yourself. It also means the line break\n * cannot move on you frame to frame — the layout is solved once, at scale 1,\n * and the block's own scale is a transform over the top of it.\n */\n\nconst { fontFamily: SANS } = loadSans(\"normal\", {\n  weights: [\"400\", \"500\", \"600\"],\n  subsets: [\"latin\"],\n});\n\n/* ─────────────────────────────────────────────────────────────────────────\n   The card, in the reference's own 710 × 388 pixels\n   ───────────────────────────────────────────────────────────────────────── */\n\nexport const REF_W = 710;\nexport const REF_H = 388;\nexport const CENTRE_X = REF_W / 2;\n/**\n * Where the lines are centred, which is not quite the middle of the card.\n *\n * A line is centred on its advance box and read back as its ink, and the two\n * differ by half the difference of the first character's left bearing and the\n * last one's right — a face-dependent three and a half pixels here.\n */\nexport const LINE_CX = REF_W / 2 - 3.53;\nexport const CENTRE_Y = REF_H / 2;\n/** The card's corner, measured off the coverage of its own antialiasing. */\nexport const RADIUS = 5.5;\n/** The recording runs out here. */\nexport const END = 49;\n\n/**\n * The recording's own clock, as the beat a composition frame falls on.\n *\n * It was captured at about 24 frames a second with a screen recorder's jitter\n * and this plays at 30, so the two only line up every fifth frame. Every table\n * below is in *beats* rather than frames for that reason: it is the only way a\n * number measured off frame 23 of the recording can be the number this scene is\n * on when frame 23 of the recording is what it is being compared to.\n */\nexport const BEATS = 39;\nexport const BEAT: readonly Pt[] = [\n  [0, 0],\n  [1, 1],\n  [2, 2],\n  [3, 3],\n  [4, 4],\n  [6, 5],\n  [7, 6],\n  [8, 7],\n  [9, 8],\n  [10, 9],\n  [12, 10],\n  [13, 11],\n  [14, 12],\n  [15, 13],\n  [17, 14],\n  [18, 15],\n  [19, 16],\n  [20, 17],\n  [22, 18],\n  [23, 19],\n  [24, 20],\n  [26, 21],\n  [27, 22],\n  [28, 23],\n  [29, 24],\n  [31, 25],\n  [32, 26],\n  [33, 27],\n  [34, 28],\n  [35, 29],\n  [37, 30],\n  [38, 31],\n  [39, 32],\n  [41, 33],\n  [42, 34],\n  [43, 35],\n  [44, 36],\n  [45, 37],\n  [47, 38],\n  [48, 39],\n];\n\nexport type Pt = readonly [number, number];\n\n/** Linear read of a `[key, value]` table, clamped at both ends. */\nexport function track(table: readonly Pt[], at: number): number {\n  const first = table[0];\n  const last = table[table.length - 1];\n  if (!first || !last) return 0;\n  if (at <= first[0]) return first[1];\n  if (at >= last[0]) return last[1];\n  for (let i = 0; i < table.length - 1; i++) {\n    const a = table[i];\n    const b = table[i + 1];\n    if (!a || !b) break;\n    if (at <= b[0]) {\n      const t = (at - a[0]) / (b[0] - a[0]);\n      return a[1] + (b[1] - a[1]) * t;\n    }\n  }\n  return last[1];\n}\n\n/* ─────────────────────────────────────────────────────────────────────────\n   Type\n   ───────────────────────────────────────────────────────────────────────── */\n\n/** Cap height and line spacing of the settled block, at scale 1. */\nexport const CAP = 35.82;\nexport const LINE_H = 48.18;\n/**\n * How far the sentence block sits below where the lead word sat alone.\n *\n * Measured, and not a rounding error: the lead word's cap box is centred on the\n * card to within a tenth of a pixel, and the two-line block that replaces it\n * sits nearly six pixels lower than the same rule would put it. Left out, every\n * baseline in the scene is six pixels high.\n */\nexport const DROP = 5.77;\n/** The face's cap height, which is what `CAP` is turned into a font size with. */\nexport const CAP_RATIO = 0.74909;\n/** The face's own hhea metrics: at `lineHeight: LEADING` the baseline is here. */\nexport const ASC = 0.9527;\nexport const LEADING = 1.2103;\nexport const TRACKING = -0.055;\n/**\n * And an extra trim on the space, on top of the tracking.\n *\n * The reference sets its words 0.15 of a cap height apart. The face's own space\n * is more than twice that, and tracking tight enough to close it would take the\n * letters with it — so the two are separate numbers, which is what a typesetter\n * would call word-spacing and letter-spacing and is exactly the distinction.\n */\nexport const SPACE_TRIM = -0.0549;\n\n/* ─────────────────────────────────────────────────────────────────────────\n   The clock\n   ───────────────────────────────────────────────────────────────────────── */\n\n/** The beat the second word drops in on, and the gap between the ones after. */\nexport const FIRST_AT = 20;\nexport const STAGGER = 1;\n/** How many beats a word stays in the accent colour after it drops in. */\nexport const SETTLE = 4;\n/** The lead word starts this far above its own resting place. */\nexport const LEAD_RISE = 20.6;\n\n/**\n * How far a word has travelled from where it dropped in to its slot, against\n * beats since it dropped.\n *\n * It is an exponential settle — about 0.7 of the remaining distance left every\n * beat — and the five words that do it are within 0.07 of each other on the\n * first beat and 0.02 by the fourth, which is the capture's sub-frame jitter\n * and not five different curves.\n */\nexport const TRAVEL: readonly Pt[] = [\n  [0, 0],\n  [1, 0.391],\n  [2, 0.563],\n  [3, 0.686],\n  [4, 0.77],\n  [5, 0.836],\n  [6, 0.887],\n  [7, 0.928],\n  [8, 0.96],\n  [9, 0.982],\n  [10, 0.991],\n  [12, 0.996],\n  [14, 1],\n];\n\n/**\n * The lead word sliding across the line, and dropping onto it.\n *\n * Two tables because the reference has two moves. `LEAD_X` is the line\n * reflowing — an ease *in* and out, twenty-one beats long, which is what a line\n * that has just been told it is about to get five more words does. `LEAD_Y` is\n * the block going from one line to two, and it is over in six.\n */\nexport const LEAD_X: readonly Pt[] = [\n  [10, 0],\n  [11, 0.004],\n  [12, 0.007],\n  [13, 0.02],\n  [14, 0.036],\n  [15, 0.063],\n  [16, 0.107],\n  [17, 0.18],\n  [18, 0.317],\n  [19, 0.547],\n  [20, 0.701],\n  [21, 0.793],\n  [22, 0.852],\n  [23, 0.892],\n  [24, 0.921],\n  [25, 0.946],\n  [26, 0.959],\n  [27, 0.97],\n  [28, 0.98],\n  [29, 0.99],\n  [30, 0.992],\n  [31, 1],\n];\n\nexport const LEAD_Y: readonly Pt[] = [\n  [0, 0],\n  [1, 0.131],\n  [2, 0.211],\n  [3, 0.27],\n  [4, 0.329],\n  [5, 0.35],\n  [6, 0.391],\n  [7, 0.411],\n  [8, 0.4341],\n  [9, 0.47],\n  [10, 0.514],\n  [11, 0.612],\n  [12, 0.817],\n  [13, 0.905],\n  [14, 0.949],\n  [15, 0.985],\n  [16, 1],\n];\n\n/**\n * The block's own scale.\n *\n * It holds at 1.289 while the words are still coming and then falls to 1 over\n * ten beats, with the whole middle of that fall inside two of them. The first\n * eighteen are flat because they are measured off one word's ink width and\n * wobble a third of a percent; the fall is every word at once and does not.\n */\nexport const SCALE: readonly Pt[] = [\n  [0, 1.289],\n  [18, 1.289],\n  [19, 1.2798],\n  [20, 1.2782],\n  [21, 1.2727],\n  [22, 1.264],\n  [23, 1.2546],\n  [24, 1.2414],\n  [25, 1.2184],\n  [26, 1.159],\n  [27, 1.0689],\n  [28, 1.0417],\n  [29, 1.0275],\n  [30, 1.0193],\n  [31, 1.0132],\n  [32, 1.0081],\n  [33, 1.0064],\n  [34, 1],\n  [BEATS, 1],\n];\n\n/**\n * Where each word drops in, relative to its own slot, in card pixels.\n *\n * Measured off the frame each word first appears on, and indexed by the word's\n * place in the sentence rather than by the order it drops in. There is no\n * pattern in them and there is not meant to be — three of the five come in flat\n * from the side and two fall past their line — so they are a table and not a\n * formula, and a sentence longer than the reference's simply reuses them.\n */\nexport const SCATTER: readonly Pt[] = [\n  [44.4, -59.2],\n  [73.8, -1],\n  [-31.7, -1.7],\n  [12.4, 41],\n  [96.2, -1.7],\n];\n\n/**\n * The order the words drop in, as offsets into the sentence.\n *\n * The last word first, then the third, the first, the fourth, the second — so\n * the sentence is never briefly readable and never briefly wrong, it is just\n * scattered until it is not.\n */\nexport const ORDER: readonly number[] = [5, 3, 1, 4, 2];\n\n/**\n * The order `n` words after the lead drop in.\n *\n * The reference's own order where it fits, folded into range where it does not,\n * and then whatever is left in reading order. It has to be a permutation — one\n * repeated index is a word that never drops and a hole in the sentence — which\n * is why it is built as a list and not as arithmetic.\n */\nexport function dropOrder(n: number): number[] {\n  const out: number[] = [];\n  const seen = new Set<number>();\n  const take = (i: number) => {\n    if (i >= 1 && i <= n && !seen.has(i)) {\n      seen.add(i);\n      out.push(i);\n    }\n  };\n  for (const seed of ORDER) take(seed <= n ? seed : ((seed - 1) % n) + 1);\n  for (let i = 1; i <= n; i++) take(i);\n  return out;\n}\n\n/** Which word drops in `k`th. */\nexport function orderAt(k: number, n: number): number {\n  return dropOrder(n)[k] ?? k + 1;\n}\n\n/* ─────────────────────────────────────────────────────────────────────────\n   Layout\n   ───────────────────────────────────────────────────────────────────────── */\n\nconst MEASURE_FALLBACK = 0.52;\n\n/** Advance width of each word, from canvas, with an SSR-safe estimate. */\nexport function measureWords(\n  words: string[],\n  fontSize: number,\n  weight: number,\n  face: string,\n): { widths: number[]; space: number } {\n  const guess = {\n    widths: words.map((w) => w.length * fontSize * MEASURE_FALLBACK),\n    space: fontSize * 0.26,\n  };\n  if (typeof document === \"undefined\") return guess;\n  const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n  if (!ctx) return guess;\n  ctx.font = `${weight} ${fontSize}px ${face}`;\n  return {\n    widths: words.map((w) => ctx.measureText(w).width),\n    space: ctx.measureText(\" \").width,\n  };\n}\n\nexport type Slot = { x: number; baseline: number };\n\n/**\n * The sentence, wrapped greedily and centred line by line.\n *\n * `maxWidth` is in card pixels at scale 1, so it is the same number whatever\n * the block's scale is doing — which is the only reason the line break holds\n * still while the block shrinks by a third.\n */\nexport function solve(\n  widths: number[],\n  space: number,\n  maxWidth: number,\n): Slot[] {\n  const lines: number[][] = [[]];\n  let run = 0;\n  for (let i = 0; i < widths.length; i++) {\n    const w = widths[i] ?? 0;\n    const line = lines[lines.length - 1] as number[];\n    const add = (line.length ? space : 0) + w;\n    if (line.length > 0 && run + add > maxWidth) {\n      lines.push([i]);\n      run = w;\n    } else {\n      line.push(i);\n      run += add;\n    }\n  }\n  const top = CENTRE_Y + CAP / 2 + DROP - ((lines.length - 1) * LINE_H) / 2;\n  const out: Slot[] = [];\n  lines.forEach((line, k) => {\n    let total = 0;\n    for (const i of line) total += widths[i] ?? 0;\n    total += space * (line.length - 1);\n    let x = LINE_CX - total / 2;\n    for (const i of line) {\n      out[i] = { x, baseline: top + k * LINE_H };\n      x += (widths[i] ?? 0) + space;\n    }\n  });\n  return out;\n}\n\n/* ─────────────────────────────────────────────────────────────────────────\n   The component\n   ───────────────────────────────────────────────────────────────────────── */\n\nexport interface WordGatherProps {\n  /** The sentence. The first word is the one that holds the frame alone. */\n  text?: string;\n  /**\n   * Where the line breaks, in card pixels at scale 1. Wider is fewer lines;\n   * the reference breaks a six-word sentence into two.\n   */\n  maxWidth?: number;\n  /** The colour a word wears while it is still travelling. */\n  accent?: string;\n  fontWeight?: number;\n  fontFamily?: string;\n  theme?: Partial<SnapCnTheme>;\n  mode?: \"light\" | \"dark\";\n  speed?: number;\n  className?: string;\n}\n\nexport function WordGather({\n  text = \"Every scene is yours to own.\",\n  maxWidth = 330,\n  accent,\n  fontWeight = 500,\n  fontFamily,\n  theme,\n  mode = \"light\",\n  speed = 1,\n  className,\n}: WordGatherProps) {\n  const frame = useCurrentFrame() * speed;\n  const { width, height } = useVideoConfig();\n  const t = useSnapCnTheme(theme, mode);\n  const face = resolveFont(fontFamily ?? t.fontFamily) ?? SANS;\n  const ink = t.foreground;\n  // The customiser hands an unset colour control through as \"\", not undefined.\n  const lit = accent || t.primary;\n\n  // Carrying each word's place in the sentence as its own id, because a\n  // sentence can say the same word twice and the pair is what identifies it.\n  const items = useMemo(\n    () =>\n      text\n        .split(/\\s+/)\n        .filter(Boolean)\n        .map((word, at) => ({ word, at, id: `${at}:${word}` })),\n    [text],\n  );\n  const words = useMemo(() => items.map((w) => w.word), [items]);\n  const size = CAP / CAP_RATIO;\n\n  // Canvas measurement is not available during SSR, so the first client render\n  // has to use the same estimate the server used or React logs a hydration\n  // mismatch; the precise widths land on the next tick.\n  const [ready, setReady] = useState(false);\n  useEffect(() => setReady(true), []);\n  const { widths, space } = useMemo(() => {\n    const raw = ready\n      ? measureWords(words, size, fontWeight, face)\n      : {\n          widths: words.map((w) => w.length * size * MEASURE_FALLBACK),\n          space: size * 0.26,\n        };\n    // `letter-spacing` lands after every character, the last one included, so a\n    // word's advance grows by its whole length and the space by one.\n    return {\n      widths: raw.widths.map(\n        (w, i) => w + (words[i]?.length ?? 0) * TRACKING * size,\n      ),\n      space: raw.space + (TRACKING + SPACE_TRIM) * size,\n    };\n  }, [ready, words, size, fontWeight, face]);\n  const slots = useMemo(\n    () => solve(widths, space, maxWidth),\n    [widths, space, maxWidth],\n  );\n\n  // Everything below is on the recording's clock, not the composition's.\n  const b = track(BEAT, frame);\n  const s = track(SCALE, b);\n  const lead = slots[0];\n  const aloneX = LINE_CX - (widths[0] ?? 0) / 2;\n  const aloneBase = CENTRE_Y + CAP / 2;\n  const lx = track(LEAD_X, b);\n  const ly = track(LEAD_Y, b);\n\n  // `cover`, so a 16:9 composition crops the card rather than letterboxing it.\n  const k = Math.max(width / REF_W, height / REF_H);\n\n  return (\n    <AbsoluteFill\n      className={className}\n      style={{ background: t.background, overflow: \"hidden\" }}\n    >\n      <AbsoluteFill\n        style={{\n          width: REF_W,\n          height: REF_H,\n          left: (width - REF_W * k) / 2,\n          top: (height - REF_H * k) / 2,\n          transform: `scale(${k})`,\n          transformOrigin: \"top left\",\n          background: t.background,\n          borderRadius: RADIUS,\n          overflow: \"hidden\",\n          fontFamily: face,\n          textRendering: \"geometricPrecision\",\n        }}\n      >\n        {items.map(({ word, at: i, id }) => {\n          const slot = slots[i];\n          if (!slot) return null;\n          let x = slot.x;\n          let base = slot.baseline;\n          let colour = ink;\n          if (i === 0 && lead) {\n            x = slot.x + (aloneX - slot.x) * (1 - lx);\n            base =\n              slot.baseline +\n              (aloneBase - slot.baseline + LEAD_RISE) * (1 - ly);\n          } else {\n            const k2 = dropSlot(i, words.length - 1);\n            const at = FIRST_AT + STAGGER * k2;\n            if (b < at) return null;\n            const p = track(TRAVEL, b - at);\n            const d = SCATTER[(i - 1) % SCATTER.length] ?? [0, 0];\n            x = slot.x + d[0] * (1 - p);\n            base = slot.baseline + d[1] * (1 - p);\n            if (b < at + SETTLE) colour = lit;\n          }\n          return (\n            <Word\n              key={id}\n              text={word}\n              x={CENTRE_X + (x - CENTRE_X) * s}\n              baseline={CENTRE_Y + (base - CENTRE_Y) * s}\n              size={size}\n              scale={s}\n              weight={fontWeight}\n              colour={colour}\n            />\n          );\n        })}\n      </AbsoluteFill>\n    </AbsoluteFill>\n  );\n}\n\n/** Which drop-in slot word `i` of `n` is on. */\nfunction dropSlot(i: number, n: number): number {\n  const k = dropOrder(n).indexOf(i);\n  return k < 0 ? i - 1 : k;\n}\n\n/* ─────────────────────────────────────────────────────────────────────────\n   One word, scaled about its own baseline\n   ───────────────────────────────────────────────────────────────────────── */\n\nfunction Word({\n  text,\n  x,\n  baseline,\n  size,\n  scale,\n  weight,\n  colour,\n}: {\n  text: string;\n  x: number;\n  baseline: number;\n  size: number;\n  scale: number;\n  weight: number;\n  colour: string;\n}) {\n  const rise = ASC * size;\n  return (\n    <div\n      style={{\n        position: \"absolute\",\n        left: x,\n        top: baseline - rise,\n        width: \"max-content\",\n      }}\n    >\n      <div\n        style={{\n          transform: `scale(${scale})`,\n          transformOrigin: `0 ${rise}px`,\n          fontSize: size,\n          lineHeight: LEADING,\n          fontWeight: weight,\n          letterSpacing: `${TRACKING}em`,\n          color: colour,\n          whiteSpace: \"pre\",\n        }}\n      >\n        {text}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/snap-cn/word-gather.tsx"
    }
  ],
  "docs": "Docs and props: https://snapcn.dev/docs/text/word-gather?ref=cli — text is the sentence and its first word is the one that holds the frame alone; maxWidth is where the line breaks, in card pixels; accent is the colour a word wears while it is still travelling. Pass mode=\"dark\" if you are rendering onto a dark frame — these default to shadcn's light palette. — 82 Pro components too — charts, launch scenes, device shots: https://snapcn.dev/docs/pricing?ref=cli-docs",
  "type": "registry:component"
}
