import React, { useEffect, useState, useRef } from "react"; import { ImageIcon, X, ChevronLeft, ChevronRight, Play, Pause, ZoomIn, ZoomOut } from "lucide-react"; type ImageItem = { src?: string; alt?: string }; type LightboxProps = { images: ImageItem[]; initialIndex?: number; onClose?: () => void; }; const Lightbox: React.FC = ({ images, initialIndex = 0, onClose }) => { const [index, setIndex] = useState(initialIndex); const [scale, setScale] = useState(1); const [playing, setPlaying] = useState(false); const timerRef = useRef(null); useEffect(() => { setIndex(initialIndex); }, [initialIndex]); useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") handleClose(); if (e.key === "ArrowRight") next(); if (e.key === "ArrowLeft") prev(); if (e.key === "+") zoomIn(); if (e.key === "-") zoomOut(); if (e.key === " ") setPlaying((p) => !p); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [index]); useEffect(() => { if (playing) { timerRef.current = window.setInterval(() => setIndex((i) => (i + 1) % images.length), 3500); } else if (timerRef.current) { window.clearInterval(timerRef.current); timerRef.current = null; } return () => { if (timerRef.current) window.clearInterval(timerRef.current); }; }, [playing, images.length]); const prev = () => setIndex((i) => (i - 1 + images.length) % images.length); const next = () => setIndex((i) => (i + 1) % images.length); const handleClose = () => { setPlaying(false); setScale(1); onClose?.(); }; const zoomIn = () => setScale((s) => Math.min(4, +(s + 0.25).toFixed(2))); const zoomOut = () => setScale((s) => Math.max(0.5, +(s - 0.25).toFixed(2))); if (!images || images.length === 0) return null; const img = images[index]; return (
{img?.src ? ( {img.alt ) : (
{img?.alt}
)}
{index + 1} / {images.length}
); }; export default Lightbox;