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:
2026-10-25 23:22:43 +01:00
co-authored by Claude Opus 5.5
parent e013f1436b
commit 18bf0311ef
38 changed files with 2392 additions and 43 deletions
+68
View File
@@ -0,0 +1,68 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import useSwipe from './useSwipe';
function Target({
onPrev,
onNext,
enabled,
}: {
onPrev: () => void;
onNext: () => void;
enabled?: boolean;
}) {
const handlers = useSwipe({ onPrev, onNext, enabled });
return <div data-testid="swipe-target" {...handlers} />;
}
function swipe(target: HTMLElement, from: number, to: number, dy = 0) {
fireEvent.touchStart(target, {
touches: [{ clientX: from, clientY: 100 }],
});
fireEvent.touchEnd(target, {
changedTouches: [{ clientX: to, clientY: 100 + dy }],
});
}
function setup(enabled?: boolean) {
const onPrev = vi.fn();
const onNext = vi.fn();
render(<Target onPrev={onPrev} onNext={onNext} enabled={enabled} />);
return { onPrev, onNext, target: screen.getByTestId('swipe-target') };
}
describe('useSwipe', () => {
it('calls onNext for a swipe to the left and onPrev for one to the right', () => {
const { onPrev, onNext, target } = setup();
swipe(target, 300, 200);
expect(onNext).toHaveBeenCalledTimes(1);
swipe(target, 200, 300);
expect(onPrev).toHaveBeenCalledTimes(1);
});
it('ignores short and vertical touches', () => {
const { onPrev, onNext, target } = setup();
swipe(target, 300, 270);
swipe(target, 300, 200, 150);
expect(onNext).not.toHaveBeenCalled();
expect(onPrev).not.toHaveBeenCalled();
});
it('ignores a touch end without a start, and clears the start after each end', () => {
const { onNext, target } = setup();
fireEvent.touchEnd(target, {
changedTouches: [{ clientX: 0, clientY: 100 }],
});
swipe(target, 300, 200);
fireEvent.touchEnd(target, {
changedTouches: [{ clientX: 0, clientY: 100 }],
});
expect(onNext).toHaveBeenCalledTimes(1);
});
it('does nothing while disabled', () => {
const { onNext, target } = setup(false);
swipe(target, 300, 200);
expect(onNext).not.toHaveBeenCalled();
});
});
+40
View File
@@ -0,0 +1,40 @@
import { useRef, type TouchEvent } from 'react';
import { type Point, swipeDirection } from '@/lib/swipeDirection';
type SwipeOptions = {
onPrev: () => void;
onNext: () => void;
// Off: touches are ignored (e.g. a slider with a single slide)
enabled?: boolean;
};
// Horizontal swipes on touch screens: a swipe to the left calls onNext, to
// the right onPrev. Shared by the hero slider and the lightbox, so both
// react to the same distance and ignore vertical scrolling the same way.
export default function useSwipe({
onPrev,
onNext,
enabled = true,
}: SwipeOptions) {
const start = useRef<Point | null>(null);
return {
onTouchStart: (event: TouchEvent<HTMLElement>) => {
const touch = event.touches[0];
start.current = { x: touch.clientX, y: touch.clientY };
},
onTouchEnd: (event: TouchEvent<HTMLElement>) => {
const from = start.current;
start.current = null;
if (!from || !enabled) return;
const touch = event.changedTouches[0];
const direction = swipeDirection(from, {
x: touch.clientX,
y: touch.clientY,
});
if (direction === 'next') onNext();
if (direction === 'prev') onPrev();
},
};
}