{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "wheel-picker",
  "title": "Wheel Picker",
  "description": "iOS-style picker wheel you drag, flick, scroll or step; groups side by side.",
  "registryDependencies": [
    "@ikas/utils"
  ],
  "files": [
    {
      "path": "registry/ui/wheel-picker.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\ntype WheelPickerOption = string | { value: string; label: string }\n\ntype WheelPickerSize = \"sm\" | \"default\"\n\ntype WheelPickerGroupProps = React.ComponentProps<\"div\"> & {\n  /** Row height: sm is 32px, default is 36px. */\n  size?: WheelPickerSize\n  /** Rows visible through the window. Use an odd number. */\n  visibleCount?: number\n  /** Dims the group and blocks every wheel in it. */\n  disabled?: boolean\n  /** A soft tick on every wheel in the group as a row crosses the band. */\n  sound?: boolean\n}\n\ntype WheelPickerProps = Omit<React.ComponentProps<\"div\">, \"defaultValue\" | \"children\" | \"onChange\"> & {\n  /** Rows to pick from. A string is both the value and the label. */\n  options: WheelPickerOption[]\n  /** Selected value. Leave undefined with defaultValue for an uncontrolled wheel. */\n  value?: string\n  /** Initial value when uncontrolled. */\n  defaultValue?: string\n  /** Called with the value under the band: live while dragging, once a flick settles. */\n  onValueChange?: (value: string) => void\n  /** Row height when used alone. Inside a WheelPickerGroup the group sets it. */\n  size?: WheelPickerSize\n  /** Rows visible when used alone. Inside a WheelPickerGroup the group sets it. */\n  visibleCount?: number\n  /** Blocks drag, wheel and keyboard input. */\n  disabled?: boolean\n  /** Plays a soft tick as each row crosses the band, while the user turns the wheel. Silent when the value changes from outside. */\n  sound?: boolean\n  /** Accessible name, e.g. \"Saat\". */\n  \"aria-label\"?: string\n}\n\nconst ROW_HEIGHT: Record<WheelPickerSize, number> = { sm: 32, default: 36 }\n\n/*\n * Physics. The snap spring uses the SPRING token values from @/lib/motion\n * (stiffness 350, damping 35: no overshoot) and is integrated here so the wheel\n * needs no animation library. A flick coasts on exponential decay, with its time\n * constant stretched or shrunk so it comes to rest exactly on a row.\n */\nconst STIFFNESS = 350\nconst DAMPING = 35\n/** ms; how long a flick keeps coasting. */\nconst COAST = 325\n/** rows per ms; caps a hard fling. */\nconst MAX_VELOCITY = 0.18\n/** ms of recent drag that set the release velocity. */\nconst VELOCITY_WINDOW = 90\n/** rows per pixel of wheel or trackpad delta. */\nconst WHEEL_RATE = 0.012\n/** ms of wheel idle before snapping to a row. */\nconst WHEEL_SETTLE = 110\n/** Drag resistance past the first and last row. */\nconst RUBBER_BAND = 0.3\n/** px of movement under which a press counts as a click on a row. */\nconst CLICK_SLOP = 4\n\nconst DEG = Math.PI / 180\nconst clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(v, hi))\nconst optionValue = (option: WheelPickerOption) => (typeof option === \"string\" ? option : option.value)\nconst optionLabel = (option: WheelPickerOption) => (typeof option === \"string\" ? option : option.label)\n\n/** Drum geometry: each row spans `angle` degrees on a cylinder of `radius`; rows past `horizon` are hidden. */\nfunction geometry(size: WheelPickerSize, visibleCount: number) {\n  const row = ROW_HEIGHT[size]\n  const side = Math.max(1, Math.floor(visibleCount / 2))\n  const horizon = side + 1\n  const angle = 90 / horizon\n  const radius = row / Math.tan(angle * DEG)\n  return { row, angle, radius, horizon, height: Math.round(2 * radius * Math.sin(side * angle * DEG) + row) }\n}\n\n/*\n * Tick. A short triangle blip that drops an octave, like a detent on a dial. One AudioContext serves\n * every wheel on the page; it is made on the first tick, which always follows a press, key or wheel\n * on a page the user has already interacted with, so the browser lets it play.\n */\nlet audio: AudioContext | null = null\nlet lastTick = -1\n/** s; ticks closer than this merge, so a hard fling purrs instead of stacking into a buzz. */\nconst TICK_GAP = 0.028\n\nfunction tick() {\n  if (typeof window === \"undefined\" || !(\"AudioContext\" in window)) return\n  audio ??= new AudioContext()\n  if (audio.state === \"suspended\") audio.resume().catch(() => undefined)\n  const now = audio.currentTime\n  if (now - lastTick < TICK_GAP) return\n  lastTick = now\n  const tone = audio.createOscillator()\n  const level = audio.createGain()\n  tone.type = \"triangle\"\n  tone.frequency.setValueAtTime(1700, now)\n  tone.frequency.exponentialRampToValueAtTime(850, now + 0.012)\n  level.gain.setValueAtTime(0.07, now)\n  level.gain.exponentialRampToValueAtTime(0.0001, now + 0.022)\n  tone.connect(level).connect(audio.destination)\n  tone.start(now)\n  tone.stop(now + 0.025)\n}\n\nconst reducedQuery = \"(prefers-reduced-motion: reduce)\"\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (onChange) => {\n      const media = window.matchMedia(reducedQuery)\n      media.addEventListener(\"change\", onChange)\n      return () => media.removeEventListener(\"change\", onChange)\n    },\n    () => window.matchMedia(reducedQuery).matches,\n    () => false\n  )\n}\n\ntype GroupContext = { size: WheelPickerSize; visibleCount: number; disabled: boolean; sound: boolean }\n\nconst WheelPickerGroupContext = React.createContext<GroupContext | null>(null)\n\n/** Frame for one or more wheels side by side, with one shared selection band (date, time). */\nfunction WheelPickerGroup({ size = \"default\", visibleCount = 5, disabled = false, sound = false, className, children, ...props }: WheelPickerGroupProps) {\n  const { row, height } = geometry(size, visibleCount)\n  const context = React.useMemo(() => ({ size, visibleCount, disabled, sound }), [size, visibleCount, disabled, sound])\n\n  return (\n    <WheelPickerGroupContext.Provider value={context}>\n      <div\n        data-slot=\"wheel-picker-group\"\n        data-size={size}\n        data-disabled={disabled || undefined}\n        role=\"group\"\n        className={cn(\n          \"relative isolate flex overflow-hidden rounded-xl border border-input bg-card px-1.5 shadow-inset transition-[border-color,box-shadow] duration-150 ease-out hover:border-border-strong has-focus-visible:border-ring has-focus-visible:ring-3 has-focus-visible:ring-ring/20 data-disabled:pointer-events-none data-disabled:opacity-50\",\n          size === \"sm\" ? \"text-[13px]\" : \"text-sm\",\n          className\n        )}\n        style={{ height }}\n        {...props}\n      >\n        <div\n          aria-hidden\n          data-slot=\"wheel-picker-band\"\n          className=\"pointer-events-none absolute inset-x-1.5 top-1/2 -z-10 -translate-y-1/2 rounded-lg bg-foreground/[0.05] shadow-[inset_0_1px_2px_0_var(--shade-1)]\"\n          style={{ height: row }}\n        />\n        {children}\n      </div>\n    </WheelPickerGroupContext.Provider>\n  )\n}\n\n/** iOS-style picker wheel: a 3D drum you drag, flick, scroll or step with the keyboard; snaps to a row. */\nfunction WheelPicker(props: WheelPickerProps) {\n  const group = React.useContext(WheelPickerGroupContext)\n  if (group)\n    return (\n      <WheelPickerColumn\n        {...props}\n        size={group.size}\n        visibleCount={group.visibleCount}\n        disabled={group.disabled || props.disabled}\n        sound={group.sound || props.sound}\n      />\n    )\n\n  const { size = \"default\", visibleCount = 5, disabled = false, className, ...rest } = props\n  return (\n    <WheelPickerGroup size={size} visibleCount={visibleCount} disabled={disabled} className={className}>\n      <WheelPickerColumn {...rest} size={size} visibleCount={visibleCount} disabled={disabled} />\n    </WheelPickerGroup>\n  )\n}\n\nfunction WheelPickerColumn({\n  options,\n  value: valueProp,\n  defaultValue,\n  onValueChange,\n  size = \"default\",\n  visibleCount = 5,\n  disabled = false,\n  sound = false,\n  className,\n  \"aria-label\": ariaLabel,\n  ...props\n}: WheelPickerProps) {\n  const reduce = useReducedMotion()\n  const { row, angle, radius, horizon } = geometry(size, visibleCount)\n  const last = options.length - 1\n\n  const [uncontrolled, setUncontrolled] = React.useState(defaultValue ?? valueProp)\n  const value = valueProp !== undefined ? valueProp : uncontrolled\n  const index = Math.max(0, options.findIndex((o) => optionValue(o) === value))\n\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const drumRef = React.useRef<HTMLUListElement>(null)\n  const bandRef = React.useRef<HTMLUListElement>(null)\n  /** Position in rows (a float index), the one source of truth for both drums. */\n  const scroll = React.useRef(index)\n  /** Row the wheel is heading to or resting on. */\n  const target = React.useRef(index)\n  const emitted = React.useRef(value)\n  const frame = React.useRef(0)\n  const wheelTimer = React.useRef<ReturnType<typeof setTimeout>>(undefined)\n  const drag = React.useRef<{ startY: number; startScroll: number; moved: boolean; samples: [number, number][] } | null>(null)\n  const [grabbing, setGrabbing] = React.useState(false)\n  /** Row last under the band, and whether the user is the one turning the wheel (only they hear it). */\n  const tickRow = React.useRef(index)\n  const audible = React.useRef(false)\n\n  // Handlers read the latest props through this ref, so listeners bind once.\n  const latest = React.useRef({ options, last, row, angle, radius, horizon, reduce, disabled, sound, onValueChange, controlled: valueProp !== undefined })\n  React.useLayoutEffect(() => {\n    latest.current = { options, last, row, angle, radius, horizon, reduce, disabled, sound, onValueChange, controlled: valueProp !== undefined }\n  })\n\n  const paint = React.useCallback((position: number) => {\n    scroll.current = position\n    const { angle, radius, horizon, last, sound } = latest.current\n    const under = clamp(Math.round(position), 0, last)\n    if (under !== tickRow.current) {\n      tickRow.current = under\n      if (sound && audible.current) tick()\n    }\n    for (const list of [drumRef.current, bandRef.current]) {\n      if (!list) continue\n      list.style.transform = `translateZ(${-radius}px) rotateX(${angle * position}deg)`\n      for (const node of list.children) {\n        const item = node as HTMLElement\n        const hidden = Math.abs(Number(item.dataset.index) - position) > horizon\n        // Write only on change: touching style every frame forces a recalc per row.\n        if (item.hidden !== hidden) item.hidden = hidden\n      }\n    }\n  }, [])\n\n  const emit = React.useCallback((i: number) => {\n    const { options, last, controlled, onValueChange } = latest.current\n    const option = options[clamp(Math.round(i), 0, last)]\n    if (!option) return\n    const next = optionValue(option)\n    if (next === emitted.current) return\n    emitted.current = next\n    if (!controlled) setUncontrolled(next)\n    onValueChange?.(next)\n  }, [])\n\n  const stop = React.useCallback(() => cancelAnimationFrame(frame.current), [])\n\n  /** Spring from the current position to row `to`, keeping `velocity` (rows per second). */\n  const springTo = React.useCallback(\n    (to: number, velocity = 0) => {\n      stop()\n      const end = clamp(Math.round(to), 0, latest.current.last)\n      target.current = end\n      if (latest.current.reduce) {\n        paint(end)\n        emit(end)\n        return\n      }\n      let position = scroll.current\n      let v = velocity\n      let then = performance.now()\n      const step = (now: number) => {\n        const dt = Math.min((now - then) / 1000, 1 / 30)\n        then = now\n        for (let i = 0; i < 4; i++) {\n          const h = dt / 4\n          v += (-STIFFNESS * (position - end) - DAMPING * v) * h\n          position += v * h\n        }\n        if (Math.abs(position - end) < 0.001 && Math.abs(v) < 0.05) {\n          paint(end)\n          emit(end)\n          return\n        }\n        paint(position)\n        frame.current = requestAnimationFrame(step)\n      }\n      frame.current = requestAnimationFrame(step)\n    },\n    [paint, emit, stop]\n  )\n\n  /** Coast a flick of `velocity` (rows per ms) to the nearest row it would reach, then hand over to the spring. */\n  const fling = React.useCallback(\n    (velocity: number) => {\n      const { last, reduce } = latest.current\n      const from = scroll.current\n      const to = clamp(Math.round(from + velocity * COAST), 0, last)\n      const distance = to - from\n      const tau = distance / velocity\n      // Out of bounds, too slow, or a coast that would crawl or snap: let the spring finish it.\n      if (reduce || from < 0 || from > last || !Number.isFinite(tau) || tau < COAST * 0.4 || tau > COAST * 2) {\n        springTo(to, from < 0 || from > last ? 0 : velocity * 1000)\n        return\n      }\n      stop()\n      target.current = to\n      const start = performance.now()\n      const step = (now: number) => {\n        const decay = Math.exp(-(now - start) / tau)\n        const position = from + distance * (1 - decay)\n        if (Math.abs(to - position) < 0.5) {\n          paint(position)\n          springTo(to, ((to - position) / tau) * 1000)\n          return\n        }\n        paint(position)\n        frame.current = requestAnimationFrame(step)\n      }\n      frame.current = requestAnimationFrame(step)\n    },\n    [paint, springTo, stop]\n  )\n\n  // Follow value changes from outside unless a drag is running, and re-cull rows when the options change.\n  const seen = React.useRef({ index, value })\n  React.useLayoutEffect(() => {\n    emitted.current = value\n    const previous = seen.current\n    seen.current = { index, value }\n    if (drag.current) return\n    if (index === previous.index) paint(scroll.current)\n    else if (value === previous.value) {\n      // Same value at a new index (rows added or removed above it, e.g. days 12–31 → 1–30): jump, don't spin.\n      stop()\n      audible.current = false\n      target.current = index\n      paint(index)\n    } else if (index !== target.current) {\n      audible.current = false\n      springTo(index)\n    } else paint(scroll.current)\n  }, [index, value, options, paint, springTo, stop])\n\n  React.useEffect(\n    () => () => {\n      cancelAnimationFrame(frame.current)\n      clearTimeout(wheelTimer.current)\n    },\n    []\n  )\n\n  // Wheel needs a non-passive listener so it can stop the page scrolling behind the drum.\n  React.useEffect(() => {\n    const root = rootRef.current\n    if (!root) return\n    const onWheel = (event: WheelEvent) => {\n      const { disabled, last } = latest.current\n      if (disabled) return\n      event.preventDefault()\n      stop()\n      audible.current = true\n      const px = event.deltaMode === 1 ? event.deltaY * 16 : event.deltaY\n      const next = clamp(scroll.current + px * WHEEL_RATE, 0, last)\n      paint(next)\n      target.current = Math.round(next)\n      emit(next)\n      clearTimeout(wheelTimer.current)\n      wheelTimer.current = setTimeout(() => springTo(scroll.current), WHEEL_SETTLE)\n    }\n    root.addEventListener(\"wheel\", onWheel, { passive: false })\n    return () => root.removeEventListener(\"wheel\", onWheel)\n  }, [paint, emit, springTo, stop])\n\n  function onPointerDown(event: React.PointerEvent<HTMLDivElement>) {\n    if (disabled || event.button !== 0) return\n    stop()\n    audible.current = true\n    try {\n      event.currentTarget.setPointerCapture(event.pointerId)\n    } catch {\n      // WebKit throws when the system already claimed the pointer; implicit capture still applies.\n    }\n    drag.current = { startY: event.clientY, startScroll: scroll.current, moved: false, samples: [[event.clientY, event.timeStamp]] }\n  }\n\n  function onPointerMove(event: React.PointerEvent<HTMLDivElement>) {\n    const d = drag.current\n    if (!d) return\n    if (!d.moved && Math.abs(event.clientY - d.startY) < CLICK_SLOP) return\n    if (!d.moved) {\n      d.moved = true\n      setGrabbing(true)\n    }\n    d.samples.push([event.clientY, event.timeStamp])\n    if (d.samples.length > 8) d.samples.shift()\n    let next = d.startScroll + (d.startY - event.clientY) / row\n    if (next < 0) next *= RUBBER_BAND\n    else if (next > last) next = last + (next - last) * RUBBER_BAND\n    paint(next)\n    target.current = clamp(Math.round(next), 0, last)\n    emit(next)\n  }\n\n  function onPointerUp(event: React.PointerEvent<HTMLDivElement>) {\n    const d = drag.current\n    if (!d) return\n    drag.current = null\n    setGrabbing(false)\n    if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId)\n    if (event.type === \"pointercancel\") return springTo(scroll.current)\n\n    if (!d.moved) {\n      // A click on a dimmed row turns the drum to it. Rows bunch up towards the horizon, hence asin.\n      const box = event.currentTarget.getBoundingClientRect()\n      const offset = clamp((event.clientY - (box.top + box.height / 2)) / radius, -1, 1)\n      return springTo(target.current + Math.round(Math.asin(offset) / (angle * DEG)))\n    }\n\n    // Release velocity averaged over the last VELOCITY_WINDOW ms, so one noisy frame can't fling.\n    const end = d.samples[d.samples.length - 1]\n    const start = d.samples.find((s) => end[1] - s[1] <= VELOCITY_WINDOW) ?? end\n    const dt = end[1] - start[1]\n    fling(dt > 0 ? clamp((start[0] - end[0]) / row / dt, -MAX_VELOCITY, MAX_VELOCITY) : 0)\n  }\n\n  function onKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {\n    if (disabled) return\n    const by: Record<string, number> = { ArrowUp: -1, ArrowDown: 1, PageUp: -5, PageDown: 5, Home: -Infinity, End: Infinity }\n    if (!(event.key in by)) return\n    event.preventDefault()\n    audible.current = true\n    springTo(clamp(target.current + by[event.key], 0, last))\n  }\n\n  const rows = (crisp: boolean) =>\n    options.map((option, i) => (\n      <li\n        key={optionValue(option)}\n        data-index={i}\n        className={cn(\n          \"absolute inset-x-0 flex items-center justify-center truncate px-2 tabular-nums backface-hidden\",\n          crisp ? \"font-medium text-foreground\" : \"text-muted-foreground\"\n        )}\n        style={{ top: -row / 2, height: row, transform: `rotateX(${-angle * i}deg) translateZ(${radius}px)` }}\n      >\n        {optionLabel(option)}\n      </li>\n    ))\n\n  const drum = \"absolute inset-x-0 top-1/2 m-0 h-0 list-none p-0 transform-3d will-change-transform\"\n  const selected = options[index]\n\n  return (\n    <div\n      {...props}\n      ref={rootRef}\n      data-slot=\"wheel-picker\"\n      // Inside a vaul Drawer, dragging the wheel must not drag the drawer.\n      data-vaul-no-drag=\"\"\n      role=\"spinbutton\"\n      aria-label={ariaLabel}\n      aria-valuemin={0}\n      aria-valuemax={last}\n      aria-valuenow={index}\n      aria-valuetext={selected ? optionLabel(selected) : undefined}\n      aria-disabled={disabled || undefined}\n      tabIndex={disabled ? -1 : 0}\n      onKeyDown={onKeyDown}\n      onPointerDown={onPointerDown}\n      onPointerMove={onPointerMove}\n      onPointerUp={onPointerUp}\n      onPointerCancel={onPointerUp}\n      className={cn(\n        \"group/wheel relative min-w-0 flex-1 touch-none overflow-hidden outline-none select-none [-webkit-touch-callout:none] [mask-image:linear-gradient(to_bottom,transparent,black_22%,black_78%,transparent)]\",\n        grabbing ? \"cursor-grabbing\" : \"cursor-grab\",\n        className\n      )}\n      style={{ perspective: 1000 }}\n    >\n      <ul ref={drumRef} aria-hidden className={drum}>\n        {rows(false)}\n      </ul>\n      {/* The same drum again, clipped to the band and drawn crisp; one transform keeps the copies in register. */}\n      <div\n        aria-hidden\n        className=\"pointer-events-none absolute inset-x-0 top-1/2 -translate-y-1/2 overflow-hidden rounded-md transition-[background-color] duration-150 ease-out group-focus-visible/wheel:bg-foreground/[0.06]\"\n        style={{ height: row, perspective: 1000 }}\n      >\n        <ul ref={bandRef} className={drum}>\n          {rows(true)}\n        </ul>\n      </div>\n    </div>\n  )\n}\n\nexport { WheelPicker, WheelPickerGroup, type WheelPickerProps, type WheelPickerGroupProps, type WheelPickerOption }\n",
      "type": "registry:ui"
    }
  ],
  "docs": "https://builders.ikas.com/tr/docs/app-development/ui-kit/components/wheel-picker",
  "categories": [
    "forms",
    "motion"
  ],
  "type": "registry:ui"
}