"use client"; import { useState, useEffect, useCallback } from "react"; interface ImageLightboxProps { images: { src: string; alt: string }[]; initialIndex: number; onClose: () => void; } export default function ImageLightbox({ images, initialIndex, onClose }: ImageLightboxProps) { const [currentIndex, setCurrentIndex] = useState( Math.max(0, Math.min(initialIndex, images.length - 1)), ); const goTo = useCallback( (i: number) => { setCurrentIndex(Math.max(0, Math.min(i, images.length - 1))); }, [images.length], ); // Close on Escape key, navigate with arrows useEffect(() => { const handleKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); if (e.key === "ArrowLeft") goTo(currentIndex - 1); if (e.key === "ArrowRight") goTo(currentIndex + 1); }; window.addEventListener("keydown", handleKey); return () => window.removeEventListener("keydown", handleKey); }, [onClose, currentIndex, goTo]); // Prevent body scroll while open useEffect(() => { document.body.style.overflow = "hidden"; return () => { document.body.style.overflow = ""; }; }, []); if (!images.length) return null; const current = images[currentIndex]; return (
{/* Faded backdrop */} ); }