{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "timer",
  "title": "TM-1 Timer",
  "author": "Shu Ding <https://x.com/shuding>",
  "description": "A countdown timer on an LCD, with a mode switch and start and reset keys.",
  "registryDependencies": [
    "https://spec-01.vercel.app/r/hardware.json",
    "https://spec-01.vercel.app/r/device.json",
    "https://spec-01.vercel.app/r/button.json",
    "https://spec-01.vercel.app/r/checkbox.json",
    "https://spec-01.vercel.app/r/icon.json",
    "https://spec-01.vercel.app/r/lcd.json",
    "https://spec-01.vercel.app/r/slide-switch.json"
  ],
  "files": [
    {
      "path": "registry/hardware/blocks/timer/timer.tsx",
      "content": "import * as React from \"react\"\nimport { Button } from \"@/registry/hardware/ui/button\"\nimport { Checkbox } from \"@/registry/hardware/ui/checkbox\"\nimport { Icon } from \"@/registry/hardware/ui/icon\"\nimport { SlideSwitch } from \"@/registry/hardware/ui/slide-switch\"\nimport { haptic } from \"@/registry/hardware/lib/haptics\"\nimport { Device } from \"@/registry/hardware/ui/device\"\nimport { Lcd, LcdDigits } from \"@/registry/hardware/ui/lcd\"\n\nconst full = 60 * 60\n\n// A kitchen timer. Drag round the dial to set up to an hour; a wedge in the\n// key color shows the time left and shrinks as it runs. At zero it beeps and\n// its LED flashes until you reset it.\nfunction Timer() {\n  const [set, setSet] = React.useState(15 * 60)\n  const [left, setLeft] = React.useState(15 * 60)\n  const [running, setRunning] = React.useState(false)\n  const [chime, setChime] = React.useState(true)\n  const done = left <= 0 && set > 0\n  const dial = React.useRef<HTMLDivElement>(null)\n  const audio = React.useRef<AudioContext | null>(null)\n\n  React.useEffect(() => {\n    if (!running) return\n    let last = performance.now()\n    let id = requestAnimationFrame(function tick(now) {\n      setLeft((t) => Math.max(0, t - (now - last) / 1000))\n      last = now\n      id = requestAnimationFrame(tick)\n    })\n    return () => cancelAnimationFrame(id)\n  }, [running])\n\n  React.useEffect(() => {\n    if (!done || !running) return\n    setRunning(false)\n    haptic(\"success\")\n    if (!chime || !audio.current) return\n    const ctx = audio.current\n    for (let i = 0; i < 3; i++) {\n      const osc = ctx.createOscillator()\n      const gain = ctx.createGain()\n      osc.frequency.value = 1320\n      gain.gain.setValueAtTime(0, ctx.currentTime + i * 0.3)\n      gain.gain.linearRampToValueAtTime(0.2, ctx.currentTime + i * 0.3 + 0.01)\n      gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + i * 0.3 + 0.22)\n      osc.connect(gain).connect(ctx.destination)\n      osc.start(ctx.currentTime + i * 0.3)\n      osc.stop(ctx.currentTime + i * 0.3 + 0.25)\n    }\n  }, [done, running, chime])\n\n  const setFrom = (event: React.PointerEvent) => {\n    const box = dial.current!.getBoundingClientRect()\n    const angle = Math.atan2(\n      event.clientX - box.left - box.width / 2,\n      -(event.clientY - box.top - box.height / 2),\n    )\n    const minutes = Math.round((((angle * 180) / Math.PI + 360) % 360) / 6)\n    const seconds = Math.max(60, minutes * 60)\n    // A tick at each minute the dial is turned through.\n    if (seconds !== set) haptic(\"selection\")\n    setSet(seconds)\n    setLeft(seconds)\n    setRunning(false)\n  }\n\n  const minutes = Math.floor(left / 60)\n  const seconds = Math.floor(left % 60)\n  const share = left / full\n\n  return (\n    <Device model=\"TM-1\" name=\"Timer\" on={running || done}>\n      <div data-timer>\n        <div\n          ref={dial}\n          data-timer-dial\n          data-done={done || undefined}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label=\"Time\"\n          aria-valuemin={1}\n          aria-valuemax={60}\n          aria-valuenow={Math.ceil(left / 60)}\n          aria-valuetext={`${minutes} minutes ${seconds} seconds`}\n          style={{ \"--share\": share } as React.CSSProperties}\n          onPointerDown={(event) => {\n            event.currentTarget.setPointerCapture(event.pointerId)\n            setFrom(event)\n          }}\n          onPointerMove={(event) => event.buttons && setFrom(event)}\n          onKeyDown={(event) => {\n            const by = { ArrowUp: 1, ArrowRight: 1, ArrowDown: -1, ArrowLeft: -1 }[event.key]\n            if (!by) return\n            event.preventDefault()\n            const next = Math.min(full, Math.max(60, (Math.round(left / 60) + by) * 60))\n            if (next !== set) haptic(\"selection\")\n            setSet(next)\n            setLeft(next)\n          }}\n        >\n          <span data-timer-wedge aria-hidden />\n          <svg data-timer-scale viewBox=\"-100 -100 200 200\" aria-hidden>\n            {Array.from({ length: 60 }, (_, i) => {\n              const a = (i * 6 * Math.PI) / 180\n              const inner = i % 5 === 0 ? 85 : 89\n              return (\n                <line\n                  key={i}\n                  x1={Math.sin(a) * inner}\n                  y1={-Math.cos(a) * inner}\n                  x2={Math.sin(a) * 95}\n                  y2={-Math.cos(a) * 95}\n                  data-major={i % 5 === 0 || undefined}\n                />\n              )\n            })}\n          </svg>\n          {[0, 10, 20, 30, 40, 50].map((m) => (\n            <span key={m} data-timer-number style={{ \"--m\": m } as React.CSSProperties} aria-hidden>\n              {m}\n            </span>\n          ))}\n          <span data-timer-hub aria-hidden />\n        </div>\n        <div data-timer-middle>\n          <div data-timer-lcd-row>\n            <Lcd data-timer-lcd size=\"lg\" tag={done ? \"Done\" : running ? \"Running\" : \"Set\"} aria-live=\"off\">\n              <LcdDigits\n                data-done={done || undefined}\n                value={`${String(minutes).padStart(2, \"0\")}:${String(seconds).padStart(2, \"0\")}`}\n              />\n            </Lcd>\n            <div data-timer-start>\n              <Button\n                tone=\"primary\"\n                round\n                aria-label={running ? \"Pause\" : \"Start\"}\n                onClick={() => {\n                  audio.current ??= new AudioContext()\n                  void audio.current.resume()\n                  if (done) setLeft(set)\n                  setRunning(!running)\n                }}\n              >\n                <Icon name={running ? \"pause\" : \"play\"} />\n              </Button>\n            </div>\n          </div>\n          <div data-timer-row>\n            <SlideSwitch\n              label=\"Preset\"\n              options={[\"1\", \"5\", \"15\", \"25\", \"45\"]}\n              defaultValue=\"15\"\n              onValueChange={(value) => {\n                const next = Number(value) * 60\n                setSet(next)\n                setLeft(next)\n                setRunning(false)\n              }}\n            />\n            <div data-timer-small>\n              <Button\n                round\n                aria-label=\"Reset\"\n                onClick={() => {\n                  setRunning(false)\n                  setLeft(set)\n                }}\n              >\n                <Icon name=\"restart\" />\n              </Button>\n              <Checkbox label=\"Chime\" icon=\"bell\" defaultChecked onCheckedChange={setChime} />\n            </div>\n          </div>\n        </div>\n      </div>\n    </Device>\n  )\n}\n\nexport { Timer }\n",
      "type": "registry:component",
      "target": "@components/blocks/timer/timer.tsx"
    }
  ],
  "categories": [
    "device"
  ],
  "type": "registry:block"
}