{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "typewriter-text",
  "title": "Typewriter Text",
  "description": "Text that types and deletes itself with a human, uneven beat, while the space it needs stays reserved so nothing around it moves.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/typewriter-text/typewriter-text.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TypewriterTextProps extends Omit<\n  React.ComponentPropsWithoutRef<\"span\">,\n  \"children\"\n> {\n  phrases?: string[];\n  /** Milliseconds per letter. */\n  speed?: number;\n  /** Milliseconds a finished phrase stays. */\n  pause?: number;\n  /** Start again after the last phrase. */\n  loop?: boolean;\n  caret?: boolean;\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n// Kept outside the component: a new array on every render would restart the typing.\nconst PHRASES = [\"Write it once.\", \"Ship it today.\", \"Change it tomorrow.\"];\n\nexport function TypewriterText({\n  phrases = PHRASES,\n  speed = 58,\n  pause = 1300,\n  loop = true,\n  caret = true,\n  className,\n  style,\n  ...props\n}: TypewriterTextProps) {\n  const reduce = useReducedMotion();\n  const caretRef = React.useRef<HTMLSpanElement>(null);\n  const [typed, setTyped] = React.useState(\"\");\n  const longest = phrases.reduce((a, b) => (b.length > a.length ? b : a), \"\");\n\n  // Type, wait, delete, next. A slightly uneven beat reads as a person rather than a loop.\n  React.useEffect(() => {\n    if (reduce || !phrases.length) return;\n    let phrase = 0;\n    let length = 0;\n    let deleting = false;\n    let timer = 0;\n    const tick = () => {\n      const text = phrases[phrase % phrases.length];\n      if (deleting) {\n        length -= 1;\n        setTyped(text.slice(0, length));\n        if (length > 0) timer = window.setTimeout(tick, speed * 0.42);\n        else {\n          deleting = false;\n          phrase += 1;\n          timer = window.setTimeout(tick, 260);\n        }\n        return;\n      }\n      length += 1;\n      setTyped(text.slice(0, length));\n      if (length < text.length) {\n        timer = window.setTimeout(tick, speed * (0.7 + ((length * 7) % 5) / 6));\n      } else if (loop || phrase < phrases.length - 1) {\n        deleting = true;\n        timer = window.setTimeout(tick, pause);\n      }\n    };\n    timer = window.setTimeout(tick, 300);\n    return () => window.clearTimeout(timer);\n  }, [phrases, speed, pause, loop, reduce]);\n\n  React.useEffect(() => {\n    if (reduce || !caret) return;\n    const run = caretRef.current?.animate(\n      [{ opacity: 1 }, { opacity: 1 }, { opacity: 0 }, { opacity: 0 }],\n      { duration: 1000, iterations: Infinity },\n    );\n    return () => run?.cancel();\n  }, [reduce, caret]);\n\n  return (\n    <span\n      className={`inline-grid ${className ?? \"\"}`}\n      style={style}\n      aria-label={phrases.join(\" \")}\n      role=\"text\"\n      {...props}\n    >\n      {/* The longest phrase holds the space, so nothing around the text moves as it types. */}\n      <span\n        aria-hidden=\"true\"\n        className=\"invisible col-start-1 row-start-1 select-none\"\n      >\n        {longest}\n      </span>\n      <span aria-hidden=\"true\" className=\"col-start-1 row-start-1\">\n        {reduce ? phrases[0] : typed}\n        {caret && (\n          <span\n            ref={caretRef}\n            className=\"ml-[0.06em] inline-block w-[0.06em] align-[-0.1em]\"\n            style={{ height: \"1em\", background: \"currentColor\" }}\n          />\n        )}\n      </span>\n    </span>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}