"use client"; import * as React from "react"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { cn } from "@/lib/utils"; export interface ScrollContainerProps extends React.HTMLAttributes { children: React.ReactNode; showArrows?: boolean; scrollAmount?: number; arrowLeftLabel?: string; arrowRightLabel?: string; } const ScrollContainer = React.forwardRef( ( { children, className, showArrows = true, scrollAmount = 400, arrowLeftLabel = "Scroll left", arrowRightLabel = "Scroll right", ...props }, ref ) => { const scrollContainerRef = React.useRef(null); const [showLeftArrow, setShowLeftArrow] = React.useState(false); const [showRightArrow, setShowRightArrow] = React.useState(true); const handleScroll = React.useCallback(() => { if (!scrollContainerRef.current) return; const { scrollLeft, scrollWidth, clientWidth } = scrollContainerRef.current; setShowLeftArrow(scrollLeft > 0); setShowRightArrow(scrollLeft < scrollWidth - clientWidth - 10); }, []); const scroll = React.useCallback( (direction: "left" | "right") => { if (!scrollContainerRef.current) return; const scrollValue = direction === "left" ? -scrollAmount : scrollAmount; scrollContainerRef.current.scrollBy({ left: scrollValue, behavior: "smooth" }); }, [scrollAmount] ); React.useEffect(() => { const container = scrollContainerRef.current; if (!container) return; handleScroll(); const resizeObserver = new ResizeObserver(handleScroll); resizeObserver.observe(container); return () => { resizeObserver.disconnect(); }; }, [handleScroll]); return (
{showArrows && showLeftArrow && ( )}
{children}
{showArrows && showRightArrow && ( )}
); } ); ScrollContainer.displayName = "ScrollContainer"; export { ScrollContainer };