import { useRef, type MouseEvent } from 'react'; import { Box, chakra, Image, VisuallyHidden, type ImageProps, } from '@chakra-ui/react'; import DeferredImage from '@/components/DeferredImage/DeferredImage'; import Reveal from '@/components/Reveal/Reveal'; import type { GalleryItem } from '@/content/types'; import { firstRow, GRID_COLUMNS } from '@/lib/firstRow'; import { GAP_ROWS, masonryRowSpan, ROWS_PER_COLUMN_WIDTH, } from '@/lib/masonryRowSpan'; const [BASE_COLUMNS, MD_COLUMNS, LG_COLUMNS] = GRID_COLUMNS.map( (entry) => entry.columns, ); const columnTemplate = (columns: number) => columns === 1 ? '1fr' : `repeat(${columns}, 1fr)`; type PortfolioGridProps = { items: readonly GalleryItem[]; // The photos of the first row (one column per screen width, see firstRow) // load straight away; the rest wait until the page has loaded and they // are near eagerFirstRow?: boolean; onOpen: (index: number) => void; // First hover or focus on a photo: a good moment to load the lightbox onIntent?: () => void; 'data-testid'?: string; }; // Masonry grid of photos in reading order (design .portfolio-grid): every // photo is shown whole in its own shape, row by row, so keyboard and screen // reader follow the order Andrea chose. Pure CSS: the implicit rows are a // fraction of the column width (container units) and each photo spans as // many as its aspect ratio needs, so nothing shifts while photos load. // Every photo is a link to its large version; a plain click asks the page // to open the lightbox instead. export default function PortfolioGrid({ items, eagerFirstRow = true, onOpen, onIntent, 'data-testid': testId = 'portfolio-grid', }: PortfolioGridProps) { const intentReported = useRef(false); const reportIntent = () => { if (intentReported.current) return; intentReported.current = true; onIntent?.(); }; const handleClick = ( event: MouseEvent, index: number, ) => { // Ctrl/Cmd/Shift/Alt or middle click: let the browser open the link if ( event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey ) { return; } event.preventDefault(); onOpen(index); }; const tile = (item: GalleryItem, index: number, imageStyle: ImageProps) => { const row = eagerFirstRow ? firstRow(index) : null; if (row === 'always') { return ( {item.thumb.alt} ); } if (row) { // From this width the source loads the photo at once, to be in // step with its preload; below it nothing loads until the image // is near. The src set later is the same file. return ( ); } return ; }; return ( {items.map((item, index) => { const span = masonryRowSpan(item.thumb); const imageStyle = { display: 'block', w: '100%', h: '100%', objectFit: 'cover', transitionProperty: 'transform', transitionDuration: 'interaction', transitionTimingFunction: 'smooth', _groupHover: { transform: 'scale(1.05)' }, _groupFocusVisible: { transform: 'scale(1.05)' }, _motionReduce: { transform: 'none !important' }, } as const; return ( handleClick(event, index) } onPointerEnter={reportIntent} onFocus={reportIntent} > {tile(item, index, imageStyle)} {/* The alt names the photo; this says what the link does */}{' '} (groot bekijken) ); })} ); }