"use client"; import Image from "next/image"; import { ChevronLeft, ChevronRight, Expand, X } from "lucide-react"; import { useEffect, useState, type MouseEvent } from "react"; export function ProductGallery({ images, alt, }: { images: string[]; alt: string; }) { const [active, setActive] = useState(0); const [lightbox, setLightbox] = useState(false); const [origin, setOrigin] = useState("50% 50%"); const previous = () => setActive((value) => (value - 1 + images.length) % images.length); const next = () => setActive((value) => (value + 1) % images.length); useEffect(() => { if (!lightbox) return; const onKey = (event: KeyboardEvent) => { if (event.key === "Escape") setLightbox(false); if (event.key === "ArrowLeft") next(); if (event.key === "ArrowRight") previous(); }; document.body.style.overflow = "hidden"; window.addEventListener("keydown", onKey); return () => { document.body.style.overflow = ""; window.removeEventListener("keydown", onKey); }; }, [lightbox, images.length]); function updateZoomOrigin(event: MouseEvent) { const bounds = event.currentTarget.getBoundingClientRect(); const x = ((event.clientX - bounds.left) / bounds.width) * 100; const y = ((event.clientY - bounds.top) / bounds.height) * 100; setOrigin(`${x}% ${y}%`); } if (images.length === 0) return null; return ( <>
{images.length > 1 && (
{images.map((image, index) => ( ))}
)}
{lightbox && (
{images.length > 1 && ( <> )}
{`${alt}
{active + 1} / {images.length}
)} ); }