{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "timer-ring",
  "type": "registry:ui",
  "title": "Timer Ring",
  "description": "A countdown drawn as a ring that empties, with the seconds left in the middle. It counts to a deadline (a timestamp), not from a start, so a page that reloads, or several players in one room, all see the same clock.",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "games",
    "game"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/games/timer-ring.tsx",
      "target": "components/ui/games/timer-ring.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/timer-ring v1.1.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/timer-ring\n// What changed since: https://ds.interlace.tools/c/timer-ring#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — TimerRing\n *\n * A countdown drawn as a ring that empties, with the seconds left in the\n * middle. It counts to a deadline (a timestamp), not from a start, so a\n * page that reloads, or several players in one room, all see the same\n * clock.\n *\n * ## Anatomy\n *\n *   TimerRing                     (div role=\"timer\" — data-slot=\"timer-ring\", data-state=running|low|done|idle)\n *     ├─ svg aria-hidden          (data-slot=\"timer-ring-track\" + data-slot=\"timer-ring-arc\")\n *     ├─ span                     (data-slot=\"timer-ring-value\": whole seconds left)\n *     └─ span sr-only aria-live   (data-slot=\"timer-ring-announce\": \"10 seconds left\", \"5 seconds left\")\n *\n * ## Time\n *\n * `deadline` is a `Date.now()`-style timestamp; pass `now` when it comes\n * from another device's clock. null shows a full, idle ring\n * (before the round starts, or once the clock is stopped). The number is\n * rounded up, so it reads 1 until the very end, and `onExpire` fires once\n * when it reaches 0. Ticks are timers aligned to the next whole second, not\n * an animation loop.\n *\n * A screen reader is not read every second: the ring announces at 10 and 5\n * seconds left (when the round is longer than that) and when time is up.\n *\n * ## Motion\n *\n * The arc empties smoothly in one linear animation from now to the deadline.\n * Under reduced motion it steps down once a second with the number instead.\n * The last `lowAt` seconds turn the arc and number to the destructive tone,\n * and the number says the same thing, so colour is never the only signal.\n *\n * ## MIN_VIEWPORT — 320\n *\n * | Rule | Concept                   | Where in this file                                   |\n * | ---- | ------------------------- | ---------------------------------------------------- |\n * | R4   | Extends native el         | `React.ComponentProps<'div'>`                        |\n * | R6   | data-slot per part        | timer-ring / -track / -arc / -value / -announce      |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`                    |\n * | R8   | Enums, not booleans       | `size`                                               |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const                 |\n * | R26  | A11y                      | role=\"timer\", labelled; sparse announcements          |\n */\n\nimport { cn } from '@/lib/utils';\nimport { useReducedMotion } from '@/hooks/use-reduced-motion';\n\nexport const MIN_VIEWPORT = 320 as const;\n\ninterface TimerRingLabels {\n  /** Names the timer. @default 'Time left' */\n  name?: string;\n  /** Spoken at the warning points. @default (s) => `${s} seconds left` */\n  remaining?: (seconds: number) => string;\n  /** Spoken at 0. @default \"Time's up\" */\n  done?: string;\n}\n\ntype TimerRingProps = Omit<React.ComponentProps<'div'>, 'children'> & {\n  /** The whole round, in seconds: what a full ring means. */\n  seconds: number;\n  /** When time runs out, as a timestamp in ms. null: idle, full ring. */\n  deadline: number | null;\n  /** Called once when the clock reaches 0. */\n  onExpire?: () => void;\n  /** Seconds left at which the ring turns to the warning tone. @default 5 */\n  lowAt?: number;\n  /** @default 'default' */\n  size?: 'sm' | 'default' | 'lg';\n  labels?: TimerRingLabels;\n  /** Your clock, corrected to the one that set `deadline` (e.g. `useRoom`'s `now`). @default Date.now */\n  now?: () => number;\n};\n\nconst SIZE = { sm: 'size-10 text-sm', default: 'size-14 text-lg', lg: 'size-20 text-2xl' } as const;\nconst R = 20;\nconst C = 2 * Math.PI * R;\n\nfunction TimerRing({\n  seconds,\n  deadline,\n  onExpire,\n  lowAt = 5,\n  size = 'default',\n  labels,\n  now: clock = Date.now,\n  className,\n  ...props\n}: TimerRingProps) {\n  const reduced = useReducedMotion();\n  const [now, setNow] = React.useState<number | null>(null);\n  const [announce, setAnnounce] = React.useState('');\n  const arcRef = React.useRef<SVGCircleElement>(null);\n  const expired = React.useRef(false);\n  const said = React.useRef(new Set<number>());\n  const onExpireRef = React.useRef(onExpire);\n  onExpireRef.current = onExpire;\n  const clockRef = React.useRef(clock);\n  clockRef.current = clock;\n\n  const total = Math.max(1, seconds) * 1000;\n  const leftMs = deadline === null || now === null ? total : Math.max(0, deadline - now);\n  const left = Math.ceil(leftMs / 1000);\n  const state = deadline === null ? 'idle' : leftMs <= 0 ? 'done' : left <= lowAt ? 'low' : 'running';\n  const words = {\n    name: labels?.name ?? 'Time left',\n    remaining: labels?.remaining ?? ((s: number) => `${s} seconds left`),\n    done: labels?.done ?? \"Time's up\",\n  };\n\n  // Tick on whole seconds until the deadline.\n  React.useEffect(() => {\n    expired.current = false;\n    said.current = new Set();\n    setAnnounce('');\n    if (deadline === null) {\n      setNow(null);\n      return;\n    }\n    let timer: ReturnType<typeof setTimeout>;\n    const tick = () => {\n      const t = clockRef.current();\n      setNow(t);\n      const ms = deadline - t;\n      if (ms <= 0) return;\n      timer = setTimeout(tick, Math.min(ms, (ms % 1000 || 1000) + 5));\n    };\n    tick();\n    return () => clearTimeout(timer);\n  }, [deadline]);\n\n  // Announce sparingly, and expire once.\n  React.useEffect(() => {\n    if (deadline === null || now === null) return;\n    if (leftMs <= 0) {\n      if (!expired.current) {\n        expired.current = true;\n        setAnnounce(words.done);\n        onExpireRef.current?.();\n      }\n      return;\n    }\n    for (const mark of [10, 5]) {\n      if (left === mark && seconds > mark && !said.current.has(mark)) {\n        said.current.add(mark);\n        setAnnounce(words.remaining(mark));\n      }\n    }\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [now, deadline]);\n\n  // One smooth sweep from now to the deadline.\n  React.useEffect(() => {\n    const arc = arcRef.current;\n    if (!arc || deadline === null || reduced || typeof arc.animate !== 'function') return;\n    const ms = Math.max(0, deadline - clockRef.current());\n    const from = C * (1 - ms / total);\n    const anim = arc.animate([{ strokeDashoffset: `${from}` }, { strokeDashoffset: `${C}` }], {\n      duration: ms,\n      easing: 'linear',\n      fill: 'forwards',\n    });\n    return () => anim.cancel();\n  }, [deadline, reduced, total]);\n\n  const offset = C * (1 - leftMs / total);\n\n  return (\n    <div\n      role=\"timer\"\n      aria-label={words.name}\n      data-slot=\"timer-ring\"\n      data-state={state}\n      data-min-viewport={String(MIN_VIEWPORT)}\n      className={cn(\n        'relative inline-grid shrink-0 place-items-center font-semibold tabular-nums',\n        SIZE[size],\n        state === 'low' || state === 'done' ? 'text-destructive' : 'text-foreground',\n        className,\n      )}\n      {...props}\n    >\n      <svg aria-hidden viewBox=\"0 0 48 48\" className=\"absolute inset-0 size-full -rotate-90\">\n        <circle data-slot=\"timer-ring-track\" cx=\"24\" cy=\"24\" r={R} fill=\"none\" strokeWidth=\"4\" className=\"stroke-muted\" />\n        <circle\n          ref={arcRef}\n          data-slot=\"timer-ring-arc\"\n          cx=\"24\"\n          cy=\"24\"\n          r={R}\n          fill=\"none\"\n          strokeWidth=\"4\"\n          strokeLinecap=\"round\"\n          strokeDasharray={C}\n          strokeDashoffset={offset}\n          className={cn(\n            'motion-safe:transition-[stroke] motion-safe:duration-(--duration-base)',\n            state === 'low' || state === 'done' ? 'stroke-destructive' : 'stroke-primary',\n          )}\n        />\n      </svg>\n      <span data-slot=\"timer-ring-value\" className=\"relative leading-none\">\n        {left}\n      </span>\n      <span data-slot=\"timer-ring-announce\" aria-live=\"polite\" className=\"sr-only\">\n        {announce}\n      </span>\n    </div>\n  );\n}\nTimerRing.displayName = 'TimerRing';\n\nexport { TimerRing };\nexport type { TimerRingProps, TimerRingLabels };\n"
    }
  ],
  "meta": {
    "tier": "game",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.1.0",
    "since": "2.0.0"
  },
  "docs": "## @interlace/timer-ring\n\nInstalled to `components/ui/games/timer-ring.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/games/timer-ring';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/timer-ring\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
