{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "in-proportion",
  "title": "in Proportion",
  "description": "Small objections (\"It'll look homemade\") are answered by a big yellow \"You'll write a component.\".",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/in-proportion/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/HubotSans\";\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 hubot = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface InProportionPair {\n  worry: string;\n  answer: string;\n}\n\nexport interface InProportionProps {\n  pairs?: InProportionPair[];\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst DEFAULT_PAIRS: InProportionPair[] = [\n  { worry: \"It’ll look homemade.\", answer: \"It’ll look like your app.\" },\n  {\n    worry: \"I’ll get lost in keyframes.\",\n    answer: \"You’ll write a component.\",\n  },\n];\n\nconst THEMES = {\n  dark: { bg: \"#1C1C1E\", ink: \"#FFFFFF\", accent: \"#FFE14D\" },\n  light: { bg: \"#FFFBEA\", ink: \"#17150F\", accent: \"#9A7A00\" },\n} as const;\n\nconst TRACK = -0.03;\nconst LH = 1.02;\nconst WORD = 3;\nconst RISE = 13;\nconst SHRINK = 24;\nconst EXIT = 12;\nconst NOTE_DIM = 0.5;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_IN = Easing.bezier(0.55, 0, 0.85, 0.35);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nfunction breakLines(ws: number[], space: number, maxEm: number) {\n  const n = ws.length;\n  if (n === 0) return [] as number[][];\n  let count = 1;\n  let x = 0;\n  for (let i = 0; i < n; i++) {\n    if (x > 0 && x + space + ws[i] > maxEm) {\n      count++;\n      x = ws[i];\n    } else {\n      x += (x > 0 ? space : 0) + ws[i];\n    }\n  }\n  if (count === 1) return [ws.map((_, i) => i)];\n  const lineW = (i: number, j: number) => {\n    let s = 0;\n    for (let k = i; k < j; k++) s += ws[k] + (k > i ? space : 0);\n    return s;\n  };\n  const avg = lineW(0, n) / count;\n  const INF = 1e12;\n  const dp = Array.from({ length: count + 1 }, () =>\n    new Array<number>(n + 1).fill(INF),\n  );\n  const back = Array.from({ length: count + 1 }, () =>\n    new Array<number>(n + 1).fill(0),\n  );\n  dp[0][0] = 0;\n  for (let l = 1; l <= count; l++) {\n    for (let j = 1; j <= n; j++) {\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 && j - i > 1) continue;\n        const cost = (lw - avg) ** 2;\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[count][n] >= INF) return [ws.map((_, i) => i)];\n  const starts: number[] = [];\n  let j = n;\n  for (let l = count; l > 0; l--) {\n    const i = back[l][j];\n    starts.unshift(i);\n    j = i;\n  }\n  return starts.map((s, l) => {\n    const e = l + 1 < starts.length ? starts[l + 1] : n;\n    return Array.from({ length: e - s }, (_, k) => s + k);\n  });\n}\n\nfunction useWidths(specs: string[]) {\n  const key = specs.join(\"\\u0001\");\n  const [state, setState] = useState<{\n    key: string;\n    w: Record<string, number>;\n  } | null>(null);\n  const [handle] = useState(() => delayRender(\"in-proportion: measuring\"));\n  const released = useRef(false);\n  const ref = useRef(specs);\n  ref.current = specs;\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      hubot.waitUntilDone(),\n      document.fonts.load(`400 100px \"${hubot.fontFamily}\"`),\n      document.fonts.load(`500 100px \"${hubot.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        for (const spec of ref.current) {\n          const cut = spec.indexOf(\"|\");\n          const weight = spec.slice(0, cut);\n          const text = spec.slice(cut + 1);\n          if (ctx) ctx.font = `${weight} 100px \"${hubot.fontFamily}\"`;\n          w[spec] = ctx ? ctx.measureText(text).width / 100 : text.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\ninterface Placed {\n  x: number;\n  line: number;\n}\n\nfunction layout(ws: number[], space: number, maxEm: number) {\n  const lines = breakLines(ws, space, maxEm);\n  const placed: Placed[] = [];\n  let widest = 0;\n  lines.forEach((idx, line) => {\n    let x = 0;\n    idx.forEach((k, m) => {\n      if (m > 0) x += space;\n      placed.push({ x, line });\n      x += ws[k];\n    });\n    widest = Math.max(widest, x);\n  });\n  return { placed, lines: lines.length, widest };\n}\n\nexport function InProportion({\n  pairs = DEFAULT_PAIRS,\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: InProportionProps) {\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 = pairs.length > 0 ? pairs : DEFAULT_PAIRS;\n  const worries = list.map((p) => p.worry.trim().split(/\\s+/));\n  const answers = list.map((p) => p.answer.trim().split(/\\s+/));\n  const specs = Array.from(\n    new Set([\n      \"400| \",\n      \"500| \",\n      ...worries.flat().map((s) => `500|${s}`),\n      ...answers.flat().map((s) => `500|${s}`),\n    ]),\n  );\n  const w = useWidths(specs);\n\n  if (!w) {\n    return (\n      <div style={{ position: \"absolute\", inset: 0, background: stage }} />\n    );\n  }\n\n  const adv = (s: string) =>\n    (w[`500|${s}`] ?? 0) + TRACK * Array.from(s).length;\n  const space = (w[\"500| \"] ?? 0.25) + 0.04;\n\n  const portrait = H > W * 1.1;\n  const unit = Math.min(W, H);\n  const mx = W * (portrait ? 0.08 : 0.06);\n  const my = H * (portrait ? 0.07 : 0.085);\n  const regionW = W - mx * 2;\n\n  const worryWs = worries.map((ws) => ws.map(adv));\n  const answerWs = answers.map((ws) => ws.map(adv));\n\n  const Fs0 = Math.max(15, unit * (portrait ? 0.045 : 0.044));\n  const noteGap = Fs0 * 0.7;\n  const maxLines = portrait ? 5 : 3;\n  const bigLays: { lay: ReturnType<typeof layout>; F: number; y0: number }[] =\n    [];\n  let noteBottom = my;\n  worryWs.forEach((ws, i) => {\n    const top = i > 0 ? noteBottom + Fs0 * 1.2 : my;\n    const regionH = H - my - top;\n    let F = H * (portrait ? 0.2 : 0.3);\n    let lay = layout(ws, space, regionW / F);\n    for (let k = 0; k < 80; k++) {\n      const fits =\n        lay.widest * F <= regionW + 0.5 &&\n        lay.lines * LH * F <= regionH &&\n        lay.lines <= maxLines;\n      if (fits) break;\n      F *= 0.97;\n      lay = layout(ws, space, regionW / F);\n    }\n    const blockH = lay.lines * LH * F;\n    bigLays.push({ lay, F, y0: top + (regionH - blockH) / 2 });\n    noteBottom += (i > 0 ? noteGap : 0) + lay.lines * LH * Fs0;\n  });\n  const noteMax = W * (portrait ? 0.6 : 0.36);\n  const noteWidest = Math.max(...bigLays.map((b) => b.lay.widest));\n  const Fs = Math.min(Fs0, noteMax / noteWidest);\n  const noteLeft = W - mx - noteWidest * Fs;\n  const noteTops: number[] = [];\n  let nt = my;\n  bigLays.forEach((b) => {\n    noteTops.push(nt);\n    nt += b.lay.lines * LH * Fs + noteGap;\n  });\n\n  const answerMax = W * (portrait ? 0.84 : 0.8);\n  const answerLinesMax = portrait ? 3 : 2;\n  let Fa = portrait ? W * 0.13 : H * 0.17;\n  let answerLays = answerWs.map((ws) => layout(ws, space, answerMax / Fa));\n  for (let k = 0; k < 60; k++) {\n    const most = Math.max(...answerLays.map((l) => l.lines));\n    const widest = Math.max(...answerLays.map((l) => l.widest));\n    if (most <= answerLinesMax && widest * Fa <= answerMax + 0.5) break;\n    Fa *= 0.96;\n    answerLays = answerWs.map((ws) => layout(ws, space, answerMax / Fa));\n  }\n\n  const worryStart: number[] = [];\n  const shrinkAt: number[] = [];\n  const answerStart: number[] = [];\n  let clock = 4;\n  list.forEach((_, i) => {\n    worryStart.push(clock);\n    clock += worries[i].length * WORD + RISE + 16;\n    shrinkAt.push(clock);\n    answerStart.push(clock + 10);\n    clock += 10 + answers[i].length * WORD + RISE;\n    clock += i < list.length - 1 ? 24 : 0;\n  });\n  const room = Math.max(1, T - Math.max(30, T * 0.18));\n  const k = clock > room ? room / clock : 1;\n\n  return (\n    <AbsoluteFill\n      style={{\n        background: stage,\n        overflow: \"hidden\",\n        fontFamily: hubot.fontFamily,\n        fontWeight: 500,\n      }}\n    >\n      {list.map((_, i) => {\n        if (t < worryStart[i] * k - 1) return null;\n        const big = bigLays[i];\n        return worries[i].map((word, j) => {\n          const at = (worryStart[i] + j * WORD) * k;\n          const e = interpolate(t, [at, at + RISE], [0, 1], {\n            ...CLAMP,\n            easing: EASE_OUT,\n          });\n          const s0 = shrinkAt[i] * k;\n          const p = interpolate(t, [s0, s0 + SHRINK * k], [0, 1], {\n            ...CLAMP,\n            easing: EASE_IO,\n          });\n          const bp = big.lay.placed[j];\n          const bx = mx + bp.x * big.F;\n          const by = big.y0 + bp.line * LH * big.F;\n          const sx = noteLeft + bp.x * Fs;\n          const sy = noteTops[i] + bp.line * LH * Fs;\n          const size = big.F + (Fs - big.F) * p;\n          const x = bx + (sx - bx) * p;\n          const y = by + (sy - by) * p;\n          return (\n            <div\n              key={`w${i}-${j}`}\n              style={{\n                position: \"absolute\",\n                left: x,\n                top: y,\n                fontSize: size,\n                lineHeight: `${size * LH}px`,\n                letterSpacing: `${TRACK}em`,\n                whiteSpace: \"pre\",\n                color: c.ink,\n                opacity: e * (1 - (1 - NOTE_DIM) * p),\n                transform: `translateY(${((1 - e) * size * 0.18).toFixed(2)}px)`,\n              }}\n            >\n              {word}\n            </div>\n          );\n        });\n      })}\n      {list.map((_, i) => {\n        const last = i === list.length - 1;\n        const leaveAt = last ? 1e9 : worryStart[i + 1] * k - 2;\n        const leave = interpolate(t, [leaveAt, leaveAt + EXIT], [0, 1], {\n          ...CLAMP,\n          easing: EASE_IN,\n        });\n        if (t < answerStart[i] * k - 1 || leave >= 1) return null;\n        const lay = answerLays[i];\n        const top = H - my - lay.lines * LH * Fa;\n        return answers[i].map((word, j) => {\n          const at = (answerStart[i] + j * WORD) * k;\n          const e = interpolate(t, [at, at + RISE], [0, 1], {\n            ...CLAMP,\n            easing: EASE_OUT,\n          });\n          const p = lay.placed[j];\n          return (\n            <div\n              key={`a${i}-${j}`}\n              style={{\n                position: \"absolute\",\n                left: mx + p.x * Fa,\n                top: top + p.line * LH * Fa,\n                fontSize: Fa,\n                lineHeight: `${Fa * LH}px`,\n                letterSpacing: `${TRACK}em`,\n                whiteSpace: \"pre\",\n                color: hi,\n                opacity: e * (1 - leave),\n                transform: `translateY(${((1 - e) * Fa * 0.22 + leave * H * 0.04).toFixed(2)}px)`,\n              }}\n            >\n              {word}\n            </div>\n          );\n        });\n      })}\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/in-proportion.tsx"
    }
  ],
  "type": "registry:component"
}
