LLC/src/components/Carousel/ProductCarousel.jsx

70 lines
2.5 KiB
React
Raw Normal View History

2025-02-19 15:04:53 +03:30
"use client"
2025-02-19 16:11:53 +03:30
2025-02-19 15:04:53 +03:30
import useEmblaCarousel from "embla-carousel-react"
import { ChevronLeft, ChevronRight } from "lucide-react"
2025-02-19 16:11:53 +03:30
import { useCallback } from "react"
2025-02-19 15:04:53 +03:30
import CardNormal from "../Cards/CardNormal/page"
2025-02-19 16:11:53 +03:30
import Autoplay from "embla-carousel-autoplay"
export default function ProductCarousel({ title, subtitle, products }) {
const [emblaRef, emblaApi] = useEmblaCarousel(
{ loop: true, align: "start" },
[
Autoplay({ delay: 3000, stopOnInteraction: true }),
]
)
2025-02-19 15:04:53 +03:30
const scrollPrev = useCallback(() => {
2025-02-19 16:11:53 +03:30
if (emblaApi) emblaApi.scrollPrev()
2025-02-19 15:04:53 +03:30
}, [emblaApi])
2025-02-19 16:11:53 +03:30
2025-02-19 15:04:53 +03:30
const scrollNext = useCallback(() => {
2025-02-19 16:11:53 +03:30
if (emblaApi) emblaApi.scrollNext()
2025-02-19 15:04:53 +03:30
}, [emblaApi])
2025-02-19 16:11:53 +03:30
2025-02-19 15:04:53 +03:30
return (
2025-02-19 16:11:53 +03:30
<section className="py-12 px-4 md:px-6 lg:px-8">
<div className="max-w-7xl mx-auto">
<h2 className="text-3xl font-bold text-center mb-2">{title}</h2>
<p className="text-xl text-center text-gray-600 mb-8">{subtitle}</p>
<div className="relative">
<div className="overflow-hidden" ref={emblaRef}>
<div className="flex">
{products.map((product) => (
<div
key={product.id}
className="flex-[0_0_100%] min-w-0 sm:flex-[0_0_50%] md:flex-[0_0_33.33%] lg:flex-[0_0_25%] pl-4"
>
<CardNormal product={product} priority={true} />
</div>
))}
</div>
</div>
<button
variant="outline"
size="icon"
className="absolute top-1/2 left-4 transform -translate-y-1/2 bg-gray-200 rounded-full size-4"
onClick={scrollPrev}
>
<ChevronLeft className="h-4 w-4" />
</button>
<button
variant="outline"
size="icon"
className="absolute top-1/2 right-4 transform -translate-y-1/2 bg-gray-200 rounded-full size-4"
onClick={scrollNext}
>
<ChevronRight className="h-4 w-4" />
</button>
</div>
2025-02-19 15:04:53 +03:30
</div>
2025-02-19 16:11:53 +03:30
</section>
2025-02-19 15:04:53 +03:30
)
2025-02-19 16:11:53 +03:30
}