{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-scramble-pro",
  "title": "Text Scramble Pro",
  "description": "Cycling phrases that decrypt from scrambled characters into readable text, left-to-right, right-to-left, or random reveal order, with a scramble-out transition and blinking cursor.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/text-scramble-pro/text-scramble-pro.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nconst SCRAMBLE_CHARS = \"!<>-_\\\\/[]{}=+*^?#@~|\";\n\nexport type TextScrambleRevealMode = \"left-to-right\" | \"right-to-left\" | \"random\";\n\ninterface CharState {\n  char: string;\n  revealed: boolean;\n  isSpace: boolean;\n}\n\nconst DEFAULT_PHRASES = [\"The future is already here\", \"Reality is just a rendering\", \"Every detail has a purpose\"];\n\nfunction randomChar() {\n  return SCRAMBLE_CHARS[Math.floor(Math.random() * SCRAMBLE_CHARS.length)];\n}\n\nfunction buildRevealOrder(target: string, mode: TextScrambleRevealMode): number[] {\n  const indices = target\n    .split(\"\")\n    .map((_, i) => i)\n    .filter((i) => target[i] !== \" \");\n  if (mode === \"right-to-left\") return [...indices].reverse();\n  if (mode === \"random\") {\n    const arr = [...indices];\n    for (let i = arr.length - 1; i > 0; i--) {\n      const j = Math.floor(Math.random() * (i + 1));\n      [arr[i], arr[j]] = [arr[j], arr[i]];\n    }\n    return arr;\n  }\n  return indices;\n}\n\nexport interface TextScrambleProProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  phrases?: string[];\n  fontSize?: number;\n  fontWeight?: number;\n  letterSpacing?: number;\n  fontColor?: string;\n  scrambleColor?: string;\n  framesPerChar?: number;\n  holdDuration?: number;\n  pauseDuration?: number;\n  fontFamily?: string;\n  textAlign?: \"left\" | \"center\" | \"right\";\n  background?: string;\n  padding?: number;\n  revealMode?: TextScrambleRevealMode;\n  scrollTrigger?: boolean;\n  scrambleOut?: boolean;\n  loop?: boolean;\n  showCursor?: boolean;\n  fadeInDuration?: number;\n}\n\nexport function TextScramblePro({\n  phrases = DEFAULT_PHRASES,\n  fontSize = 32,\n  fontWeight = 400,\n  letterSpacing = 0.04,\n  fontColor = \"#ffffff\",\n  scrambleColor = \"#555555\",\n  framesPerChar = 10,\n  holdDuration = 2200,\n  pauseDuration = 400,\n  fontFamily = '\"Courier New\", Courier, monospace',\n  textAlign = \"center\",\n  background = \"transparent\",\n  padding = 40,\n  revealMode = \"left-to-right\",\n  scrollTrigger = false,\n  scrambleOut = true,\n  loop = true,\n  showCursor = true,\n  fadeInDuration = 600,\n  className,\n  ...props\n}: TextScrambleProProps) {\n  const [chars, setChars] = React.useState<CharState[]>([]);\n  const [phraseIndex, setPhraseIndex] = React.useState(0);\n  const [started, setStarted] = React.useState(false);\n  const [done, setDone] = React.useState(false);\n  const [visible, setVisible] = React.useState(false);\n  const [cursorOn, setCursorOn] = React.useState(true);\n\n  const containerRef = React.useRef<HTMLDivElement>(null);\n  const measureRef = React.useRef<HTMLSpanElement>(null);\n  const rafRef = React.useRef<number | null>(null);\n  const timeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n\n  const [containerWidth, setContainerWidth] = React.useState(0);\n  const [scaledFontSize, setScaledFontSize] = React.useState(fontSize);\n\n  const p = React.useRef({ phrases, framesPerChar, holdDuration, pauseDuration, revealMode: revealMode, scrambleOut, loop: loop });\n  React.useEffect(() => {\n    p.current = { phrases, framesPerChar, holdDuration, pauseDuration, revealMode: revealMode, scrambleOut, loop: loop };\n  });\n\n  React.useEffect(() => {\n    if (!loop || !done) return;\n    const id = setTimeout(() => {\n      setDone(false);\n      setPhraseIndex(0);\n    }, 0);\n    return () => clearTimeout(id);\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [loop]);\n\n  React.useEffect(() => {\n    const el = containerRef.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => setContainerWidth(el.clientWidth));\n    ro.observe(el);\n    setContainerWidth(el.clientWidth);\n    return () => ro.disconnect();\n  }, []);\n\n  React.useLayoutEffect(() => {\n    const measureEl = measureRef.current;\n    if (!measureEl || containerWidth === 0) return;\n    const available = containerWidth - padding * 2;\n    if (available <= 0) return;\n    const natural = measureEl.scrollWidth;\n    setScaledFontSize(natural > available ? Math.floor(fontSize * (available / natural)) : fontSize);\n  }, [containerWidth, fontSize, padding, phrases]);\n\n  React.useEffect(() => {\n    if (!showCursor) return;\n    const id = setInterval(() => setCursorOn((v) => !v), 530);\n    return () => clearInterval(id);\n  }, [showCursor]);\n\n  React.useEffect(() => {\n    if (!scrollTrigger) {\n      const t = setTimeout(() => {\n        setVisible(true);\n        setStarted(true);\n      }, 50);\n      return () => clearTimeout(t);\n    }\n    const el = containerRef.current;\n    if (!el) return;\n    const observer = new IntersectionObserver(\n      ([entry]) => {\n        if (entry.isIntersecting) {\n          setVisible(true);\n          setStarted(true);\n          observer.disconnect();\n        }\n      },\n      { threshold: 0.25 },\n    );\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, [scrollTrigger]);\n\n  const clearTimers = React.useCallback(() => {\n    if (rafRef.current != null) {\n      cancelAnimationFrame(rafRef.current);\n      rafRef.current = null;\n    }\n    if (timeoutRef.current != null) {\n      clearTimeout(timeoutRef.current);\n      timeoutRef.current = null;\n    }\n  }, []);\n\n  const animatePhrase = React.useCallback((target: string) => {\n    if (rafRef.current != null) {\n      cancelAnimationFrame(rafRef.current);\n      rafRef.current = null;\n    }\n    if (timeoutRef.current != null) {\n      clearTimeout(timeoutRef.current);\n      timeoutRef.current = null;\n    }\n\n    const splitChars = target.split(\"\");\n    const len = splitChars.length;\n    const revealOrder = buildRevealOrder(target, p.current.revealMode);\n    const totalFrames = revealOrder.length * p.current.framesPerChar;\n\n    const revealAt: number[] = new Array(len).fill(Infinity);\n    revealOrder.forEach((ci, i) => {\n      revealAt[ci] = i * p.current.framesPerChar;\n    });\n\n    const advancePhrase = () => {\n      setPhraseIndex((prev) => {\n        const next = (prev + 1) % p.current.phrases.length;\n        if (!p.current.loop && next === 0) {\n          setDone(true);\n          return prev;\n        }\n        return next;\n      });\n    };\n\n    const runScrambleOut = (order: number[]) => {\n      const outOrder = [...order].reverse();\n      const totalOut = outOrder.length * p.current.framesPerChar;\n      const unrevealAt: number[] = new Array(len).fill(Infinity);\n      outOrder.forEach((ci, i) => {\n        unrevealAt[ci] = i * p.current.framesPerChar;\n      });\n      let outFrame = 0;\n      const tickOut = () => {\n        setChars(\n          splitChars.map((c, i) => {\n            if (c === \" \") return { char: \" \", revealed: true, isSpace: true };\n            if (outFrame >= unrevealAt[i]) return { char: randomChar(), revealed: false, isSpace: false };\n            return { char: c, revealed: true, isSpace: false };\n          }),\n        );\n        if (outFrame < totalOut) {\n          outFrame++;\n          rafRef.current = requestAnimationFrame(tickOut);\n        } else {\n          advancePhrase();\n        }\n      };\n      rafRef.current = requestAnimationFrame(tickOut);\n    };\n\n    let frame = 0;\n    const tick = () => {\n      setChars(\n        splitChars.map((c, i) => {\n          if (c === \" \") return { char: \" \", revealed: true, isSpace: true };\n          if (frame >= revealAt[i]) return { char: c, revealed: true, isSpace: false };\n          return { char: randomChar(), revealed: false, isSpace: false };\n        }),\n      );\n      if (frame < totalFrames) {\n        frame++;\n        rafRef.current = requestAnimationFrame(tick);\n      } else {\n        timeoutRef.current = setTimeout(\n          () => {\n            if (p.current.scrambleOut) runScrambleOut(revealOrder);\n            else advancePhrase();\n          },\n          p.current.holdDuration,\n        );\n      }\n    };\n\n    timeoutRef.current = setTimeout(() => {\n      rafRef.current = requestAnimationFrame(tick);\n    }, p.current.pauseDuration);\n  }, []);\n\n  React.useEffect(() => {\n    if (!started || done) return;\n    const target = phrases[phraseIndex] ?? \"\";\n    const id = setTimeout(() => {\n      setChars(target.split(\"\").map((c) => ({ char: c === \" \" ? \" \" : randomChar(), revealed: false, isSpace: c === \" \" })));\n      animatePhrase(target);\n    }, 0);\n    return () => {\n      clearTimeout(id);\n      clearTimers();\n    };\n  }, [phraseIndex, phrases, animatePhrase, clearTimers, started, done]);\n\n  const justifyMap: Record<string, string> = { left: \"flex-start\", center: \"center\", right: \"flex-end\" };\n\n  return (\n    <div\n      ref={containerRef}\n      className={cn(\"relative box-border flex w-full h-full items-center overflow-hidden\", className)}\n      style={{\n        background,\n        justifyContent: justifyMap[textAlign] ?? \"center\",\n        padding: `0 ${padding}px`,\n        opacity: visible ? 1 : 0,\n        transition: fadeInDuration > 0 ? `opacity ${fadeInDuration}ms ease` : undefined,\n      }}\n      {...props}\n    >\n      <span\n        ref={measureRef}\n        aria-hidden\n        className=\"absolute pointer-events-none whitespace-nowrap invisible\"\n        style={{ fontFamily, fontSize, fontWeight, letterSpacing: `${letterSpacing}em` }}\n      >\n        {phrases.reduce((a, b) => (a.length >= b.length ? a : b), \"\")}\n      </span>\n\n      <span\n        className=\"whitespace-nowrap select-none\"\n        style={{ fontFamily, fontSize: scaledFontSize, fontWeight, lineHeight: 1.4, letterSpacing: `${letterSpacing}em` }}\n      >\n        {chars.map((c, i) => (\n          <span\n            key={i}\n            className=\"inline-block\"\n            style={{ color: c.isSpace ? \"transparent\" : c.revealed ? fontColor : scrambleColor, minWidth: c.isSpace ? `${scaledFontSize * 0.4}px` : undefined }}\n          >\n            {c.char}\n          </span>\n        ))}\n        {showCursor && (\n          <span className=\"ml-0.5 inline-block transition-opacity duration-75\" style={{ color: fontColor, opacity: cursorOn ? 1 : 0 }}>\n            |\n          </span>\n        )}\n      </span>\n\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}