Adds the shared gallery building blocks
A masonry photo grid that keeps the reading order (pure CSS, no measuring in the browser), an accessible lightbox on Chakra's Dialog with wrap-around browsing, keys, swipe and preloading, and a filter bar of toggle buttons. They work on generic gallery items, so other pages can use them. The swipe logic moves out of the hero into a shared hook; the hero behaves as before. The rules behind them live in pure helpers with property-based tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
import { useRef, type MouseEvent } from 'react';
|
||||
import { Box, chakra, Image, VisuallyHidden } from '@chakra-ui/react';
|
||||
import DeferredImage from '@/components/DeferredImage/DeferredImage';
|
||||
import Reveal from '@/components/Reveal/Reveal';
|
||||
import type { GalleryItem } from '@/content/types';
|
||||
import {
|
||||
GAP_ROWS,
|
||||
masonryRowSpan,
|
||||
ROWS_PER_COLUMN_WIDTH,
|
||||
} from '@/lib/masonryRowSpan';
|
||||
|
||||
type PortfolioGridProps = {
|
||||
items: readonly GalleryItem[];
|
||||
// How many photos at the start load straight away (the first row on
|
||||
// desktop); the rest wait until the page has loaded and they are near
|
||||
eagerCount?: number;
|
||||
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,
|
||||
eagerCount = 3,
|
||||
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<HTMLAnchorElement>,
|
||||
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);
|
||||
};
|
||||
|
||||
return (
|
||||
<Box containerType="inline-size">
|
||||
<Reveal>
|
||||
<chakra.ul
|
||||
data-testid={testId}
|
||||
listStyleType="none"
|
||||
display="grid"
|
||||
gridTemplateColumns={{
|
||||
base: '1fr',
|
||||
md: 'repeat(2, 1fr)',
|
||||
lg: 'repeat(3, 1fr)',
|
||||
}}
|
||||
columnGap="gridGap"
|
||||
// One implicit row = column width / ROWS_PER_COLUMN_WIDTH
|
||||
gridAutoRows="var(--row)"
|
||||
css={{
|
||||
'--cols': '1',
|
||||
md: { '--cols': '2' },
|
||||
lg: { '--cols': '3' },
|
||||
'--row': `calc((100cqi - (var(--cols) - 1) * {spacing.gridGap}) / var(--cols) / ${ROWS_PER_COLUMN_WIDTH})`,
|
||||
}}
|
||||
>
|
||||
{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 (
|
||||
<chakra.li
|
||||
key={item.id}
|
||||
gridRowEnd={`span ${span}`}
|
||||
data-row-span={span}
|
||||
>
|
||||
<chakra.a
|
||||
href={item.full.src}
|
||||
data-testid={`portfolio-tile-${index + 1}`}
|
||||
className="group"
|
||||
display="block"
|
||||
position="relative"
|
||||
overflow="hidden"
|
||||
bg="paper.deep"
|
||||
cursor="zoom-in"
|
||||
// The photo; the last rows are the gap
|
||||
h={`calc(100% - ${GAP_ROWS} * var(--row))`}
|
||||
// Keeps the focus ring above neighbours
|
||||
_focusVisible={{ zIndex: 1 }}
|
||||
// Darkening overlay (design ::after)
|
||||
_after={{
|
||||
content: '""',
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
bg: 'ink',
|
||||
opacity: 0,
|
||||
transitionProperty: 'opacity',
|
||||
transitionDuration: 'interaction',
|
||||
pointerEvents: 'none',
|
||||
}}
|
||||
_hover={{ _after: { opacity: 0.25 } }}
|
||||
onClick={(event) =>
|
||||
handleClick(event, index)
|
||||
}
|
||||
onPointerEnter={reportIntent}
|
||||
onFocus={reportIntent}
|
||||
>
|
||||
{index < eagerCount ? (
|
||||
<Image
|
||||
src={item.thumb.src}
|
||||
alt={item.thumb.alt}
|
||||
htmlWidth={item.thumb.width}
|
||||
htmlHeight={item.thumb.height}
|
||||
fetchPriority="high"
|
||||
decoding="async"
|
||||
{...imageStyle}
|
||||
/>
|
||||
) : (
|
||||
<DeferredImage
|
||||
image={item.thumb}
|
||||
{...imageStyle}
|
||||
/>
|
||||
)}
|
||||
{/* The alt names the photo; this says what
|
||||
the link does */}{' '}
|
||||
<VisuallyHidden>
|
||||
(groot bekijken)
|
||||
</VisuallyHidden>
|
||||
</chakra.a>
|
||||
</chakra.li>
|
||||
);
|
||||
})}
|
||||
</chakra.ul>
|
||||
</Reveal>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user