{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-scramble-center",
  "type": "registry:component",
  "title": "Text Scramble Center",
  "description": "Scrambled text that locks in from the middle outward. Each letter flares with a soft glow the moment it resolves.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "text",
    "text",
    "scramble",
    "glow",
    "center",
    "reveal",
    "headline"
  ],
  "docs": "https://hyperblur.dev/effects/text-scramble-center",
  "files": [
    {
      "path": "registry/hyperblur/text-scramble-center.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/text-scramble-center.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TextScrambleCenterProps extends React.HTMLAttributes<HTMLDivElement> {\n  text: string;\n  speed?: number;\n  charset?: string;\n  accent?: string;\n  glow?: number;\n  replay?: boolean;\n}\n\nconst css = `\n.hb-zz2 {\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-zz2 *, .hb-zz2 *::before, .hb-zz2 *::after { box-sizing: border-box; }\n.hb-zz2[data-replay] { cursor: pointer; }\n.hb-zz2[data-replay]:focus-visible { outline: 2px solid var(--hb-zz2-accent); outline-offset: 4px; border-radius: 4px; }\n.hb-zz2-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-zz2-t { display: block; }\n.hb-zz2-w { display: inline-block; white-space: nowrap; }\n.hb-zz2-c { position: relative; display: inline-block; }\n.hb-zz2[data-idle] .hb-zz2-g { opacity: 0; }\n.hb-zz2-g {\n  color: color-mix(in srgb, var(--hb-zz2-accent) calc(var(--hb-zz2-k, 0) * 100%), currentColor);\n  text-shadow: 0 0 calc(var(--hb-zz2-k, 0) * 0.55em) var(--hb-zz2-accent), 0 0 calc(var(--hb-zz2-k, 0) * 1.2em) color-mix(in srgb, var(--hb-zz2-accent) 55%, transparent);\n}\n.hb-zz2-o {\n  position: absolute;\n  top: 0;\n  left: -1em;\n  right: -1em;\n  text-align: center;\n  color: color-mix(in srgb, var(--hb-zz2-accent) 70%, transparent);\n  pointer-events: none;\n}\n.hb-zz2-c[data-s] .hb-zz2-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 }[] };\ntype Cell = { glyph: string | null; k: 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 TextScrambleCenter({\n  text,\n  speed = 70,\n  charset = \"!<>-_/[]{}=+*^?#%&$01\",\n  accent = \"#7fb3ff\",\n  glow = 1,\n  replay = true,\n  className = \"\",\n  style,\n  onClick,\n  onKeyDown,\n  ...rest\n}: TextScrambleCenterProps) {\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<Cell[]>([]);\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 mid = (total - 1) / 2;\n    const reveal = Array.from(\n      { length: total },\n      (_, i) => 180 + Math.abs(i - mid) * gap + (mix(i * 211 + 29) % 1000) / 1000 * gap * 0.9,\n    );\n    const fade = 520;\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 / 50);\n      const cells: Cell[] = [];\n      let alive = 0;\n      for (let i = 0; i < total; i++) {\n        if (t < reveal[i]) {\n          alive++;\n          cells.push({ glyph: pool[mix(i * 7919 + step * 104729 + 5) % pool.length], k: 0 });\n        } else {\n          const age = (t - reveal[i]) / fade;\n          if (age < 1) {\n            alive++;\n            const k = Math.round((1 - age) * (1 - age) * 10) / 10;\n            cells.push({ glyph: null, k });\n          } else cells.push({ glyph: null, k: 0 });\n        }\n      }\n      if (alive === 0) {\n        setFrame([]);\n        setPhase(\"done\");\n        return;\n      }\n      const key = cells.map((c) => `${c.glyph ?? \"\"}${c.k}`).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  const strength = Math.max(0, glow);\n\n  return (\n    <div\n      {...rest}\n      ref={ref}\n      className={`hb-zz2${className ? ` ${className}` : \"\"}`}\n      style={{ [\"--hb-zz2-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-center\" precedence=\"default\">{css}</style>\n      {replay ? null : <span className=\"hb-zz2-sr\">{text}</span>}\n      <span className=\"hb-zz2-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-zz2-w\">\n              {word.letters.map((l) => {\n                const cell = running ? frame[l.n] : undefined;\n                const glyph = cell?.glyph ?? null;\n                const k = cell && !glyph ? Math.min(1, cell.k * strength) : 0;\n                return (\n                  <span key={l.n} className=\"hb-zz2-c\" data-s={glyph ? \"\" : undefined}>\n                    <span className=\"hb-zz2-g\" style={k > 0 ? { [\"--hb-zz2-k\" as string]: k.toFixed(2) } : undefined}>\n                      {l.ch}\n                    </span>\n                    {glyph ? <span className=\"hb-zz2-o\">{glyph}</span> : null}\n                  </span>\n                );\n              })}\n            </span>\n          ),\n        )}\n      </span>\n    </div>\n  );\n}\n"
    }
  ]
}