"use client"; import { useEffect, useMemo, useRef, useState } from 'react'; import { motion, useMotionValue, useTransform } from 'motion/react'; // replace icons with your own if needed import { FiCircle, FiCode, FiFileText, FiLayers, FiLayout } from 'react-icons/fi'; import './Carousel.css'; const DEFAULT_ITEMS = [ { title: 'Text Animations', description: 'Cool text animations for your projects.', id: 1, icon: }, { title: 'Animations', description: 'Smooth animations for your projects.', id: 2, icon: }, { title: 'Components', description: 'Reusable components for your projects.', id: 3, icon: }, { title: 'Backgrounds', description: 'Beautiful backgrounds and patterns for your projects.', id: 4, icon: }, { title: 'Common UI', description: 'Common UI components are coming soon!', id: 5, icon: } ]; const DRAG_BUFFER = 0; const VELOCITY_THRESHOLD = 500; const GAP = 16; const SPRING_OPTIONS = { type: 'spring', stiffness: 300, damping: 30 }; function CarouselItem({ item, index, itemWidth, round, trackItemOffset, x, transition }) { const range = [-(index + 1) * trackItemOffset, -index * trackItemOffset, -(index - 1) * trackItemOffset]; const outputRange = [90, 0, -90]; const rotateY = useTransform(x, range, outputRange, { clamp: false }); return (
{item.icon}
{item.title}

{item.description}

); } export default function Carousel({ items = DEFAULT_ITEMS, baseWidth = 300, autoplay = false, autoplayDelay = 3000, pauseOnHover = false, loop = false, round = false }) { const containerPadding = 16; const itemWidth = baseWidth - containerPadding * 2; const trackItemOffset = itemWidth + GAP; const itemsForRender = useMemo(() => { if (!loop) return items; if (items.length === 0) return []; return [items[items.length - 1], ...items, items[0]]; }, [items, loop]); const [position, setPosition] = useState(loop ? 1 : 0); const x = useMotionValue(0); const [isHovered, setIsHovered] = useState(false); const [isJumping, setIsJumping] = useState(false); const [isAnimating, setIsAnimating] = useState(false); const containerRef = useRef(null); useEffect(() => { if (pauseOnHover && containerRef.current) { const container = containerRef.current; const handleMouseEnter = () => setIsHovered(true); const handleMouseLeave = () => setIsHovered(false); container.addEventListener('mouseenter', handleMouseEnter); container.addEventListener('mouseleave', handleMouseLeave); return () => { container.removeEventListener('mouseenter', handleMouseEnter); container.removeEventListener('mouseleave', handleMouseLeave); }; } }, [pauseOnHover]); useEffect(() => { if (!autoplay || itemsForRender.length <= 1) return undefined; if (pauseOnHover && isHovered) return undefined; const timer = setInterval(() => { setPosition(prev => Math.min(prev + 1, itemsForRender.length - 1)); }, autoplayDelay); return () => clearInterval(timer); }, [autoplay, autoplayDelay, isHovered, pauseOnHover, itemsForRender.length]); useEffect(() => { const startingPosition = loop ? 1 : 0; setPosition(startingPosition); x.set(-startingPosition * trackItemOffset); }, [items.length, loop, trackItemOffset, x]); useEffect(() => { if (!loop && position > itemsForRender.length - 1) { setPosition(Math.max(0, itemsForRender.length - 1)); } }, [itemsForRender.length, loop, position]); const effectiveTransition = isJumping ? { duration: 0 } : SPRING_OPTIONS; const handleAnimationStart = () => { setIsAnimating(true); }; const handleAnimationComplete = () => { if (!loop || itemsForRender.length <= 1) { setIsAnimating(false); return; } const lastCloneIndex = itemsForRender.length - 1; if (position === lastCloneIndex) { setIsJumping(true); const target = 1; setPosition(target); x.set(-target * trackItemOffset); requestAnimationFrame(() => { setIsJumping(false); setIsAnimating(false); }); return; } if (position === 0) { setIsJumping(true); const target = items.length; setPosition(target); x.set(-target * trackItemOffset); requestAnimationFrame(() => { setIsJumping(false); setIsAnimating(false); }); return; } setIsAnimating(false); }; const handleDragEnd = (_, info) => { const { offset, velocity } = info; const direction = offset.x < -DRAG_BUFFER || velocity.x < -VELOCITY_THRESHOLD ? 1 : offset.x > DRAG_BUFFER || velocity.x > VELOCITY_THRESHOLD ? -1 : 0; if (direction === 0) return; setPosition(prev => { const next = prev + direction; const max = itemsForRender.length - 1; return Math.max(0, Math.min(next, max)); }); }; const dragProps = loop ? {} : { dragConstraints: { left: -trackItemOffset * Math.max(itemsForRender.length - 1, 0), right: 0 } }; const activeIndex = items.length === 0 ? 0 : loop ? (position - 1 + items.length) % items.length : Math.min(position, items.length - 1); return (
{itemsForRender.map((item, index) => ( ))}
{items.map((_, index) => ( setPosition(loop ? index + 1 : index)} transition={{ duration: 0.15 }} /> ))}
); }