{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "crunch",
  "title": "Crunch",
  "description": "A timestamped story line, \"It shipped, with time to ...\", fades in over a dark brown ground.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/crunch/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Afacad\";\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 afacad = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface StoryBeat {\n  slug: string;\n  line: string;\n}\n\nexport interface CrunchProps {\n  setup?: StoryBeat;\n  snag?: StoryBeat;\n  resolution?: StoryBeat;\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst THEMES = {\n  dark: {\n    bg: \"#3A1F1A\",\n    ink: \"#FBF3EA\",\n    accent: \"#F2B544\",\n    wall: \"#241210\",\n    edge: \"#4F2C25\",\n  },\n  light: {\n    bg: \"#FBF6EE\",\n    ink: \"#3B2414\",\n    accent: \"#B07A00\",\n    wall: \"#3B2414\",\n    edge: \"#E9DCC8\",\n  },\n} as const;\n\nconst DEFAULT_SETUP: StoryBeat = {\n  slug: \"Monday, 10:00 AM\",\n  line: \"Maya had a launch on Friday.\",\n};\nconst DEFAULT_SNAG: StoryBeat = {\n  slug: \"Thursday, 11:40 PM\",\n  line: \"Nine hours left, and still no video.\",\n};\nconst DEFAULT_RESOLUTION: StoryBeat = {\n  slug: \"Friday, 9:00 AM\",\n  line: \"It shipped, with time to spare.\",\n};\n\nconst LINE_TRACK = -0.02;\nconst SLUG_TRACK = 0.08;\nconst LH = 1.04;\nconst SLUG_RATIO = 0.3;\nconst PX = 100;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_SQUEEZE = Easing.bezier(0.55, 0, 0.75, 1);\nconst EASE_IN = Easing.bezier(0.5, 0, 0.75, 0);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nfunction words(s: string) {\n  return s.split(/\\s+/).filter(Boolean);\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(\"crunch: 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      afacad.waitUntilDone(),\n      document.fonts.load(`500 ${PX}px \"${afacad.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 = `500 ${PX}px \"${afacad.fontFamily}\"`;\n        for (const s of ref.current) {\n          w[s] = ctx ? ctx.measureText(s).width / PX : s.length * 0.45;\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\ninterface Laid {\n  x: number[];\n  line: number[];\n  widths: number[];\n  lines: number;\n}\n\nfunction greedy(ws: number[], space: number, max: number): Laid {\n  const x: number[] = [];\n  const line: number[] = [];\n  const widths: number[] = [0];\n  let l = 0;\n  let cx = 0;\n  ws.forEach((w, i) => {\n    if (i > 0 && cx + space + w > max) {\n      l++;\n      widths.push(0);\n      cx = 0;\n    } else if (i > 0) {\n      cx += space;\n    }\n    x.push(cx);\n    line.push(l);\n    cx += w;\n    widths[l] = cx;\n  });\n  return { x, line, widths, lines: l + 1 };\n}\n\nfunction balanced(ws: number[], space: number, max: number): Laid {\n  const base = greedy(ws, space, max);\n  if (base.lines <= 1) return base;\n  let lo = Math.max(...ws);\n  let hi = max;\n  for (let k = 0; k < 18; k++) {\n    const mid = (lo + hi) / 2;\n    if (greedy(ws, space, mid).lines <= base.lines) hi = mid;\n    else lo = mid;\n  }\n  return greedy(ws, space, hi);\n}\n\ninterface Block {\n  lineWords: string[];\n  slugWords: string[];\n}\n\nexport function Crunch({\n  setup = DEFAULT_SETUP,\n  snag = DEFAULT_SNAG,\n  resolution = DEFAULT_RESOLUTION,\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: CrunchProps) {\n  const frame = useCurrentFrame();\n  const t = frame * 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 blocks: Block[] = [setup, snag, resolution].map((b) => ({\n    lineWords: words(b.line),\n    slugWords: words(b.slug.toUpperCase()),\n  }));\n  const strings = Array.from(\n    new Set([\" \", ...blocks.flatMap((b) => [...b.lineWords, ...b.slugWords])]),\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 portrait = H > W * 1.1;\n  const unit = Math.min(W, H);\n  const space = w[\" \"] ?? 0.22;\n  const lineAdv = (s: string) =>\n    (w[s] ?? 0) + LINE_TRACK * Array.from(s).length;\n  const slugAdv = (s: string) =>\n    (w[s] ?? 0) + SLUG_TRACK * Array.from(s).length;\n\n  const wideCol = W * (portrait ? 0.86 : 0.8);\n  const pad = unit * 0.05;\n\n  let F = portrait ? W * 0.14 : H * 0.17;\n  const fitWide = () =>\n    blocks.every(\n      (b) =>\n        balanced(b.lineWords.map(lineAdv), space, wideCol / F).lines <=\n        (portrait ? 4 : 2),\n    );\n  for (let k = 0; k < 60 && !fitWide(); k++) F *= 0.96;\n\n  const room = H * (portrait ? 0.8 : 0.86);\n  const snagStack = (size: number, col: number) => {\n    const ss = Math.max(14, size * SLUG_RATIO);\n    const a = balanced(\n      blocks[1].slugWords.map((s) => slugAdv(s) * ss),\n      space * ss,\n      col,\n    );\n    const b = balanced(\n      blocks[1].lineWords.map((s) => lineAdv(s) * size),\n      space * size,\n      col,\n    );\n    return a.lines * ss * 1.25 + ss * 0.9 + b.lines * size * LH;\n  };\n  for (let k = 0; k < 60 && snagStack(F, wideCol) > room; k++) F *= 0.96;\n  const S = Math.max(14, F * SLUG_RATIO);\n  const minWord =\n    Math.max(\n      ...blocks[1].lineWords.map((s) => lineAdv(s) * F),\n      ...blocks[1].slugWords.map((s) => slugAdv(s) * S),\n    ) * 1.02;\n  let lo = minWord;\n  let hiCol = Math.max(minWord, wideCol);\n  for (let k = 0; k < 24; k++) {\n    const mid = (lo + hiCol) / 2;\n    if (snagStack(F, mid) <= room) hiCol = mid;\n    else lo = mid;\n  }\n  const tightCol = hiCol;\n\n  const k = Math.min(1.6, Math.max(0.75, T / 180));\n  const at = (f: number) => f * k;\n  const setupIn = at(4);\n  const setupOut = at(44);\n  const snagIn = at(52);\n  const squeezeA = at(74);\n  const squeezeB = at(122);\n  const release = at(134);\n  const resIn = at(140);\n\n  const maxInset = Math.max(0, (W - tightCol) / 2 - pad);\n  const squeeze = interpolate(t, [squeezeA, squeezeB], [0, 1], {\n    ...CLAMP,\n    easing: EASE_SQUEEZE,\n  });\n  const open = interpolate(t, [release, release + 12], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const inset = maxInset * squeeze * (1 - open);\n  const colAt = (tt: number) => {\n    const sq = interpolate(tt, [squeezeA, squeezeB], [0, 1], {\n      ...CLAMP,\n      easing: EASE_SQUEEZE,\n    });\n    return Math.max(\n      tightCol,\n      Math.min(wideCol, W - 2 * maxInset * sq - 2 * pad),\n    );\n  };\n\n  const centreY = H * (portrait ? 0.5 : 0.52);\n\n  const layout = (b: Block, col: number) => {\n    const sw = b.slugWords.map((s) => slugAdv(s) * S);\n    const lw = b.lineWords.map((s) => lineAdv(s) * F);\n    const a = balanced(sw, space * S, col);\n    const l = balanced(lw, space * F, col);\n    const slugLH = S * 1.25;\n    const gap = S * 0.9;\n    const total = a.lines * slugLH + gap + l.lines * F * LH;\n    const top = centreY - total / 2;\n    const pts: { x: number; y: number }[] = [];\n    b.slugWords.forEach((_, i) => {\n      pts.push({\n        x: W / 2 - a.widths[a.line[i]] / 2 + a.x[i],\n        y: top + a.line[i] * slugLH,\n      });\n    });\n    b.lineWords.forEach((_, i) => {\n      pts.push({\n        x: W / 2 - l.widths[l.line[i]] / 2 + l.x[i],\n        y: top + a.lines * slugLH + gap + l.line[i] * F * LH,\n      });\n    });\n    return pts;\n  };\n\n  const wideFor = (b: Block) => layout(b, wideCol);\n\n  const snagPts = (() => {\n    const start = Math.floor(squeezeA);\n    const end = Math.min(Math.floor(t), Math.floor(release));\n    let pos = layout(blocks[1], colAt(start));\n    for (let f = start + 1; f <= end; f++) {\n      const target = layout(blocks[1], colAt(f));\n      pos = pos.map((p, i) => ({\n        x: p.x + (target[i].x - p.x) * 0.75,\n        y: p.y + (target[i].y - p.y) * 0.75,\n      }));\n    }\n    return pos;\n  })();\n\n  const renderBlock = (\n    b: Block,\n    pts: { x: number; y: number }[],\n    key: string,\n    inAt: number,\n    outAt: number | null,\n    lineColor: string,\n    slugColor: string,\n  ) => {\n    const n = b.slugWords.length;\n    const all = [...b.slugWords, ...b.lineWords];\n    return all.map((word, i) => {\n      const isSlug = i < n;\n      const s0 = inAt + i * Math.min(2.2, 16 / all.length);\n      const e = interpolate(t, [s0, s0 + 16], [0, 1], {\n        ...CLAMP,\n        easing: EASE_OUT,\n      });\n      let o = e;\n      let dy = (1 - e) * (isSlug ? S : F) * 0.45;\n      if (outAt !== null) {\n        const x0 = outAt + i * 0.8;\n        const x = interpolate(t, [x0, x0 + 9], [0, 1], {\n          ...CLAMP,\n          easing: EASE_IN,\n        });\n        o *= 1 - x;\n        dy -= x * F * 0.35;\n      }\n      if (o <= 0.001) return null;\n      const size = isSlug ? S : F;\n      return (\n        <div\n          key={`${key}${i}`}\n          style={{\n            position: \"absolute\",\n            left: pts[i].x,\n            top: pts[i].y,\n            fontSize: size,\n            lineHeight: `${isSlug ? size * 1.25 : size * LH}px`,\n            whiteSpace: \"pre\",\n            fontWeight: isSlug ? 500 : 400,\n            letterSpacing: `${isSlug ? SLUG_TRACK : LINE_TRACK}em`,\n            color: isSlug ? slugColor : lineColor,\n            opacity: o,\n            transform: `translateY(${dy.toFixed(2)}px)`,\n          }}\n        >\n          {word}\n        </div>\n      );\n    });\n  };\n\n  const wallW = inset + pad * 0.35 * squeeze * (1 - open);\n  const wallStyle = (side: \"left\" | \"right\") => ({\n    position: \"absolute\" as const,\n    top: 0,\n    bottom: 0,\n    [side]: 0,\n    width: Math.max(0, wallW),\n    background: c.wall,\n    boxShadow:\n      wallW > 1\n        ? `inset ${side === \"left\" ? -1 : 1}px 0 0 ${c.edge}`\n        : undefined,\n  });\n\n  return (\n    <AbsoluteFill\n      style={{\n        background: stage,\n        overflow: \"hidden\",\n        fontFamily: afacad.fontFamily,\n      }}\n    >\n      {renderBlock(\n        blocks[0],\n        wideFor(blocks[0]),\n        \"a\",\n        setupIn,\n        setupOut,\n        c.ink,\n        hi,\n      )}\n      {renderBlock(blocks[1], snagPts, \"b\", snagIn, release - 4, c.ink, hi)}\n      <div style={wallStyle(\"left\")} />\n      <div style={wallStyle(\"right\")} />\n      {renderBlock(blocks[2], wideFor(blocks[2]), \"c\", resIn, null, hi, hi)}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/crunch.tsx"
    }
  ],
  "type": "registry:component"
}
