{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-code-reveal",
  "type": "registry:component",
  "title": "Motion Code Reveal",
  "description": "An editor window that types its code as you scroll, with natural pauses at line ends, syntax colors, a caret and a minimap filling in. When the last line lands, a terminal slides up and runs it.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "motion",
    "scroll",
    "code",
    "typewriter",
    "editor",
    "terminal",
    "developer"
  ],
  "docs": "https://hyperblur.dev/effects/motion-code-reveal",
  "files": [
    {
      "path": "registry/hyperblur/motion-code-reveal.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/motion-code-reveal.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface MotionCodeRevealProps extends React.HTMLAttributes<HTMLElement> {\n  code?: string;\n  filename?: string;\n  tabs?: string[];\n  output?: string[];\n  accent?: string;\n  progress?: number;\n  autoPlay?: boolean;\n  duration?: number;\n  length?: number;\n  height?: string;\n}\n\nconst defaultCode = `import { createServer } from \"@tessel/edge\";\n\nconst app = createServer({ region: \"auto\" });\n\napp.get(\"/health\", () => ({ ok: true }));\n\napp.post(\"/orders\", async (req) => {\n  const order = await req.json();\n  const id = await db.orders.insert(order);\n  return { id, status: \"queued\" };\n});\n\napp.listen(8080);`;\n\nconst defaultOutput = [\"$ tessel dev\", \"Compiled server.ts in 182ms\", \"Listening on port 8080, 2 routes ready\"];\n\nconst css = `\n.hb-wh10 {\n  --hb-wh10-a: #2f7bff;\n  position: relative;\n  width: 100%;\n  height: var(--hb-wh10-h);\n  color: #ededed;\n  font-family: inherit;\n  background: #0a0a0a;\n}\n.hb-wh10[data-pinned=\"true\"] { height: calc(var(--hb-wh10-h) * var(--hb-wh10-len)); }\n.hb-wh10 *, .hb-wh10 *::before, .hb-wh10 *::after { box-sizing: border-box; }\n.hb-wh10-stage {\n  position: sticky;\n  top: 0;\n  height: var(--hb-wh10-h);\n  overflow: hidden;\n  container-type: size;\n  display: grid;\n  place-items: center;\n  padding: clamp(8px, 6cqh, 64px) clamp(8px, 6cqw, 80px);\n  background: radial-gradient(60% 60% at 50% 0%, color-mix(in srgb, var(--hb-wh10-a) 12%, transparent), transparent 70%), #0a0a0a;\n}\n.hb-wh10-win {\n  position: relative;\n  display: grid;\n  grid-template-rows: auto 1fr;\n  width: 100%;\n  max-width: 880px;\n  height: 100%;\n  max-height: 560px;\n  border-radius: clamp(8px, 2cqh, 14px);\n  border: 1px solid #262626;\n  background: linear-gradient(180deg, #121212, #0e0e0e);\n  box-shadow: 0 30px 80px -30px rgba(0, 0, 0, 0.9), inset 0 1px 0 rgba(255, 255, 255, 0.04);\n  overflow: hidden;\n  font-family: var(--font-mono, ui-monospace, monospace);\n}\n.hb-wh10-bar {\n  display: flex;\n  align-items: center;\n  gap: 0.8em;\n  height: clamp(22px, 7cqh, 40px);\n  padding: 0 0.8em;\n  border-bottom: 1px solid #1f1f1f;\n  background: #0f0f0f;\n  font-size: clamp(8px, 2.2cqh, 12px);\n  color: #7a7a7a;\n}\n.hb-wh10-dots { display: flex; gap: 0.4em; }\n.hb-wh10-dots i { width: 0.75em; height: 0.75em; border-radius: 50%; background: #2a2a2a; }\n.hb-wh10-tab {\n  position: relative;\n  padding: 0.35em 0.7em;\n  border-radius: 0.4em;\n  white-space: nowrap;\n}\n.hb-wh10-tab[data-on=\"true\"] { color: #e8e8e8; background: #1a1a1a; box-shadow: inset 0 0 0 1px #262626; }\n.hb-wh10-state { display: inline-flex; align-items: center; gap: 0.45em; margin-left: auto; white-space: nowrap; }\n.hb-wh10-state i {\n  width: 0.55em;\n  height: 0.55em;\n  border-radius: 50%;\n  background: #f5b84a;\n  box-shadow: 0 0 8px currentColor;\n  color: #f5b84a;\n}\n.hb-wh10-state[data-s=\"run\"] i { background: var(--hb-wh10-a); color: var(--hb-wh10-a); animation: hb-wh10-pulse 0.9s ease-in-out infinite; }\n.hb-wh10-state[data-s=\"ready\"] i { background: #3ecf8e; color: #3ecf8e; }\n@keyframes hb-wh10-pulse { 50% { opacity: 0.35; } }\n.hb-wh10-body { position: relative; display: grid; grid-template-columns: 1fr auto; min-height: 0; }\n.hb-wh10-code {\n  position: relative;\n  overflow: hidden;\n  padding: 0.7em 0;\n  font-size: clamp(8.5px, 2.9cqh, 15px);\n  line-height: 1.62;\n}\n.hb-wh10-scroll { transition: transform 0.35s cubic-bezier(0.22, 1, 0.36, 1); }\n.hb-wh10-line {\n  position: relative;\n  display: flex;\n  min-height: 1.62em;\n  white-space: pre;\n}\n.hb-wh10-line[data-cur=\"true\"] { background: linear-gradient(90deg, color-mix(in srgb, var(--hb-wh10-a) 10%, transparent), transparent 70%); }\n.hb-wh10-ln {\n  flex: none;\n  width: 3.4em;\n  padding-right: 1.1em;\n  text-align: right;\n  color: #3a3a3a;\n  user-select: none;\n}\n.hb-wh10-line[data-typed=\"true\"] .hb-wh10-ln { color: #5f5f5f; }\n.hb-wh10-line[data-cur=\"true\"] .hb-wh10-ln { color: #cfcfcf; }\n.hb-wh10-kw { color: #c792ff; }\n.hb-wh10-str { color: #9ee6a8; }\n.hb-wh10-num { color: #ffb86b; }\n.hb-wh10-fn { color: #7fb3ff; }\n.hb-wh10-prop { color: #e6e6e6; }\n.hb-wh10-id { color: #d4d4d4; }\n.hb-wh10-pu { color: #8a8a8a; }\n.hb-wh10-caret {\n  display: inline-block;\n  width: 0.55em;\n  height: 1.2em;\n  margin-top: 0.21em;\n  background: var(--hb-wh10-a);\n  box-shadow: 0 0 10px color-mix(in srgb, var(--hb-wh10-a) 70%, transparent);\n  animation: hb-wh10-blink 1.05s steps(1) infinite;\n}\n@keyframes hb-wh10-blink { 55% { opacity: 0; } }\n.hb-wh10-map {\n  display: none;\n  width: 54px;\n  padding: 0.9em 0.7em;\n  border-left: 1px solid #1a1a1a;\n}\n.hb-wh10-map i {\n  display: block;\n  height: 3px;\n  margin-bottom: 3px;\n  border-radius: 2px;\n  background: #2b2b2b;\n  transition: width 0.2s ease;\n}\n.hb-wh10-map i[data-cur=\"true\"] { background: var(--hb-wh10-a); }\n.hb-wh10-term {\n  position: absolute;\n  left: 0;\n  right: 0;\n  bottom: 0;\n  padding: 0.6em 1em 0.8em;\n  border-top: 1px solid #262626;\n  background: linear-gradient(180deg, rgba(14, 14, 14, 0.94), #0b0b0b);\n  backdrop-filter: blur(6px);\n  -webkit-backdrop-filter: blur(6px);\n  font-size: clamp(8px, 2.6cqh, 13px);\n  line-height: 1.6;\n  transform: translateY(calc((1 - var(--hb-wh10-t, 0)) * 105%));\n}\n.hb-wh10-term-h {\n  display: flex;\n  justify-content: space-between;\n  margin-bottom: 0.3em;\n  font-size: 0.82em;\n  letter-spacing: 0.14em;\n  text-transform: uppercase;\n  color: #5c5c5c;\n}\n.hb-wh10-out { display: flex; gap: 0.6em; opacity: 0; transform: translateY(4px); transition: opacity 0.3s ease, transform 0.35s ease; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }\n.hb-wh10-out[data-on=\"true\"] { opacity: 1; transform: none; }\n.hb-wh10-out b { font-weight: 500; color: #3ecf8e; }\n.hb-wh10-out:first-child b { color: var(--hb-wh10-a); }\n.hb-wh10-out span { color: #bdbdbd; }\n.hb-wh10-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  overflow: hidden;\n  clip-path: inset(50%);\n  white-space: nowrap;\n}\n@container (min-width: 620px) {\n  .hb-wh10-map { display: block; }\n}\n@container (max-width: 380px) {\n  .hb-wh10-tab + .hb-wh10-tab { display: none; }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-wh10-caret, .hb-wh10-state i { animation: none; }\n  .hb-wh10-scroll, .hb-wh10-out, .hb-wh10-map i { transition: none; }\n}\n`;\n\nfunction clamp01(x: number) {\n  return Math.min(1, Math.max(0, x));\n}\n\nfunction inOut(x: number) {\n  return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;\n}\n\nfunction hash(n: number) {\n  let x = (n + 1) * 2654435761;\n  x = Math.imul(x ^ (x >>> 15), 2246822519);\n  x = Math.imul(x ^ (x >>> 13), 3266489917);\n  return (x ^ (x >>> 16)) >>> 0;\n}\n\nfunction findScroller(el: HTMLElement): HTMLElement | null {\n  let node = el.parentElement;\n  while (node && node !== document.body && node !== document.documentElement) {\n    const oy = getComputedStyle(node).overflowY;\n    if ((oy === \"auto\" || oy === \"scroll\" || oy === \"overlay\") && node.scrollHeight > node.clientHeight + 1) return node;\n    node = node.parentElement;\n  }\n  return null;\n}\n\nfunction loopAt(ms: number, duration: number) {\n  const d = Math.max(1, duration) * 1000;\n  const back = d * 0.25;\n  const hold = 2200;\n  const t = ms % (d + hold + back + 600);\n  if (t < d) return t / d;\n  if (t < d + hold) return 1;\n  if (t < d + hold + back) return 1 - inOut((t - d - hold) / back);\n  return 0;\n}\n\nfunction useProgress(\n  ref: React.RefObject<HTMLElement | null>,\n  apply: (p: number) => void,\n  progress: number | undefined,\n  autoPlay: boolean,\n  duration: number,\n) {\n  const applyRef = React.useRef(apply);\n  React.useLayoutEffect(() => {\n    applyRef.current = apply;\n  });\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const run = (p: number) => applyRef.current(clamp01(p));\n    if (typeof progress === \"number\") {\n      run(progress);\n      return;\n    }\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      run(1);\n      return;\n    }\n    let raf = 0;\n    if (autoPlay) {\n      let visible = true;\n      let last = 0;\n      let t = 0;\n      const io = new IntersectionObserver((entries) => {\n        visible = entries[0]?.isIntersecting ?? true;\n      });\n      io.observe(el);\n      const loop = (now: number) => {\n        const dt = last ? Math.min(64, now - last) : 0;\n        last = now;\n        if (visible) {\n          t += dt;\n          run(loopAt(t, duration));\n        }\n        raf = requestAnimationFrame(loop);\n      };\n      raf = requestAnimationFrame(loop);\n      return () => {\n        cancelAnimationFrame(raf);\n        io.disconnect();\n      };\n    }\n    const sc = findScroller(el);\n    const measure = () => {\n      raf = 0;\n      const r = el.getBoundingClientRect();\n      const top = sc ? r.top - sc.getBoundingClientRect().top : r.top;\n      const vh = sc ? sc.clientHeight : window.innerHeight;\n      const span = r.height - vh;\n      run(span > 1 ? -top / span : (vh - top) / (vh + r.height));\n    };\n    const onScroll = () => {\n      if (!raf) raf = requestAnimationFrame(measure);\n    };\n    measure();\n    const target: HTMLElement | Window = sc ?? window;\n    target.addEventListener(\"scroll\", onScroll, { passive: true });\n    window.addEventListener(\"resize\", onScroll);\n    return () => {\n      cancelAnimationFrame(raf);\n      target.removeEventListener(\"scroll\", onScroll);\n      window.removeEventListener(\"resize\", onScroll);\n    };\n  }, [ref, progress, autoPlay, duration]);\n}\n\ninterface Token {\n  text: string;\n  kind: string;\n  start: number;\n}\n\nconst KEYWORDS = new Set([\n  \"import\", \"from\", \"const\", \"let\", \"var\", \"async\", \"await\", \"return\", \"export\", \"default\", \"function\",\n  \"new\", \"if\", \"else\", \"true\", \"false\", \"null\", \"undefined\", \"for\", \"of\", \"in\", \"type\", \"interface\",\n]);\n\nconst TOKEN = /(\"(?:[^\"\\\\]|\\\\.)*\"|'(?:[^'\\\\]|\\\\.)*'|`[^`]*`)|(\\d+(?:\\.\\d+)?)|([A-Za-z_$][\\w$]*)|(\\s+)|([^\\s])/g;\n\nfunction tokenize(code: string) {\n  const lines: { tokens: Token[]; start: number; length: number }[] = [];\n  let offset = 0;\n  for (const line of code.split(\"\\n\")) {\n    const tokens: Token[] = [];\n    TOKEN.lastIndex = 0;\n    let m: RegExpExecArray | null;\n    while ((m = TOKEN.exec(line))) {\n      const text = m[0];\n      let kind = \"pu\";\n      if (m[1]) kind = \"str\";\n      else if (m[2]) kind = \"num\";\n      else if (m[3]) {\n        const before = line[m.index - 1];\n        const after = line.slice(m.index + text.length).trimStart()[0];\n        if (KEYWORDS.has(text)) kind = \"kw\";\n        else if (after === \"(\") kind = \"fn\";\n        else if (before === \".\") kind = \"prop\";\n        else kind = \"id\";\n      } else if (m[4]) kind = \"ws\";\n      tokens.push({ text, kind, start: offset + m.index });\n    }\n    lines.push({ tokens, start: offset, length: line.length });\n    offset += line.length + 1;\n  }\n  return lines;\n}\n\nfunction timeline(code: string) {\n  const cum: number[] = [];\n  let sum = 0;\n  for (let i = 0; i < code.length; i++) {\n    const ch = code[i];\n    const h = (hash(i) % 1000) / 1000;\n    const w = ch === \"\\n\" ? 6 : ch === \" \" ? 0.7 : 0.55 + h * 0.9;\n    sum += w;\n    cum.push(sum);\n  }\n  return { cum, sum };\n}\n\nfunction typedAt(t: { cum: number[]; sum: number }, x: number) {\n  const target = x * t.sum;\n  let lo = 0;\n  let hi = t.cum.length;\n  while (lo < hi) {\n    const mid = (lo + hi) >> 1;\n    if (t.cum[mid] <= target) lo = mid + 1;\n    else hi = mid;\n  }\n  return lo;\n}\n\nexport function MotionCodeReveal({\n  code = defaultCode,\n  filename = \"server.ts\",\n  tabs = [\"db.ts\"],\n  output = defaultOutput,\n  accent = \"#2f7bff\",\n  progress,\n  autoPlay = false,\n  duration = 7,\n  length = 3,\n  height = \"100vh\",\n  className,\n  style,\n  ...rest\n}: MotionCodeRevealProps) {\n  const ref = React.useRef<HTMLElement>(null);\n  const codeRef = React.useRef<HTMLDivElement>(null);\n  const [p, setP] = React.useState(typeof progress === \"number\" ? clamp01(progress) : 0);\n  const [rows, setRows] = React.useState(12);\n  const pinned = progress === undefined && !autoPlay;\n  const lines = React.useMemo(() => tokenize(code), [code]);\n  const time = React.useMemo(() => timeline(code), [code]);\n\n  React.useEffect(() => {\n    const el = codeRef.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => {\n      const lh = parseFloat(getComputedStyle(el).lineHeight) || 20;\n      const r = Math.max(3, Math.floor((el.clientHeight - lh * 0.8) / lh));\n      setRows((old) => (old === r ? old : r));\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  useProgress(\n    ref,\n    (v) => {\n      const q = Math.round(v * 1500) / 1500;\n      setP((old) => (old === q ? old : q));\n    },\n    progress,\n    autoPlay,\n    duration,\n  );\n\n  const typeEnd = output.length ? 0.74 : 0.96;\n  const n = typedAt(time, clamp01(p / typeEnd));\n  const done = n >= code.length;\n  let curLine = 0;\n  for (let i = 0; i < lines.length; i++) if (lines[i].start <= n) curLine = i;\n  const term = output.length ? inOut(clamp01((p - typeEnd - 0.02) / 0.08)) : 0;\n  const visibleRows = term > 0.5 ? Math.max(2, rows - output.length - 1) : rows;\n  const shift = Math.max(0, Math.min(curLine - visibleRows + 2, lines.length - visibleRows));\n  const outOn = output.map((_, i) => p >= typeEnd + 0.1 + i * ((0.98 - typeEnd - 0.1) / Math.max(1, output.length)));\n  const status = !done ? \"type\" : outOn[outOn.length - 1] || !output.length ? \"ready\" : \"run\";\n\n  return (\n    <section\n      ref={ref}\n      className={[\"hb-wh10\", className].filter(Boolean).join(\" \")}\n      data-pinned={pinned ? \"true\" : \"false\"}\n      style={\n        {\n          \"--hb-wh10-a\": accent,\n          \"--hb-wh10-h\": height,\n          \"--hb-wh10-len\": length,\n          ...style,\n        } as React.CSSProperties\n      }\n      {...rest}\n    >\n      <style href=\"hb-motion-code-reveal\" precedence=\"default\">{css}</style>\n      <div className=\"hb-wh10-stage\">\n        <div className=\"hb-wh10-win\">\n          <div className=\"hb-wh10-bar\" aria-hidden=\"true\">\n            <span className=\"hb-wh10-dots\">\n              <i />\n              <i />\n              <i />\n            </span>\n            <span className=\"hb-wh10-tab\" data-on=\"true\">\n              {filename}\n            </span>\n            {tabs.map((t) => (\n              <span className=\"hb-wh10-tab\" key={t}>\n                {t}\n              </span>\n            ))}\n            <span className=\"hb-wh10-state\" data-s={status}>\n              <i />\n              {status === \"type\" ? \"Editing\" : status === \"run\" ? \"Running\" : \"Ready\"}\n            </span>\n          </div>\n          <div className=\"hb-wh10-body\">\n            <div className=\"hb-wh10-code\" ref={codeRef}>\n              <pre className=\"hb-wh10-sr\">{code}</pre>\n              <div\n                className=\"hb-wh10-scroll\"\n                aria-hidden=\"true\"\n                style={{ transform: `translateY(${(-shift * 1.62).toFixed(2)}em)` }}\n              >\n                {lines.map((line, li) => {\n                  const typed = n > line.start || (n === line.start && li === curLine);\n                  const cur = li === curLine;\n                  return (\n                    <div key={li} className=\"hb-wh10-line\" data-typed={typed ? \"true\" : \"false\"} data-cur={cur && !done ? \"true\" : \"false\"}>\n                      <span className=\"hb-wh10-ln\">{li + 1}</span>\n                      <span>\n                        {line.tokens.map((tk, ti) => {\n                          if (tk.start >= n) return null;\n                          const text = tk.start + tk.text.length > n ? tk.text.slice(0, n - tk.start) : tk.text;\n                          return (\n                            <span key={ti} className={tk.kind === \"ws\" ? undefined : `hb-wh10-${tk.kind}`}>\n                              {text}\n                            </span>\n                          );\n                        })}\n                        {cur ? <span className=\"hb-wh10-caret\" /> : null}\n                      </span>\n                    </div>\n                  );\n                })}\n              </div>\n            </div>\n            <div className=\"hb-wh10-map\" aria-hidden=\"true\">\n              {lines.map((line, li) => {\n                const typedChars = Math.max(0, Math.min(line.length, n - line.start));\n                return (\n                  <i\n                    key={li}\n                    data-cur={li === curLine && !done ? \"true\" : \"false\"}\n                    style={{ width: `${Math.min(100, typedChars * 2.4).toFixed(0)}%` }}\n                  />\n                );\n              })}\n            </div>\n            {output.length ? (\n              <div className=\"hb-wh10-term\" style={{ \"--hb-wh10-t\": term.toFixed(3) } as React.CSSProperties} aria-hidden={term < 0.5 ? \"true\" : undefined}>\n                <div className=\"hb-wh10-term-h\">\n                  <span>Terminal</span>\n                  <span>zsh</span>\n                </div>\n                {output.map((line, i) => (\n                  <div key={i} className=\"hb-wh10-out\" data-on={outOn[i] ? \"true\" : \"false\"}>\n                    <b>{i === 0 ? \">\" : \"✓\"}</b>\n                    <span>{i === 0 ? line.replace(/^\\$\\s+/, \"\") : line}</span>\n                  </div>\n                ))}\n              </div>\n            ) : null}\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n"
    }
  ]
}