"use client"; import { useEffect, useState } from "react"; import { Placeholder } from "@/components/Placeholder"; import { IconChevron, IconClose } from "./Icons"; export function ProductGallery({ images, title }: { images: string[]; title: string }) { const gallery = images.filter(Boolean); const [active, setActive] = useState(0); const [open, setOpen] = useState(false); const [origin, setOrigin] = useState("50% 50%"); const current = gallery[active] || gallery[0]; useEffect(() => { if (!open) return; const onKey = (event: KeyboardEvent) => { if (event.key === "Escape") setOpen(false); if (gallery.length < 2) return; if (event.key === "ArrowRight") setActive((index) => (index + 1) % gallery.length); if (event.key === "ArrowLeft") setActive((index) => (index - 1 + gallery.length) % gallery.length); }; const previous = document.body.style.overflow; document.body.style.overflow = "hidden"; window.addEventListener("keydown", onKey); return () => { document.body.style.overflow = previous; window.removeEventListener("keydown", onKey); }; }, [open, gallery.length]); if (!current) return null; const show = (index: number) => { setActive((index + gallery.length) % gallery.length); }; return (
{gallery.length > 1 ? (
{gallery.slice(0, 8).map((image, index) => ( ))}
) : null} {open ? (
setOpen(false)} role="dialog" aria-modal="true" aria-label={title} > {gallery.length > 1 ? ( <> ) : null} {title} event.stopPropagation()} />
) : null}
); }