{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "wheel-picker",
  "type": "registry:ui",
  "registryDependencies": [],
  "dependencies": ["motion"],
  "devDependencies": [],
  "files": [
    {
      "path": "wheel-picker.tsx",
      "content": "\"use client\";\n\nimport {\n  type AnimationPlaybackControls,\n  animate,\n  type MotionValue,\n  motion,\n  useMotionTemplate,\n  useMotionValue,\n  useReducedMotion,\n  useTransform,\n} from \"motion/react\";\nimport {\n  type CSSProperties,\n  createContext,\n  type KeyboardEvent as ReactKeyboardEvent,\n  type ReactNode,\n  type PointerEvent as ReactPointerEvent,\n  useCallback,\n  useContext,\n  useEffect,\n  useId,\n  useMemo,\n  useRef,\n  useState,\n} from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nconst HIDE_ANGLE = Math.PI / 2 - 0.04;\nconst MAX_ANGLE = Math.PI / 2;\nconst RUBBER_BAND = 0.32;\nconst FLICK_POWER = 0.22;\n\nexport type WheelPickerOption =\n  | string\n  | { value: string; label?: ReactNode; disabled?: boolean };\n\ntype NormalizedOption = {\n  value: string;\n  label: ReactNode;\n  disabled: boolean;\n};\n\nexport interface WheelPickerProps {\n  children: ReactNode;\n  className?: string;\n  itemHeight?: number;\n  visibleCount?: 3 | 5 | 7;\n  lens?: boolean;\n  \"aria-label\"?: string;\n}\n\nexport interface WheelPickerColumnProps {\n  options: WheelPickerOption[];\n  value?: string;\n  defaultValue?: string;\n  onChange?: (\n    value: string,\n    option: { value: string; label: ReactNode }\n  ) => void;\n  onValueCommit?: (\n    value: string,\n    option: { value: string; label: ReactNode }\n  ) => void;\n  loop?: boolean;\n  disabled?: boolean;\n  name?: string;\n  className?: string;\n  id?: string;\n  \"aria-label\"?: string;\n}\n\ntype WheelPickerContextValue = {\n  itemHeight: number;\n  visibleCount: number;\n  lens: boolean;\n};\n\nconst WheelPickerContext = createContext<WheelPickerContextValue>({\n  itemHeight: 44,\n  visibleCount: 5,\n  lens: true,\n});\n\nfunction clamp(value: number, min: number, max: number) {\n  return Math.min(Math.max(value, min), max);\n}\n\nfunction normalizeOption(option: WheelPickerOption): NormalizedOption {\n  if (typeof option === \"string\") {\n    return { value: option, label: option, disabled: false };\n  }\n\n  return {\n    value: option.value,\n    label: option.label ?? option.value,\n    disabled: option.disabled ?? false,\n  };\n}\n\nfunction WheelPickerItem({\n  index,\n  itemHeight,\n  itemId,\n  label,\n  loopSize,\n  offset,\n  optionDisabled,\n  selected,\n  visibleCount,\n}: {\n  index: number;\n  itemHeight: number;\n  itemId: string;\n  label: ReactNode;\n  loopSize: number;\n  offset: MotionValue<number>;\n  optionDisabled: boolean;\n  selected: boolean;\n  visibleCount: number;\n}) {\n  const step = Math.PI / (visibleCount + 2);\n  const radius = itemHeight / step;\n\n  const distance = useTransform(offset, (current: number) => {\n    let delta = index * itemHeight - current;\n\n    if (loopSize > 0) {\n      delta = ((delta % loopSize) + loopSize) % loopSize;\n      if (delta > loopSize / 2) {\n        delta -= loopSize;\n      }\n    }\n\n    return delta / itemHeight;\n  });\n\n  const angle = useTransform(distance, (d) =>\n    clamp(d * step, -MAX_ANGLE, MAX_ANGLE)\n  );\n  const y = useTransform(angle, (a) => radius * Math.sin(a));\n  const rotateX = useTransform(angle, (a) => `${(-a * 180) / Math.PI}deg`);\n  const opacity = useTransform(distance, (d) => {\n    const a = Math.abs(d) * step;\n\n    if (a >= HIDE_ANGLE) {\n      return 0;\n    }\n\n    return Math.cos(a) ** 1.15;\n  });\n  const visibility = useTransform(distance, (d) =>\n    Math.abs(d) * step >= HIDE_ANGLE ? \"hidden\" : \"visible\"\n  );\n  const emphasis = useTransform(distance, (d) => clamp(1 - Math.abs(d), 0, 1));\n  const mixPercent = useTransform(emphasis, (e) => (e * 100).toFixed(1));\n  const color = useMotionTemplate`color-mix(in oklab, var(--foreground) ${mixPercent}%, var(--muted-foreground))`;\n\n  return (\n    <motion.div\n      aria-disabled={optionDisabled || undefined}\n      aria-selected={selected}\n      className=\"absolute inset-x-0 top-1/2\"\n      id={itemId}\n      role=\"option\"\n      style={{\n        backfaceVisibility: \"hidden\",\n        height: itemHeight,\n        marginTop: -itemHeight / 2,\n        opacity,\n        rotateX,\n        visibility,\n        y,\n      }}\n    >\n      <motion.span\n        className={cn(\n          \"absolute inset-0 flex items-center justify-center truncate px-1 font-medium text-[17px] tabular-nums leading-none\",\n          optionDisabled && \"text-muted-foreground/40\"\n        )}\n        style={optionDisabled ? undefined : { color }}\n      >\n        {label}\n      </motion.span>\n    </motion.div>\n  );\n}\n\nexport function WheelPickerColumn({\n  options,\n  value,\n  defaultValue,\n  onChange,\n  onValueCommit,\n  loop = false,\n  disabled = false,\n  name,\n  className,\n  id: idProp,\n  \"aria-label\": ariaLabel,\n}: WheelPickerColumnProps) {\n  const { itemHeight, visibleCount, lens } = useContext(WheelPickerContext);\n  const generatedId = useId();\n  const id = idProp ?? `wheel-column-${generatedId.replace(/[«»:]/g, \"\")}`;\n  const prefersReducedMotion = useReducedMotion();\n\n  const items = useMemo(() => options.map(normalizeOption), [options]);\n  const count = items.length;\n  const canLoop = loop && count >= visibleCount + 2;\n  const loopSize = canLoop ? count * itemHeight : 0;\n  const maxOffset = Math.max(0, (count - 1) * itemHeight);\n  const step = Math.PI / (visibleCount + 2);\n  const radius = itemHeight / step;\n\n  const initialIndexRef = useRef<number | null>(null);\n\n  if (initialIndexRef.current === null) {\n    const initial = value ?? defaultValue;\n    const initialIndex =\n      initial == null ? -1 : items.findIndex((item) => item.value === initial);\n    const firstEnabled = items.findIndex((item) => !item.disabled);\n    initialIndexRef.current =\n      initialIndex >= 0 ? initialIndex : Math.max(0, firstEnabled);\n  }\n\n  const offset = useMotionValue(initialIndexRef.current * itemHeight);\n  const [activeIndex, setActiveIndex] = useState(initialIndexRef.current);\n\n  const activeIndexRef = useRef(activeIndex);\n  const committedIndexRef = useRef(activeIndex);\n  const itemsRef = useRef(items);\n  const onChangeRef = useRef(onChange);\n  const onValueCommitRef = useRef(onValueCommit);\n  const animationRef = useRef<AnimationPlaybackControls | null>(null);\n  const wheelTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n  const containerRef = useRef<HTMLDivElement | null>(null);\n  const dragRef = useRef<{\n    pointerId: number;\n    startClientY: number;\n    startOffset: number;\n    moved: boolean;\n  } | null>(null);\n\n  useEffect(() => {\n    itemsRef.current = items;\n    onChangeRef.current = onChange;\n    onValueCommitRef.current = onValueCommit;\n  });\n\n  const wrapIndex = useCallback(\n    (index: number) => {\n      if (count === 0) {\n        return 0;\n      }\n\n      if (canLoop) {\n        return ((index % count) + count) % count;\n      }\n\n      return clamp(index, 0, count - 1);\n    },\n    [canLoop, count]\n  );\n\n  const nearestEnabled = useCallback(\n    (index: number, direction: number) => {\n      if (count === 0 || !items[wrapIndex(index)]?.disabled) {\n        return index;\n      }\n\n      const preferred = direction === 0 ? [1, -1] : [direction, -direction];\n\n      for (let span = 1; span < count; span += 1) {\n        for (const sign of preferred) {\n          const candidate = index + sign * span;\n\n          if (!canLoop && (candidate < 0 || candidate > count - 1)) {\n            continue;\n          }\n\n          if (!items[wrapIndex(candidate)]?.disabled) {\n            return candidate;\n          }\n        }\n      }\n\n      return index;\n    },\n    [canLoop, count, items, wrapIndex]\n  );\n\n  const stopAnimation = useCallback(() => {\n    animationRef.current?.stop();\n    animationRef.current = null;\n  }, []);\n\n  const commit = useCallback(() => {\n    const index = activeIndexRef.current;\n\n    if (committedIndexRef.current === index) {\n      return;\n    }\n\n    committedIndexRef.current = index;\n    const item = itemsRef.current[index];\n\n    if (item) {\n      onValueCommitRef.current?.(item.value, {\n        value: item.value,\n        label: item.label,\n      });\n    }\n  }, []);\n\n  const animateTo = useCallback(\n    (target: number, velocity = 0) => {\n      stopAnimation();\n\n      if (prefersReducedMotion) {\n        animationRef.current = animate(offset, target, {\n          duration: 0.16,\n          ease: \"easeOut\",\n          onComplete: commit,\n        });\n        return;\n      }\n\n      animationRef.current = animate(offset, target, {\n        type: \"spring\",\n        stiffness: 200,\n        damping: 24,\n        mass: 0.9,\n        velocity,\n        restDelta: 0.25,\n        restSpeed: 2,\n        onComplete: commit,\n      });\n    },\n    [commit, offset, prefersReducedMotion, stopAnimation]\n  );\n\n  const snapToIndex = useCallback(\n    (index: number, velocity = 0) => {\n      if (count === 0) {\n        return;\n      }\n\n      const safeIndex = clamp(wrapIndex(index), 0, count - 1);\n      let target: number;\n\n      if (canLoop) {\n        const current = Math.round(offset.get() / itemHeight);\n        let diff = (((safeIndex - wrapIndex(current)) % count) + count) % count;\n\n        if (diff > count / 2) {\n          diff -= count;\n        }\n\n        target = (current + diff) * itemHeight;\n      } else {\n        target = safeIndex * itemHeight;\n      }\n\n      animateTo(target, velocity);\n    },\n    [animateTo, canLoop, count, itemHeight, offset, wrapIndex]\n  );\n\n  const settle = useCallback(() => {\n    if (count === 0) {\n      return;\n    }\n\n    const velocity = offset.getVelocity();\n    let projected = offset.get() + velocity * FLICK_POWER;\n\n    if (!canLoop) {\n      projected = clamp(projected, 0, maxOffset);\n    }\n\n    const direction = velocity > 20 ? 1 : velocity < -20 ? -1 : 0;\n    const index = nearestEnabled(Math.round(projected / itemHeight), direction);\n    snapToIndex(wrapIndex(index), velocity);\n  }, [\n    canLoop,\n    count,\n    itemHeight,\n    maxOffset,\n    nearestEnabled,\n    offset,\n    snapToIndex,\n    wrapIndex,\n  ]);\n\n  useEffect(\n    () =>\n      offset.on(\"change\", (current) => {\n        const index = wrapIndex(Math.round(current / itemHeight));\n\n        if (index === activeIndexRef.current) {\n          return;\n        }\n\n        activeIndexRef.current = index;\n        setActiveIndex(index);\n        const item = itemsRef.current[index];\n\n        if (item) {\n          onChangeRef.current?.(item.value, {\n            value: item.value,\n            label: item.label,\n          });\n        }\n      }),\n    [itemHeight, offset, wrapIndex]\n  );\n\n  useEffect(() => {\n    if (value == null) {\n      return;\n    }\n\n    const index = items.findIndex((item) => item.value === value);\n\n    if (index < 0 || index === activeIndexRef.current || dragRef.current) {\n      return;\n    }\n\n    snapToIndex(index);\n  }, [items, snapToIndex, value]);\n\n  useEffect(() => {\n    if (count === 0 || dragRef.current || animationRef.current) {\n      return;\n    }\n\n    const maxIndex = count - 1;\n\n    if (activeIndexRef.current > maxIndex) {\n      snapToIndex(maxIndex);\n    }\n  }, [count, snapToIndex]);\n\n  useEffect(() => {\n    if (dragRef.current || animationRef.current) {\n      return;\n    }\n\n    offset.set(activeIndexRef.current * itemHeight);\n  }, [itemHeight, offset]);\n\n  useEffect(() => {\n    if (canLoop || count === 0 || dragRef.current) {\n      return;\n    }\n\n    if (offset.get() > maxOffset) {\n      snapToIndex(count - 1);\n    }\n  }, [canLoop, count, maxOffset, offset, snapToIndex]);\n\n  useEffect(() => {\n    const node = containerRef.current;\n\n    if (!node || disabled) {\n      return;\n    }\n\n    const handleWheel = (event: WheelEvent) => {\n      event.preventDefault();\n      stopAnimation();\n\n      const delta =\n        event.deltaMode === 1 ? event.deltaY * (itemHeight / 2) : event.deltaY;\n      let next = offset.get() + delta * 0.55;\n\n      if (!canLoop) {\n        next = clamp(next, 0, maxOffset);\n      }\n\n      offset.set(next);\n\n      if (wheelTimerRef.current) {\n        clearTimeout(wheelTimerRef.current);\n      }\n\n      wheelTimerRef.current = setTimeout(() => {\n        wheelTimerRef.current = null;\n        settle();\n      }, 130);\n    };\n\n    node.addEventListener(\"wheel\", handleWheel, { passive: false });\n\n    return () => {\n      node.removeEventListener(\"wheel\", handleWheel);\n    };\n  }, [canLoop, disabled, itemHeight, maxOffset, offset, settle, stopAnimation]);\n\n  useEffect(\n    () => () => {\n      animationRef.current?.stop();\n\n      if (wheelTimerRef.current) {\n        clearTimeout(wheelTimerRef.current);\n      }\n    },\n    []\n  );\n\n  const handlePointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {\n    if (disabled || event.button > 0) {\n      return;\n    }\n\n    stopAnimation();\n    event.currentTarget.setPointerCapture(event.pointerId);\n    dragRef.current = {\n      pointerId: event.pointerId,\n      startClientY: event.clientY,\n      startOffset: offset.get(),\n      moved: false,\n    };\n  };\n\n  const handlePointerMove = (event: ReactPointerEvent<HTMLDivElement>) => {\n    const drag = dragRef.current;\n\n    if (!drag || drag.pointerId !== event.pointerId) {\n      return;\n    }\n\n    const delta = drag.startClientY - event.clientY;\n\n    if (Math.abs(delta) > 3) {\n      drag.moved = true;\n    }\n\n    let next = drag.startOffset + delta;\n\n    if (!canLoop) {\n      if (next < 0) {\n        next *= RUBBER_BAND;\n      } else if (next > maxOffset) {\n        next = maxOffset + (next - maxOffset) * RUBBER_BAND;\n      }\n    }\n\n    offset.set(next);\n  };\n\n  const handlePointerEnd = (event: ReactPointerEvent<HTMLDivElement>) => {\n    const drag = dragRef.current;\n\n    if (!drag || drag.pointerId !== event.pointerId) {\n      return;\n    }\n\n    if (event.currentTarget.hasPointerCapture(event.pointerId)) {\n      event.currentTarget.releasePointerCapture(event.pointerId);\n    }\n\n    dragRef.current = null;\n\n    if (drag.moved || event.type === \"pointercancel\") {\n      settle();\n      return;\n    }\n\n    const node = containerRef.current;\n\n    if (!node) {\n      return;\n    }\n\n    const rect = node.getBoundingClientRect();\n    const tapY = event.clientY - (rect.top + rect.height / 2);\n    const steps = Math.round(Math.asin(clamp(tapY / radius, -1, 1)) / step);\n\n    if (steps === 0) {\n      commit();\n      return;\n    }\n\n    const current = Math.round(offset.get() / itemHeight);\n    const index = nearestEnabled(current + steps, steps > 0 ? 1 : -1);\n    snapToIndex(wrapIndex(index));\n  };\n\n  const moveBy = useCallback(\n    (delta: number) => {\n      const current = Math.round(offset.get() / itemHeight);\n      const index = nearestEnabled(current + delta, delta > 0 ? 1 : -1);\n      snapToIndex(wrapIndex(index));\n    },\n    [itemHeight, nearestEnabled, offset, snapToIndex, wrapIndex]\n  );\n\n  const handleKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {\n    if (disabled) {\n      return;\n    }\n\n    switch (event.key) {\n      case \"ArrowDown\":\n        event.preventDefault();\n        moveBy(1);\n        break;\n      case \"ArrowUp\":\n        event.preventDefault();\n        moveBy(-1);\n        break;\n      case \"PageDown\":\n        event.preventDefault();\n        moveBy(visibleCount);\n        break;\n      case \"PageUp\":\n        event.preventDefault();\n        moveBy(-visibleCount);\n        break;\n      case \"Home\":\n        event.preventDefault();\n        snapToIndex(nearestEnabled(0, 1));\n        break;\n      case \"End\":\n        event.preventDefault();\n        snapToIndex(nearestEnabled(count - 1, -1));\n        break;\n      default:\n        break;\n    }\n  };\n\n  return (\n    <div\n      aria-activedescendant={\n        disabled ? undefined : `${id}-option-${activeIndex}`\n      }\n      aria-disabled={disabled || undefined}\n      aria-label={ariaLabel}\n      aria-orientation=\"vertical\"\n      className={cn(\n        \"relative min-w-0 flex-1 cursor-pointer touch-none select-none overflow-hidden outline-none [-webkit-tap-highlight-color:transparent] focus-visible:ring-2 focus-visible:ring-ring/60 active:cursor-grabbing\",\n        \"[mask-image:linear-gradient(to_bottom,transparent_0%,rgba(0,0,0,0.14)_10%,rgba(0,0,0,0.4)_20%,rgba(0,0,0,0.72)_28%,black_38%,black_62%,rgba(0,0,0,0.72)_72%,rgba(0,0,0,0.4)_80%,rgba(0,0,0,0.14)_90%,transparent_100%)]\",\n        disabled && \"pointer-events-none opacity-45\",\n        className\n      )}\n      id={id}\n      onKeyDown={handleKeyDown}\n      onPointerCancel={handlePointerEnd}\n      onPointerDown={handlePointerDown}\n      onPointerMove={handlePointerMove}\n      onPointerUp={handlePointerEnd}\n      ref={containerRef}\n      role=\"listbox\"\n      style={{ height: itemHeight * visibleCount }}\n      tabIndex={disabled ? -1 : 0}\n    >\n      {lens ? (\n        <div\n          aria-hidden\n          className=\"pointer-events-none absolute inset-x-1 top-1/2 h-[var(--wheel-item-height)] -translate-y-1/2 rounded-xl bg-foreground/5\"\n          style={{ \"--wheel-item-height\": `${itemHeight}px` } as CSSProperties}\n        />\n      ) : null}\n      <motion.div\n        className=\"absolute inset-0\"\n        style={{ perspective: itemHeight * 21, transformStyle: \"preserve-3d\" }}\n      >\n        {items.map((item, index) => (\n          <WheelPickerItem\n            index={index}\n            itemHeight={itemHeight}\n            itemId={`${id}-option-${index}`}\n            key={`${item.value}-${index}`}\n            label={item.label}\n            loopSize={loopSize}\n            offset={offset}\n            optionDisabled={item.disabled}\n            selected={index === activeIndex}\n            visibleCount={visibleCount}\n          />\n        ))}\n      </motion.div>\n      {name ? (\n        <input\n          name={name}\n          type=\"hidden\"\n          value={items[activeIndex]?.value ?? \"\"}\n        />\n      ) : null}\n    </div>\n  );\n}\n\nexport function WheelPicker({\n  children,\n  className,\n  itemHeight = 44,\n  visibleCount = 5,\n  lens = true,\n  \"aria-label\": ariaLabel,\n}: WheelPickerProps) {\n  const context = useMemo(\n    () => ({ itemHeight, visibleCount, lens }),\n    [itemHeight, lens, visibleCount]\n  );\n\n  return (\n    <WheelPickerContext.Provider value={context}>\n      {/* biome-ignore lint/a11y/useSemanticElements: fieldset chrome would fight the barrel layout; a grouped div matches the listbox columns. */}\n      <div\n        aria-label={ariaLabel}\n        className={cn(\"flex w-full min-w-0 items-stretch\", className)}\n        role=\"group\"\n      >\n        {children}\n      </div>\n    </WheelPickerContext.Provider>\n  );\n}\n",
      "type": "registry:ui"
    }
  ],
  "title": "Wheel Picker",
  "description": "iOS-style wheel picker with a 3D barrel, drag and flick inertia, detent snapping, velocity-driven motion blur, a per-row emphasis morph into the selection lens, optional looping columns, and reduced-motion aware springs."
}
