{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tail-knob","type":"registry:ui","title":"Tail Knob","description":"A drag-around knob whose indicator trails a glowing comet tail, with a live readout and full arrow-key support.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/tail-knob.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from \"react\";\n\nexport interface TailKnobProps {\n  min?: number;\n  max?: number;\n  step?: number;\n  /** Controlled value. */\n  value?: number;\n  /** Initial value when uncontrolled. */\n  defaultValue?: number;\n  /** Called with the new value while dragging or using the keyboard. */\n  onChange?: (value: number) => void;\n  /** Comet and arc color. */\n  color?: string;\n  /** Diameter in px. */\n  size?: number;\n  /** Label under the readout; also the accessible name. */\n  label?: string;\n  /** Suffix after the number, e.g. \"%\". */\n  unit?: string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst START = 135; // deg, bottom-left\nconst SWEEP = 270;\nconst R = 78;\nconst TAIL = 18;\nconst TICKS = 31;\n\nconst point = (f: number, r = R) => {\n  const a = ((START + f * SWEEP) * Math.PI) / 180;\n  // Rounded so server and client trig agree (avoids hydration mismatches).\n  const q = (n: number) => Math.round(n * 1000) / 1000;\n  return [q(100 + Math.cos(a) * r), q(100 + Math.sin(a) * r)] as const;\n};\nconst [ax, ay] = point(0);\nconst [bx, by] = point(1);\nconst ARC = `M${ax} ${ay}A${R} ${R} 0 1 1 ${bx} ${by}`;\n\nexport function TailKnob({\n  min = 0,\n  max = 100,\n  step = 1,\n  value,\n  defaultValue = 40,\n  onChange,\n  color = \"#38bdf8\",\n  size = 220,\n  label = \"Volume\",\n  unit = \"%\",\n  disabled,\n  className,\n}: TailKnobProps) {\n  const [inner, setInner] = useState(defaultValue);\n  const v = Math.min(max, Math.max(min, value ?? inner));\n  const frac = (x: number) => (max > min ? (x - min) / (max - min) : 0);\n\n  const root = useRef<HTMLDivElement>(null);\n  const fill = useRef<SVGPathElement>(null);\n  const head = useRef<SVGGElement>(null);\n  const tail = useRef<(SVGCircleElement | null)[]>([]);\n  const st = useRef({ cur: -1, target: 0, trail: [] as number[], raf: 0, drag: false, calm: false });\n\n  const draw = () => {\n    const s = st.current;\n    const [hx, hy] = point(s.cur);\n    head.current?.setAttribute(\"transform\", `translate(${hx} ${hy})`);\n    if (fill.current) fill.current.style.strokeDashoffset = `${1 - s.cur}`;\n    tail.current.forEach((c, i) => {\n      if (!c) return;\n      const [x, y] = point(s.trail[i] ?? s.cur);\n      c.setAttribute(\"cx\", `${x}`);\n      c.setAttribute(\"cy\", `${y}`);\n    });\n  };\n\n  const tick = () => {\n    const s = st.current;\n    s.cur += (s.target - s.cur) * (s.calm ? 1 : 0.2);\n    s.trail.unshift(s.cur);\n    s.trail.length = TAIL;\n    draw();\n    const settled = Math.abs(s.target - s.cur) < 1e-4 && s.trail.every((t) => Math.abs(t - s.cur) < 1e-3);\n    s.raf = settled ? 0 : requestAnimationFrame(tick);\n  };\n\n  const target = v === undefined ? 0 : frac(v);\n  useEffect(() => {\n    const s = st.current;\n    s.calm = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    s.target = target;\n    if (s.cur < 0) {\n      s.cur = target;\n      s.trail = Array(TAIL).fill(target);\n      draw();\n      return;\n    }\n    if (!s.raf) s.raf = requestAnimationFrame(tick);\n    // draw/tick only touch refs\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [target]);\n\n  useEffect(() => {\n    const s = st.current;\n    return () => cancelAnimationFrame(s.raf);\n  }, []);\n\n  const commit = (next: number) => {\n    const snapped = Math.min(max, Math.max(min, min + Math.round((next - min) / step) * step));\n    const clean = Number(snapped.toFixed(6));\n    if (clean === v) return;\n    setInner(clean);\n    onChange?.(clean);\n  };\n\n  const fromPointer = (e: PointerEvent<HTMLDivElement>) => {\n    const r = e.currentTarget.getBoundingClientRect();\n    const a = (Math.atan2(e.clientY - (r.top + r.height / 2), e.clientX - (r.left + r.width / 2)) * 180) / Math.PI;\n    let rel = (a - START + 720) % 360;\n    if (rel > SWEEP) rel = rel - SWEEP < (360 - SWEEP) / 2 ? SWEEP : 0;\n    let f = rel / SWEEP;\n    // Don't wrap from max to min (or back) through the dead zone while dragging.\n    if (st.current.drag && Math.abs(f - frac(v)) > 0.5) f = frac(v) > 0.5 ? 1 : 0;\n    commit(min + f * (max - min));\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLDivElement>) => {\n    const big = step * Math.max(1, Math.round((max - min) / step / 10));\n    const map: Record<string, number> = {\n      ArrowRight: v + step,\n      ArrowUp: v + step,\n      ArrowLeft: v - step,\n      ArrowDown: v - step,\n      PageUp: v + big,\n      PageDown: v - big,\n      Home: min,\n      End: max,\n    };\n    if (!(e.key in map) || disabled) return;\n    e.preventDefault();\n    commit(map[e.key]);\n  };\n\n  const lit = frac(v);\n  const shown = Number.isInteger(step) ? Math.round(v) : v.toFixed(String(step).split(\".\")[1]?.length ?? 1);\n\n  return (\n    <div\n      ref={root}\n      role=\"slider\"\n      tabIndex={disabled ? -1 : 0}\n      aria-label={label}\n      aria-valuemin={min}\n      aria-valuemax={max}\n      aria-valuenow={v}\n      aria-valuetext={`${shown}${unit}`}\n      aria-disabled={disabled || undefined}\n      onKeyDown={onKey}\n      onPointerDown={(e) => {\n        if (disabled || e.button !== 0) return;\n        e.currentTarget.setPointerCapture(e.pointerId);\n        st.current.drag = false;\n        fromPointer(e);\n        st.current.drag = true;\n      }}\n      onPointerMove={(e) => {\n        if (st.current.drag) fromPointer(e);\n      }}\n      onPointerUp={() => (st.current.drag = false)}\n      onPointerCancel={() => (st.current.drag = false)}\n      className={`group relative touch-none select-none rounded-full outline-none focus-visible:ring-[3px] focus-visible:ring-ring/60 focus-visible:ring-offset-4 focus-visible:ring-offset-background ${\n        disabled ? \"cursor-not-allowed opacity-50\" : \"cursor-grab active:cursor-grabbing\"\n      } ${className ?? \"\"}`}\n      style={{ width: size, height: size }}\n    >\n      <svg aria-hidden viewBox=\"0 0 200 200\" className=\"absolute inset-0 size-full overflow-visible\">\n        {Array.from({ length: TICKS }, (_, i) => {\n          const f = i / (TICKS - 1);\n          const [x1, y1] = point(f, 92);\n          const [x2, y2] = point(f, i % 5 ? 96 : 99);\n          return (\n            <line\n              key={i}\n              x1={x1}\n              y1={y1}\n              x2={x2}\n              y2={y2}\n              strokeWidth={1.6}\n              strokeLinecap=\"round\"\n              className={f <= lit + 1e-6 ? undefined : \"stroke-border\"}\n              style={{ stroke: f <= lit + 1e-6 ? color : undefined, transition: \"stroke .25s\" }}\n            />\n          );\n        })}\n        <path d={ARC} fill=\"none\" strokeWidth={10} strokeLinecap=\"round\" className=\"stroke-muted\" />\n        <path\n          ref={fill}\n          d={ARC}\n          pathLength={1}\n          fill=\"none\"\n          stroke={color}\n          strokeWidth={10}\n          strokeLinecap=\"round\"\n          strokeDasharray=\"1 1\"\n          strokeDashoffset={1 - lit}\n          style={{ opacity: 0.35 }}\n        />\n        {Array.from({ length: TAIL }, (_, i) => {\n          const k = 1 - i / TAIL;\n          return (\n            <circle\n              key={i}\n              ref={(el) => {\n                tail.current[i] = el;\n              }}\n              r={1 + 5.5 * k}\n              fill={color}\n              opacity={0.55 * k * k}\n              cx={point(lit)[0]}\n              cy={point(lit)[1]}\n            />\n          );\n        })}\n        <g ref={head} transform={`translate(${point(lit)[0]} ${point(lit)[1]})`}>\n          <circle r={14} fill={color} opacity={0.18} />\n          <circle r={8} fill={color} style={{ filter: `drop-shadow(0 0 6px ${color})` }} />\n          <circle r={3.2} fill=\"#fff\" />\n        </g>\n      </svg>\n      <div className=\"absolute inset-[24%] grid place-items-center rounded-full border border-border bg-card shadow-[inset_0_1px_0_rgb(255_255_255/.08),0_14px_30px_-12px_rgb(0_0_0/.6)] transition-[scale] duration-200 group-active:scale-[.97]\">\n        <div className=\"text-center leading-none\">\n          <div className=\"text-3xl font-semibold tabular-nums text-foreground\" style={{ fontSize: size * 0.15 }}>\n            {shown}\n            <span className=\"ml-0.5 text-[0.5em] text-muted-foreground\">{unit}</span>\n          </div>\n          <div className=\"mt-1.5 text-[11px] font-medium uppercase tracking-[0.18em] text-muted-foreground\">{label}</div>\n        </div>\n      </div>\n    </div>\n  );\n}\n"}]}