Lumen & Gesso / website · lumengesso.com / app / components / Lightbox.tsx
| 1 | 'use client'; |
| 2 | |
| 3 | import Image from 'next/image'; |
| 4 | import { useEffect, useState } from 'react'; |
| 5 | // next/image used for the inline thumb only; the modal uses a plain |
| 6 | // <img> so the rendered size matches the photo's natural aspect. |
| 7 | |
| 8 | // A tiny click-to-enlarge wrapper around a Next/Image. The thumb sits |
| 9 | // inline at whatever size the parent gives it; clicking opens a |
| 10 | // fixed-position modal with a darkened backdrop and the same image |
| 11 | // rendered at a larger size. Escape or backdrop click closes. |
| 12 | |
| 13 | type LightboxProps = { |
| 14 | src: string; |
| 15 | alt: string; |
| 16 | /** Sizes attribute for the inline thumb (passed straight through). */ |
| 17 | sizes: string; |
| 18 | }; |
| 19 | |
| 20 | export function Lightbox({ src, alt, sizes }: LightboxProps) { |
| 21 | const [open, setOpen] = useState(false); |
| 22 | |
| 23 | // Close on Escape + lock body scroll while open. |
| 24 | useEffect(() => { |
| 25 | if (!open) return; |
| 26 | const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); }; |
| 27 | document.addEventListener('keydown', onKey); |
| 28 | const prevOverflow = document.body.style.overflow; |
| 29 | document.body.style.overflow = 'hidden'; |
| 30 | return () => { |
| 31 | document.removeEventListener('keydown', onKey); |
| 32 | document.body.style.overflow = prevOverflow; |
| 33 | }; |
| 34 | }, [open]); |
| 35 | |
| 36 | return ( |
| 37 | <> |
| 38 | <button |
| 39 | type="button" |
| 40 | className="lightbox-trigger" |
| 41 | onClick={() => setOpen(true)} |
| 42 | aria-label={`Enlarge: ${alt}`} |
| 43 | > |
| 44 | <Image src={src} alt={alt} fill sizes={sizes} /> |
| 45 | </button> |
| 46 | |
| 47 | {open && ( |
| 48 | <div |
| 49 | className="lightbox-modal" |
| 50 | role="dialog" |
| 51 | aria-modal="true" |
| 52 | aria-label={alt} |
| 53 | // Close on any click that isn't the image itself or the close |
| 54 | // button. The frame wrapper uses object-fit:contain, so for |
| 55 | // portrait / square photos there's letterbox space inside the |
| 56 | // frame that we still want to treat as "outside the image". |
| 57 | onClick={(e) => { |
| 58 | const t = e.target as HTMLElement; |
| 59 | if (t.tagName !== 'IMG' && !t.closest('.lightbox-close')) { |
| 60 | setOpen(false); |
| 61 | } |
| 62 | }} |
| 63 | > |
| 64 | <button |
| 65 | type="button" |
| 66 | className="lightbox-close" |
| 67 | onClick={() => setOpen(false)} |
| 68 | aria-label="Close enlarged image" |
| 69 | > |
| 70 | <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"> |
| 71 | <path d="M5 5l14 14M19 5L5 19" /> |
| 72 | </svg> |
| 73 | </button> |
| 74 | {/* Plain <img> instead of next/image so the rendered element |
| 75 | sizes to the image's natural aspect — no letterbox padding |
| 76 | above/below for portrait or square photos. max-w/max-h cap |
| 77 | it to the viewport. */} |
| 78 | {/* eslint-disable-next-line @next/next/no-img-element */} |
| 79 | <img src={src} alt={alt} className="lightbox-image" /> |
| 80 | </div> |
| 81 | )} |
| 82 | </> |
| 83 | ); |
| 84 | } |