"use client"; import { useRef, useState, useEffect, ReactNode } from "react"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { cn } from "@/utils/cn"; import { useIsMobile, useIsTablet } from "@/hooks/useMediaQuery"; interface HorizontalCarouselProps { children: ReactNode; className?: string; itemClassName?: string; showArrows?: boolean; gap?: "sm" | "md" | "lg"; } export function HorizontalCarousel({ children, className, itemClassName, showArrows = true, gap = "md", }: HorizontalCarouselProps) { const scrollRef = useRef(null); const [canScrollLeft, setCanScrollLeft] = useState(false); const [canScrollRight, setCanScrollRight] = useState(false); const isMobile = useIsMobile(); const isTablet = useIsTablet(); const isMobileOrTablet = isMobile || isTablet; const gapClass = { sm: "gap-2", md: "gap-3", lg: "gap-4", }[gap]; const checkScroll = () => { const el = scrollRef.current; if (!el) return; setCanScrollLeft(el.scrollLeft > 0); setCanScrollRight(el.scrollLeft < el.scrollWidth - el.clientWidth - 1); }; useEffect(() => { checkScroll(); const el = scrollRef.current; if (el) { el.addEventListener("scroll", checkScroll); window.addEventListener("resize", checkScroll); } return () => { if (el) { el.removeEventListener("scroll", checkScroll); } window.removeEventListener("resize", checkScroll); }; }, [children]); const scroll = (direction: "left" | "right") => { const el = scrollRef.current; if (!el) return; // Scroll by approximately 3 items worth const scrollAmount = el.clientWidth * 0.8; el.scrollBy({ left: direction === "left" ? -scrollAmount : scrollAmount, behavior: "smooth", }); }; return (
{/* Left arrow */} {showArrows && !isMobileOrTablet && canScrollLeft && ( )} {/* Scrollable container */}
{children}
{/* Right arrow */} {showArrows && !isMobileOrTablet && canScrollRight && ( )} {/* Fade edges */} {canScrollLeft && !isMobileOrTablet && (
)} {canScrollRight && !isMobileOrTablet && (
)}
); } // Wrapper for carousel items with consistent sizing interface CarouselItemProps { children: ReactNode; className?: string; } export function CarouselItem({ children, className }: CarouselItemProps) { return (
{children}
); }