{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scramble-text",
  "title": "Scramble text",
  "description": "Text scrambles through random glyphs and resolves left to right on mount, on view, on hover or on demand; renders the real text on the server.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/components/text/scramble-text.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { useInView, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\n\nconst GLYPHS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#%&*+=<>/\"\n\nexport type ScrambleTextProps = Omit<React.HTMLAttributes<HTMLSpanElement>, \"children\"> & {\n  /** The final text; it is rendered as-is on the server and before the first run. */\n  text: string\n  /** When to play: on mount, when scrolled into view, on hover/focus (of the text or its closest link/button), or only via `replay`. */\n  trigger?: \"mount\" | \"view\" | \"hover\" | \"manual\"\n  /** Change this value (e.g. a counter) to play again. */\n  replay?: number | string\n  /** Milliseconds until the last character resolves. */\n  duration?: number\n  /** Milliseconds between glyph changes. */\n  speed?: number\n  /** Glyphs used while scrambling. */\n  characters?: string\n  /** Classes for the not-yet-resolved part (default: 60% opacity of the current color). */\n  scrambledClassName?: string\n  /** Called when the last character has resolved. */\n  onComplete?: () => void\n}\n\n/**\n * Scrambles through random glyphs, then resolves left to right. Renders the real text on the server and before\n * the first run; randomness only happens after mount. Static for reduced motion.\n */\nexport function ScrambleText({\n  text,\n  trigger = \"mount\",\n  replay,\n  duration = 900,\n  speed = 45,\n  characters = GLYPHS,\n  scrambledClassName,\n  onComplete,\n  className,\n  onPointerEnter,\n  onFocus,\n  ...props\n}: ScrambleTextProps) {\n  const reduce = useReducedMotion() ?? false\n  const ref = React.useRef<HTMLSpanElement>(null)\n  const inView = useInView(ref, { once: true, amount: 0.6 })\n  const [frame, setFrame] = React.useState<{ done: string; tail: string } | null>(null)\n  const raf = React.useRef(0)\n  const running = React.useRef(false)\n  const completeRef = React.useRef(onComplete)\n  completeRef.current = onComplete\n\n  const play = React.useCallback(() => {\n    if (reduce) return\n    cancelAnimationFrame(raf.current)\n    running.current = true\n    const chars = Array.from(text)\n    const n = chars.length\n    const start = performance.now()\n    let lastSwap = -Infinity\n    let tail = \"\"\n    let resolved = -1\n    const random = () => characters[Math.floor(Math.random() * characters.length)] ?? \"\"\n    const tick = (now: number) => {\n      const r = Math.min(n, Math.floor(((now - start) / duration) * (n + 1)))\n      if (r >= n) {\n        running.current = false\n        setFrame(null)\n        completeRef.current?.()\n        return\n      }\n      if (now - lastSwap >= speed || r !== resolved) {\n        if (now - lastSwap >= speed) {\n          lastSwap = now\n          tail = chars\n            .slice(r)\n            .map((c) => (/\\s/.test(c) ? c : random()))\n            .join(\"\")\n        } else {\n          tail = Array.from(tail).slice(r - resolved).join(\"\")\n        }\n        resolved = r\n        setFrame({ done: chars.slice(0, r).join(\"\"), tail })\n      }\n      raf.current = requestAnimationFrame(tick)\n    }\n    raf.current = requestAnimationFrame(tick)\n  }, [text, duration, speed, characters, reduce])\n\n  React.useEffect(() => () => cancelAnimationFrame(raf.current), [])\n\n  // The span itself is not focusable, so with trigger=\"hover\" it also listens to its closest link or button.\n  const playRef = React.useRef(play)\n  playRef.current = play\n  React.useEffect(() => {\n    if (trigger !== \"hover\") return\n    const host = ref.current?.parentElement?.closest<HTMLElement>(\"a[href], button, summary, [tabindex], [role=button], [role=link], [role=tab], [role=menuitem]\")\n    if (!host) return\n    const onIn = () => {\n      if (!running.current) playRef.current()\n    }\n    host.addEventListener(\"focusin\", onIn)\n    host.addEventListener(\"pointerenter\", onIn)\n    return () => {\n      host.removeEventListener(\"focusin\", onIn)\n      host.removeEventListener(\"pointerenter\", onIn)\n    }\n  }, [trigger])\n\n  React.useEffect(() => {\n    if (trigger === \"mount\") play()\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [trigger, text])\n\n  React.useEffect(() => {\n    if (trigger === \"view\" && inView) play()\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [trigger, inView])\n\n  const first = React.useRef(true)\n  React.useEffect(() => {\n    if (first.current) {\n      first.current = false\n      return\n    }\n    play()\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [replay])\n\n  return (\n    <span\n      ref={ref}\n      className={cn(\"whitespace-pre-wrap\", className)}\n      onPointerEnter={(e) => {\n        if (trigger === \"hover\" && !running.current) play()\n        onPointerEnter?.(e)\n      }}\n      onFocus={(e) => {\n        if (trigger === \"hover\" && !running.current) play()\n        onFocus?.(e)\n      }}\n      {...props}\n    >\n      <span className=\"sr-only\">{text}</span>\n      {frame ? (\n        <span aria-hidden=\"true\">\n          {frame.done}\n          {/* Dimmed with opacity, not a muted color, so the glyphs keep contrast on muted or tinted backgrounds. */}\n          <span className={cn(\"opacity-60\", scrambledClassName)}>{frame.tail}</span>\n        </span>\n      ) : (\n        <span aria-hidden=\"true\">{text}</span>\n      )}\n    </span>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/scramble-text.tsx"
    }
  ],
  "categories": [
    "components",
    "text"
  ],
  "type": "registry:component"
}