'use client'; import Image from 'next/image'; import { useEffect, useState } from 'react'; // next/image used for the inline thumb only; the modal uses a plain // so the rendered size matches the photo's natural aspect. // A tiny click-to-enlarge wrapper around a Next/Image. The thumb sits // inline at whatever size the parent gives it; clicking opens a // fixed-position modal with a darkened backdrop and the same image // rendered at a larger size. Escape or backdrop click closes. type LightboxProps = { src: string; alt: string; /** Sizes attribute for the inline thumb (passed straight through). */ sizes: string; }; export function Lightbox({ src, alt, sizes }: LightboxProps) { const [open, setOpen] = useState(false); // Close on Escape + lock body scroll while open. useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); }; document.addEventListener('keydown', onKey); const prevOverflow = document.body.style.overflow; document.body.style.overflow = 'hidden'; return () => { document.removeEventListener('keydown', onKey); document.body.style.overflow = prevOverflow; }; }, [open]); return ( <> {open && (
{ const t = e.target as HTMLElement; if (t.tagName !== 'IMG' && !t.closest('.lightbox-close')) { setOpen(false); } }} > {/* Plain instead of next/image so the rendered element sizes to the image's natural aspect — no letterbox padding above/below for portrait or square photos. max-w/max-h cap it to the viewport. */} {/* eslint-disable-next-line @next/next/no-img-element */} {alt}
)} ); }