{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "typewriter-machine",
  "title": "Typewriter Machine",
  "author": "VenusUI",
  "description": "A typewriter whose carriage slides as you type, with a bell near the end of each line.",
  "files": [
    {
      "path": "registry/default/ui/typewriter-machine.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface TypewriterMachineProps extends Omit<React.ComponentProps<\"div\">, \"onChange\"> {\n  defaultValue?: string\n  /** Characters per line before the bell rings. */\n  columns?: number\n  onValueChange?: (text: string) => void\n}\n\nconst ROWS = [\"QWERTYUIOP\", \"ASDFGHJKL\", \"ZXCVBNM\"]\nconst CH = 9.6\n\n/**\n * A typewriter. Type on your keyboard or press the round keys: the carriage\n * slides along under the strike point, the bell rings near the end of a line\n * and Return feeds a new one. The paper is a real text field underneath.\n */\nfunction TypewriterMachine({ defaultValue = \"Dear reader,\\n\", columns = 30, onValueChange, className, ...props }: TypewriterMachineProps) {\n  const [text, setText] = React.useState(defaultValue)\n  const [struck, setStruck] = React.useState<{ key: string; at: number } | null>(null)\n  const field = React.useRef<HTMLTextAreaElement>(null)\n  const lines = text.split(\"\\n\")\n  const column = lines.at(-1)?.length ?? 0\n  const bell = column >= columns - 3\n\n  const update = (next: string) => {\n    // Wrap long lines like a carriage return would.\n    const wrapped = next\n      .split(\"\\n\")\n      .flatMap((line) => (line.length > columns ? (line.match(new RegExp(`.{1,${columns}}`, \"g\")) ?? [line]) : [line]))\n      .join(\"\\n\")\n    setText(wrapped)\n    onValueChange?.(wrapped)\n  }\n\n  const press = (key: string) => {\n    setStruck((last) => ({ key, at: (last?.at ?? 0) + 1 }))\n    if (key === \"RETURN\") update(`${text}\\n`)\n    else if (key === \"SPACE\") update(`${text} `)\n    else if (key === \"DELETE\") update(text.slice(0, -1))\n    else update(text + key.toLowerCase())\n    field.current?.focus()\n  }\n\n  return (\n    <div data-slot=\"typewriter-machine\" className={cn(\"w-full max-w-md\", className)} {...props}>\n      {/* Paper and carriage */}\n      <div className=\"relative mx-auto h-44 cursor-text overflow-hidden\" onClick={() => field.current?.focus()}>\n        <div\n          className=\"absolute bottom-0 left-1/2 w-[19rem] transition-transform duration-100 ease-out motion-reduce:transition-none\"\n          style={{ transform: `translateX(${-column * CH}px)` }}\n        >\n          <div className=\"min-h-44 rounded-t-sm bg-[#fdfcf7] px-3 pt-3 pb-6 font-mono text-[16px] leading-6 whitespace-pre text-neutral-900 shadow-[0_-4px_14px_-6px_rgb(0_0_0/0.25)] ring-1 ring-black/5\">\n            {lines.slice(-6).map((line, i, shown) => (\n              <div key={`${lines.length - shown.length + i}`}>\n                {line}\n                {i === shown.length - 1 ? <span aria-hidden=\"true\" className=\"inline-block h-5 w-[2px] translate-y-1 animate-pulse bg-neutral-900\" /> : null}\n              </div>\n            ))}\n          </div>\n        </div>\n        <span aria-hidden=\"true\" className=\"absolute bottom-0 left-1/2 h-3 w-px -translate-x-1/2 bg-red-500\" />\n      </div>\n      <textarea\n        ref={field}\n        aria-label=\"Typewriter paper\"\n        value={text}\n        onChange={(event) => update(event.target.value)}\n        // A typewriter only types at the end of the page.\n        onSelect={(event) => {\n          const end = event.currentTarget.value.length\n          if (event.currentTarget.selectionStart !== end) event.currentTarget.setSelectionRange(end, end)\n        }}\n        onKeyDown={(event) => {\n          const key = event.key.length === 1 ? event.key.toUpperCase() : event.key === \"Enter\" ? \"RETURN\" : event.key === \" \" ? \"SPACE\" : \"\"\n          setStruck((last) => ({ key, at: (last?.at ?? 0) + 1 }))\n        }}\n        className=\"sr-only\"\n      />\n\n      {/* Body and keys */}\n      <div className=\"relative -mt-1 rounded-b-3xl rounded-t-lg bg-linear-to-b from-neutral-800 to-neutral-950 px-4 pt-4 pb-5 shadow-[0_18px_30px_-16px_rgb(0_0_0/0.7)]\">\n        <div className=\"mb-3 flex items-center justify-between px-1\">\n          <span className=\"font-serif text-xs tracking-[0.3em] text-neutral-400 uppercase\">Correspondent</span>\n          <span aria-live=\"polite\" className={cn(\"font-mono text-[10px] tabular-nums transition-colors\", bell ? \"text-amber-300\" : \"text-neutral-500\")}>\n            {bell ? \"ding\" : `${column}/${columns}`}\n          </span>\n        </div>\n        <div className=\"space-y-1.5\">\n          {ROWS.map((row, r) => (\n            <div key={row} className=\"flex justify-center gap-1.5\" style={{ paddingLeft: r * 10 }}>\n              {[...row].map((key) => (\n                <Key key={key} label={key} struck={struck?.key === key ? struck.at : 0} onPress={() => press(key)} />\n              ))}\n              {r === 2 ? <Key label=\"DELETE\" wide struck={0} onPress={() => press(\"DELETE\")} /> : null}\n            </div>\n          ))}\n          <div className=\"flex justify-center gap-1.5 pt-1\">\n            <Key label=\"SPACE\" wide={3} struck={struck?.key === \"SPACE\" ? struck.at : 0} onPress={() => press(\"SPACE\")} />\n            <Key label=\"RETURN\" wide struck={struck?.key === \"RETURN\" ? struck.at : 0} onPress={() => press(\"RETURN\")} />\n          </div>\n        </div>\n      </div>\n    </div>\n  )\n}\n\nfunction Key({ label, wide, struck, onPress }: { label: string; wide?: boolean | number; struck: number; onPress: () => void }) {\n  return (\n    <button\n      key={struck}\n      type=\"button\"\n      tabIndex={-1}\n      onClick={onPress}\n      aria-label={label === \"SPACE\" ? \"Space\" : label === \"RETURN\" ? \"Return\" : label === \"DELETE\" ? \"Delete\" : label}\n      className={cn(\n        \"grid h-7 place-items-center rounded-full bg-linear-to-b from-neutral-100 to-neutral-300 font-mono text-[10px] font-semibold text-neutral-800 shadow-[0_3px_0_#404040,inset_0_0_0_2px_#a3a3a3] outline-none transition-transform active:translate-y-[2px] active:shadow-[0_1px_0_#404040,inset_0_0_0_2px_#a3a3a3] motion-reduce:transition-none\",\n        wide ? (wide === 3 ? \"w-40 rounded-md\" : \"w-14 rounded-md text-[8px]\") : \"w-7\",\n        struck > 0 && \"animate-typewriter-machine-strike motion-reduce:animate-none\"\n      )}\n    >\n      {label === \"SPACE\" ? \"\" : label}\n    </button>\n  )\n}\n\nexport { TypewriterMachine }\n",
      "type": "registry:ui"
    }
  ],
  "cssVars": {
    "theme": {
      "animate-typewriter-machine-strike": "typewriter-machine-strike 0.12s ease-out"
    }
  },
  "css": {
    "@keyframes typewriter-machine-strike": {
      "0%": {
        "transform": "translateY(2px) scale(.96)"
      },
      "100%": {
        "transform": "translateY(0) scale(1)"
      }
    }
  },
  "meta": {
    "version": "0.1.0"
  },
  "categories": [
    "inputs",
    "interactive"
  ],
  "type": "registry:ui"
}