import React, { useCallback, useEffect } from 'react' import { AnimatePresence, motion } from 'framer-motion' import { MdClose, MdChevronLeft, MdChevronRight } from 'react-icons/md' type Props = { images: string[] index: number | null onClose: () => void onChange: (index: number) => void } export function Lightbox({ images, index, onClose, onChange }: Props) { const isOpen = index !== null const next = useCallback(() => { if (index === null) return onChange((index + 1) % images.length) }, [index, images.length, onChange]) const prev = useCallback(() => { if (index === null) return onChange((index - 1 + images.length) % images.length) }, [index, images.length, onChange]) useEffect(() => { if (!isOpen) return const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose() if (e.key === 'ArrowRight') next() if (e.key === 'ArrowLeft') prev() } window.addEventListener('keydown', onKey) document.body.style.overflow = 'hidden' return () => { window.removeEventListener('keydown', onKey) document.body.style.overflow = '' } }, [isOpen, onClose, next, prev]) return ( {isOpen && ( {/* Solid Brand Background */}