{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-scramble",
  "title": "Text Scramble",
  "description": "A text scramble reveal animation that decodes characters on mount or when re-triggered, powered by Motion.",
  "dependencies": ["motion"],
  "files": [
    {
      "path": "registry/primitives/texts/text-scramble/index.tsx",
      "content": "\"use client\";\n\nimport { motion, useReducedMotion } from \"motion/react\";\nimport type { MotionProps } from \"motion/react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport type {\n  ComponentType,\n  ElementType,\n  MouseEventHandler,\n  ReactNode,\n} from \"react\";\n\nconst motionComponentCache = new Map<\n  ElementType,\n  ComponentType<MotionProps & { children?: ReactNode }>\n>();\n\nfunction getMotionComponent(\n  Component: ElementType\n): ComponentType<MotionProps & { children?: ReactNode }> {\n  const cached = motionComponentCache.get(Component);\n  if (cached) {\n    return cached;\n  }\n\n  const created = motion.create(Component as never);\n  motionComponentCache.set(Component, created);\n  return created;\n}\n\ntype MotionTextComponent = ComponentType<\n  MotionProps & {\n    children?: ReactNode;\n    className?: string;\n    onMouseEnter?: MouseEventHandler<Element>;\n  }\n>;\n\ninterface ScrambleFrame {\n  revealedLength: number;\n  text: string;\n}\n\nexport type TextScrambleProps = {\n  children: string;\n  duration?: number;\n  speed?: number;\n  characterSet?: string;\n  as?: ElementType;\n  className?: string;\n  onMouseEnter?: MouseEventHandler<Element>;\n  /**\n   * Color for characters still scrambling. Revealed characters keep the root\n   * text color (from `className` or `style`).\n   */\n  scrambleColor?: string;\n  /**\n   * Seconds to scramble in place before the left-to-right reveal begins.\n   * @default 0\n   */\n  holdDuration?: number;\n  /**\n   * When true, runs the scramble on mount or when this value changes.\n   * Ignored while `triggerOnHover` is true.\n   * @default true\n   */\n  trigger?: boolean;\n  /**\n   * When true, runs the scramble on hover instead of on mount.\n   * Mount `trigger` is skipped automatically.\n   * @default false\n   */\n  triggerOnHover?: boolean;\n  onScrambleComplete?: () => void;\n} & Omit<MotionProps, \"onMouseEnter\">;\n\nconst defaultChars =\n  \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\";\n\nfunction randomScrambleChar(characterSet: string): string {\n  return characterSet[Math.floor(Math.random() * characterSet.length)] ?? \"\";\n}\n\nfunction buildScrambledText(\n  source: string,\n  revealedLength: number,\n  characterSet: string\n): string {\n  if (!source) {\n    return \"\";\n  }\n\n  let scrambled = \"\";\n\n  for (let i = 0; i < source.length; i++) {\n    if (source[i] === \" \") {\n      scrambled += \" \";\n      continue;\n    }\n\n    if (i < revealedLength) {\n      scrambled += source[i];\n    } else {\n      scrambled += randomScrambleChar(characterSet);\n    }\n  }\n\n  return scrambled;\n}\n\nexport function TextScramble({\n  children,\n  duration = 0.8,\n  speed = 0.04,\n  characterSet = defaultChars,\n  className,\n  as: Component = \"p\",\n  scrambleColor,\n  holdDuration = 0,\n  trigger = true,\n  triggerOnHover = false,\n  onScrambleComplete,\n  onMouseEnter,\n  ...props\n}: TextScrambleProps) {\n  const prefersReducedMotion = useReducedMotion();\n  const MotionComponent = getMotionComponent(Component) as MotionTextComponent;\n  const [frame, setFrame] = useState<ScrambleFrame | null>(null);\n  const [isAnimating, setIsAnimating] = useState(false);\n  const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);\n  const text = String(children ?? \"\");\n  const shouldTriggerOnMount = trigger && !triggerOnHover;\n\n  const clearAnimationInterval = () => {\n    if (intervalRef.current) {\n      clearInterval(intervalRef.current);\n      intervalRef.current = null;\n    }\n  };\n\n  const scramble = () => {\n    if (prefersReducedMotion || isAnimating || !text) {\n      return;\n    }\n\n    clearAnimationInterval();\n    setIsAnimating(true);\n\n    const holdSteps = holdDuration / speed;\n    const revealSteps = duration / speed;\n    const totalSteps = holdSteps + revealSteps;\n    let step = 0;\n\n    intervalRef.current = setInterval(() => {\n      if (step < holdSteps) {\n        setFrame({\n          text: buildScrambledText(text, 0, characterSet),\n          revealedLength: 0,\n        });\n      } else {\n        const revealStep = step - holdSteps;\n        const progress = revealStep / revealSteps;\n        const revealedLength = Math.floor(progress * text.length);\n\n        setFrame({\n          text: buildScrambledText(text, revealedLength, characterSet),\n          revealedLength,\n        });\n      }\n\n      step++;\n\n      if (step > totalSteps) {\n        clearAnimationInterval();\n        setFrame(null);\n        setIsAnimating(false);\n        onScrambleComplete?.();\n      }\n    }, speed * 1000);\n  };\n\n  // oxlint-disable-next-line react-hooks/exhaustive-deps -- unmount cleanup only\n  useEffect(\n    () => () => {\n      clearAnimationInterval();\n    },\n    []\n  );\n\n  /* oxlint-disable react-hooks/exhaustive-deps -- re-run only when mount trigger changes */\n  useEffect(() => {\n    if (!shouldTriggerOnMount || prefersReducedMotion) {\n      return;\n    }\n\n    scramble();\n  }, [shouldTriggerOnMount, trigger]);\n  /* oxlint-enable react-hooks/exhaustive-deps */\n\n  // oxlint-disable-next-line react-hooks/exhaustive-deps -- re-run when mount trigger changes\n  useEffect(() => {\n    if (!(prefersReducedMotion && shouldTriggerOnMount)) {\n      return;\n    }\n\n    onScrambleComplete?.();\n  }, [prefersReducedMotion, shouldTriggerOnMount, trigger, onScrambleComplete]);\n\n  const handleMouseEnter: MouseEventHandler<Element> = (event) => {\n    onMouseEnter?.(event);\n\n    if (triggerOnHover && !prefersReducedMotion) {\n      scramble();\n    }\n  };\n\n  const renderContent = () => {\n    if (!frame) {\n      return children;\n    }\n\n    if (!scrambleColor) {\n      return frame.text;\n    }\n\n    return frame.text.split(\"\").map((char, index) => (\n      <span\n        // oxlint-disable-next-line react/no-array-index-key, react-doctor/no-array-index-as-key -- static string indices per frame\n        key={index}\n        style={\n          index < frame.revealedLength ? undefined : { color: scrambleColor }\n        }\n      >\n        {char}\n      </span>\n    ));\n  };\n\n  if (prefersReducedMotion) {\n    return (\n      <MotionComponent\n        className={className}\n        onMouseEnter={handleMouseEnter}\n        {...props}\n      >\n        {children}\n      </MotionComponent>\n    );\n  }\n\n  return (\n    <MotionComponent\n      aria-label={frame ? text : undefined}\n      className={className}\n      onMouseEnter={handleMouseEnter}\n      {...props}\n    >\n      {renderContent()}\n    </MotionComponent>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/sora-ui/texts/text-scramble.tsx"
    }
  ],
  "meta": {
    "demoProps": {
      "TextScramble": {
        "children": {
          "value": "Sora UI"
        },
        "holdDuration": {
          "value": 0,
          "min": 0,
          "max": 2,
          "step": 0.1
        },
        "duration": {
          "value": 0.5,
          "min": 0.2,
          "max": 3,
          "step": 0.1
        },
        "speed": {
          "value": 0.03,
          "min": 0.01,
          "max": 0.15,
          "step": 0.01
        },
        "trigger": {
          "value": true
        },
        "triggerOnHover": {
          "value": false
        }
      }
    }
  },
  "type": "registry:ui"
}
