import { useCallback, useEffect, useRef, useState, type ReactNode } from "react"; import { IconChevronLeft, IconChevronRight } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import classes from "./card-carousel.module.css"; type Props = { children: ReactNode; ariaLabel?: string; }; export default function CardCarousel({ children, ariaLabel }: Props) { const { t } = useTranslation(); const trackRef = useRef(null); const [canScrollLeft, setCanScrollLeft] = useState(false); const [canScrollRight, setCanScrollRight] = useState(false); const updateScrollState = useCallback(() => { const el = trackRef.current; if (!el) return; const maxScroll = el.scrollWidth - el.clientWidth; setCanScrollLeft(el.scrollLeft > 1); setCanScrollRight(el.scrollLeft < maxScroll - 1); }, []); useEffect(() => { updateScrollState(); const el = trackRef.current; if (!el) return; const observer = new ResizeObserver(updateScrollState); observer.observe(el); for (const child of Array.from(el.children)) { observer.observe(child); } return () => observer.disconnect(); }, [updateScrollState, children]); const scrollBy = (direction: 1 | -1) => { const el = trackRef.current; if (!el) return; el.scrollBy({ left: direction * el.clientWidth * 0.85, behavior: "smooth" }); }; return (
{children}
); }