{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "micro-comet-dial",
  "type": "registry:component",
  "title": "Comet Dial",
  "description": "A rotary dial you flick. It spins with inertia, clicks into detents while the readout snaps to each one, and a comet tail follows the pointer around the rim.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "dial",
    "knob",
    "rotary",
    "inertia",
    "detent",
    "control"
  ],
  "docs": "https://hyperblur.dev/effects/micro-comet-dial",
  "files": [
    {
      "path": "registry/hyperblur/micro-comet-dial.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/micro-comet-dial.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface MicroCometDialProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  value?: number;\n  defaultValue?: number;\n  detents?: number;\n  labels?: string[];\n  size?: number;\n  accent?: string;\n  label?: string;\n  idle?: boolean;\n  onChange?: (value: number) => void;\n}\n\nconst TAIL = 20;\nconst RC = 90;\n\nconst css = `\n.hb-qla {\n  position: relative;\n  display: block;\n  box-sizing: border-box;\n  font-family: inherit;\n  cursor: grab;\n  touch-action: none;\n  user-select: none;\n  -webkit-user-select: none;\n  -webkit-tap-highlight-color: transparent;\n  border-radius: 50%;\n  --hb-qla-c: #2f7bff;\n}\n.hb-qla *, .hb-qla *::before, .hb-qla *::after { box-sizing: border-box; }\n.hb-qla:active { cursor: grabbing; }\n.hb-qla:focus-visible { outline: 2px solid var(--hb-qla-c); outline-offset: 4px; }\n.hb-qla-v { display: block; width: 100%; height: 100%; overflow: visible; }\n.hb-qla-o { fill: none; stroke: #1f1f1f; stroke-width: 2; }\n.hb-qla-t { stroke: #3a3a3a; stroke-width: 2; stroke-linecap: round; transition: stroke 0.2s ease; }\n.hb-qla-t[data-on=\"true\"] { stroke: var(--hb-qla-c); stroke-width: 3.2; }\n.hb-qla-k { stroke: #363636; stroke-width: 1.5; }\n.hb-qla-n { stroke: var(--hb-qla-c); stroke-width: 4; stroke-linecap: round; }\n.hb-qla-d { fill: none; stroke: #262626; stroke-width: 3; stroke-dasharray: 1.5 5.2; }\n.hb-qla-s { fill: none; stroke: var(--hb-qla-c); stroke-linecap: butt; }\n.hb-qla-e { fill: color-mix(in srgb, var(--hb-qla-c) 40%, #fff); }\n.hb-qla-r {\n  fill: none;\n  stroke: var(--hb-qla-c);\n  stroke-width: 2;\n  transform-box: fill-box;\n  transform-origin: 50% 50%;\n  opacity: 0;\n  animation: hb-qla-click 0.4s ease-out both;\n}\n.hb-qla-x {\n  font-family: inherit;\n  font-size: 34px;\n  font-weight: 600;\n  fill: #ededed;\n  font-variant-numeric: tabular-nums;\n  pointer-events: none;\n}\n@keyframes hb-qla-click {\n  0% { opacity: 0.7; transform: scale(1); }\n  100% { opacity: 0; transform: scale(1.12); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-qla-t { transition: none; }\n  .hb-qla-r { animation: none; }\n}\n`;\n\nfunction wrap(a: number) {\n  return ((((a + 180) % 360) + 360) % 360) - 180;\n}\n\nexport function MicroCometDial({\n  value,\n  defaultValue = 0,\n  detents = 12,\n  labels,\n  size = 152,\n  accent = \"#2f7bff\",\n  label = \"Dial\",\n  idle = true,\n  onChange,\n  className = \"\",\n  style,\n  onPointerEnter,\n  onPointerLeave,\n  onFocus,\n  onBlur,\n  ...rest\n}: MicroCometDialProps) {\n  const n = Math.max(2, Math.round(detents));\n  const stepDeg = 360 / n;\n  const [inner, setInner] = React.useState(defaultValue);\n  const val = (((value ?? inner) % n) + n) % n;\n  const [shown, setShown] = React.useState(val);\n  const [flash, setFlash] = React.useState(0);\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const knobRef = React.useRef<SVGGElement>(null);\n  const headRef = React.useRef<SVGCircleElement>(null);\n  const segs = React.useRef<(SVGPathElement | null)[]>([]);\n  const state = React.useRef({\n    th: val * (360 / n),\n    w: 0,\n    ho: 0,\n    pa: 0,\n    lastT: 0,\n    drag: false,\n    user: false,\n    rest: null as number | null,\n    hover: false,\n    focus: false,\n    resumeAt: 0,\n    nextFlick: -1,\n    dir: 1,\n    ghosted: false,\n    settledSince: -1,\n    hist: [] as number[],\n    shown: val,\n  });\n  const valRef = React.useRef(val);\n  const api = React.useRef({ wake: () => {}, sync: () => {}, reduced: false });\n  const latest = React.useRef({ n, step: stepDeg, idle, value, onChange });\n\n  React.useEffect(() => {\n    const s = state.current;\n    valRef.current = val;\n    latest.current = { n, step: stepDeg, idle, value, onChange };\n    const idx = ((Math.round(s.th / stepDeg) % n) + n) % n;\n    if (!s.drag && !s.user && idx !== val) {\n      const base = val * stepDeg;\n      s.rest = base + 360 * Math.round((s.th - base) / 360);\n    }\n    api.current.wake();\n  }, [val, n, stepDeg, idle, value, onChange]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const knob = knobRef.current;\n    const head = headRef.current;\n    if (!root || !knob || !head) return;\n    const s = state.current;\n    const mq = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    api.current.reduced = mq.matches;\n    let raf = 0;\n    let last = 0;\n    let visible = true;\n\n    const pt = (a: number) => {\n      const r = (a * Math.PI) / 180;\n      return { x: 100 + RC * Math.sin(r), y: 100 - RC * Math.cos(r) };\n    };\n\n    const sync = () => {\n      const c = latest.current;\n      const idx = ((Math.round(s.th / c.step) % c.n) + c.n) % c.n;\n      if (idx !== s.shown) {\n        s.shown = idx;\n        setShown(idx);\n        setFlash((f) => f + 1);\n      }\n      if (s.user && idx !== valRef.current) {\n        valRef.current = idx;\n        if (c.value === undefined) setInner(idx);\n        c.onChange?.(idx);\n      }\n      const headAngle = s.th + s.ho;\n      s.hist.unshift(headAngle);\n      if (s.hist.length > TAIL) s.hist.length = TAIL;\n      knob.setAttribute(\"transform\", `rotate(${s.th.toFixed(2)} 100 100)`);\n      const hp = pt(headAngle);\n      head.setAttribute(\"cx\", hp.x.toFixed(2));\n      head.setAttribute(\"cy\", hp.y.toFixed(2));\n      for (let i = 0; i < TAIL - 1; i++) {\n        const seg = segs.current[i];\n        if (!seg) continue;\n        const a = s.hist[i] ?? headAngle;\n        const b = s.hist[i + 1] ?? a;\n        const p = pt(a);\n        const q = pt(b);\n        const k = 1 - i / (TAIL - 1);\n        seg.setAttribute(\"d\", `M ${p.x.toFixed(2)} ${p.y.toFixed(2)} L ${q.x.toFixed(2)} ${q.y.toFixed(2)}`);\n        seg.setAttribute(\"stroke-width\", (0.6 + 4.6 * k).toFixed(2));\n        seg.setAttribute(\"stroke-opacity\", (0.9 * k * k).toFixed(3));\n      }\n    };\n\n    const tick = (now: number) => {\n      raf = 0;\n      if (!visible) return;\n      const dt = Math.min(0.05, Math.max(0.001, (now - last) / 1000));\n      last = now;\n      const c = latest.current;\n      const idleOn =\n        c.idle && !api.current.reduced && !s.hover && !s.focus && !s.drag && s.rest === null && now >= s.resumeAt;\n      if (idleOn) {\n        if (s.nextFlick < 0) s.nextFlick = now + 1000;\n        if (now >= s.nextFlick) {\n          s.user = false;\n          s.w = s.dir * 520;\n          s.dir = -s.dir;\n          s.nextFlick = now + 4600;\n          s.ghosted = true;\n          s.settledSince = -1;\n        }\n        if (s.ghosted && Math.abs(s.w) < 3) {\n          if (s.settledSince < 0) s.settledSince = now;\n          else if (now - s.settledSince > 900) {\n            const idx = ((Math.round(s.th / c.step) % c.n) + c.n) % c.n;\n            const base = valRef.current * c.step;\n            if (idx !== valRef.current) s.rest = base + 360 * Math.round((s.th - base) / 360);\n            s.ghosted = false;\n          }\n        }\n      } else {\n        s.nextFlick = -1;\n      }\n      let rem = dt;\n      while (rem > 0) {\n        const h = Math.min(rem, 1 / 120);\n        if (!s.drag) {\n          const phase = (s.th / c.step) * 2 * Math.PI;\n          let acc = -150 * (c.step / (2 * Math.PI)) * Math.sin(phase);\n          let damp = 1.4 + 8 * (1 - Math.min(1, Math.abs(s.w) / 160));\n          if (s.rest !== null) {\n            acc += 120 * (s.rest - s.th);\n            damp = 16;\n          }\n          s.w += (acc - damp * s.w) * h;\n          s.th += s.w * h;\n        }\n        rem -= h;\n      }\n      if (s.rest !== null && Math.abs(s.rest - s.th) < 0.15 && Math.abs(s.w) < 2) {\n        s.th = s.rest;\n        s.w = 0;\n        s.rest = null;\n      }\n      s.ho = s.drag ? wrap(s.pa - s.th) : s.ho * Math.exp(-dt * 9);\n      sync();\n      const near = Math.abs(s.th - Math.round(s.th / c.step) * c.step) < 0.05;\n      const settled = !s.drag && s.rest === null && Math.abs(s.w) < 0.2 && near && Math.abs(s.ho) < 0.2;\n      if (settled && !idleOn) {\n        s.th = Math.round(s.th / c.step) * c.step;\n        s.w = 0;\n        s.ho = 0;\n        s.hist.fill(s.th);\n        sync();\n        return;\n      }\n      raf = requestAnimationFrame(tick);\n    };\n\n    const wake = () => {\n      if (api.current.reduced) {\n        if (s.rest !== null) {\n          s.th = s.rest;\n          s.rest = null;\n        }\n        s.ho = s.drag ? wrap(s.pa - s.th) : 0;\n        sync();\n        return;\n      }\n      if (!raf && visible) {\n        last = performance.now();\n        raf = requestAnimationFrame(tick);\n      }\n    };\n\n    api.current.wake = wake;\n    api.current.sync = sync;\n    s.hist.length = 0;\n    sync();\n    wake();\n\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1]?.isIntersecting ?? true;\n      if (visible) wake();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(root);\n    const onMq = () => {\n      api.current.reduced = mq.matches;\n      if (mq.matches) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n        s.w = 0;\n        s.th = Math.round(s.th / latest.current.step) * latest.current.step;\n      }\n      wake();\n    };\n    mq.addEventListener(\"change\", onMq);\n\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      mq.removeEventListener(\"change\", onMq);\n    };\n  }, []);\n\n  function angleOf(e: React.PointerEvent<HTMLDivElement>) {\n    const r = e.currentTarget.getBoundingClientRect();\n    const dx = e.clientX - (r.left + r.width / 2);\n    const dy = e.clientY - (r.top + r.height / 2);\n    return (Math.atan2(dx, -dy) * 180) / Math.PI;\n  }\n\n  function stepBy(delta: number) {\n    const s = state.current;\n    const base = Math.round(s.th / stepDeg) + delta;\n    s.user = true;\n    s.w = 0;\n    s.ghosted = false;\n    s.rest = base * stepDeg;\n    s.resumeAt = performance.now() + 2600;\n    api.current.wake();\n  }\n\n  const vars = { \"--hb-qla-c\": accent, width: size, height: size, ...style } as React.CSSProperties;\n  const text = labels?.[shown] ?? String(shown).padStart(2, \"0\");\n  const ticks = Array.from({ length: n }, (_, i) => {\n    const a = (i * stepDeg * Math.PI) / 180;\n    return {\n      x1: (100 + 72 * Math.sin(a)).toFixed(2),\n      y1: (100 - 72 * Math.cos(a)).toFixed(2),\n      x2: (100 + 82 * Math.sin(a)).toFixed(2),\n      y2: (100 - 82 * Math.cos(a)).toFixed(2),\n    };\n  });\n\n  return (\n    <>\n      <style href=\"hb-micro-comet-dial\" precedence=\"default\">{css}</style>\n      <div\n        {...rest}\n        ref={rootRef}\n        role=\"slider\"\n        tabIndex={0}\n        aria-label={label}\n        aria-valuemin={0}\n        aria-valuemax={n - 1}\n        aria-valuenow={val}\n        aria-valuetext={labels?.[val] ?? String(val)}\n        className={`hb-qla ${className}`}\n        style={vars}\n        onPointerEnter={(e) => {\n          onPointerEnter?.(e);\n          const s = state.current;\n          s.hover = true;\n          if (s.ghosted) {\n            const base = valRef.current * stepDeg;\n            s.rest = base + 360 * Math.round((s.th - base) / 360);\n            s.ghosted = false;\n            api.current.wake();\n          }\n        }}\n        onPointerLeave={(e) => {\n          onPointerLeave?.(e);\n          state.current.hover = false;\n          state.current.resumeAt = performance.now() + 1800;\n          api.current.wake();\n        }}\n        onFocus={(e) => {\n          onFocus?.(e);\n          state.current.focus = true;\n        }}\n        onBlur={(e) => {\n          onBlur?.(e);\n          state.current.focus = false;\n          state.current.resumeAt = performance.now() + 1800;\n          api.current.wake();\n        }}\n        onKeyDown={(e) => {\n          if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") {\n            e.preventDefault();\n            stepBy(1);\n          } else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") {\n            e.preventDefault();\n            stepBy(-1);\n          } else if (e.key === \"PageUp\") {\n            e.preventDefault();\n            stepBy(3);\n          } else if (e.key === \"PageDown\") {\n            e.preventDefault();\n            stepBy(-3);\n          }\n        }}\n        onPointerDown={(e) => {\n          if (e.button !== 0) return;\n          const s = state.current;\n          e.currentTarget.setPointerCapture(e.pointerId);\n          s.drag = true;\n          s.user = true;\n          s.rest = null;\n          s.ghosted = false;\n          s.w = 0;\n          s.pa = angleOf(e);\n          s.lastT = e.timeStamp;\n          api.current.wake();\n        }}\n        onPointerMove={(e) => {\n          const s = state.current;\n          if (!s.drag) return;\n          const a = angleOf(e);\n          const d = wrap(a - s.pa);\n          s.pa = a;\n          s.th += d;\n          const dtm = Math.max(1, e.timeStamp - s.lastT);\n          s.w = s.w * 0.5 + ((d / dtm) * 1000) * 0.5;\n          s.lastT = e.timeStamp;\n          if (api.current.reduced) {\n            s.ho = wrap(s.pa - s.th);\n            api.current.sync();\n          } else api.current.wake();\n        }}\n        onPointerUp={(e) => {\n          const s = state.current;\n          if (!s.drag) return;\n          if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);\n          s.drag = false;\n          if (e.timeStamp - s.lastT > 90) s.w = 0;\n          s.w = Math.max(-1400, Math.min(1400, s.w));\n          s.resumeAt = performance.now() + 2600;\n          if (api.current.reduced) {\n            s.th = Math.round(s.th / stepDeg) * stepDeg;\n            s.w = 0;\n            s.ho = 0;\n            api.current.sync();\n          } else api.current.wake();\n        }}\n        onPointerCancel={() => {\n          state.current.drag = false;\n          api.current.wake();\n        }}\n      >\n        <svg className=\"hb-qla-v\" viewBox=\"0 0 200 200\" aria-hidden=\"true\">\n          <circle className=\"hb-qla-o\" cx=\"100\" cy=\"100\" r=\"95\" />\n          {ticks.map((t, i) => (\n            <line key={i} className=\"hb-qla-t\" data-on={i === shown} x1={t.x1} y1={t.y1} x2={t.x2} y2={t.y2} />\n          ))}\n          {Array.from({ length: TAIL - 1 }, (_, i) => (\n            <path\n              key={i}\n              ref={(el) => {\n                segs.current[i] = el;\n              }}\n              className=\"hb-qla-s\"\n              d=\"\"\n              strokeOpacity=\"0\"\n            />\n          ))}\n          <circle ref={headRef} className=\"hb-qla-e\" cx=\"100\" cy=\"10\" r=\"4.6\" />\n          <g ref={knobRef}>\n            <circle className=\"hb-qla-k\" cx=\"100\" cy=\"100\" r=\"62\" fill=\"#1c1c1c\" />\n            <circle className=\"hb-qla-d\" cx=\"100\" cy=\"100\" r=\"56\" />\n            <line className=\"hb-qla-n\" x1=\"100\" y1=\"46\" x2=\"100\" y2=\"58\" />\n          </g>\n          <circle cx=\"100\" cy=\"100\" r=\"40\" fill=\"#0c0c0c\" stroke=\"#222222\" strokeWidth=\"1.5\" />\n          {flash > 0 ? <circle key={flash} className=\"hb-qla-r\" cx=\"100\" cy=\"100\" r=\"62\" /> : null}\n          <text className=\"hb-qla-x\" x=\"100\" y=\"100\" textAnchor=\"middle\" dominantBaseline=\"central\">\n            {text}\n          </text>\n        </svg>\n      </div>\n    </>\n  );\n}\n"
    }
  ]
}