Adds the portfolio page
The ten live photos in a filterable masonry grid with an accessible lightbox, filters per shoot type kept in the URL (?shoot=), the shared Instagram block, and an indexed page with image gallery structured data and its own preview image. Soorten fotoshoots now only links to the portfolio for shoot types that have photos there. The filter bar stays on one row, so the web font loading no longer shifts the photos below it, and no photo loads before the page has: the grid fades in, so loading its photos early only delayed the fonts and the intro text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { act, screen, waitFor, within } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import portfolioContent from '@/content/portfolioContent';
|
||||
import shootTypes from '@/content/shootTypes';
|
||||
import type { ShootTypeId } from '@/content/types';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import PortfolioGallery from './PortfolioGallery';
|
||||
|
||||
const photos = portfolioContent.photos;
|
||||
|
||||
// Lets a test change the shoot from outside, as the route would
|
||||
let setShootFromOutside: (shoot: string | undefined) => void = () => {};
|
||||
|
||||
// The route's part: it keeps the chosen shoot (in the URL)
|
||||
function Harness({
|
||||
start,
|
||||
onShootChange = () => {},
|
||||
}: {
|
||||
start?: string;
|
||||
onShootChange?: (shoot: ShootTypeId | undefined) => void;
|
||||
}) {
|
||||
const [shoot, setShoot] = useState<string | undefined>(start);
|
||||
useEffect(() => {
|
||||
setShootFromOutside = setShoot;
|
||||
}, []);
|
||||
return (
|
||||
<PortfolioGallery
|
||||
photos={photos}
|
||||
shootTypes={shootTypes}
|
||||
shoot={shoot}
|
||||
onShootChange={(next) => {
|
||||
setShoot(next);
|
||||
onShootChange(next);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// By test id: while the closed dialog fades out (in jsdom it never
|
||||
// finishes), the page behind it is still hidden from the accessibility tree
|
||||
const tiles = () =>
|
||||
screen
|
||||
.getByTestId('portfolio-grid')
|
||||
.querySelectorAll('[data-testid^="portfolio-tile-"]');
|
||||
|
||||
describe('PortfolioGallery', () => {
|
||||
it('is a section named by a hidden heading, with filters for the types that have photos', async () => {
|
||||
const { container } = await renderWithProviders(<Harness />);
|
||||
expect(
|
||||
screen.getByRole('region', { name: "Foto's" }),
|
||||
).toBeInTheDocument();
|
||||
const group = screen.getByRole('group', {
|
||||
name: 'Filter op soort shoot',
|
||||
});
|
||||
expect(
|
||||
within(group)
|
||||
.getAllByRole('button')
|
||||
.map((b) => b.textContent),
|
||||
).toEqual(['Alles', 'Newborn', 'Zwangerschap', 'Gezin']);
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Alles', pressed: true }),
|
||||
).toBeInTheDocument();
|
||||
expect(tiles()).toHaveLength(10);
|
||||
// Every photo waits until the page has loaded (none eager)
|
||||
for (const image of screen.getAllByRole('img')) {
|
||||
expect(image).not.toHaveAttribute('fetchpriority');
|
||||
expect(image).toHaveAttribute('loading', 'lazy');
|
||||
}
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('filters on a shoot type, announces the count and reports the choice', async () => {
|
||||
const onShootChange = vi.fn();
|
||||
await renderWithProviders(<Harness onShootChange={onShootChange} />);
|
||||
const user = userEvent.setup();
|
||||
await user.click(screen.getByRole('button', { name: 'Gezin' }));
|
||||
expect(onShootChange).toHaveBeenLastCalledWith('gezin');
|
||||
expect(tiles()).toHaveLength(2);
|
||||
expect(screen.getByTestId('portfolio-count')).toHaveTextContent(
|
||||
"2 foto's",
|
||||
);
|
||||
await user.click(screen.getByRole('button', { name: 'Alles' }));
|
||||
expect(onShootChange).toHaveBeenLastCalledWith(undefined);
|
||||
expect(tiles()).toHaveLength(10);
|
||||
});
|
||||
|
||||
it('starts with the shoot from the URL', async () => {
|
||||
await renderWithProviders(<Harness start="zwangerschap" />);
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Zwangerschap', pressed: true }),
|
||||
).toBeInTheDocument();
|
||||
expect(tiles()).toHaveLength(2);
|
||||
});
|
||||
|
||||
it.each(['loveshoot', 'onzin', '<script>'])(
|
||||
'shows everything for ?shoot=%s',
|
||||
async (value) => {
|
||||
await renderWithProviders(<Harness start={value} />);
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Alles', pressed: true }),
|
||||
).toBeInTheDocument();
|
||||
expect(tiles()).toHaveLength(10);
|
||||
expect(document.body).not.toHaveTextContent(value);
|
||||
},
|
||||
);
|
||||
|
||||
it('opens the lightbox within the filtered photos, captioned with the shoot type', async () => {
|
||||
await renderWithProviders(<Harness start="gezin" />);
|
||||
const user = userEvent.setup();
|
||||
await user.click(screen.getByTestId('portfolio-tile-2'));
|
||||
const counter = await screen.findByTestId('lightbox-counter');
|
||||
expect(counter).toHaveTextContent('2 / 2');
|
||||
expect(screen.getByTestId('lightbox-caption')).toHaveTextContent(
|
||||
'Gezinsshoot',
|
||||
);
|
||||
expect(screen.getByTestId('lightbox-image')).toHaveAttribute(
|
||||
'src',
|
||||
photos.find((p) => p.id === 'f41a7138')?.full.src,
|
||||
);
|
||||
});
|
||||
|
||||
it('closes the lightbox when the filter changes', async () => {
|
||||
await renderWithProviders(<Harness />);
|
||||
const user = userEvent.setup();
|
||||
await user.click(screen.getByTestId('portfolio-tile-1'));
|
||||
expect(await screen.findByTestId('lightbox')).toHaveAttribute(
|
||||
'data-state',
|
||||
'open',
|
||||
);
|
||||
// The dialog makes the page inert, so the filter changes from
|
||||
// outside, as through the URL
|
||||
act(() => setShootFromOutside('newborn'));
|
||||
expect(tiles()).toHaveLength(6);
|
||||
// A close from the parent reaches the dialog in an effect
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('lightbox')).toHaveAttribute(
|
||||
'data-state',
|
||||
'closed',
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,136 @@
|
||||
import { lazy, Suspense, useReducer, useRef, useState } from 'react';
|
||||
import { Box, chakra, Heading, VisuallyHidden } from '@chakra-ui/react';
|
||||
import FilterBar from '@/components/FilterBar/FilterBar';
|
||||
import PageSection from '@/components/PageSection/PageSection';
|
||||
import PortfolioGrid from '@/components/PortfolioGrid/PortfolioGrid';
|
||||
import type { PortfolioPhoto, ShootType, ShootTypeId } from '@/content/types';
|
||||
import { closedLightbox, lightboxReducer } from '@/lib/lightboxReducer';
|
||||
import {
|
||||
ALL_FILTER,
|
||||
filterOptions,
|
||||
filterPhotos,
|
||||
photoCountLabel,
|
||||
shootFromSearch,
|
||||
shootTypesWithPhotos,
|
||||
toGalleryItems,
|
||||
} from '@/lib/portfolioFilters';
|
||||
|
||||
// Loaded on the first hover, focus or click on a photo (application design Q7)
|
||||
const Lightbox = lazy(() => import('@/components/Lightbox/Lightbox'));
|
||||
|
||||
const TITLE_ID = 'portfolio-title';
|
||||
|
||||
type PortfolioGalleryProps = {
|
||||
photos: readonly PortfolioPhoto[];
|
||||
shootTypes: readonly ShootType[];
|
||||
// The shoot from ?shoot=; checked here against the types with photos
|
||||
shoot: string | undefined;
|
||||
onShootChange: (shoot: ShootTypeId | undefined) => void;
|
||||
};
|
||||
|
||||
// The portfolio's photos: filters per shoot type, the masonry grid and the
|
||||
// lightbox, connected. The URL holds the chosen filter (via the route); this
|
||||
// component holds which photo is open.
|
||||
export default function PortfolioGallery({
|
||||
photos,
|
||||
shootTypes,
|
||||
shoot,
|
||||
onShootChange,
|
||||
}: PortfolioGalleryProps) {
|
||||
const available = shootTypesWithPhotos(shootTypes, photos);
|
||||
const active = shootFromSearch(
|
||||
shoot,
|
||||
available.map((type) => type.id),
|
||||
);
|
||||
const shown = filterPhotos(photos, active);
|
||||
const items = toGalleryItems(shown, shootTypes);
|
||||
|
||||
const [lightbox, dispatch] = useReducer(lightboxReducer, closedLightbox);
|
||||
const [lightboxRequested, setLightboxRequested] = useState(false);
|
||||
const [announcement, setAnnouncement] = useState('');
|
||||
const grid = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Another list, however the filter changed: the open photo may no longer
|
||||
// be in it, so the lightbox closes (BR-FL-05). Adjusted during render,
|
||||
// as React advises for state that follows a prop.
|
||||
const [listFor, setListFor] = useState(active);
|
||||
if (listFor !== active) {
|
||||
setListFor(active);
|
||||
dispatch({ type: 'listChanged', count: items.length });
|
||||
}
|
||||
|
||||
const chooseFilter = (id: string) => {
|
||||
const next = id === ALL_FILTER ? undefined : (id as ShootTypeId);
|
||||
setAnnouncement(photoCountLabel(filterPhotos(photos, next).length));
|
||||
onShootChange(next);
|
||||
};
|
||||
|
||||
const open = (index: number) => {
|
||||
setLightboxRequested(true);
|
||||
dispatch({ type: 'open', index, count: items.length });
|
||||
};
|
||||
|
||||
// Back to the tile of the photo shown last (BR-LB-08)
|
||||
const tileOfShownPhoto = () =>
|
||||
lightbox.open
|
||||
? (grid.current?.querySelector<HTMLElement>(
|
||||
`[data-testid="portfolio-tile-${lightbox.index + 1}"]`,
|
||||
) ?? null)
|
||||
: null;
|
||||
|
||||
return (
|
||||
<PageSection
|
||||
labelledBy={TITLE_ID}
|
||||
spacing="flushTop"
|
||||
data-testid="portfolio-gallery"
|
||||
>
|
||||
{/* The design shows no heading here; one for screen readers
|
||||
names the section */}
|
||||
<VisuallyHidden>
|
||||
<Heading as="h2" id={TITLE_ID}>
|
||||
Foto's
|
||||
</Heading>
|
||||
</VisuallyHidden>
|
||||
<Box mb={{ base: 6, md: 10 }}>
|
||||
<FilterBar
|
||||
options={filterOptions(available, 'Alles')}
|
||||
value={active ?? ALL_FILTER}
|
||||
onChange={chooseFilter}
|
||||
label="Filter op soort shoot"
|
||||
/>
|
||||
</Box>
|
||||
<chakra.p srOnly aria-live="polite" data-testid="portfolio-count">
|
||||
{announcement}
|
||||
</chakra.p>
|
||||
<Box ref={grid}>
|
||||
<PortfolioGrid
|
||||
items={items}
|
||||
// No photo loads before the page has: the grid fades in, so
|
||||
// its photos are never the largest paint, and loading them
|
||||
// early only slowed the fonts and the intro text (measured
|
||||
// 0.80 vs 0.91 on mobile; decision 2026-10-27)
|
||||
eagerCount={0}
|
||||
onOpen={open}
|
||||
onIntent={() => setLightboxRequested(true)}
|
||||
/>
|
||||
</Box>
|
||||
{lightboxRequested && (
|
||||
<Suspense fallback={null}>
|
||||
<Lightbox
|
||||
items={items}
|
||||
index={lightbox.open ? lightbox.index : null}
|
||||
onIndexChange={(index) =>
|
||||
dispatch({
|
||||
type: 'open',
|
||||
index,
|
||||
count: items.length,
|
||||
})
|
||||
}
|
||||
onClose={() => dispatch({ type: 'close' })}
|
||||
finalFocusEl={tileOfShownPhoto}
|
||||
/>
|
||||
</Suspense>
|
||||
)}
|
||||
</PageSection>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user