"use client"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { useCallback, useEffect, useRef, useState } from "react"; import { ProductCard } from "@/components/product/product-card"; import type { Product } from "@/lib/headless/types"; export function ProductCarousel({ products }: { products: Product[] }) { const scroller = useRef(null); const [canPrevious, setCanPrevious] = useState(false); const [canNext, setCanNext] = useState(products.length > 1); const updateControls = useCallback(() => { const node = scroller.current; if (!node) return; setCanPrevious(node.scrollLeft > 8); setCanNext(node.scrollLeft + node.clientWidth < node.scrollWidth - 8); }, []); useEffect(() => { const node = scroller.current; if (!node) return; updateControls(); node.addEventListener("scroll", updateControls, { passive: true }); const observer = new ResizeObserver(updateControls); observer.observe(node); return () => { node.removeEventListener("scroll", updateControls); observer.disconnect(); }; }, [updateControls]); const move = (direction: -1 | 1) => { const node = scroller.current; if (!node) return; node.scrollBy({ left: direction * Math.max(280, node.clientWidth * 0.82), behavior: "smooth" }); }; return (
{ if (event.key === "ArrowLeft") move(-1); if (event.key === "ArrowRight") move(1); }} className="flex snap-x snap-mandatory gap-5 overflow-x-auto scroll-smooth pb-5 pr-5 outline-none [overscroll-behavior-inline:contain] [scrollbar-width:none]"> {products.map((product, index) =>
)}
); }