{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scramble-text",
  "title": "Scramble Text",
  "description": "Text that decodes itself: noise sweeps across, then each character locks in.",
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/use-entrance"
  ],
  "files": [
    {
      "path": "registry/default/ui/scramble-text.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useMemo, useRef, type ComponentProps, type Ref } from \"react\";\n\nimport { useEntrance, type EntranceTrigger } from \"@/registry/default/hooks/use-entrance\";\nimport { cn } from \"@/registry/default/lib/utils\";\n\nfunction mergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === \"function\") ref(node);\n      else if (ref) ref.current = node;\n    }\n  };\n}\n\nfunction prefersReducedMotion() {\n  return window.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches === true;\n}\n\nconst NBSP = \"\\u00a0\";\n\n/**\n * When each character settles, in ms after the start: a sweep from the left,\n * scattered a little so it reads as decoding rather than typing. The\n * scatter comes from the position, so every run of the same text matches.\n */\nfunction settleTimes(glyphs: string[], gap: number) {\n  return glyphs.map((_, index) => {\n    const scatter = Math.sin(index * 7.31 + glyphs.length * 1.7) * 0.5 + 0.5;\n    return (index + scatter * 2.5) * gap;\n  });\n}\n\n/** The line at a moment: settled characters as they are, the rest as noise. Spaces stay spaces. */\nfunction frame(glyphs: string[], elapsed: number, times: number[], characters: string) {\n  return glyphs\n    .map((glyph, index) =>\n      /\\s/.test(glyph) || elapsed >= (times[index] ?? 0)\n        ? glyph\n        : characters[Math.floor(Math.random() * characters.length)]\n    )\n    .join(\"\");\n}\n\n// Fresh noise at most this often, so it flickers rather than smears.\nconst NOISE_EVERY = 45;\n\ninterface ScrambleTextProps extends Omit<ComponentProps<\"span\">, \"children\"> {\n  /** Plain text. */\n  children: string;\n  /** Milliseconds between one character settling and the next. */\n  speed?: number;\n  /** Wait before it starts, in ms. */\n  delay?: number;\n  /** The noise it cycles through before the text settles. */\n  characters?: string;\n  /** \"load\" starts as soon as the page runs; \"in-view\" when the text scrolls into view. */\n  trigger?: EntranceTrigger;\n  /** In-view: play the first time only. */\n  once?: boolean;\n  /** Takes over from `trigger`: `false` holds it blank, `true` runs it. */\n  play?: boolean;\n}\n\n/**\n * Text that decodes itself: the line starts as noise and the real\n * characters settle into place in a loose sweep from the left. It writes straight to the DOM once\n * per frame instead of re-rendering React. The text is in the server HTML\n * for search engines and for browsers without JavaScript, screen readers\n * get it in one piece, and with `prefers-reduced-motion` it just appears.\n * A monospace font (the default) keeps the width steady while it runs.\n *\n * @example\n * <ScrambleText className=\"text-sm\">Signal acquired</ScrambleText>\n */\nfunction ScrambleText({\n  children: text,\n  speed = 40,\n  delay = 0,\n  characters = \"01<>[]{}/\\\\|=~^\",\n  trigger = \"load\",\n  once = true,\n  play,\n  className,\n  ref,\n  ...props\n}: ScrambleTextProps) {\n  const [observe, phase] = useEntrance<HTMLSpanElement>({ trigger, once, play });\n  const mergedRef = useMemo(() => mergeRefs(ref, observe), [ref, observe]);\n  const output = useRef<HTMLSpanElement>(null);\n\n  useEffect(() => {\n    const node = output.current;\n    if (!node) return;\n    // Change the value of React's own text node rather than replacing it,\n    // so React's later updates still land on the node that is on screen.\n    const write = (value: string) => {\n      const child = node.firstChild;\n      if (child?.nodeType === Node.TEXT_NODE) child.nodeValue = value;\n      else node.textContent = value;\n      node.style.visibility = \"visible\";\n    };\n    const settle = () => write(text);\n    if (phase === \"static\" || prefersReducedMotion()) return settle();\n    if (phase === \"armed\") return write(NBSP.repeat(Math.max(text.length, 1)));\n\n    const glyphs = Array.from(text);\n    const times = settleTimes(glyphs, Math.max(speed, 1));\n    const end = Math.max(0, ...times);\n    const noise = characters || \"#\";\n    const start = performance.now() + delay;\n    let written = -Infinity;\n    let id = 0;\n    const tick = (now: number) => {\n      const elapsed = now - start;\n      if (elapsed >= end) return settle();\n      if (elapsed >= 0 && now - written >= NOISE_EVERY) {\n        written = now;\n        write(frame(glyphs, elapsed, times, noise));\n      }\n      id = requestAnimationFrame(tick);\n    };\n    id = requestAnimationFrame(tick);\n    return () => cancelAnimationFrame(id);\n  }, [phase, text, speed, delay, characters]);\n\n  return (\n    <span\n      ref={mergedRef}\n      data-slot=\"scramble-text\"\n      data-state={phase}\n      className={cn(\"inline-block font-mono whitespace-pre-wrap\", className)}\n      {...props}\n    >\n      <span className=\"sr-only\">{text}</span>\n      {/* Hidden until the script takes over when it is about to play, so the text does not flash; shown as is without JavaScript. */}\n      <span\n        ref={output}\n        aria-hidden=\"true\"\n        data-slot=\"scramble-text-output\"\n        className={cn(phase === \"play\" && \"[@media(scripting:enabled)]:invisible\")}\n      >\n        {text}\n      </span>\n    </span>\n  );\n}\n\nexport { ScrambleText, type ScrambleTextProps };\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "text"
  ],
  "type": "registry:ui"
}