{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"typewriter","type":"registry:ui","title":"Typewriter","description":"Types a list of phrases one character at a time, deletes them and moves on, with a blinking caret.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/typewriter.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { useEffect, useState } from \"react\";\n\ntype TypewriterProps = {\n  phrases: string[];\n  /** Average milliseconds per typed character. */\n  typeSpeed?: number;\n  /** Milliseconds per deleted character. */\n  deleteSpeed?: number;\n  /** Milliseconds a finished phrase stays before it is deleted. */\n  pause?: number;\n  /** Start again from the first phrase after the last one. */\n  loop?: boolean;\n  /** Show a blinking caret after the text. */\n  caret?: boolean;\n  caretColor?: string;\n  className?: string;\n};\n\ntype Step = { index: number; length: number; deleting: boolean };\n\n/** Types a list of phrases one character at a time, deletes them and moves on, with a blinking caret. */\nexport function Typewriter({\n  phrases,\n  typeSpeed = 70,\n  deleteSpeed = 40,\n  pause = 1400,\n  loop = true,\n  caret = true,\n  caretColor = \"#ff784f\",\n  className = \"\",\n}: TypewriterProps) {\n  const reduce = useReducedMotion();\n  const [step, setStep] = useState<Step>({ index: 0, length: 0, deleting: false });\n  const count = phrases.length;\n  const index = count ? step.index % count : 0;\n  const chars = Array.from(phrases[index] ?? \"\");\n  const full = chars.length;\n  const isLast = index === count - 1;\n  const typed = !step.deleting && step.length >= full;\n  const finished = typed && isLast && !loop;\n\n  useEffect(() => {\n    if (reduce || count === 0 || finished) return;\n    const { length, deleting } = step;\n    let wait: number;\n    let next: Step;\n    if (!deleting && length < full) {\n      // A little jitter makes the typing feel human.\n      wait = typeSpeed * (0.6 + Math.random() * 0.8);\n      next = { index, length: length + 1, deleting: false };\n    } else if (!deleting) {\n      wait = pause;\n      next = { index, length, deleting: true };\n    } else if (length > 0) {\n      wait = deleteSpeed;\n      next = { index, length: length - 1, deleting: true };\n    } else {\n      wait = Math.max(220, typeSpeed * 3);\n      next = { index: (index + 1) % count, length: 0, deleting: false };\n    }\n    const id = setTimeout(() => setStep(next), wait);\n    return () => clearTimeout(id);\n  }, [step, index, full, count, finished, reduce, typeSpeed, deleteSpeed, pause]);\n\n  const shown = reduce ? (loop ? phrases[0] : phrases[count - 1]) ?? \"\" : chars.slice(0, step.length).join(\"\");\n  // Solid while typing or deleting, blinking while it waits.\n  const blink = !reduce && (typed || (step.deleting && step.length === 0));\n\n  return (\n    <span className={className}>\n      <span className=\"sr-only\">{phrases.join(\", \")}</span>\n      <span aria-hidden=\"true\">\n        {shown || \"​\"}\n        {caret ? (\n          <motion.span\n            className=\"ml-[0.06em] inline-block h-[0.9em] w-[0.08em] translate-y-[0.1em] rounded-[1px]\"\n            style={{ backgroundColor: caretColor }}\n            animate={blink ? { opacity: [1, 1, 0, 0] } : { opacity: 1 }}\n            transition={\n              blink ? { duration: 1, times: [0, 0.5, 0.55, 1], ease: \"linear\", repeat: Infinity } : { duration: 0 }\n            }\n          />\n        ) : null}\n      </span>\n    </span>\n  );\n}\n"}]}