{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "typewriter-swap",
  "type": "registry:component",
  "title": "Typewriter Swap",
  "description": "Types a phrase, holds it, then deletes only what differs before typing the next one. A shared opening stays on screen and is dimmed while the rest is erased.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "text",
    "text",
    "typewriter",
    "rotating",
    "phrases",
    "diff",
    "caret"
  ],
  "docs": "https://hyperblur.dev/effects/typewriter-swap",
  "files": [
    {
      "path": "registry/hyperblur/typewriter-swap.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/typewriter-swap.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TypewriterSwapProps extends React.HTMLAttributes<HTMLSpanElement> {\n  phrases: string[];\n  typeSpeed?: number;\n  deleteSpeed?: number;\n  hold?: number;\n  loop?: boolean;\n  accent?: string;\n}\n\nconst css = `\n@keyframes hb-zz4-blink {\n  0%, 49.9% { opacity: 1; }\n  50%, 100% { opacity: 0; }\n}\n.hb-zz4 {\n  display: inline;\n  font-family: inherit;\n  white-space: pre-wrap;\n  overflow-wrap: anywhere;\n}\n.hb-zz4-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  padding: 0;\n  overflow: hidden;\n  clip: rect(0, 0, 0, 0);\n  white-space: nowrap;\n  border: 0;\n}\n.hb-zz4-s { opacity: 0.62; transition: opacity 0.3s ease; }\n.hb-zz4-k { position: relative; }\n.hb-zz4-k::after {\n  content: \"\";\n  position: absolute;\n  left: 0.04em;\n  top: 0.12em;\n  bottom: 0.08em;\n  width: 0.095em;\n  border-radius: 1px;\n  background: var(--hb-zz4-accent);\n  animation: hb-zz4-blink 1.05s linear infinite;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-zz4-k::after { animation: none; }\n  .hb-zz4-s { transition: none; }\n}\n`;\n\nfunction mix(n: number) {\n  let h = Math.imul(n ^ 0x9e3779b9, 0x85ebca6b);\n  h ^= h >>> 13;\n  h = Math.imul(h, 0xc2b2ae35);\n  h ^= h >>> 16;\n  return h >>> 0;\n}\n\nfunction shared(a: string[], b: string[]) {\n  let n = 0;\n  while (n < a.length && n < b.length && a[n] === b[n]) n++;\n  return n;\n}\n\nexport function TypewriterSwap({\n  phrases,\n  typeSpeed = 62,\n  deleteSpeed = 30,\n  hold = 1700,\n  loop = true,\n  accent = \"#7fb3ff\",\n  className = \"\",\n  style,\n  ...rest\n}: TypewriterSwapProps) {\n  const ref = React.useRef<HTMLSpanElement>(null);\n  const [typed, setTyped] = React.useState(\"\");\n  const [keep, setKeep] = React.useState(0);\n  const [active, setActive] = React.useState(false);\n  const key = phrases.join(\"\\u0001\");\n\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const io = new IntersectionObserver((entries) => {\n      for (const e of entries) setActive(e.isIntersecting);\n    });\n    io.observe(el);\n    return () => io.disconnect();\n  }, []);\n\n  React.useEffect(() => {\n    if (!active) return;\n    const list = key === \"\" ? [] : key.split(\"\\u0001\").map((p) => Array.from(p));\n    if (list.length === 0) return;\n    let timer = 0;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      timer = window.setTimeout(() => {\n        setTyped(list[0].join(\"\"));\n        setKeep(0);\n      }, 0);\n      return () => window.clearTimeout(timer);\n    }\n\n    let index = 0;\n    let cur: string[] = [];\n    let target = list[0];\n    let floor = 0;\n    let deleting = false;\n    let tick = 0;\n\n    const show = () => {\n      setTyped(cur.join(\"\"));\n      setKeep(deleting ? floor : 0);\n    };\n\n    const step = () => {\n      if (!deleting) {\n        if (cur.length < target.length) {\n          cur.push(target[cur.length]);\n          show();\n          tick++;\n          const jitter = 0.7 + ((mix(tick * 31 + index) % 100) / 100) * 0.6;\n          timer = window.setTimeout(step, typeSpeed * jitter);\n          return;\n        }\n        if (!loop && index === list.length - 1) return;\n        const next = (index + 1) % list.length;\n        floor = shared(cur, list[next]);\n        if (list.length === 1) floor = 0;\n        index = next;\n        deleting = true;\n        show();\n        timer = window.setTimeout(step, hold);\n        return;\n      }\n      if (cur.length > floor) {\n        cur.pop();\n        show();\n        timer = window.setTimeout(step, deleteSpeed);\n        return;\n      }\n      deleting = false;\n      target = list[index];\n      show();\n      timer = window.setTimeout(step, typeSpeed * 1.6);\n    };\n\n    cur = [];\n    timer = window.setTimeout(step, 350);\n    return () => window.clearTimeout(timer);\n  }, [active, key, typeSpeed, deleteSpeed, hold, loop]);\n\n  const chars = Array.from(typed);\n  const stem = Math.min(keep, chars.length);\n\n  return (\n    <span\n      {...rest}\n      ref={ref}\n      className={`hb-zz4${className ? ` ${className}` : \"\"}`}\n      style={{ [\"--hb-zz4-accent\" as string]: accent, ...style }}\n    >\n      <style href=\"hb-typewriter-swap\" precedence=\"default\">{css}</style>\n      <span className=\"hb-zz4-sr\">{phrases.join(\". \")}</span>\n      <span aria-hidden=\"true\">\n        {stem > 0 ? <span className=\"hb-zz4-s\">{chars.slice(0, stem).join(\"\")}</span> : null}\n        {chars.slice(stem).join(\"\")}\n        <span className=\"hb-zz4-k\" />\n      </span>\n    </span>\n  );\n}\n"
    }
  ]
}