{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "wheel-option-picker",
  "type": "registry:component",
  "title": "Wheel Option Picker",
  "description": "A vertical drum picker. Options sit on a cylinder in perspective and snap to the center after a drag, flick, wheel step or key press. Set the text size with style or className on the picker.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "components",
    "picker",
    "wheel",
    "select",
    "drum",
    "time",
    "form"
  ],
  "docs": "https://hyperblur.dev/effects/wheel-option-picker",
  "files": [
    {
      "path": "registry/hyperblur/wheel-option-picker.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/wheel-option-picker.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type WheelOption = string | { value: string; label?: string };\n\nexport interface WheelOptionPickerProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\" | \"children\"> {\n  options?: WheelOption[];\n  value?: string;\n  defaultValue?: string;\n  onChange?: (value: string) => void;\n  itemHeight?: number;\n  rows?: number;\n  accent?: string;\n  hint?: boolean;\n  label?: string;\n}\n\nconst defaultOptions: WheelOption[] = [\n  \"January\",\n  \"February\",\n  \"March\",\n  \"April\",\n  \"May\",\n  \"June\",\n  \"July\",\n  \"August\",\n  \"September\",\n  \"October\",\n  \"November\",\n  \"December\",\n];\n\nconst STEP = 20;\n\nconst css = `\n.hb-qu3 {\n  --hb-qu3-a: #2f7bff;\n  --hb-qu3-h: 40px;\n  --hb-qu3-rows: 5;\n  position: relative;\n  box-sizing: border-box;\n  width: 100%;\n  height: calc(var(--hb-qu3-h) * var(--hb-qu3-rows));\n  overflow: hidden;\n  perspective: 700px;\n  perspective-origin: 50% 50%;\n  font-family: inherit;\n  font-size: 17px;\n  color: #8f8f8f;\n  cursor: grab;\n  user-select: none;\n  -webkit-user-select: none;\n  -webkit-tap-highlight-color: transparent;\n  touch-action: pan-x;\n  border-radius: 14px;\n}\n.hb-qu3 *, .hb-qu3 *::before, .hb-qu3 *::after { box-sizing: border-box; }\n.hb-qu3[data-drag=\"true\"] { cursor: grabbing; }\n.hb-qu3:focus-visible { outline: 2px solid var(--hb-qu3-a); outline-offset: 2px; }\n.hb-qu3-band {\n  position: absolute;\n  left: 0;\n  right: 0;\n  top: 50%;\n  height: var(--hb-qu3-h);\n  margin-top: calc(var(--hb-qu3-h) / -2);\n  border-radius: 10px;\n  background: color-mix(in srgb, var(--hb-qu3-a) 12%, transparent);\n  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--hb-qu3-a) 38%, transparent);\n  pointer-events: none;\n}\n.hb-qu3-opt {\n  position: absolute;\n  left: 0;\n  right: 0;\n  top: 50%;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  height: var(--hb-qu3-h);\n  margin-top: calc(var(--hb-qu3-h) / -2);\n  padding: 0 12px;\n  font-size: 1em;\n  font-weight: 500;\n  line-height: 1;\n  white-space: nowrap;\n  color: #8f8f8f;\n  backface-visibility: hidden;\n  transition: color 0.2s ease;\n  will-change: transform, opacity;\n}\n.hb-qu3-opt > span { overflow: hidden; text-overflow: ellipsis; }\n.hb-qu3-opt[data-on=\"true\"] { color: #ededed; font-weight: 600; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-qu3-opt { transition: none; will-change: auto; }\n}\n`;\n\nfunction normalize(options: WheelOption[]) {\n  return options.map((o) =>\n    typeof o === \"string\" ? { value: o, label: o } : { value: o.value, label: o.label ?? o.value },\n  );\n}\n\nfunction place(k: number, pos: number, radius: number) {\n  const phi = (k - pos) * STEP;\n  const a = Math.abs(phi);\n  const op = a >= 84 ? 0 : Math.max(0, 1 - Math.pow(a / 84, 2.2));\n  return {\n    transform: `translateZ(${(-radius).toFixed(2)}px) rotateX(${(-phi).toFixed(3)}deg) translateZ(${radius.toFixed(2)}px)`,\n    opacity: op.toFixed(3),\n    on: Math.abs(k - pos) < 0.5,\n    hidden: a >= 84,\n  };\n}\n\ninterface Api {\n  commit: (i: number) => void;\n}\n\nexport function WheelOptionPicker({\n  options = defaultOptions,\n  value,\n  defaultValue,\n  onChange,\n  itemHeight = 40,\n  rows = 5,\n  accent = \"#2f7bff\",\n  hint = true,\n  label = \"Option picker\",\n  className = \"\",\n  style,\n  ...rest\n}: WheelOptionPickerProps) {\n  const items = React.useMemo(() => normalize(options), [options]);\n  const n = items.length;\n  const controlled = value !== undefined;\n  const [inner, setInner] = React.useState(defaultValue ?? items[0]?.value ?? \"\");\n  const current = controlled ? value : inner;\n  const found = items.findIndex((o) => o.value === current);\n  const index = found < 0 ? 0 : found;\n  const uid = React.useId();\n  const [first] = React.useState(index);\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const optEls = React.useRef<(HTMLDivElement | null)[]>([]);\n  const posRef = React.useRef(index);\n  const targetRef = React.useRef(index);\n  const loopApi = React.useRef<{ kick: () => void }>({ kick: () => undefined });\n  const api = React.useRef<Api>({ commit: () => undefined });\n  const radius = itemHeight / (2 * Math.tan((STEP * Math.PI) / 360));\n\n  React.useEffect(() => {\n    api.current = {\n      commit: (i: number) => {\n        const clamped = Math.max(0, Math.min(n - 1, i));\n        targetRef.current = clamped;\n        loopApi.current.kick();\n        const next = items[clamped];\n        if (!next) return;\n        if (!controlled) setInner(next.value);\n        onChange?.(next.value);\n      },\n    };\n  });\n\n  React.useEffect(() => {\n    if (targetRef.current !== index) {\n      targetRef.current = index;\n      loopApi.current.kick();\n    }\n  }, [index]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    if (!root || n === 0) return;\n    const els = optEls.current;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const s = {\n      raf: 0,\n      prev: 0,\n      nudge: 0,\n      touched: false,\n      hover: false,\n      focus: false,\n      drag: false,\n      id: -1,\n      active: false,\n      startY: 0,\n      startPos: 0,\n      lastY: 0,\n      lastT: 0,\n      v: 0,\n      acc: 0,\n      accTimer: 0,\n    };\n\n    function apply() {\n      for (let k = 0; k < n; k++) {\n        const el = els[k];\n        if (!el) continue;\n        const p = place(k, posRef.current, radius);\n        el.style.transform = p.transform;\n        el.style.opacity = p.opacity;\n        el.style.pointerEvents = p.hidden ? \"none\" : \"auto\";\n        el.dataset.on = String(p.on);\n      }\n    }\n\n    function tick(now: number) {\n      const dt = Math.min(0.05, Math.max(0, (now - s.prev) / 1000));\n      s.prev = now;\n      if (!s.drag) {\n        const goal = targetRef.current + s.nudge;\n        const diff = goal - posRef.current;\n        if (reduce || Math.abs(diff) < 0.002) {\n          posRef.current = goal;\n          s.raf = 0;\n          apply();\n          return;\n        }\n        posRef.current += diff * (1 - Math.exp(-dt * 9));\n      }\n      apply();\n      s.raf = requestAnimationFrame(tick);\n    }\n\n    function kick() {\n      if (s.raf) return;\n      s.prev = performance.now();\n      s.raf = requestAnimationFrame(tick);\n    }\n\n    loopApi.current = { kick };\n\n    function onDown(e: PointerEvent) {\n      if (e.pointerType === \"mouse\" && e.button !== 0) return;\n      s.id = e.pointerId;\n      s.active = false;\n      s.startY = e.clientY;\n      s.startPos = posRef.current;\n      s.lastY = e.clientY;\n      s.lastT = e.timeStamp;\n      s.v = 0;\n    }\n\n    function onMove(e: PointerEvent) {\n      if (e.pointerId !== s.id) return;\n      const dy = e.clientY - s.startY;\n      if (!s.active) {\n        if (Math.abs(dy) < 5) return;\n        s.active = true;\n        s.drag = true;\n        s.touched = true;\n        s.nudge = 0;\n        root!.dataset.drag = \"true\";\n        root!.setPointerCapture(e.pointerId);\n      }\n      const dtm = Math.max(0.001, (e.timeStamp - s.lastT) / 1000);\n      s.v = s.v * 0.6 + (-(e.clientY - s.lastY) / itemHeight / dtm) * 0.4;\n      s.lastY = e.clientY;\n      s.lastT = e.timeStamp;\n      posRef.current = Math.max(-0.45, Math.min(n - 0.55, s.startPos - dy / itemHeight));\n      apply();\n    }\n\n    function onUp(e: PointerEvent) {\n      if (e.pointerId !== s.id) return;\n      s.id = -1;\n      if (!s.active) return;\n      s.active = false;\n      s.drag = false;\n      root!.dataset.drag = \"false\";\n      if (root!.hasPointerCapture(e.pointerId)) root!.releasePointerCapture(e.pointerId);\n      const fling = Math.max(-12, Math.min(12, s.v)) * 0.22;\n      api.current.commit(Math.round(posRef.current + fling));\n    }\n\n    function onWheel(e: WheelEvent) {\n      const dir = e.deltaY > 0 ? 1 : -1;\n      const next = targetRef.current + dir;\n      if (next < 0 || next > n - 1) return;\n      e.preventDefault();\n      s.touched = true;\n      s.nudge = 0;\n      s.acc += e.deltaY;\n      window.clearTimeout(s.accTimer);\n      s.accTimer = window.setTimeout(() => {\n        s.acc = 0;\n      }, 180);\n      if (Math.abs(s.acc) >= 40) {\n        s.acc = 0;\n        api.current.commit(next);\n      }\n    }\n\n    function onKey(e: KeyboardEvent) {\n      const t = targetRef.current;\n      let next = -1;\n      if (e.key === \"ArrowDown\") next = t + 1;\n      else if (e.key === \"ArrowUp\") next = t - 1;\n      else if (e.key === \"PageDown\") next = t + 3;\n      else if (e.key === \"PageUp\") next = t - 3;\n      else if (e.key === \"Home\") next = 0;\n      else if (e.key === \"End\") next = n - 1;\n      else if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) {\n        const ch = e.key.toLowerCase();\n        for (let o = 1; o <= n; o++) {\n          const k = (t + o) % n;\n          if (items[k].label.toLowerCase().startsWith(ch)) {\n            next = k;\n            break;\n          }\n        }\n      }\n      if (next < 0) return;\n      e.preventDefault();\n      s.touched = true;\n      s.nudge = 0;\n      api.current.commit(next);\n    }\n\n    function onEnter(e: PointerEvent) {\n      if (e.pointerType === \"mouse\") s.hover = true;\n    }\n    function onLeave() {\n      s.hover = false;\n    }\n    function onFocusIn() {\n      s.focus = true;\n    }\n    function onFocusOut() {\n      s.focus = false;\n    }\n\n    root.addEventListener(\"pointerdown\", onDown);\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerup\", onUp);\n    root.addEventListener(\"pointercancel\", onUp);\n    root.addEventListener(\"pointerenter\", onEnter);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"focusin\", onFocusIn);\n    root.addEventListener(\"focusout\", onFocusOut);\n    root.addEventListener(\"keydown\", onKey);\n    root.addEventListener(\"wheel\", onWheel, { passive: false });\n\n    apply();\n\n    let hintTimer = 0;\n    let backTimer = 0;\n    let flip = 1;\n    if (hint && !reduce) {\n      const run = () => {\n        if (!s.touched && !s.hover && !s.focus && !s.drag) {\n          s.nudge = 0.42 * flip;\n          flip = -flip;\n          kick();\n          backTimer = window.setTimeout(() => {\n            s.nudge = 0;\n            kick();\n          }, 520);\n        }\n        hintTimer = window.setTimeout(run, 3400);\n      };\n      hintTimer = window.setTimeout(run, 1600);\n    }\n\n    return () => {\n      cancelAnimationFrame(s.raf);\n      s.raf = 0;\n      window.clearTimeout(hintTimer);\n      window.clearTimeout(backTimer);\n      window.clearTimeout(s.accTimer);\n      root.removeEventListener(\"pointerdown\", onDown);\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerup\", onUp);\n      root.removeEventListener(\"pointercancel\", onUp);\n      root.removeEventListener(\"pointerenter\", onEnter);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"focusin\", onFocusIn);\n      root.removeEventListener(\"focusout\", onFocusOut);\n      root.removeEventListener(\"keydown\", onKey);\n      root.removeEventListener(\"wheel\", onWheel);\n    };\n  }, [n, items, itemHeight, radius, hint]);\n\n  const vars = {\n    \"--hb-qu3-a\": accent,\n    \"--hb-qu3-h\": `${itemHeight}px`,\n    \"--hb-qu3-rows\": String(rows),\n    ...style,\n  } as React.CSSProperties;\n\n  return (\n    <>\n      <style href=\"hb-wheel-option-picker\" precedence=\"default\">{css}</style>\n      <div\n        ref={rootRef}\n        role=\"listbox\"\n        aria-label={label}\n        aria-activedescendant={n > 0 ? `${uid}-${index}` : undefined}\n        tabIndex={0}\n        data-drag=\"false\"\n        className={`hb-qu3 ${className}`}\n        style={vars}\n        {...rest}\n      >\n        <div className=\"hb-qu3-band\" aria-hidden=\"true\" />\n        {items.map((o, k) => {\n          const p = place(k, first, radius);\n          return (\n            <div\n              key={`${o.value}-${k}`}\n              id={`${uid}-${k}`}\n              ref={(el) => {\n                optEls.current[k] = el;\n              }}\n              role=\"option\"\n              aria-selected={k === index}\n              data-on={p.on}\n              className=\"hb-qu3-opt\"\n              style={{\n                transform: p.transform,\n                opacity: p.opacity,\n                pointerEvents: p.hidden ? \"none\" : \"auto\",\n              }}\n              onClick={() => api.current.commit(k)}\n            >\n              <span>{o.label}</span>\n            </div>\n          );\n        })}\n      </div>\n    </>\n  );\n}\n"
    }
  ]
}