{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "encoded-reveal",
  "title": "Encoded Reveal",
  "description": "Text that decrypts into place: random characters scramble, then resolve letter by letter.",
  "dependencies": [
    "motion"
  ],
  "files": [
    {
      "path": "packages/encoded-reveal/encoded-reveal.tsx",
      "content": "\"use client\";\r\n\r\nimport { useEffect, useRef, useState, useContext, useSyncExternalStore } from \"react\";\r\nimport { MotionConfigContext, useInView } from \"motion/react\";\r\nimport { cn } from \"@/lib/utils\";\r\n\r\n/**\r\n * \"user\" (the default) follows a surrounding <MotionConfig reducedMotion>,\r\n * then the visitor's \"reduce motion\" device setting. \"always\" and \"never\"\r\n * force the calm or the full version.\r\n */\r\nexport type ReducedMotion = \"user\" | \"always\" | \"never\";\r\n\r\nconst REDUCED_MOTION_QUERY = \"(prefers-reduced-motion: reduce)\";\r\n\r\nfunction subscribeToReducedMotion(onChange: () => void) {\r\n  const query = window.matchMedia(REDUCED_MOTION_QUERY);\r\n  query.addEventListener(\"change\", onChange);\r\n  return () => query.removeEventListener(\"change\", onChange);\r\n}\r\n\r\n// The prop wins, then MotionConfig, then the device setting. The device is\r\n// read as \"no\" while server rendering, so hydration always matches.\r\nfunction useShouldReduceMotion(mode: ReducedMotion = \"user\") {\r\n  const device = useSyncExternalStore(\r\n    subscribeToReducedMotion,\r\n    () => window.matchMedia(REDUCED_MOTION_QUERY).matches,\r\n    () => false,\r\n  );\r\n  const config = useContext(MotionConfigContext).reducedMotion;\r\n  if (mode !== \"user\") return mode === \"always\";\r\n  if (config === \"always\" || config === \"never\") return config === \"always\";\r\n  return device;\r\n}\r\n\r\ninterface EncodedRevealProps extends Omit<React.ComponentProps<\"span\">, \"children\"> {\r\n  /** The text to decode. Plain text only. */\r\n  children: string;\r\n  /**\r\n   * Seconds the whole scramble takes, whatever the length of the text. Very long text can\r\n   * take a little longer: the scramble never runs faster than one frame per screen refresh.\r\n   * @default 1\r\n   */\r\n  duration?: number;\r\n  /** Seconds to wait before decoding starts. */\r\n  delay?: number;\r\n  /** Classes for the text element. */\r\n  className?: string;\r\n  /** Wait until the text scrolls into view before decoding it. */\r\n  inView?: boolean;\r\n  /** With inView, decode only the first time it comes into view. */\r\n  once?: boolean;\r\n  /**\r\n   * \"user\" follows a surrounding MotionConfig, then the visitor's reduce-motion setting.\r\n   * \"always\" and \"never\" force the calm or the full version.\r\n   */\r\n  reducedMotion?: ReducedMotion;\r\n}\r\n\r\nconst RANDOM_CHARS = \"_!X$0-+*#\";\r\n\r\nfunction getRandomChar(prevChar?: string): string {\r\n  let char: string;\r\n  do {\r\n    char = RANDOM_CHARS[Math.floor(Math.random() * RANDOM_CHARS.length)];\r\n  } while (char === prevChar);\r\n  return char;\r\n}\r\n\r\nconst EncodedReveal = ({\r\n  children,\r\n  duration = 1,\r\n  delay = 0,\r\n  className,\r\n  inView = false,\r\n  once = true,\r\n  reducedMotion = \"user\",\r\n  ref,\r\n  ...props\r\n}: EncodedRevealProps) => {\r\n  const containerRef = useRef<HTMLSpanElement | null>(null);\r\n  const isInView = useInView(containerRef, { once, margin: \"-100px\" });\r\n  const shouldAnimate = inView ? isInView : true;\r\n  // Reduced motion skips the scramble and shows the text as it is.\r\n  const reduce = useShouldReduceMotion(reducedMotion);\r\n  const text = children ?? \"\";\r\n  // null until the first frame, which holds the text's width with blanks.\r\n  const [frame, setFrame] = useState<string | null>(null);\r\n\r\n  useEffect(() => {\r\n    if (reduce || !shouldAnimate) return;\r\n\r\n    const length = text.length;\r\n    let phase: 1 | 2 = 1;\r\n    let step = 0;\r\n    let interval: number | undefined;\r\n\r\n    const tick = () => {\r\n      const chars: string[] = [];\r\n      if (phase === 1) {\r\n        // Phase 1: a growing run of random characters fills the width.\r\n        const shown = Math.min(step + 1, length);\r\n        for (let i = 0; i < shown; i++) chars.push(getRandomChar(chars[i - 1]));\r\n        for (let i = shown; i < length; i++) chars.push(\" \");\r\n        setFrame(chars.join(\"\"));\r\n        if (step < length * 2 - 1) step++;\r\n        else {\r\n          phase = 2;\r\n          step = 0;\r\n        }\r\n        return;\r\n      }\r\n\r\n      // Phase 2: the real text locks in left to right behind a cursor.\r\n      const revealed = Math.floor(step / 2);\r\n      for (let i = 0; i < revealed && i < length; i++) chars.push(text[i]);\r\n      if (revealed < length) chars.push(step % 2 === 0 ? \"_\" : getRandomChar());\r\n      for (let i = chars.length; i < length; i++) chars.push(getRandomChar());\r\n      if (step < length * 2 - 1) {\r\n        setFrame(chars.join(\"\"));\r\n        step++;\r\n      } else {\r\n        setFrame(text);\r\n        window.clearInterval(interval);\r\n      }\r\n    };\r\n\r\n    // Both phases take length * 2 frames, so the whole scramble is length * 4.\r\n    const frameMs = Math.max(16, (Math.max(0, duration) * 1000) / Math.max(1, length * 4));\r\n    const start = window.setTimeout(() => {\r\n      interval = window.setInterval(tick, frameMs);\r\n    }, Math.max(0, delay) * 1000);\r\n\r\n    return () => {\r\n      window.clearTimeout(start);\r\n      window.clearInterval(interval);\r\n    };\r\n  }, [text, duration, delay, shouldAnimate, reduce]);\r\n\r\n  const display = reduce ? text : (frame ?? \" \".repeat(text.length));\r\n\r\n  return (\r\n    <span\r\n      ref={(node) => {\r\n        containerRef.current = node;\r\n        if (typeof ref === \"function\") return ref(node);\r\n        if (ref) ref.current = node;\r\n      }}\r\n      // whitespace-pre holds the width steady: every frame is as long as the\r\n      // text, and the font is monospaced. Size and line height come from the\r\n      // surrounding text, so it works at any size.\r\n      className={cn(\"inline-flex whitespace-pre font-mono font-medium\", className)}\r\n      {...props}\r\n    >\r\n      <span className=\"sr-only\">{text}</span>\r\n      <span aria-hidden=\"true\">{display}</span>\r\n    </span>\r\n  );\r\n};\r\n\r\nexport default EncodedReveal;\r\n",
      "type": "registry:component",
      "target": "components/satori-ui/encoded-reveal.tsx"
    }
  ],
  "meta": {
    "usage": "<EncodedReveal inView>Access granted</EncodedReveal>"
  },
  "type": "registry:component"
}