{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "block-swap",
  "title": "Block Swap",
  "description": "A block overwrites each line with the next, ending 'Render the launch.' on a gradient pill.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/block-swap/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont as loadSpace } from \"@remotion/google-fonts/SpaceGrotesk\";\nimport type React from \"react\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  interpolateColors,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst space = loadSpace(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface BlockSwapProps {\n  lines?: string[];\n  highlight?: string;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst FLUTED = [\"#1d2a8f\", \"#2f7bff\", \"#ff7a3d\", \"#ffe2c4\"] as const;\n\nconst THEMES = {\n  dark: { bg: \"#0A0A0B\", ink: \"#F4F3F0\" },\n  light: { bg: \"#F5F4F0\", ink: \"#111113\" },\n} as const;\n\nconst TRACK = -0.03;\nconst LEAD = 1.12;\nconst BLOCK_TOP = 0.86;\nconst BLOCK_BOT = 0.24;\nconst PAD_X = 0.08;\nconst RADIUS = 0.2;\n\nconst STAGGER = 2;\nconst RUN = 10;\nconst MORPH = 20;\nconst MORPH_STAGGER = 1;\nconst TEXT_IN = 6;\nconst HOLD = 14;\nconst OPEN_SPAWN = 2;\nconst END_HOLD = 30;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IN_OUT = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_WIPE = Easing.bezier(0.45, 0, 0.25, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\ninterface Rect {\n  x: number;\n  y: number;\n  w: number;\n  h: number;\n}\n\ninterface Word {\n  text: string;\n  x: number;\n  base: number;\n  w: number;\n  block: Rect;\n}\n\ninterface Layout {\n  F: number;\n  heads: Word[][];\n  asc: number;\n  desc: number;\n}\n\nfunction measureLayout(lines: string[], W: number, H: number): Layout {\n  const canvas = document.createElement(\"canvas\");\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) throw new Error(\"block-swap: no 2d context\");\n  const PX = 200;\n  ctx.font = `500 ${PX}px \"${space.fontFamily}\"`;\n  const wOf = (s: string) =>\n    ctx.measureText(s).width / PX + TRACK * Array.from(s).length;\n  const fm = ctx.measureText(\"Hg\");\n  const asc = fm.fontBoundingBoxAscent / PX;\n  const desc = fm.fontBoundingBoxDescent / PX;\n  const sp = wOf(\" \");\n\n  const words = lines.map((l) => l.split(/\\s+/).filter(Boolean));\n  const cuts = words.map((ws) => {\n    const n = ws.length;\n    const tw = ws.map(wOf);\n    const lw = (a: number, b: number) => {\n      let s = 0;\n      for (let i = a; i < b; i++) s += tw[i] + (i > a ? sp : 0);\n      return s;\n    };\n    const options: { cut: number[]; max: number }[] = [];\n    for (let L = 1; L <= Math.min(3, n); L++) {\n      let best: { cut: number[]; max: number } | null = null;\n      const rec = (start: number, left: number, acc: number[], mx: number) => {\n        if (best && mx >= best.max) return;\n        if (left === 1) {\n          const m = Math.max(mx, lw(start, n));\n          if (!best || m < best.max) best = { cut: [...acc], max: m };\n          return;\n        }\n        for (let i = start + 1; i <= n - left + 1; i++)\n          rec(i, left - 1, [...acc, i], Math.max(mx, lw(start, i)));\n      };\n      rec(0, L, [], 0);\n      if (best) options.push(best);\n    }\n    return { tw, lw, options };\n  });\n\n  const sizeFor = (max: number, L: number) =>\n    Math.min((W * 0.84) / max, (H * 0.56) / (L * LEAD), H * 0.19);\n  const pick = cuts.map(({ options }) => {\n    let best = options[0];\n    let bestF = sizeFor(best.max, 1);\n    options.forEach((o, i) => {\n      const f = sizeFor(o.max, i + 1);\n      if (f > bestF * 1.15) {\n        best = o;\n        bestF = f;\n      }\n    });\n    return { cut: best.cut, F: bestF };\n  });\n  const F = Math.min(...pick.map((p) => p.F));\n\n  const heads = words.map((ws, hi) => {\n    const { tw, lw } = cuts[hi];\n    const bounds = [0, ...pick[hi].cut, ws.length];\n    const L = bounds.length - 1;\n    const lineH = F * LEAD;\n    const top = H * 0.48 - (L * lineH) / 2;\n    const out: Word[] = [];\n    for (let li = 0; li < L; li++) {\n      const a = bounds[li];\n      const b = bounds[li + 1];\n      let x = W / 2 - (lw(a, b) * F) / 2;\n      const base = top + li * lineH + (lineH - (asc + desc) * F) / 2 + asc * F;\n      for (let i = a; i < b; i++) {\n        const w = tw[i] * F;\n        out.push({\n          text: ws[i],\n          x,\n          base,\n          w,\n          block: {\n            x: x - PAD_X * F,\n            y: base - BLOCK_TOP * F,\n            w: w + 2 * PAD_X * F - TRACK * F,\n            h: (BLOCK_TOP + BLOCK_BOT) * F,\n          },\n        });\n        x += w + sp * F;\n      }\n    }\n    return out;\n  });\n  return { F, heads, asc, desc };\n}\n\nfunction useLayout(lines: string[], W: number, H: number) {\n  const [layout, setLayout] = useState<Layout | null>(null);\n  const [handle] = useState(() => delayRender(\"block-swap: measuring\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      space.waitUntilDone(),\n      document.fonts.load(`500 100px \"${space.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setLayout(measureLayout(lines, W, H));\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [lines, W, H, handle]);\n  return layout;\n}\n\ninterface Swap {\n  to: number;\n  from: number | null;\n  start: number;\n  covered: number;\n  morphAt: number;\n  textAt: number;\n  uncoverAt: number;\n  end: number;\n}\n\nfunction plan(counts: number[]): Swap[] {\n  const out: Swap[] = [];\n  let at = 0;\n  counts.forEach((n, i) => {\n    if (i === 0) {\n      const covered = OPEN_SPAWN + (n - 1) * STAGGER + RUN + 2;\n      const textAt = covered;\n      const uncoverAt = textAt + TEXT_IN;\n      const end = uncoverAt + (n - 1) * STAGGER + RUN;\n      out.push({\n        to: 0,\n        from: null,\n        start: 0,\n        covered,\n        morphAt: 0,\n        textAt,\n        uncoverAt,\n        end,\n      });\n      at = end + HOLD;\n      return;\n    }\n    const m = counts[i - 1];\n    const k = Math.max(m, n);\n    const covered = at + (m - 1) * STAGGER + RUN;\n    const morphAt = covered - 2;\n    const textAt = morphAt + MORPH + (k - 1) * MORPH_STAGGER;\n    const uncoverAt = textAt + TEXT_IN;\n    const end = uncoverAt + (n - 1) * STAGGER + RUN;\n    out.push({\n      to: i,\n      from: i - 1,\n      start: at,\n      covered,\n      morphAt,\n      textAt,\n      uncoverAt,\n      end,\n    });\n    at = end + HOLD;\n  });\n  return out;\n}\n\nexport function blockSwapDuration(lines: string[] = DEFAULT_LINES) {\n  const counts = lines.map((l) => l.split(/\\s+/).filter(Boolean).length);\n  const p = plan(counts);\n  return p[p.length - 1].end + END_HOLD;\n}\n\nconst DEFAULT_LINES = [\"Pick the beats you need.\", \"Render the launch.\"];\n\nconst lerpRect = (a: Rect, b: Rect, k: number): Rect => ({\n  x: a.x + (b.x - a.x) * k,\n  y: a.y + (b.y - a.y) * k,\n  w: a.w + (b.w - a.w) * k,\n  h: a.h + (b.h - a.h) * k,\n});\n\nconst bare = (s: string) => s.replace(/[.,!?;:…]+$/, \"\").toLowerCase();\n\nexport function BlockSwap({\n  lines = DEFAULT_LINES,\n  highlight = \"launch\",\n  accent = \"#3B5BFD\",\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: BlockSwapProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H } = useVideoConfig();\n  const c = THEMES[theme];\n  const bg = background ?? c.bg;\n  const safeLines = useMemo(\n    () => (lines.length > 0 ? lines : DEFAULT_LINES),\n    [lines],\n  );\n  const layout = useLayout(safeLines, W, H);\n  const swaps = useMemo(\n    () => plan(safeLines.map((l) => l.split(/\\s+/).filter(Boolean).length)),\n    [safeLines],\n  );\n\n  if (!layout) {\n    return <div style={{ position: \"absolute\", inset: 0, background: bg }} />;\n  }\n\n  const { F, heads } = layout;\n  let sw = swaps[0];\n  for (const s of swaps) if (t >= s.start) sw = s;\n  const isLast = sw.to === heads.length - 1;\n  const next = heads[sw.to];\n  const prev = sw.from === null ? null : heads[sw.from];\n  const k = Math.max(next.length, prev ? prev.length : 0);\n  const hiIndex =\n    isLast && highlight\n      ? next.findIndex((w) => bare(w.text) === bare(highlight))\n      : -1;\n\n  const r = RADIUS * F;\n  const drift = (t / 30) * 0.18;\n  const shimmer = `linear-gradient(90deg, ${FLUTED[0]} 0%, ${FLUTED[1]} 18%, ${accent} 30%, ${FLUTED[2]} 48%, ${FLUTED[3]} 58%, ${FLUTED[1]} 76%, ${FLUTED[0]} 100%)`;\n  const pill = `linear-gradient(100deg, ${accent} 0%, ${FLUTED[1]} 45%, ${FLUTED[2]} 100%)`;\n\n  const textStyle = {\n    position: \"absolute\" as const,\n    fontFamily: space.fontFamily,\n    fontWeight: 500,\n    fontSize: F,\n    lineHeight: 1,\n    letterSpacing: `${TRACK}em`,\n    whiteSpace: \"pre\" as const,\n  };\n  const baseInBox = ((1 - layout.asc - layout.desc) * F) / 2 + layout.asc * F;\n  const wordAt = (w: Word, color: string, dx = 0, dy = 0) => (\n    <div\n      style={{\n        ...textStyle,\n        left: w.x - dx,\n        top: w.base - baseInBox - dy,\n        color,\n      }}\n    >\n      {w.text}\n    </div>\n  );\n\n  const nodes: React.ReactNode[] = [];\n\n  if (prev) {\n    prev.forEach((w, i) => {\n      const done = sw.start + i * STAGGER + RUN;\n      if (t < done) nodes.push(<div key={`old-${i}`}>{wordAt(w, c.ink)}</div>);\n    });\n  }\n  next.forEach((w, i) => {\n    if (i === hiIndex) return;\n    const off = sw.uncoverAt + i * STAGGER;\n    if (t >= off) nodes.push(<div key={`new-${i}`}>{wordAt(w, c.ink)}</div>);\n  });\n\n  for (let j = 0; j < k; j++) {\n    const target = next[Math.min(j, next.length - 1)];\n    const merging = j >= next.length;\n    let rect: Rect;\n    let fill = 0;\n    let inner: { w: Word; o: number } | null = null;\n    let alpha = 1;\n\n    if (prev) {\n      const source = prev[Math.min(j, prev.length - 1)];\n      const splitting = j >= prev.length;\n      const cv = splitting\n        ? 1\n        : interpolate(\n            t,\n            [sw.start + j * STAGGER, sw.start + j * STAGGER + RUN],\n            [0, 1],\n            { ...CLAMP, easing: EASE_OUT },\n          );\n      const sq = source.block.h;\n      const coverRect: Rect = {\n        ...source.block,\n        w: sq + (source.block.w - sq) * cv,\n      };\n      const mk = interpolate(\n        t,\n        [\n          sw.morphAt + j * MORPH_STAGGER,\n          sw.morphAt + j * MORPH_STAGGER + MORPH,\n        ],\n        [0, 1],\n        { ...CLAMP, easing: EASE_IN_OUT },\n      );\n      rect = lerpRect(coverRect, target.block, mk);\n      if (splitting && t < sw.morphAt) alpha = 0;\n      if (!splitting && t < sw.start + j * STAGGER) alpha = 0;\n      if (merging && mk >= 1) alpha = 0;\n      fill =\n        interpolate(t, [sw.morphAt, sw.morphAt + 6], [0, 1], CLAMP) *\n        (1 - interpolate(t, [sw.textAt - 2, sw.textAt + 4], [0, 1], CLAMP));\n      if (!splitting && t < sw.morphAt + 4) {\n        inner = {\n          w: source,\n          o: 1 - interpolate(t, [sw.morphAt, sw.morphAt + 4], [0, 1], CLAMP),\n        };\n      }\n    } else {\n      const at = OPEN_SPAWN + j * STAGGER;\n      const run = interpolate(t, [at, at + RUN + 2], [0, 1], {\n        ...CLAMP,\n        easing: EASE_OUT,\n      });\n      const sq = target.block.h;\n      rect = { ...target.block, w: sq + (target.block.w - sq) * run };\n      alpha = interpolate(t, [at - 2, at + 2], [0, 1], CLAMP);\n      fill = 1 - interpolate(t, [sw.textAt - 2, sw.textAt + 4], [0, 1], CLAMP);\n    }\n\n    if (!merging && t >= sw.textAt) {\n      inner = {\n        w: target,\n        o: interpolate(t, [sw.textAt, sw.textAt + TEXT_IN], [0, 1], CLAMP),\n      };\n    }\n    const keep = j === hiIndex;\n    if (!merging && !keep && t >= sw.uncoverAt) {\n      const off = sw.uncoverAt + j * STAGGER;\n      const u = interpolate(t, [off, off + RUN], [0, 1], {\n        ...CLAMP,\n        easing: EASE_WIPE,\n      });\n      const right = target.block.x + target.block.w;\n      const left = target.block.x + target.block.w * u;\n      rect = { ...target.block, x: left, w: right - left };\n      if (rect.w < F * 0.03) alpha = 0;\n    }\n    const pillOn = keep\n      ? interpolate(t, [sw.uncoverAt + 2, sw.uncoverAt + 12], [0, 1], {\n          ...CLAMP,\n          easing: EASE_IN_OUT,\n        })\n      : 0;\n    if (alpha <= 0.001) continue;\n\n    nodes.push(\n      <div\n        key={`blk-${j}`}\n        style={{\n          position: \"absolute\",\n          left: rect.x,\n          top: rect.y,\n          width: rect.w,\n          height: rect.h,\n          borderRadius: Math.min(r, rect.w / 2),\n          overflow: \"hidden\",\n          opacity: alpha,\n          background: c.ink,\n        }}\n      >\n        {fill > 0 ? (\n          <div\n            style={{\n              position: \"absolute\",\n              left: -rect.x,\n              top: -rect.y,\n              width: W * 2,\n              height: H,\n              backgroundImage: shimmer,\n              backgroundSize: `${W}px ${H}px`,\n              backgroundRepeat: \"repeat-x\",\n              transform: `translateX(${(-((drift * W) % W)).toFixed(2)}px)`,\n              opacity: fill,\n            }}\n          />\n        ) : null}\n        {pillOn > 0 ? (\n          <div\n            style={{\n              position: \"absolute\",\n              inset: 0,\n              backgroundImage: pill,\n              opacity: pillOn,\n            }}\n          />\n        ) : null}\n        {inner && inner.o > 0 ? (\n          <div style={{ opacity: inner.o }}>\n            {wordAt(\n              inner.w,\n              pillOn > 0\n                ? interpolateColors(pillOn, [0, 0.3], [c.bg, \"#FFFFFF\"])\n                : c.bg,\n              rect.x,\n              rect.y,\n            )}\n          </div>\n        ) : null}\n      </div>,\n    );\n  }\n\n  return (\n    <AbsoluteFill style={{ background: bg, overflow: \"hidden\" }}>\n      {nodes}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/block-swap.tsx"
    }
  ],
  "type": "registry:component"
}
