import { useEffect, useRef, useState, type KeyboardEvent, type MouseEvent, } from 'react'; import { Box, chakra, Dialog, Portal, Text, useRecipe } from '@chakra-ui/react'; import { Icon } from '@iconify/react/offline'; import closeOutlined from '@iconify-icons/ant-design/close-outlined'; import leftOutlined from '@iconify-icons/ant-design/left-outlined'; import rightOutlined from '@iconify-icons/ant-design/right-outlined'; import type { GalleryItem } from '@/content/types'; import useSwipe from '@/hooks/useSwipe'; import { t } from '@/i18n'; import { neighbourIndexes } from '@/lib/neighbourIndexes'; import { wrapIndex } from '@/lib/wrapIndex'; type LightboxProps = { items: readonly GalleryItem[]; // The photo on screen, or null when closed index: number | null; onIndexChange: (index: number) => void; onClose: () => void; // Name of the dialog for assistive technology label?: string; // Where focus goes on close: the grid tile of the photo shown last finalFocusEl?: () => HTMLElement | null; }; // Large photos loaded ahead on each side of the one on screen (FD Q8 = B) const PRELOAD_RADIUS = 2; type LoadState = 'loading' | 'loaded' | 'failed'; // Shows one photo of a list large, in a modal dialog (design .lightbox). // Chakra's Dialog traps focus, closes on Escape, makes the page behind it // inert and stops it scrolling; this component adds browsing (buttons, // arrow keys, Home/End, swipe), the counter, the caption and loading. // Default export, so the page can load it lazily on first use. export default function Lightbox({ items, index, onIndexChange, onClose, label = t('common.lightbox.label'), finalFocusEl, }: LightboxProps) { const count = items.length; const open = index !== null && count > 0; const current = open ? wrapIndex(index, count) : 0; const item = items[current]; const browsable = count > 1; const closeButton = useRef(null); // Said after the visitor browses; not on opening (the dialog name and // the focused close button are announced then) const [announcement, setAnnouncement] = useState(''); // Clear the announcement when the dialog closes, so reopening is quiet. // Adjusted during render, as React advises for state that follows a prop. const [wasOpen, setWasOpen] = useState(open); if (wasOpen !== open) { setWasOpen(open); if (!open) setAnnouncement(''); } // Load the photos around this one, so browsing shows them at once useEffect(() => { if (!open) return; for (const i of neighbourIndexes(current, count, PRELOAD_RADIUS)) { const preload = new Image(); preload.src = items[i].full.src; } }, [open, current, count, items]); const show = (to: number) => { const next = wrapIndex(to, count); onIndexChange(next); const caption = items[next].caption; const position = next + 1; setAnnouncement( caption ? t('common.lightbox.announcementWithCaption', { position, count, caption, }) : t('common.lightbox.announcement', { position, count }), ); }; const swipe = useSwipe({ onPrev: () => show(current - 1), onNext: () => show(current + 1), enabled: browsable, }); const handleKeyDown = (event: KeyboardEvent) => { if (!browsable) return; const target: Record = { ArrowRight: current + 1, ArrowLeft: current - 1, Home: 0, End: count - 1, }; if (event.key in target) { event.preventDefault(); show(target[event.key]); } }; // A click on the dark area around the photo closes; on the photo itself // it does nothing (FD Q3) const closeOnBackground = (event: MouseEvent) => { if (event.target === event.currentTarget) onClose(); }; return ( { if (!details.open) onClose(); }} initialFocusEl={() => closeButton.current} finalFocusEl={finalFocusEl} lazyMount unmountOnExit > {item && ( {label} {/* Keyed per photo: a new photo starts loading from scratch */} {browsable && ( // Phones: one bar at the bottom, within reach of // the thumb and never over the photo (FD Q2). // Larger screens: the bar dissolves and each // control takes its place from the design. show(current - 1)} position={{ md: 'absolute' }} left={{ md: 4 }} top={{ md: '50%' }} transform={{ md: 'translateY(-50%)' }} > {current + 1} / {count} show(current + 1)} position={{ md: 'absolute' }} right={{ md: 4 }} top={{ md: '50%' }} transform={{ md: 'translateY(-50%)' }} > )} {announcement} )} ); } type PhotoProps = { item: GalleryItem; swipe: ReturnType; onBackground: (event: MouseEvent) => void; }; // The photo with its caption. Until the large version has loaded, the grid // version (already in the browser cache) stands in at the same size; if // the large one fails, the grid version stays and a short note says so. function Photo({ item, swipe, onBackground }: PhotoProps) { const [load, setLoad] = useState('loading'); const onLoad = () => setLoad('loaded'); const onError = () => setLoad('failed'); const caption = load === 'failed' ? t('common.lightbox.loadFailed') : item.caption; return ( {load !== 'failed' && ( )} {caption && ( {caption} )} ); } type ControlButtonProps = React.ComponentProps; // Buttons over the dark backdrop: the iconControl look (white, half // transparent, full on hover/focus, white focus ring) with a hairline // square around it as in the design (.lightbox__btn) function ControlButton(props: ControlButtonProps) { const iconControl = useRecipe({ key: 'iconControl' }); return ( ); }