{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "overheard",
  "title": "Overheard",
  "description": "A question quote, \"Who made our launch video?\", answered by \"I did. It's in the repo.\".",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/overheard/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/YoungSerif\";\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst serif = loadFont(\"normal\", { weights: [\"400\"], subsets: [\"latin\"] });\n\nexport interface OverheardLine {\n  quote: string;\n  name: string;\n}\n\nexport interface OverheardProps {\n  lines?: OverheardLine[];\n  highlight?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst DEFAULT_LINES: OverheardLine[] = [\n  { quote: \"Who made our launch video?\", name: \"Mia Okafor, CEO\" },\n  { quote: \"I did. It’s in the repo.\", name: \"Leo Marsh, engineer\" },\n];\n\nconst THEMES = {\n  dark: { bg: \"#101828\", ink: \"#BFE3FF\", accent: \"#F6C453\" },\n  light: { bg: \"#CFE8FF\", ink: \"#101828\", accent: \"#A66E00\" },\n} as const;\n\nconst TRACK = -0.02;\nconst LH = 1.1;\nconst WORD = 4;\nconst PAUSE = 10;\nconst BETWEEN = 26;\nconst RISE = 14;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\ninterface Word {\n  text: string;\n  hot: boolean;\n}\n\nfunction stripQuotes(s: string) {\n  return s\n    .trim()\n    .replace(/^[\"“”]+/, \"\")\n    .replace(/[\"“”]+$/, \"\");\n}\n\nfunction wordsOf(quote: string, highlight: string): Word[] {\n  const body = stripQuotes(quote);\n  const at = highlight ? body.indexOf(highlight) : -1;\n  const parts = body.split(\" \").filter(Boolean);\n  let cursor = 0;\n  return parts.map((part, i) => {\n    const s = body.indexOf(part, cursor);\n    const e = s + part.length;\n    cursor = e;\n    let text = part;\n    if (i === 0) text = `“${text}`;\n    if (i === parts.length - 1) text = `${text}”`;\n    return { text, hot: at >= 0 && s < at + highlight.length && e > at };\n  });\n}\n\nfunction place(\n  words: Word[],\n  adv: (s: string) => number,\n  space: number,\n  maxEm: number,\n) {\n  const pos: { x: number; line: number }[] = [];\n  const widths: number[] = [0];\n  let line = 0;\n  let x = 0;\n  for (const wd of words) {\n    const w = adv(wd.text);\n    if (x > 0 && x + space + w > maxEm) {\n      line++;\n      widths.push(0);\n      pos.push({ x: 0, line });\n      x = w;\n    } else {\n      const gap = x === 0 ? 0 : space;\n      pos.push({ x: x + gap, line });\n      x += gap + w;\n    }\n    widths[line] = x;\n  }\n  return { pos, lines: line + 1, widths };\n}\n\nfunction wrap(\n  toks: Word[],\n  adv: (s: string) => number,\n  space: number,\n  maxEm: number,\n) {\n  const greedy = place(toks, adv, space, maxEm);\n  const L = greedy.lines;\n  if (L <= 1) return greedy;\n  const n = toks.length;\n  const ws = toks.map((tk) => adv(tk.text));\n  const glued = (_k: number) => false;\n  const lineW = (i: number, j: number) => {\n    let x = 0;\n    for (let k = i; k < j; k++) x += (k === i || glued(k) ? 0 : space) + ws[k];\n    return x;\n  };\n  const avg = lineW(0, n) / L;\n  const INF = 1e12;\n  const dp = Array.from({ length: L + 1 }, () =>\n    new Array<number>(n + 1).fill(INF),\n  );\n  const back = Array.from({ length: L + 1 }, () =>\n    new Array<number>(n + 1).fill(0),\n  );\n  dp[0][0] = 0;\n  for (let l = 1; l <= L; l++) {\n    for (let j = 1; j <= n; j++) {\n      if (j < n && glued(j)) continue;\n      for (let i = l - 1; i < j; i++) {\n        if (dp[l - 1][i] >= INF) continue;\n        const lw = lineW(i, j);\n        if (lw > maxEm + 1e-6) continue;\n        let cost = (lw - avg) ** 2;\n        if (j < n) {\n          const end = toks[j - 1].text;\n          if (/[.?!…][\"”’]?$/.test(end)) cost -= avg * avg * 0.5;\n          else if (/[,;:—–][\"”’]?$/.test(end)) cost -= avg * avg * 0.15;\n        }\n        if (dp[l - 1][i] + cost < dp[l][j]) {\n          dp[l][j] = dp[l - 1][i] + cost;\n          back[l][j] = i;\n        }\n      }\n    }\n  }\n  if (dp[L][n] >= INF) return greedy;\n  const starts: number[] = [];\n  let j = n;\n  for (let l = L; l > 0; l--) {\n    const i = back[l][j];\n    starts.unshift(i);\n    j = i;\n  }\n  const pos: { x: number; line: number }[] = [];\n  const widths: number[] = [];\n  starts.forEach((s, l) => {\n    const e = l + 1 < starts.length ? starts[l + 1] : n;\n    let x = 0;\n    for (let k = s; k < e; k++) {\n      const gap = k === s || glued(k) ? 0 : space;\n      pos.push({ x: x + gap, line: l });\n      x += gap + ws[k];\n    }\n    widths.push(x);\n  });\n  return { pos, lines: L, widths };\n}\n\nfunction useWidths(strings: string[]) {\n  const key = strings.join(\"\\u0001\");\n  const [state, setState] = useState<{\n    key: string;\n    w: Record<string, number>;\n  } | null>(null);\n  const [handle] = useState(() => delayRender(\"overheard: measuring\"));\n  const released = useRef(false);\n  const ref = useRef(strings);\n  ref.current = strings;\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      serif.waitUntilDone(),\n      document.fonts.load(`400 100px \"${serif.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n        const w: Record<string, number> = {};\n        if (ctx) ctx.font = `400 100px \"${serif.fontFamily}\"`;\n        for (const s of ref.current) {\n          w[s] = ctx ? ctx.measureText(s).width / 100 : s.length * 0.5;\n        }\n        setState({ key, w });\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [key, handle]);\n  return state && state.key === key ? state.w : null;\n}\n\nexport function Overheard({\n  lines = DEFAULT_LINES,\n  highlight = \"It’s in the repo.\",\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: OverheardProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, durationInFrames: T } = useVideoConfig();\n  const c = THEMES[theme];\n  const hi = accent ?? c.accent;\n  const stage =\n    background === \"transparent\" ? \"transparent\" : (background ?? c.bg);\n\n  const list = lines.length > 0 ? lines : DEFAULT_LINES;\n  const n = list.length;\n  const voices = list.map((l, i) =>\n    wordsOf(l.quote, i === n - 1 ? highlight : \"\"),\n  );\n  const strings = Array.from(\n    new Set([\n      \" \",\n      ...voices.flatMap((v) => v.map((wd) => wd.text)),\n      ...list.map((l) => `— ${l.name}`),\n    ]),\n  );\n  const w = useWidths(strings);\n\n  if (!w) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const adv = (s: string) => (w[s] ?? 0) + TRACK * Array.from(s).length;\n  const space = w[\" \"] ?? 0.25;\n\n  const portrait = H > W * 1.1;\n  const unit = Math.min(W, H);\n  const marginX = W * (portrait ? 0.09 : 0.085);\n  const marginY = H * (portrait ? 0.1 : 0.1);\n  const measure = W * (portrait ? 0.82 : 0.6);\n  const maxLines = portrait ? 4 : 2;\n  const nameRatio = 0.36;\n  const nameGap = 0.35;\n\n  const blockH = (lineCount: number, F: number) =>\n    lineCount * LH * F + F * (nameGap + nameRatio * 1.3);\n  let F = portrait ? W * 0.12 : H * 0.13;\n  let lays = voices.map((v) => wrap(v, adv, space, measure / F));\n  for (let k = 0; k < 60; k++) {\n    const most = Math.max(...lays.map((l) => l.lines));\n    const total = lays.reduce((s, l) => s + blockH(l.lines, F), 0);\n    const room = H - marginY * 2 - F * 0.4 * (n - 1);\n    if (most <= maxLines && total <= room) break;\n    F *= 0.96;\n    lays = voices.map((v) => wrap(v, adv, space, measure / F));\n  }\n  const L = Math.max(16, Math.max(unit * 0.03, F * nameRatio));\n\n  const natural: number[][] = [];\n  const nameAt: number[] = [];\n  const starts: number[] = [];\n  let clock = 6;\n  voices.forEach((v, i) => {\n    starts.push(clock);\n    const times: number[] = [];\n    v.forEach((wd) => {\n      times.push(clock);\n      clock += WORD;\n      if (/[.?!,…]”?$/.test(wd.text)) clock += PAUSE;\n    });\n    natural.push(times);\n    nameAt.push(clock + 2);\n    clock += i < n - 1 ? BETWEEN : 0;\n  });\n  const speechEnd = clock + RISE;\n  const room = Math.max(1, T - Math.max(30, T * 0.2));\n  const k = speechEnd > room ? room / speechEnd : 1;\n\n  const heights = lays.map((l) => blockH(l.lines, F));\n  const free = H - marginY * 2 - heights.reduce((s, h) => s + h, 0);\n  const tops: number[] = [];\n  let y = marginY;\n  heights.forEach((h, i) => {\n    tops.push(y);\n    y += h + (n > 1 ? free / (n - 1) : 0);\n    if (i === 0 && n === 1) tops[0] = (H - h) / 2;\n  });\n\n  const lineH = LH * F;\n\n  return (\n    <AbsoluteFill\n      style={{\n        background: stage,\n        overflow: \"hidden\",\n        fontFamily: serif.fontFamily,\n        fontWeight: 400,\n      }}\n    >\n      {voices.map((v, i) => {\n        const right = i % 2 === 1;\n        const lay = lays[i];\n        const next = i < n - 1 ? starts[i + 1] * k : 1e6;\n        const back = interpolate(t, [next, next + 16], [0, 1], {\n          ...CLAMP,\n          easing: EASE_IO,\n        });\n        const dim = 1 - back * 0.55;\n        const nIn = interpolate(\n          t,\n          [nameAt[i] * k, nameAt[i] * k + 16],\n          [0, 1],\n          {\n            ...CLAMP,\n            easing: EASE_OUT,\n          },\n        );\n        const nameText = `— ${list[i].name}`;\n        const nameW = adv(nameText) * L;\n        const nameTop = tops[i] + lay.lines * lineH + F * nameGap;\n        return (\n          <div\n            key={`v${i}`}\n            style={{ position: \"absolute\", inset: 0, opacity: dim }}\n          >\n            {v.map((wd, j) => {\n              const p = lay.pos[j];\n              const lw = lay.widths[p.line];\n              const x = right\n                ? W - marginX - (lw - p.x) * F\n                : marginX + p.x * F;\n              const at = natural[i][j] * k;\n              const e = interpolate(t, [at, at + RISE], [0, 1], {\n                ...CLAMP,\n                easing: EASE_OUT,\n              });\n              return (\n                <div\n                  key={`w${j}`}\n                  style={{\n                    position: \"absolute\",\n                    left: x,\n                    top: tops[i] + p.line * lineH,\n                    height: lineH,\n                    lineHeight: `${lineH}px`,\n                    fontSize: F,\n                    whiteSpace: \"pre\",\n                    letterSpacing: `${TRACK}em`,\n                    color: wd.hot ? hi : c.ink,\n                    opacity: e,\n                    transform: `translateY(${((1 - e) * F * 0.28).toFixed(2)}px)`,\n                  }}\n                >\n                  {wd.text}\n                </div>\n              );\n            })}\n            <div\n              style={{\n                position: \"absolute\",\n                left: right ? W - marginX - nameW : marginX,\n                top: nameTop,\n                fontSize: L,\n                lineHeight: `${L * 1.3}px`,\n                whiteSpace: \"pre\",\n                letterSpacing: `${TRACK}em`,\n                color: c.ink,\n                opacity: nIn * 0.7,\n                transform: `translateY(${((1 - nIn) * L * 0.6).toFixed(2)}px)`,\n              }}\n            >\n              {nameText}\n            </div>\n          </div>\n        );\n      })}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/overheard.tsx"
    }
  ],
  "type": "registry:component"
}
