{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "encrypted-text",
  "type": "registry:component",
  "title": "Encrypted Text",
  "description": "Text that decodes itself: every character shuffles through a charset in a fixed-width cell so the line never jitters, then a lock wave runs left to right and each character drops into place with a spring pop. Start it on load, on hover, on click or when it scrolls into view, and loop it if you like.",
  "dependencies": [
    "framer-motion",
    "react"
  ],
  "registryDependencies": [
    "https://www.nexus-ui.com/api/registry/nexus-font",
    "utils"
  ],
  "files": [
    {
      "path": "next-ui/encrypted-text/encrypted-text.tsx",
      "type": "registry:component",
      "target": "components/encrypted-text/encrypted-text.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { animate, useInView, useReducedMotion } from \"framer-motion\";\nimport { cn } from \"@/lib/utils\";\n\nconst DEFAULT_CHARSET =\n  \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#$%&*<>/[]{}=+\";\n\nexport type EncryptedTextTrigger = \"load\" | \"hover\" | \"viewport\" | \"click\";\n\nexport interface EncryptedTextProps {\n  /** The string that ends up on screen. */\n  text: string;\n  className?: string;\n  /** Milliseconds between one character locking in and the next. */\n  speed?: number;\n  /** Milliseconds between scramble ticks. */\n  shuffleSpeed?: number;\n  /** Milliseconds before the decode starts. */\n  revealDelay?: number;\n  /** Characters used while scrambling. */\n  characters?: string;\n  /** Scramble ticks before the first character locks. */\n  iterations?: number;\n  /** What starts the decode. */\n  trigger?: EncryptedTextTrigger;\n  /** Scrambles and decodes again after a pause. */\n  loop?: boolean;\n  /** Milliseconds between loops. */\n  loopDelay?: number;\n  /** Element used as the root. */\n  as?: \"span\" | \"p\" | \"h1\" | \"h2\" | \"h3\" | \"div\";\n  /** Called each time every character has locked in. */\n  onComplete?: () => void;\n  /** Classes for the scrambling glyphs. Defaults to the accent blue. */\n  scrambleClassName?: string;\n  /** Replays the decode every `loopDelay` ms until someone hovers, focuses or clicks it. */\n  autoplay?: boolean;\n}\n\nconst isGap = (c: string) => c === \" \" || c === \"\\n\" || c === \"\\t\";\n\n/**\n * Text that decodes itself. Every unlocked character shuffles through a charset in a fixed-width\n * cell (so the line never jitters), then a lock wave runs left to right and each character drops\n * into place with a spring pop. Glyphs are written straight to the DOM, so the scramble costs no\n * React renders.\n */\nexport function EncryptedText({\n  text,\n  className,\n  speed = 50,\n  shuffleSpeed = 35,\n  revealDelay = 0,\n  characters = DEFAULT_CHARSET,\n  iterations = 8,\n  trigger = \"load\",\n  loop = false,\n  loopDelay = 2000,\n  as: Tag = \"span\",\n  onComplete,\n  scrambleClassName,\n  autoplay = false,\n}: EncryptedTextProps) {\n  const still = useReducedMotion() === true;\n  const root = React.useRef<HTMLElement>(null);\n  const finals = React.useRef<(HTMLSpanElement | null)[]>([]);\n  const glyphs = React.useRef<(HTMLSpanElement | null)[]>([]);\n  const touched = React.useRef(false);\n  const stopRun = React.useRef<() => void>(() => {});\n  const cfg = React.useRef({\n    speed,\n    shuffleSpeed,\n    iterations,\n    characters,\n    loop,\n    loopDelay,\n    onComplete,\n    autoplay,\n  });\n  React.useEffect(() => {\n    cfg.current = {\n      speed,\n      shuffleSpeed,\n      iterations,\n      characters,\n      loop,\n      loopDelay,\n      onComplete,\n      autoplay,\n    };\n  });\n  const inView = useInView(root, { once: !(loop || autoplay), amount: 0.3 });\n  const chars = React.useMemo(() => Array.from(text), [text]);\n  const scrambledAtRest = trigger === \"load\" || trigger === \"viewport\";\n\n  const showFinal = React.useCallback(() => {\n    finals.current.forEach((el) => {\n      if (el) el.style.opacity = \"1\";\n    });\n    glyphs.current.forEach((el) => {\n      if (el) el.style.opacity = \"0\";\n    });\n  }, []);\n\n  const play = React.useCallback(() => {\n    const start = () => {\n      stopRun.current();\n      const {\n        speed: sp,\n        shuffleSpeed: ss,\n        iterations: it,\n        characters: cs,\n      } = cfg.current;\n      const set = cs.length ? cs : DEFAULT_CHARSET;\n      const pick = () => set[Math.floor(Math.random() * set.length)] ?? \"#\";\n      const open = chars\n        .map((c, i) => (isGap(c) ? -1 : i))\n        .filter((i) => i >= 0);\n      let dead = false;\n      const timers: ReturnType<typeof setTimeout>[] = [];\n      const pops: { stop: () => void }[] = [];\n      open.forEach((i) => {\n        const f = finals.current[i];\n        const g = glyphs.current[i];\n        if (f) f.style.opacity = \"0\";\n        if (g) {\n          g.style.opacity = \"0.85\";\n          g.textContent = pick();\n        }\n      });\n      const locked = new Set<number>();\n      const shuffle = setInterval(\n        () => {\n          open.forEach((i) => {\n            if (locked.has(i)) return;\n            const g = glyphs.current[i];\n            if (g) g.textContent = pick();\n          });\n        },\n        Math.max(16, ss),\n      );\n      let k = 0;\n      let lockTimer: ReturnType<typeof setInterval> | undefined;\n      timers.push(\n        setTimeout(\n          () => {\n            lockTimer = setInterval(\n              () => {\n                if (dead) return;\n                const i = open[k++];\n                if (i === undefined) {\n                  clearInterval(lockTimer);\n                  clearInterval(shuffle);\n                  cfg.current.onComplete?.();\n                  const { loop: lp, loopDelay: ld, autoplay: ap } = cfg.current;\n                  if (lp || (ap && !touched.current))\n                    timers.push(\n                      setTimeout(() => !dead && start(), Math.max(0, ld)),\n                    );\n                  return;\n                }\n                locked.add(i);\n                const f = finals.current[i];\n                const g = glyphs.current[i];\n                if (g) g.style.opacity = \"0\";\n                if (f) {\n                  f.style.opacity = \"1\";\n                  pops.push(\n                    animate(\n                      f,\n                      { y: [\"-0.28em\", \"0em\"], scale: [1.32, 1] },\n                      { type: \"spring\", stiffness: 620, damping: 17 },\n                    ),\n                  );\n                }\n              },\n              Math.max(8, sp),\n            );\n          },\n          Math.max(0, it) * Math.max(16, ss),\n        ),\n      );\n      stopRun.current = () => {\n        dead = true;\n        clearInterval(shuffle);\n        if (lockTimer) clearInterval(lockTimer);\n        timers.forEach(clearTimeout);\n        pops.forEach((p) => p.stop());\n      };\n    };\n    start();\n  }, [chars]);\n\n  // Clean everything up when the text changes or the component goes away.\n  const pending = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n  React.useEffect(\n    () => () => {\n      clearTimeout(pending.current);\n      stopRun.current();\n    },\n    [chars],\n  );\n\n  React.useEffect(() => {\n    if (still) {\n      showFinal();\n      return;\n    }\n    if (trigger === \"load\" || (trigger === \"viewport\" && inView)) {\n      const t = setTimeout(play, Math.max(0, revealDelay));\n      return () => clearTimeout(t);\n    }\n    if ((trigger === \"hover\" || trigger === \"click\") && autoplay) {\n      const t = setTimeout(play, Math.max(0, revealDelay) + 600);\n      return () => clearTimeout(t);\n    }\n  }, [still, trigger, inView, revealDelay, play, showFinal, autoplay]);\n\n  const interact = () => {\n    touched.current = true;\n    if (still) return;\n    clearTimeout(pending.current);\n    pending.current = setTimeout(play, Math.max(0, revealDelay));\n  };\n\n  const groups = React.useMemo(() => {\n    const out: { start: number; chars: string[]; gap: boolean }[] = [];\n    chars.forEach((c, i) => {\n      const gap = isGap(c);\n      const last = out[out.length - 1];\n      if (last && last.gap === gap && !gap) last.chars.push(c);\n      else out.push({ start: i, chars: [c], gap });\n    });\n    return out;\n  }, [chars]);\n\n  const interactive = trigger === \"hover\" || trigger === \"click\";\n\n  return (\n    <Tag\n      ref={root as React.Ref<never>}\n      aria-label={trigger === \"click\" ? text : undefined}\n      role={trigger === \"click\" ? \"button\" : undefined}\n      tabIndex={trigger === \"click\" ? 0 : undefined}\n      onPointerEnter={\n        trigger === \"hover\"\n          ? interact\n          : () => {\n              touched.current = true;\n            }\n      }\n      onFocus={trigger === \"hover\" ? interact : undefined}\n      onClick={trigger === \"click\" ? interact : undefined}\n      onKeyDown={\n        trigger === \"click\"\n          ? (e: React.KeyboardEvent) => {\n              if (e.key === \"Enter\" || e.key === \" \") {\n                e.preventDefault();\n                interact();\n              }\n            }\n          : undefined\n      }\n      className={cn(\n        \"font-mono whitespace-pre-wrap\",\n        interactive &&\n          \"cursor-pointer rounded-[6px] outline-none focus-visible:ring-2 focus-visible:ring-[#0071e3] dark:focus-visible:ring-[#0a84ff]\",\n        className,\n      )}\n    >\n      <span className=\"sr-only\">{text}</span>\n      <span aria-hidden=\"true\">\n        {groups.map((g) =>\n          g.gap ? (\n            <React.Fragment key={`${text}-g${g.start}`}>\n              {g.chars.join(\"\")}\n            </React.Fragment>\n          ) : (\n            <span\n              key={`${text}-w${g.start}`}\n              className=\"inline-block whitespace-nowrap\"\n            >\n              {g.chars.map((c, k) => {\n                const i = g.start + k;\n                return (\n                  <span key={i} className=\"relative inline-block\">\n                    <span\n                      ref={(el) => {\n                        finals.current[i] = el;\n                      }}\n                      className=\"inline-block\"\n                      style={{ opacity: scrambledAtRest ? 0 : 1 }}\n                    >\n                      {c}\n                    </span>\n                    <span\n                      ref={(el) => {\n                        glyphs.current[i] = el;\n                      }}\n                      className={cn(\n                        \"pointer-events-none absolute inset-0 flex select-none justify-center font-mono [text-shadow:0_0_8px_currentColor]\",\n                        scrambleClassName ??\n                          \"text-[#0071e3] dark:text-[#0a84ff]\",\n                      )}\n                      style={{ opacity: scrambledAtRest ? 0.85 : 0 }}\n                    >\n                      {scrambledAtRest\n                        ? DEFAULT_CHARSET[(i * 7 + 3) % DEFAULT_CHARSET.length]\n                        : \"\"}\n                    </span>\n                  </span>\n                );\n              })}\n            </span>\n          ),\n        )}\n      </span>\n    </Tag>\n  );\n}\n"
    },
    {
      "path": "next-ui/encrypted-text/index.ts",
      "type": "registry:lib",
      "target": "components/encrypted-text/index.ts",
      "content": "export type { EncryptedTextProps, EncryptedTextTrigger } from \"./encrypted-text\";\nexport { EncryptedText } from \"./encrypted-text\";\n"
    }
  ]
}
