{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rewrite-bars",
  "title": "Rewrite Bars",
  "description": "Bars sweep over a headline to rewrite it, 'Your launch video, rendered from code.'.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/rewrite-bars/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont as loadFigtree } from \"@remotion/google-fonts/Figtree\";\nimport { loadFont as loadMono } from \"@remotion/google-fonts/GeistMono\";\nimport type React from \"react\";\nimport { Fragment, 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 figtree = loadFigtree(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\nconst mono = loadMono(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface RewriteBarsProps {\n  headlines?: string[];\n  eyebrows?: string[];\n  highlight?: string;\n  align?: \"center\" | \"left\";\n  hold?: number;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst BEAM = [\"#f4f7ff\", \"#8fb4ff\", \"#3d5bff\", \"#1a1660\"] as const;\n\nconst THEMES = {\n  dark: {\n    bg: \"#0A0A0B\",\n    fg: \"#F3F2EF\",\n    eyebrow: \"#8F8E96\",\n    bar: (a: string) =>\n      `linear-gradient(90deg, ${BEAM[3]} 0%, color-mix(in oklab, ${a} 80%, ${BEAM[3]}) 22%, ${BEAM[1]} 40%, ${BEAM[0]} 50%, ${BEAM[1]} 60%, color-mix(in oklab, ${a} 80%, ${BEAM[3]}) 78%, ${BEAM[3]} 100%)`,\n    hl: (a: string) =>\n      `linear-gradient(100deg, ${BEAM[1]} 0%, color-mix(in oklab, ${a} 55%, ${BEAM[1]}) 45%, ${BEAM[0]} 100%)`,\n    ground: (a: string) =>\n      `radial-gradient(70% 55% at 50% 118%, color-mix(in oklab, ${BEAM[2]} 34%, transparent) 0%, color-mix(in oklab, ${a} 12%, transparent) 50%, transparent 78%)`,\n    front: \"rgba(244,247,255,0.95)\",\n  },\n  light: {\n    bg: \"#F5F4F0\",\n    fg: \"#121214\",\n    eyebrow: \"#6E6C67\",\n    bar: (a: string) =>\n      `linear-gradient(90deg, #c9d4ff 0%, ${BEAM[1]} 22%, color-mix(in oklab, ${a} 85%, white) 40%, ${a} 50%, color-mix(in oklab, ${a} 85%, white) 60%, ${BEAM[1]} 78%, #c9d4ff 100%)`,\n    hl: (a: string) =>\n      `linear-gradient(100deg, ${BEAM[3]} 0%, ${a} 50%, #6f8dff 100%)`,\n    ground: (a: string) =>\n      `radial-gradient(60% 50% at 50% 120%, color-mix(in oklab, ${BEAM[1]} 30%, transparent) 0%, color-mix(in oklab, ${a} 6%, transparent) 55%, transparent 80%)`,\n    front: \"rgba(255,255,255,0.95)\",\n  },\n} as const;\n\nconst TRACK = -0.032;\nconst LINE = 1.08;\nconst BAR_H = 0.5;\nconst BAR_MID = 0.34;\n\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_SWEEP = Easing.bezier(0.45, 0, 0.2, 1);\n\nconst RESOLVE = 14;\nconst COLLAPSE = 12;\nconst REFLOW = 14;\nconst LINE_STAGGER = 3;\n\nconst norm = (w: string) => w.toLowerCase().replace(/[^\\p{L}\\p{N}]+/gu, \"\");\n\nfunction useWordWidths(words: string[]) {\n  const key = words.join(\"\\u0001\");\n  const estimate = words.map((w) => w.length * 0.5);\n  const [widths, setWidths] = useState<{\n    key: string;\n    w: number[];\n    space: number;\n  } | null>(null);\n  const [handle] = useState(() => delayRender(\"rewrite-bars: measuring\"));\n  const released = useRef(false);\n\n  useEffect(() => {\n    let live = true;\n    const font = `500 100px \"${figtree.fontFamily}\"`;\n    Promise.all([figtree.waitUntilDone(), document.fonts.load(font)])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n        const list = key.split(\"\\u0001\");\n        let w = list.map((s) => s.length * 0.5);\n        let space = 0.24;\n        if (ctx) {\n          ctx.font = font;\n          w = list.map(\n            (s) => ctx.measureText(s).width / 100 + TRACK * [...s].length,\n          );\n          space = ctx.measureText(\" \").width / 100 + TRACK;\n        }\n        setWidths({ key, w, space });\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [key, handle]);\n\n  return widths && widths.key === key\n    ? widths\n    : { key, w: estimate, space: 0.24 };\n}\n\nfunction balance(w: number[], space: number, lines: number) {\n  const n = w.length;\n  const rowW = (a: number, b: number) => {\n    let s = 0;\n    for (let i = a; i < b; i++) s += w[i] + (i > a ? space : 0);\n    return s;\n  };\n  const best: number[][] = [];\n  const cut: number[][] = [];\n  for (let k = 0; k <= lines; k++) {\n    best.push(new Array(n + 1).fill(Number.POSITIVE_INFINITY));\n    cut.push(new Array(n + 1).fill(0));\n  }\n  best[0][0] = 0;\n  for (let k = 1; k <= lines; k++) {\n    for (let i = 1; i <= n; i++) {\n      for (let j = k - 1; j < i; j++) {\n        const v = Math.max(best[k - 1][j], rowW(j, i));\n        if (v < best[k][i]) {\n          best[k][i] = v;\n          cut[k][i] = j;\n        }\n      }\n    }\n  }\n  const rows: [number, number][] = [];\n  let i = n;\n  for (let k = lines; k >= 1; k--) {\n    const j = cut[k][i];\n    rows.unshift([j, i]);\n    i = j;\n  }\n  return { rows, widest: best[lines][n], rowW };\n}\n\ninterface Line {\n  words: string[];\n  x: number;\n  base: number;\n  w: number;\n}\n\ninterface Layout {\n  lines: Line[];\n  top: number;\n}\n\nexport function RewriteBars({\n  headlines = [\n    \"Scenes you copy, not configure.\",\n    \"Captions that move with the voice.\",\n    \"Your launch video, rendered from code.\",\n  ],\n  eyebrows = [\"01 · Scenes\", \"02 · Captions\", \"03 · Render\"],\n  highlight = \"rendered from code\",\n  align = \"center\",\n  hold = 20,\n  accent = \"#3B5BFD\",\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: RewriteBarsProps) {\n  const frame = useCurrentFrame() * speed;\n  const { width, height } = useVideoConfig();\n  const t = THEMES[theme];\n\n  const heads = headlines.length > 0 ? headlines : [\"remocn\"];\n  const split = heads.map((h) => h.split(/\\s+/).filter(Boolean));\n  const all = split.flat();\n  const measured = useWordWidths(all);\n\n  const portrait = height > width * 1.1;\n  const s = portrait\n    ? (width / 1280) * 1.5\n    : Math.min(width / 1280, height / 720);\n  const maxW = width * (portrait ? 0.84 : 0.8);\n  const maxFs = (portrait ? 104 : 96) * s;\n  const eyeFs = (portrait ? 22 : 18) * s;\n  const hasEyebrows = eyebrows.length > 0;\n\n  let offset = 0;\n  const wordW = split.map((ws) => {\n    const out = measured.w.slice(offset, offset + ws.length);\n    offset += ws.length;\n    return out;\n  });\n  const fitRows = (wsW: number[], fs: number) => {\n    for (let lines = 1; lines <= Math.min(3, wsW.length); lines++) {\n      const b = balance(wsW, measured.space, lines);\n      if (b.widest * fs <= maxW) return b;\n    }\n    return null;\n  };\n  let fs = maxFs;\n  for (let guard = 0; guard < 40; guard++) {\n    if (wordW.every((w) => w.length === 0 || fitRows(w, fs))) break;\n    fs *= 0.95;\n  }\n\n  const layouts: Layout[] = wordW.map((wsW, h) => {\n    const b = fitRows(wsW, fs) ?? balance(wsW, measured.space, 3);\n    const lineH = fs * LINE;\n    const eyeBlock = hasEyebrows ? eyeFs + fs * 0.42 : 0;\n    const blockH = eyeBlock + b.rows.length * lineH;\n    const top = height / 2 - blockH / 2 + eyeBlock;\n    const left = (width - maxW) / 2;\n    const lines = b.rows.map(([a, z], r) => {\n      const w = b.rowW(a, z) * fs;\n      return {\n        words: split[h].slice(a, z),\n        x: align === \"center\" ? width / 2 - w / 2 : left,\n        base: top + r * lineH + fs * 0.86,\n        w,\n      };\n    });\n    return { lines, top };\n  });\n\n  const resolveAt: number[] = [];\n  const collapseAt: number[] = [];\n  const collapseEnd: number[] = [];\n  let tt = 12;\n  for (let h = 0; h < heads.length; h++) {\n    const stagger = LINE_STAGGER * (layouts[h].lines.length - 1);\n    resolveAt.push(tt);\n    collapseAt.push(tt + RESOLVE + stagger + hold);\n    collapseEnd.push(collapseAt[h] + COLLAPSE + stagger);\n    tt = collapseEnd[h] + REFLOW;\n  }\n\n  let cur = 0;\n  for (let h = 0; h < heads.length - 1; h++) {\n    if (frame >= collapseEnd[h]) cur = h + 1;\n  }\n  const last = heads.length - 1;\n  const L = layouts[cur];\n  const prev = cur > 0 ? layouts[cur - 1] : null;\n\n  const reflowStart = cur > 0 ? collapseEnd[cur - 1] : -2;\n  const reflowK = interpolate(\n    frame,\n    [reflowStart, reflowStart + REFLOW],\n    [0, 1],\n    { ...CLAMP, easing: EASE_IO },\n  );\n\n  const shimmer = frame * 0.012;\n\n  const fromLines: { x: number; base: number; w: number }[] = prev\n    ? prev.lines\n    : L.lines.map((ln) => ({\n        x: ln.x + ln.w * 0.36,\n        base: ln.base,\n        w: ln.w * 0.28,\n      }));\n  const nBars = Math.max(fromLines.length, L.lines.length);\n\n  const textHl = new Set(\n    highlight\n      .split(/\\s+/)\n      .map(norm)\n      .filter((w) => w.length > 0),\n  );\n\n  const lineH = fs * LINE;\n  const barH = fs * BAR_H;\n  const isLast = cur === last;\n\n  const bars: React.ReactNode[] = [];\n  const texts: React.ReactNode[] = [];\n\n  for (let j = 0; j < nBars; j++) {\n    const a = fromLines[Math.min(j, fromLines.length - 1)];\n    const b = L.lines[Math.min(j, L.lines.length - 1)];\n    const x = interpolate(reflowK, [0, 1], [a.x, b.x]);\n    const w = interpolate(reflowK, [0, 1], [a.w, b.w]);\n    const base = interpolate(reflowK, [0, 1], [a.base, b.base]);\n    const splitting = j >= fromLines.length;\n    const merging = j >= L.lines.length;\n    let barO = 1;\n    if (splitting)\n      barO = interpolate(reflowK, [0, 0.45], [0, 1], CLAMP) * (cur > 0 ? 1 : 0);\n    if (merging) barO = interpolate(reflowK, [0.5, 1], [1, 0], CLAMP);\n    if (cur === 0 && reflowK <= 0)\n      barO *= interpolate(frame, [-2, 4], [0, 1], CLAMP);\n\n    const lineIdx = Math.min(j, L.lines.length - 1);\n    const rs = resolveAt[cur] + lineIdx * LINE_STAGGER;\n    const res = interpolate(frame, [rs, rs + RESOLVE], [0, 1], {\n      ...CLAMP,\n      easing: EASE_SWEEP,\n    });\n    const cs = collapseAt[cur] + lineIdx * LINE_STAGGER;\n    const col =\n      cur < last\n        ? interpolate(frame, [cs, cs + COLLAPSE], [0, 1], {\n            ...CLAMP,\n            easing: EASE_SWEEP,\n          })\n        : 0;\n\n    const textVisible = res > 0 && !merging;\n    const barFrom = col > 0 ? 0 : res;\n    const barTo = col > 0 ? col : 1;\n    const pad = barH * 0.5;\n    const bx = x - pad * 0.4;\n    const bw = w + pad * 0.8;\n    const barTop = base - fs * BAR_MID - barH / 2;\n    if (barTo - barFrom > 0.001 && barO > 0.001) {\n      const leftCut = barFrom * bw;\n      const rightCut = (1 - barTo) * bw;\n      bars.push(\n        <div\n          key={`bar${j}`}\n          style={{\n            position: \"absolute\",\n            left: bx,\n            top: barTop,\n            width: bw,\n            height: barH,\n            borderRadius: barH / 2,\n            backgroundImage: t.bar(accent),\n            backgroundSize: `${width * 1.2}px 100%`,\n            backgroundPositionX: `${-bx + ((shimmer % 1) - 0.5) * width * 1.2}px`,\n            backgroundRepeat: \"repeat-x\",\n            opacity: barO,\n            clipPath: `inset(-2px ${rightCut}px -2px ${leftCut}px round ${barH / 2}px)`,\n          }}\n        />,\n      );\n    }\n\n    const frontK = col > 0 && col < 1 ? col : res > 0 && res < 1 ? res : -1;\n    if (frontK >= 0 && !merging) {\n      const fx = bx + bw * frontK;\n      const fo = Math.sin(frontK * Math.PI);\n      bars.push(\n        <div\n          key={`front${j}`}\n          style={{\n            position: \"absolute\",\n            left: fx - fs * 0.5,\n            top: base - fs * 0.86,\n            width: fs * 0.5,\n            height: fs * 1.06,\n            background: `linear-gradient(90deg, transparent 0%, ${t.front} 100%)`,\n            opacity: fo * 0.22,\n            maskImage:\n              \"linear-gradient(180deg, transparent 0%, #000 30%, #000 70%, transparent 100%)\",\n            WebkitMaskImage:\n              \"linear-gradient(180deg, transparent 0%, #000 30%, #000 70%, transparent 100%)\",\n          }}\n        />,\n      );\n    }\n\n    if (textVisible) {\n      const ln = L.lines[j];\n      const lw = ln.w + fs * 0.3;\n      const leftEdge = col > 0 ? bx + bw * col - ln.x : -fs * 0.2;\n      const rightEdge = col > 0 ? lw : bx + bw * res - ln.x;\n      texts.push(\n        <div\n          key={`t${cur}-${j}`}\n          style={{\n            position: \"absolute\",\n            left: ln.x,\n            top: ln.base - fs * 0.86,\n            height: lineH,\n            whiteSpace: \"pre\",\n            fontFamily: figtree.fontFamily,\n            fontWeight: 500,\n            fontSize: fs,\n            lineHeight: `${fs * 0.86 + fs * 0.22}px`,\n            letterSpacing: `${TRACK}em`,\n            color: t.fg,\n            width: lw,\n            clipPath: `inset(-20% ${lw - rightEdge}px -30% ${leftEdge}px)`,\n          }}\n        >\n          {ln.words.map((wd, k) => {\n            const hl = isLast && textHl.has(norm(wd));\n            return (\n              <Fragment key={k}>\n                {k > 0 ? \" \" : null}\n                <span\n                  style={\n                    hl\n                      ? {\n                          backgroundImage: t.hl(accent),\n                          WebkitBackgroundClip: \"text\",\n                          backgroundClip: \"text\",\n                          color: \"transparent\",\n                        }\n                      : undefined\n                  }\n                >\n                  {wd}\n                </span>\n              </Fragment>\n            );\n          })}\n        </div>,\n      );\n    }\n  }\n\n  let eyebrow: React.ReactNode = null;\n  if (hasEyebrows) {\n    const label = eyebrows[Math.min(cur, eyebrows.length - 1)] ?? \"\";\n    const prevTop = prev ? prev.top : L.top;\n    const top = interpolate(reflowK, [0, 1], [prevTop, L.top]);\n    const inO = interpolate(\n      frame,\n      [resolveAt[cur] - 4, resolveAt[cur] + 8],\n      [0, 1],\n      CLAMP,\n    );\n    const outO =\n      cur < last\n        ? interpolate(\n            frame,\n            [collapseAt[cur], collapseAt[cur] + 8],\n            [1, 0],\n            CLAMP,\n          )\n        : 1;\n    const firstX = L.lines[0]?.x ?? 0;\n    eyebrow = (\n      <div\n        style={{\n          position: \"absolute\",\n          left: align === \"center\" ? 0 : firstX,\n          right: align === \"center\" ? 0 : undefined,\n          top: top - fs * 0.42 - eyeFs * 0.2,\n          textAlign: align === \"center\" ? \"center\" : \"left\",\n          fontFamily: mono.fontFamily,\n          fontWeight: 400,\n          fontSize: eyeFs,\n          lineHeight: 1,\n          letterSpacing: \"0.04em\",\n          textTransform: \"uppercase\",\n          color: t.eyebrow,\n          opacity: Math.min(inO, outO),\n        }}\n      >\n        {label}\n      </div>\n    );\n  }\n\n  const transparent = background === \"transparent\";\n\n  return (\n    <AbsoluteFill\n      style={{\n        background: transparent ? \"transparent\" : (background ?? t.bg),\n        overflow: \"hidden\",\n      }}\n    >\n      {!transparent && background === undefined ? (\n        <div\n          style={{\n            position: \"absolute\",\n            inset: 0,\n            background: t.ground(accent),\n            opacity: interpolate(frame, [0, 30], [0.6, 1], {\n              ...CLAMP,\n              easing: EASE_OUT,\n            }),\n          }}\n        />\n      ) : null}\n      {eyebrow}\n      {bars}\n      {texts}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/rewrite-bars.tsx"
    }
  ],
  "type": "registry:component"
}
