{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"drum-picker","type":"registry:ui","title":"Drum Picker","description":"iOS-style picker wheel: rows curve around a 3D cylinder, snap into place and respond to wheel, drag, touch and arrow keys.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/drum-picker.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState, type KeyboardEvent, type PointerEvent } from \"react\";\n\nexport interface DrumPickerProps {\n  options: string[];\n  /** Initially selected index. */\n  defaultIndex?: number;\n  onChange?: (value: string, index: number) => void;\n  /** Row height in px. */\n  itemHeight?: number;\n  /** Rows visible at once (odd numbers look best). */\n  visibleCount?: number;\n  /** Degrees each row turns away from the center: the cylinder's curvature. */\n  curvature?: number;\n  /** Loop the wheel so it never hits an end. */\n  loop?: boolean;\n  /** Accessible name. */\n  label?: string;\n  className?: string;\n}\n\nconst LOOP_COPIES = 21;\n\nexport function DrumPicker({\n  options,\n  defaultIndex = 0,\n  onChange,\n  itemHeight = 40,\n  visibleCount = 5,\n  curvature = 20,\n  loop = false,\n  label = \"Options\",\n  className,\n}: DrumPickerProps) {\n  const scroller = useRef<HTMLDivElement>(null);\n  const rows = useRef<(HTMLDivElement | null)[]>([]);\n  const n = options.length;\n  const copies = loop ? LOOP_COPIES : 1;\n  const total = n * copies;\n  const base = loop ? n * Math.floor(copies / 2) : 0;\n  const [selected, setSelected] = useState(() => Math.min(Math.max(defaultIndex, 0), n - 1));\n  const selectedRef = useRef(selected);\n  const onChangeRef = useRef(onChange);\n  const id = useId();\n  const pad = ((visibleCount - 1) / 2) * itemHeight;\n\n  useEffect(() => {\n    onChangeRef.current = onChange;\n  });\n\n  // Scroll -> 3D cylinder. Rows stay in normal flow (so native scroll + snap do the physics);\n  // each one is pushed onto a cylinder of radius R around the viewport's center line.\n  useEffect(() => {\n    const el = scroller.current!;\n    const rad = (curvature * Math.PI) / 180;\n    const R = rad > 0 ? itemHeight / rad : 0;\n    const reduce = matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let raf = 0;\n    let settle = 0;\n\n    const paint = () => {\n      raf = 0;\n      const center = el.scrollTop / itemHeight;\n      for (let i = 0; i < rows.current.length; i++) {\n        const row = rows.current[i];\n        if (!row) continue;\n        const d = i - center;\n        if (Math.abs(d) > visibleCount) {\n          row.style.visibility = \"hidden\";\n          continue;\n        }\n        row.style.visibility = \"\";\n        const theta = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, d * rad));\n        const y = R ? R * Math.sin(theta) - d * itemHeight : 0;\n        const z = R ? R * Math.cos(theta) - R : 0;\n        row.style.transform = `translate3d(0, ${y}px, ${z}px) rotateX(${(-theta * 180) / Math.PI}deg)`;\n        row.style.opacity = String(Math.max(0, 1 - Math.abs(d) * (0.7 / ((visibleCount - 1) / 2 || 1))));\n      }\n    };\n\n    const commit = () => {\n      let i = Math.round(el.scrollTop / itemHeight);\n      // Loop mode: quietly jump back to the middle copy so the wheel never runs out.\n      if (loop && (i < n * 3 || i > total - n * 3)) {\n        const mid = base + (((i % n) + n) % n);\n        el.scrollTop += (mid - i) * itemHeight;\n        i = mid;\n      }\n      const idx = ((i % n) + n) % n;\n      if (idx !== selectedRef.current) {\n        selectedRef.current = idx;\n        setSelected(idx);\n        onChangeRef.current?.(options[idx], idx);\n        if (!reduce) navigator.vibrate?.(4);\n      }\n    };\n\n    const onScroll = () => {\n      if (!raf) raf = requestAnimationFrame(paint);\n      clearTimeout(settle);\n      settle = window.setTimeout(commit, 120);\n    };\n\n    el.scrollTop = (base + selectedRef.current) * itemHeight;\n    paint();\n    el.addEventListener(\"scroll\", onScroll, { passive: true });\n    return () => {\n      cancelAnimationFrame(raf);\n      clearTimeout(settle);\n      el.removeEventListener(\"scroll\", onScroll);\n    };\n  }, [itemHeight, visibleCount, curvature, loop, n, total, base, options]);\n\n  const scrollToIndex = (i: number, smooth = true) => {\n    const el = scroller.current!;\n    const current = Math.round(el.scrollTop / itemHeight);\n    const target = loop ? current + i - (((current % n) + n) % n) : Math.max(0, Math.min(n - 1, i));\n    el.scrollTo({ top: target * itemHeight, behavior: smooth ? \"smooth\" : \"auto\" });\n  };\n\n  const stepBy = (delta: number) => {\n    const el = scroller.current!;\n    const current = Math.round(el.scrollTop / itemHeight);\n    const target = loop ? current + delta : Math.max(0, Math.min(n - 1, current + delta));\n    el.scrollTo({ top: target * itemHeight, behavior: \"smooth\" });\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLDivElement>) => {\n    const map: Record<string, () => void> = {\n      ArrowUp: () => stepBy(-1),\n      ArrowDown: () => stepBy(1),\n      PageUp: () => stepBy(-(visibleCount - 1)),\n      PageDown: () => stepBy(visibleCount - 1),\n      Home: () => scrollToIndex(0),\n      End: () => scrollToIndex(n - 1),\n    };\n    if (!map[e.key]) return;\n    e.preventDefault();\n    map[e.key]();\n  };\n\n  // Mouse drag (touch already scrolls natively). Snap is off while dragging, then we fling + snap.\n  const drag = useRef<{ y: number; top: number; t: number; v: number; moved: boolean } | null>(null);\n  const onDown = (e: PointerEvent<HTMLDivElement>) => {\n    if (e.pointerType !== \"mouse\" || e.button !== 0) return;\n    const el = scroller.current!;\n    drag.current = { y: e.clientY, top: el.scrollTop, t: e.timeStamp, v: 0, moved: false };\n  };\n  const onMove = (e: PointerEvent<HTMLDivElement>) => {\n    const d = drag.current;\n    if (!d) return;\n    if (!e.buttons) return onUp();\n    const el = scroller.current!;\n    // Capture only once it is a real drag, so a plain click still reaches the row under the pointer.\n    if (!d.moved) {\n      if (Math.abs(e.clientY - d.y) <= 3) return;\n      d.moved = true;\n      el.style.scrollSnapType = \"none\";\n      e.currentTarget.setPointerCapture(e.pointerId);\n    }\n    const now = e.timeStamp;\n    const prev = el.scrollTop;\n    el.scrollTop = d.top - (e.clientY - d.y);\n    d.v = (el.scrollTop - prev) / Math.max(1, now - d.t);\n    d.t = now;\n  };\n  const onUp = () => {\n    const d = drag.current;\n    if (!d) return;\n    drag.current = null;\n    const el = scroller.current!;\n    const fling = el.scrollTop + d.v * 180;\n    const max = loop ? Infinity : (n - 1) * itemHeight;\n    const target = Math.max(0, Math.min(max, Math.round(fling / itemHeight) * itemHeight));\n    el.style.scrollSnapType = \"\";\n    if (d.moved) el.scrollTo({ top: target, behavior: \"smooth\" });\n  };\n\n  const height = itemHeight * visibleCount;\n\n  return (\n    <div\n      className={`relative select-none ${className ?? \"\"}`}\n      style={{ height }}\n    >\n      <div\n        aria-hidden\n        className=\"pointer-events-none absolute inset-x-0 top-1/2 -translate-y-1/2 rounded-xl border border-border bg-muted/70\"\n        style={{ height: itemHeight }}\n      />\n      <div\n        ref={scroller}\n        role=\"listbox\"\n        tabIndex={0}\n        aria-label={label}\n        aria-activedescendant={`${id}-${selected}`}\n        onKeyDown={onKey}\n        onPointerDown={onDown}\n        onPointerMove={onMove}\n        onPointerUp={onUp}\n        onPointerCancel={onUp}\n        className=\"relative h-full overflow-y-scroll overscroll-contain rounded-xl outline-none [scrollbar-width:none] focus-visible:ring-2 focus-visible:ring-ring [&::-webkit-scrollbar]:hidden\"\n        style={{\n          scrollSnapType: \"y mandatory\",\n          perspective: itemHeight * 16,\n          cursor: \"grab\",\n          maskImage: \"linear-gradient(to bottom, transparent, #000 30%, #000 70%, transparent)\",\n        }}\n      >\n        <div style={{ height: pad }} />\n        {Array.from({ length: total }, (_, i) => {\n          const idx = i % n;\n          const isSel = idx === selected;\n          return (\n            <div\n              key={i}\n              ref={(el) => {\n                rows.current[i] = el;\n              }}\n              id={i === base + idx ? `${id}-${idx}` : undefined}\n              role=\"option\"\n              aria-selected={isSel}\n              onClick={() => scroller.current!.scrollTo({ top: i * itemHeight, behavior: \"smooth\" })}\n              className={`grid place-items-center whitespace-nowrap px-4 text-lg tabular-nums transition-colors duration-200 ${isSel ? \"font-semibold text-foreground\" : \"text-muted-foreground\"}`}\n              style={{ height: itemHeight, scrollSnapAlign: \"center\", backfaceVisibility: \"hidden\" }}\n            >\n              {options[idx]}\n            </div>\n          );\n        })}\n        <div style={{ height: pad }} />\n      </div>\n    </div>\n  );\n}\n"}]}