The mobile menu works through CSS, and controls that need JavaScript are hidden without it. The contact photo gets its own preload, and the tests cover the new Reveal behaviour, the first row and the noscript markup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
199 lines
8.0 KiB
TypeScript
199 lines
8.0 KiB
TypeScript
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<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);
|
|
};
|
|
|
|
const tile = (item: GalleryItem, index: number, imageStyle: ImageProps) => {
|
|
const row = eagerFirstRow ? firstRow(index) : null;
|
|
if (row === 'always') {
|
|
return (
|
|
<Image
|
|
src={item.thumb.src}
|
|
alt={item.thumb.alt}
|
|
htmlWidth={item.thumb.width}
|
|
htmlHeight={item.thumb.height}
|
|
fetchPriority="high"
|
|
decoding="async"
|
|
{...imageStyle}
|
|
/>
|
|
);
|
|
}
|
|
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 (
|
|
<chakra.picture display="contents">
|
|
<source media={row.media} srcSet={item.thumb.src} />
|
|
<DeferredImage
|
|
image={item.thumb}
|
|
{...imageStyle}
|
|
loading="eager"
|
|
fetchPriority="high"
|
|
/>
|
|
</chakra.picture>
|
|
);
|
|
}
|
|
return <DeferredImage image={item.thumb} {...imageStyle} />;
|
|
};
|
|
|
|
return (
|
|
<Box containerType="inline-size">
|
|
<Reveal>
|
|
<chakra.ul
|
|
data-testid={testId}
|
|
listStyleType="none"
|
|
display="grid"
|
|
gridTemplateColumns={{
|
|
base: columnTemplate(BASE_COLUMNS),
|
|
md: columnTemplate(MD_COLUMNS),
|
|
lg: columnTemplate(LG_COLUMNS),
|
|
}}
|
|
columnGap="gridGap"
|
|
// One implicit row = column width / ROWS_PER_COLUMN_WIDTH
|
|
gridAutoRows="var(--row)"
|
|
css={{
|
|
'--cols': String(BASE_COLUMNS),
|
|
md: { '--cols': String(MD_COLUMNS) },
|
|
lg: { '--cols': String(LG_COLUMNS) },
|
|
'--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}
|
|
>
|
|
{tile(item, index, imageStyle)}
|
|
{/* The alt names the photo; this says what
|
|
the link does */}{' '}
|
|
<VisuallyHidden>
|
|
(groot bekijken)
|
|
</VisuallyHidden>
|
|
</chakra.a>
|
|
</chakra.li>
|
|
);
|
|
})}
|
|
</chakra.ul>
|
|
</Reveal>
|
|
</Box>
|
|
);
|
|
}
|