{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"3d-wheel-carousel","title":"3D Wheel Carousel","author":"CoDesign-Spa27","description":"A reusable, drag-controlled 3D card carousel with auto-rotation, keyboard navigation, blur depth, and reduced-motion support.","dependencies":["motion"],"files":[{"path":"components/ui-components/3d-wheel-carousel.tsx","content":"\"use client\";\n\nimport {\n  animate,\n  type MotionValue,\n  motion,\n  useAnimationFrame,\n  useMotionValue,\n  useMotionValueEvent,\n  useReducedMotion,\n  useSpring,\n  useTransform,\n  useVelocity,\n} from \"motion/react\";\nimport {\n  type ComponentPropsWithoutRef,\n  type CSSProperties,\n  type KeyboardEvent,\n  type ReactNode,\n  useEffect,\n  useRef,\n  useState,\n} from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport type WheelCarouselItem = {\n  id: string;\n  content: ReactNode;\n  label?: string;\n};\n\nexport type WheelCarouselProps = Omit<ComponentPropsWithoutRef<\"div\">, \"children\"> & {\n  items: readonly WheelCarouselItem[];\n  autoRotateSpeed?: number;\n  cardClassName?: string;\n  cardHeight?: number;\n  cardWidth?: number;\n  dragSensitivity?: number;\n  direction?: \"left\" | \"right\";\n  initialIndex?: number;\n  maxBlur?: number;\n  maxMotionBlur?: number;\n  /** Scale applied below the `sm` breakpoint. Desktop always renders at full size. */\n  mobileScale?: number;\n  motionBlurExitDelay?: number;\n  motionBlurExitDuration?: number;\n  onActiveIndexChange?: (index: number) => void;\n  pauseOnHover?: boolean;\n  paused?: boolean;\n  radius?: number;\n  spring?: {\n    damping?: number;\n    mass?: number;\n    stiffness?: number;\n  };\n};\n\nconst wrap = (value: number, length: number) => ((value % length) + length) % length;\n\nexport function WheelCarousel({\n  \"aria-label\": ariaLabel = \"3D carousel\",\n  autoRotateSpeed = 10,\n  cardClassName,\n  cardHeight = 380,\n  cardWidth = 280,\n  className,\n  direction = \"left\",\n  dragSensitivity = 0.35,\n  initialIndex = 0,\n  items,\n  maxBlur = 12,\n  maxMotionBlur = 40,\n  mobileScale = 0.68,\n  motionBlurExitDelay = 0.7,\n  motionBlurExitDuration = 1.2,\n  onActiveIndexChange,\n  pauseOnHover = true,\n  paused = false,\n  radius = 560,\n  spring = {\n    damping: 90,\n    mass: 1,\n    stiffness: 200,\n  },\n  ...props\n}: WheelCarouselProps) {\n  const itemCount = items.length;\n  const safeItemCount = Math.max(itemCount, 1);\n  const angleStep = itemCount > 0 ? 360 / itemCount : 0;\n  const reduceMotion = useReducedMotion();\n  const rotation = useMotionValue(-wrap(initialIndex, safeItemCount) * angleStep);\n  const smoothRotation = useSpring(rotation, spring);\n  const rotationVelocity = useVelocity(smoothRotation);\n  const smoothVelocity = useSpring(rotationVelocity, {\n    stiffness: 200,\n    damping: 30,\n  });\n  const dragging = useRef(false);\n  const hovered = useRef(false);\n  const travelDirection = useRef(direction === \"left\" ? -1 : 1);\n  const lastActiveIndex = useRef(wrap(initialIndex, safeItemCount));\n  const [isDragging, setIsDragging] = useState(false);\n\n  useEffect(() => {\n    travelDirection.current = direction === \"left\" ? -1 : 1;\n  }, [direction]);\n\n  useAnimationFrame((_, delta) => {\n    if (\n      itemCount < 2 ||\n      paused ||\n      reduceMotion ||\n      dragging.current ||\n      (pauseOnHover && hovered.current)\n    ) {\n      return;\n    }\n    const seconds = delta / 1000;\n    const rotationChange = autoRotateSpeed * travelDirection.current * seconds;\n    rotation.set(rotation.get() + rotationChange);\n  });\n\n  useMotionValueEvent(smoothRotation, \"change\", (latest) => {\n    if (itemCount === 0) {\n      return;\n    }\n    const next = wrap(Math.round(-latest / angleStep), itemCount);\n    if (next !== lastActiveIndex.current) {\n      lastActiveIndex.current = next;\n      onActiveIndexChange?.(next);\n    }\n  });\n\n  const rotateBy = (steps: number) => {\n    if (itemCount < 2) {\n      return;\n    }\n    rotation.set(rotation.get() - steps * angleStep);\n  };\n\n  const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n    if (event.key === \"ArrowRight\") {\n      event.preventDefault();\n      rotateBy(1);\n    } else if (event.key === \"ArrowLeft\") {\n      event.preventDefault();\n      rotateBy(-1);\n    } else if (event.key === \"Home\") {\n      event.preventDefault();\n      rotation.set(0);\n    } else if (event.key === \"End\") {\n      event.preventDefault();\n      rotation.set(-(itemCount - 1) * angleStep);\n    }\n  };\n\n  if (itemCount === 0) {\n    return null;\n  }\n\n  return (\n    <section\n      aria-label={ariaLabel}\n      aria-roledescription=\"carousel\"\n      className={cn(\n        \"relative flex min-h-96 w-full items-center justify-center overflow-hidden outline-none perspective-[5000px] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 sm:min-h-[32rem]\",\n        className,\n      )}\n      onBlur={() => {\n        hovered.current = false;\n      }}\n      onFocus={() => {\n        hovered.current = true;\n      }}\n      onKeyDown={handleKeyDown}\n      onMouseEnter={() => {\n        hovered.current = true;\n      }}\n      onMouseLeave={() => {\n        hovered.current = false;\n      }}\n      // The carousel surface receives focus for its arrow-key controls.\n      // biome-ignore lint/a11y/noNoninteractiveTabindex: keyboard-operated carousel region\n      tabIndex={0}\n      {...props}\n    >\n      <motion.div\n        className={cn(\n          \"relative origin-center touch-none select-none [scale:var(--wheel-mobile-scale)] sm:[scale:1]\",\n          isDragging ? \"cursor-grabbing\" : \"cursor-grab\",\n        )}\n        onPan={(_event, info) => {\n          const delta = info.delta.x * dragSensitivity;\n          if (delta > 0) {\n            travelDirection.current = 1;\n          } else if (delta < 0) {\n            travelDirection.current = -1;\n          }\n          rotation.set(rotation.get() + delta);\n        }}\n        onPanStart={() => {\n          dragging.current = true;\n          setIsDragging(true);\n        }}\n        onPanEnd={() => {\n          dragging.current = false;\n          setIsDragging(false);\n        }}\n        style={\n          {\n            height: cardHeight,\n            rotateY: smoothRotation,\n            transformStyle: \"preserve-3d\",\n            width: cardWidth,\n            \"--wheel-mobile-scale\": mobileScale,\n          } as CSSProperties\n        }\n      >\n        {items.map((item, index) => (\n          <WheelCarouselCard\n            angle={angleStep * index}\n            cardClassName={cardClassName}\n            index={index}\n            isDragging={isDragging}\n            item={item}\n            itemCount={itemCount}\n            key={item.id}\n            maxBlur={maxBlur}\n            maxMotionBlur={maxMotionBlur}\n            motionBlurExitDelay={motionBlurExitDelay}\n            motionBlurExitDuration={motionBlurExitDuration}\n            radius={radius}\n            rotation={smoothRotation}\n            velocity={smoothVelocity}\n          />\n        ))}\n      </motion.div>\n\n      <p className=\"sr-only\">Use the left and right arrow keys to move between slides.</p>\n    </section>\n  );\n}\n\ntype WheelCarouselCardProps = {\n  angle: number;\n  cardClassName?: string;\n  index: number;\n  isDragging: boolean;\n  item: WheelCarouselItem;\n  itemCount: number;\n  maxBlur: number;\n  maxMotionBlur: number;\n  motionBlurExitDelay: number;\n  motionBlurExitDuration: number;\n  radius: number;\n  rotation: MotionValue<number>;\n  velocity: MotionValue<number>;\n};\n\nfunction WheelCarouselCard({\n  angle,\n  cardClassName,\n  index,\n  isDragging,\n  item,\n  itemCount,\n  maxBlur,\n  maxMotionBlur,\n  motionBlurExitDelay,\n  motionBlurExitDuration,\n  radius,\n  rotation,\n  velocity,\n}: WheelCarouselCardProps) {\n  const angleStep = 360 / itemCount;\n  const distance = useTransform(rotation, (current) => {\n    const frontIndex = wrap(Math.round(-current / angleStep), itemCount);\n    const direct = Math.abs(index - frontIndex);\n    return Math.min(direct, itemCount - direct);\n  });\n\n  const positionBlur = useTransform(distance, (value) => {\n    if (value <= 1) {\n      return 0;\n    }\n    const furthest = Math.max(Math.floor(itemCount / 2), 1);\n    const availableSteps = Math.max(furthest - 1, 1);\n    const progress = (value - 1) / availableSteps;\n    return progress * maxBlur;\n  });\n\n  const motionBlur = useMotionValue(0);\n\n  useMotionValueEvent(velocity, \"change\", (currentVelocity) => {\n    if (!isDragging) {\n      return;\n    }\n    const speed = Math.abs(currentVelocity);\n    const blur = Math.min(speed / 70, maxMotionBlur);\n    motionBlur.set(blur);\n  });\n\n  useEffect(() => {\n    if (isDragging) {\n      return;\n    }\n    const controls = animate(motionBlur, 0, {\n      delay: motionBlurExitDelay,\n      duration: motionBlurExitDuration,\n      ease: [0.23, 1, 0.32, 1],\n    });\n    return () => {\n      controls.stop();\n    };\n  }, [isDragging, motionBlur, motionBlurExitDelay, motionBlurExitDuration]);\n\n  const totalBlur = useTransform(\n    [positionBlur, motionBlur],\n    ([depthBlur, velocityBlur]) => Number(depthBlur) + Number(velocityBlur),\n  );\n\n  const filter = useTransform(totalBlur, (blur) => `blur(${blur}px)`);\n\n  return (\n    <motion.div\n      aria-label={item.label ?? `Slide ${index + 1} of ${itemCount}`}\n      aria-roledescription=\"slide\"\n      className={cn(\n        \"absolute inset-0 overflow-hidden rounded-[var(--wheel-card-radius,1rem)] \",\n        cardClassName,\n      )}\n      role=\"group\"\n      style={{\n        filter,\n        transform: `\n          rotateY(${angle}deg)\n          translateZ(${radius}px)\n        `,\n      }}\n    >\n      {item.content}\n    </motion.div>\n  );\n}\n\nexport default WheelCarousel;\n","type":"registry:ui"}],"categories":["carousel","media","animation"],"type":"registry:ui"}