import { ReactNode, useRef, useState } from "react"; import { GoToArrowLined } from "components/Icons/GoToArrowLined"; import { GalleryItemClasses } from "./shared"; export function ImageGalleryCarousel(props: { count: number; renderItem: (index: number, classes: GalleryItemClasses) => ReactNode; }) { let scrollRef = useRef(null); let [current, setCurrent] = useState(0); let count = props.count; // Derive the active slide from scroll position so native side-scrolling // (touch swipe, trackpad, shift+wheel) and the arrow buttons stay in sync. let onScroll = () => { let el = scrollRef.current; if (!el || el.clientWidth === 0) return; let index = Math.round(el.scrollLeft / el.clientWidth); setCurrent(Math.max(0, Math.min(count - 1, index))); }; let scrollToIndex = (index: number) => { let el = scrollRef.current; if (!el) return; el.scrollTo({ left: index * el.clientWidth, behavior: "smooth" }); }; return (
{Array.from({ length: count }).map((_, i) => (
{props.renderItem(i, { buttonClassName: "block", imgClassName: "w-full max-h-[70vh] object-contain", })}
))}
{current !== 0 && ( scrollToIndex(current - 1)} /> )} {current < count - 1 && ( scrollToIndex(current + 1)} /> )}
{current + 1} / {count}
); } function CarouselArrow(props: { direction: "left" | "right"; className?: string; onClick: () => void; }) { return ( ); }