{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-scramble-view",
  "type": "registry:component",
  "title": "Text Scramble View",
  "description": "Text that churns through random glyphs and settles left to right when it scrolls into view. Click or press Enter to replay it.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "text",
    "text",
    "scramble",
    "decode",
    "reveal",
    "in-view",
    "headline"
  ],
  "docs": "https://hyperblur.dev/effects/text-scramble-view",
  "files": [
    {
      "path": "registry/hyperblur/text-scramble-view.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/text-scramble-view.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TextScrambleViewProps extends React.HTMLAttributes<HTMLDivElement> {\n  text: string;\n  speed?: number;\n  charset?: string;\n  accent?: string;\n  replay?: boolean;\n}\n\nconst css = `\n.hb-zz1 {\n  display: block;\n  position: relative;\n  box-sizing: border-box;\n  font-family: inherit;\n  line-height: inherit;\n  color: inherit;\n  outline: none;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-zz1 *, .hb-zz1 *::before, .hb-zz1 *::after { box-sizing: border-box; }\n.hb-zz1[data-replay] { cursor: pointer; }\n.hb-zz1[data-replay]:focus-visible { outline: 2px solid var(--hb-zz1-accent); outline-offset: 4px; border-radius: 4px; }\n.hb-zz1-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  padding: 0;\n  overflow: hidden;\n  clip: rect(0, 0, 0, 0);\n  white-space: nowrap;\n  border: 0;\n}\n.hb-zz1-t { display: block; }\n.hb-zz1-w { display: inline-block; white-space: nowrap; }\n.hb-zz1-c { position: relative; display: inline-block; }\n.hb-zz1[data-idle] .hb-zz1-g { opacity: 0; }\n.hb-zz1-o {\n  position: absolute;\n  top: 0;\n  left: -1em;\n  right: -1em;\n  text-align: center;\n  color: var(--hb-zz1-accent);\n  pointer-events: none;\n}\n.hb-zz1-c[data-s] .hb-zz1-g { opacity: 0; }\n`;\n\nfunction mix(n: number) {\n  let h = Math.imul(n ^ 0x9e3779b9, 0x85ebca6b);\n  h ^= h >>> 13;\n  h = Math.imul(h, 0xc2b2ae35);\n  h ^= h >>> 16;\n  return h >>> 0;\n}\n\ntype Word = { space: boolean; letters: { ch: string; n: number }[] };\n\nfunction tokenize(text: string): Word[] {\n  let n = 0;\n  return text\n    .split(/(\\s+)/)\n    .filter((t) => t !== \"\")\n    .map((value) => {\n      if (/^\\s+$/.test(value)) return { space: true, letters: [{ ch: value, n: -1 }] };\n      return { space: false, letters: Array.from(value).map((ch) => ({ ch, n: n++ })) };\n    });\n}\n\nexport function TextScrambleView({\n  text,\n  speed = 45,\n  charset = \"!<>-_/[]{}=+*^?#%&$01\",\n  accent = \"#7fb3ff\",\n  replay = true,\n  className = \"\",\n  style,\n  onClick,\n  onKeyDown,\n  ...rest\n}: TextScrambleViewProps) {\n  const ref = React.useRef<HTMLDivElement>(null);\n  const raf = React.useRef(0);\n  const [phase, setPhase] = React.useState<\"idle\" | \"run\" | \"done\">(\"idle\");\n  const [frame, setFrame] = React.useState<(string | null)[]>([]);\n  const words = React.useMemo(() => tokenize(text), [text]);\n  const total = React.useMemo(() => Array.from(text.replace(/\\s+/g, \"\")).length, [text]);\n  const pool = React.useMemo(() => Array.from(charset), [charset]);\n\n  const play = React.useCallback(() => {\n    cancelAnimationFrame(raf.current);\n    if (total === 0 || pool.length === 0 || window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      setFrame([]);\n      setPhase(\"done\");\n      return;\n    }\n    const gap = Math.max(8, speed);\n    const reveal = Array.from({ length: total }, (_, i) => 160 + i * gap + (mix(i * 131 + 17) % 1000) / 1000 * gap * 2.5);\n    const start = performance.now();\n    let lastKey = \"\";\n    setPhase(\"run\");\n    const tick = (now: number) => {\n      const t = now - start;\n      const step = Math.floor(t / 48);\n      const cells: (string | null)[] = [];\n      let pending = 0;\n      for (let i = 0; i < total; i++) {\n        if (t >= reveal[i]) cells.push(null);\n        else {\n          pending++;\n          cells.push(pool[mix(i * 7919 + step * 104729 + 3) % pool.length]);\n        }\n      }\n      if (pending === 0) {\n        setFrame([]);\n        setPhase(\"done\");\n        return;\n      }\n      const key = cells.map((c) => c ?? \"\").join(\"\");\n      if (key !== lastKey) {\n        lastKey = key;\n        setFrame(cells);\n      }\n      raf.current = requestAnimationFrame(tick);\n    };\n    tick(start);\n  }, [pool, speed, total]);\n\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const io = new IntersectionObserver(\n      (entries) => {\n        if (entries.some((e) => e.isIntersecting)) {\n          io.disconnect();\n          play();\n        }\n      },\n      { threshold: 0.35 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(raf.current);\n    };\n  }, [play]);\n\n  const running = phase === \"run\" && frame.length > 0;\n\n  return (\n    <div\n      {...rest}\n      ref={ref}\n      className={`hb-zz1${className ? ` ${className}` : \"\"}`}\n      style={{ [\"--hb-zz1-accent\" as string]: accent, ...style }}\n      data-idle={phase === \"idle\" ? \"\" : undefined}\n      data-replay={replay ? \"\" : undefined}\n      role={replay ? \"button\" : undefined}\n      tabIndex={replay ? 0 : undefined}\n      aria-label={replay ? `${text}. Replay` : undefined}\n      onClick={(e) => {\n        onClick?.(e);\n        if (replay && !e.defaultPrevented) play();\n      }}\n      onKeyDown={(e) => {\n        onKeyDown?.(e);\n        if (replay && !e.defaultPrevented && (e.key === \"Enter\" || e.key === \" \")) {\n          e.preventDefault();\n          play();\n        }\n      }}\n    >\n      <style href=\"hb-text-scramble-view\" precedence=\"default\">{css}</style>\n      {replay ? null : <span className=\"hb-zz1-sr\">{text}</span>}\n      <span className=\"hb-zz1-t\" aria-hidden=\"true\">\n        {words.map((word, wi) =>\n          word.space ? (\n            <React.Fragment key={wi}>{\" \"}</React.Fragment>\n          ) : (\n            <span key={wi} className=\"hb-zz1-w\">\n              {word.letters.map((l) => {\n                const glyph = running ? frame[l.n] : null;\n                return (\n                  <span key={l.n} className=\"hb-zz1-c\" data-s={glyph ? \"\" : undefined}>\n                    <span className=\"hb-zz1-g\">{l.ch}</span>\n                    {glyph ? <span className=\"hb-zz1-o\">{glyph}</span> : null}\n                  </span>\n                );\n              })}\n            </span>\n          ),\n        )}\n      </span>\n    </div>\n  );\n}\n"
    }
  ]
}