"use client"; import { useState, useEffect, useRef } from "react"; import { motion, AnimatePresence } from "framer-motion"; import Image from "next/image"; const banners = [ { id: 1, src: "/banners/b1.svg", alt: "Mozimo Chocolate Banner 1" }, { id: 2, src: "/banners/b3.svg", alt: "Mozimo Chocolate Banner 2" }, { id: 3, src: "/banners/b2.svg", alt: "Mozimo Chocolate Banner 3" }, { id: 4, src: "/banners/b4.svg", alt: "Mozimo Chocolate Banner 4" }, ]; export default function BannerSlider() { const [currentIndex, setCurrentIndex] = useState(0); const [direction, setDirection] = useState(0); const [showArrows, setShowArrows] = useState(false); const [isHovered, setIsHovered] = useState(false); const containerRef = useRef(null); // Auto-advance slider useEffect(() => { const timer = setInterval(() => { if (!isHovered) { setDirection(1); setCurrentIndex((prevIndex) => (prevIndex + 1) % banners.length); } }, 4000); return () => clearInterval(timer); }, [isHovered]); const goToSlide = (index: number) => { setDirection(index > currentIndex ? 1 : -1); setCurrentIndex(index); }; const goToPrevious = () => { setDirection(-1); setCurrentIndex((prevIndex) => prevIndex === 0 ? banners.length - 1 : prevIndex - 1 ); }; const goToNext = () => { setDirection(1); setCurrentIndex((prevIndex) => (prevIndex + 1) % banners.length); }; const slideVariants = { enter: (direction: number) => ({ x: direction > 0 ? "100%" : "-100%", opacity: 1, }), center: { x: 0, opacity: 1, }, exit: (direction: number) => ({ x: direction < 0 ? "100%" : "-100%", opacity: 1, }), }; const swipeConfidenceThreshold = 10000; const swipePower = (offset: number, velocity: number) => { return Math.abs(offset) * velocity; }; const paginate = (newDirection: number) => { if (newDirection > 0) { goToNext(); } else { goToPrevious(); } }; return (
{ setShowArrows(true); setIsHovered(true); }} onMouseLeave={() => { setShowArrows(false); setIsHovered(false); }} > {/* Premium Background Pattern */}
{/* Banner Images with Premium Animations */} { const swipe = swipePower(offset.x, velocity.x); if (swipe < -swipeConfidenceThreshold) { paginate(1); } else if (swipe > swipeConfidenceThreshold) { paginate(-1); } }} className="absolute inset-0" > {banners[currentIndex].alt} {/* Premium overlay with gradient */}
{/* Premium Shop Now Button */} {currentIndex === 0 && (
Shop Now
)} {/* Premium Navigation Arrows - Hidden on mobile */} {showArrows && ( <> )} {/* Mobile Navigation Arrows */}
{/* Premium Dots Indicator */}
{banners.map((_, index) => ( goToSlide(index)} className={`w-2 h-2 md:w-3 md:h-3 rounded-full transition-all duration-300 ${ index === currentIndex ? "bg-white scale-110 shadow-glow" : "bg-white/50 hover:bg-white/70 hover:scale-110" }`} whileHover={{ scale: 1.2 }} whileTap={{ scale: 0.9 }} aria-label={`Go to banner ${index + 1}`} /> ))}
{/* Premium Progress Bar */}
); }