LLC/src/components/Carousel/ProductCarousel.jsx

83 lines
3.1 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-22 19:14:53 +03:30
import CardNormal from "../Cards/CardNormal"
2025-02-19 16:11:53 +03:30
import Autoplay from "embla-carousel-autoplay"
2025-02-22 19:14:53 +03:30
import Link from "next/link"
import { useLocale, useTranslations } from "next-intl"
2025-02-19 16:11:53 +03:30
2025-02-22 19:14:53 +03:30
export default function ProductCarousel({ title, subtitle, products, showMoreLink }) {
const t = useTranslations("Utils")
const locale = useLocale()
2025-02-19 16:11:53 +03:30
const [emblaRef, emblaApi] = useEmblaCarousel(
2025-02-22 19:14:53 +03:30
{ loop: true, align: "start", direction: locale === "en" ? "ltr" : "rtl" },
2025-02-19 16:11:53 +03:30
[
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-22 22:59:55 +03:30
<section className="py-12 max-w-screen-xl mx-auto h-fit" >
2025-02-19 16:11:53 +03:30
2025-02-22 19:14:53 +03:30
<div className="flex sm:justify-between flex-col sm:flex-row pb-4 items-center">
<div className="flex flex-col gap-2">
<h2 className="text-3xl font-bold text-center mb-2">{title}</h2>
{
subtitle &&
<p className="text-xl text-center text-gray-600 mb-8">{subtitle}</p>
}
</div>
<Link href={showMoreLink} className="text-sm text-blue-600 underline">
{t("showMoreLink")}
</Link>
</div>
2025-02-22 22:59:55 +03:30
<div className="relative h-fit">
2025-02-22 19:14:53 +03:30
<div className="overflow-hidden" ref={emblaRef} >
<div className="flex">
{products.map((product) => (
<div
key={product.id}
2025-02-22 22:59:55 +03:30
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 my-2"
2025-02-22 19:14:53 +03:30
>
<CardNormal product={product} priority={true} />
</div>
))}
2025-02-19 16:11:53 +03:30
</div>
</div>
2025-02-22 19:14:53 +03:30
<button
variant="outline"
size="icon"
2025-02-22 22:59:55 +03:30
className="absolute top-1/2 left-4 transform -translate-y-1/2 bg-gray-200 rounded-full size-10 border border-white flex flex-col items-center justify-center"
2025-02-22 19:14:53 +03:30
onClick={scrollPrev}
>
2025-02-22 22:59:55 +03:30
<ChevronLeft className="size-10" />
2025-02-22 19:14:53 +03:30
</button>
<button
variant="outline"
size="icon"
2025-02-22 22:59:55 +03:30
className="absolute top-1/2 right-4 transform -translate-y-1/2 bg-gray-200 rounded-full size-10 border border-white flex flex-col items-center justify-center"
2025-02-22 19:14:53 +03:30
onClick={scrollNext}
>
2025-02-22 22:59:55 +03:30
<ChevronRight className="size-10" />
2025-02-22 19:14:53 +03:30
</button>
2025-02-19 15:04:53 +03:30
</div>
2025-02-22 19:14:53 +03:30
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
}