{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "typewriter",
  "title": "Typewriter",
  "description": "Types and deletes a list of phrases with a human rhythm and a caret that only blinks while idle; works as an animated input placeholder.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json"
  ],
  "files": [
    {
      "path": "registry/components/text/typewriter.tsx",
      "content": "\"use client\"\r\n\r\nimport * as React from \"react\"\r\nimport { useReducedMotion } from \"motion/react\"\r\nimport { cn } from \"@/lib/utils\"\r\nimport { GooduiTokens } from \"@/lib/goodui-motion\"\r\n\r\nconst CSS = `\r\n@keyframes pc-typewriter-blink { 0%, 45% { opacity: 1 } 50%, 95% { opacity: 0 } 100% { opacity: 1 } }\r\n.pc-typewriter-caret { animation: pc-typewriter-blink 1.05s step-end infinite }\r\n.pc-typewriter-caret[data-typing] { animation: none; opacity: 1 }\r\n@media (prefers-reduced-motion: reduce) { .pc-typewriter-caret { animation: none } }\r\n`\r\n\r\nexport type TypewriterProps = Omit<React.HTMLAttributes<HTMLSpanElement>, \"children\"> & {\r\n  /** One phrase or a list of phrases typed one after another. */\r\n  phrases: string | string[]\r\n  /** Milliseconds per typed character (lightly randomised so it feels human). */\r\n  typeSpeed?: number\r\n  /** Milliseconds per deleted character. */\r\n  deleteSpeed?: number\r\n  /** Milliseconds a finished phrase stays on screen. */\r\n  pause?: number\r\n  /** Milliseconds before the first character. */\r\n  startDelay?: number\r\n  /** Start again from the first phrase after the last one. */\r\n  loop?: boolean\r\n  /** Show the blinking caret. */\r\n  caret?: boolean\r\n  /** bar is a thin line; block is a terminal cell. */\r\n  caretStyle?: \"bar\" | \"block\"\r\n  /** Shown before the typed text and never deleted, e.g. \"$\" in a terminal. */\r\n  prompt?: React.ReactNode\r\n  /** Classes for the caret, e.g. a color or width. */\r\n  caretClassName?: string\r\n  /** Freeze typing where it is, for example while an input is focused. */\r\n  paused?: boolean\r\n  /** Called with the phrase index each time a phrase is fully typed. */\r\n  onPhraseComplete?: (index: number) => void\r\n}\r\n\r\ntype State = { index: number; length: number; deleting: boolean; started: boolean }\r\n\r\n/** Types and deletes phrases with a caret, best for terminals and command demos; for AI answers use streaming-text. Screen readers get every phrase as plain text. */\r\nexport function Typewriter({\r\n  phrases,\r\n  typeSpeed = 55,\r\n  deleteSpeed = 28,\r\n  pause = 1600,\r\n  startDelay = 300,\r\n  loop = true,\r\n  caret = true,\r\n  caretStyle = \"bar\",\r\n  prompt,\r\n  caretClassName,\r\n  paused = false,\r\n  onPhraseComplete,\r\n  className,\r\n  ...props\r\n}: TypewriterProps) {\r\n  const list = React.useMemo(() => (Array.isArray(phrases) ? phrases : [phrases]), [phrases])\r\n  const reduce = useReducedMotion() ?? false\r\n  const [state, setState] = React.useState<State>({ index: 0, length: 0, deleting: false, started: false })\r\n  const completeRef = React.useRef(onPhraseComplete)\r\n  completeRef.current = onPhraseComplete\r\n\r\n  const chars = React.useMemo(() => Array.from(list[state.index] ?? \"\"), [list, state.index])\r\n  const last = state.index === list.length - 1\r\n\r\n  React.useEffect(() => {\r\n    if (paused || list.length === 0) return\r\n    let t: ReturnType<typeof setTimeout>\r\n\r\n    // Reduced motion: show whole phrases and swap them without typing.\r\n    if (reduce) {\r\n      if (state.length !== chars.length) {\r\n        setState((s) => ({ ...s, length: chars.length, started: true }))\r\n        return\r\n      }\r\n      if (list.length > 1 && (loop || !last)) {\r\n        t = setTimeout(() => setState((s) => ({ ...s, index: (s.index + 1) % list.length, length: Array.from(list[(s.index + 1) % list.length]!).length })), pause * 1.5)\r\n      }\r\n      return () => clearTimeout(t)\r\n    }\r\n\r\n    if (!state.deleting) {\r\n      if (state.length < chars.length) {\r\n        const wait = !state.started ? startDelay : typeSpeed * (0.6 + Math.random() * 0.8)\r\n        t = setTimeout(() => setState((s) => ({ ...s, length: s.length + 1, started: true })), wait)\r\n      } else {\r\n        completeRef.current?.(state.index)\r\n        if (last && !loop) return\r\n        if (list.length === 1 && !loop) return\r\n        t = setTimeout(() => setState((s) => ({ ...s, deleting: true })), pause)\r\n      }\r\n    } else if (state.length > 0) {\r\n      t = setTimeout(() => setState((s) => ({ ...s, length: s.length - 1 })), deleteSpeed)\r\n    } else {\r\n      t = setTimeout(() => setState((s) => ({ ...s, deleting: false, index: (s.index + 1) % list.length })), typeSpeed * 3)\r\n    }\r\n    return () => clearTimeout(t)\r\n  }, [state, chars.length, list, last, loop, pause, paused, reduce, startDelay, typeSpeed, deleteSpeed])\r\n\r\n  const typing = state.deleting || (state.length > 0 && state.length < chars.length)\r\n\r\n  return (\r\n    <span className={cn(\"whitespace-pre-wrap\", className)} {...props}>\r\n      <GooduiTokens />\r\n      <style href=\"pc-typewriter\" precedence=\"default\">\r\n        {CSS}\r\n      </style>\r\n      <span className=\"sr-only\">{list.join(\" \")}</span>\r\n      {prompt != null && <span aria-hidden=\"true\" className=\"me-[1ch] select-none opacity-60\">{prompt}</span>}\r\n      <span aria-hidden=\"true\">{chars.slice(0, state.length).join(\"\")}</span>\r\n      {caret && (\r\n        <span\r\n          aria-hidden=\"true\"\r\n          data-typing={typing ? \"\" : undefined}\r\n          className={cn(\"pc-typewriter-caret ms-[0.06em] inline-block h-[1.05em] translate-y-[0.15em] bg-current align-baseline\", caretStyle === \"bar\" ? \"w-[2px] rounded-full\" : \"w-[1ch] rounded-[1px] opacity-80\", caretClassName)}\r\n        />\r\n      )}\r\n    </span>\r\n  )\r\n}\r\n",
      "type": "registry:component",
      "target": "components/goodui/typewriter.tsx"
    }
  ],
  "categories": [
    "components",
    "text"
  ],
  "type": "registry:component"
}