Makes Reveal, deferred images and the first row work from the HTML

Reveal content is visible in the HTML and only content below the viewport hides
after hydration. Deferred photos get a noscript twin, and the first row of the
portfolio loads eagerly per column count. Some old tests still expect the former
behaviour; they are rewritten next, so this commit's test run is not fully green.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 22:57:05 +01:00
co-authored by Claude Opus 5.5
parent 4139c8f732
commit 44125155bc
19 changed files with 422 additions and 70 deletions
@@ -35,16 +35,37 @@ export default function DeferredImage({
});
const load = pageLoaded && (near ?? inView);
// The twin is for visitors without JavaScript: the parser reads noscript
// content as text when scripting is on, so it is never fetched, and React
// ignores it when hydrating. It has the same style props, so the same
// class and box, and leaves out what must stay unique.
const twinProps: Record<string, unknown> = { ...rest };
delete twinProps.id;
delete twinProps['data-testid'];
return (
<Image
ref={ref}
src={load ? image.src : undefined}
alt={image.alt}
htmlWidth={image.width}
htmlHeight={image.height}
loading="lazy"
decoding="async"
{...rest}
/>
<>
<Image
ref={ref}
src={load ? image.src : undefined}
alt={image.alt}
htmlWidth={image.width}
htmlHeight={image.height}
loading="lazy"
decoding="async"
data-deferred
{...rest}
/>
<noscript>
<Image
src={image.src}
alt={image.alt}
htmlWidth={image.width}
htmlHeight={image.height}
decoding="async"
{...twinProps}
/>
</noscript>
</>
);
}
@@ -0,0 +1,11 @@
import { NO_SCRIPT_CSS } from './noScriptCss';
// Rendered in the head by the root shell. The browser ignores the children
// when scripting is on, on the server and when hydrating alike.
export default function NoScriptStyles() {
return (
<noscript>
<style>{NO_SCRIPT_CSS}</style>
</noscript>
);
}
@@ -0,0 +1,10 @@
// The CSS of every fallback for a visitor without JavaScript. It sits in a
// <noscript><style> in the head, so a browser with scripting on never parses
// it. No `</`, `url(` or `@import`: it must not end the noscript early or
// fetch anything (BR-NAV-02).
export const NO_SCRIPT_CSS = [
// The wrapper takes no part in flex, grid or positioning of its twin
'noscript{display:contents}',
// The empty deferred photo would draw as a broken-image box
'img[data-deferred]:not([src]){display:none!important}',
].join('');
@@ -35,7 +35,7 @@ describe('PortfolioGrid', () => {
await renderWithProviders(
<PortfolioGrid
items={galleryItems}
eagerCount={3}
eagerFirstRow
onOpen={() => {}}
/>,
);
@@ -1,8 +1,15 @@
import { useRef, type MouseEvent } from 'react';
import { Box, chakra, Image, VisuallyHidden } from '@chakra-ui/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 } from '@/lib/firstRow';
import {
GAP_ROWS,
masonryRowSpan,
@@ -11,9 +18,10 @@ import {
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;
// 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;
@@ -29,7 +37,7 @@ type PortfolioGridProps = {
// to open the lightbox instead.
export default function PortfolioGrid({
items,
eagerCount = 3,
eagerFirstRow = true,
onOpen,
onIntent,
'data-testid': testId = 'portfolio-grid',
@@ -59,6 +67,40 @@ export default function PortfolioGrid({
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>
@@ -132,22 +174,7 @@ export default function PortfolioGrid({
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}
/>
)}
{tile(item, index, imageStyle)}
{/* The alt names the photo; this says what
the link does */}{' '}
<VisuallyHidden>
+20 -8
View File
@@ -6,11 +6,12 @@ type RevealProps = BoxProps & {
delay?: number;
};
// Fades its content in and up once it scrolls into view (design .reveal).
// The content is always in the DOM and readable by assistive technology;
// only its opacity and position change.
// Fades its content in and up once it scrolls into view (design .reveal),
// when it started wholly below the viewport; content in view is simply
// shown. The content is always in the DOM and readable by assistive
// technology; only its opacity and position change.
export default function Reveal({ delay = 0, children, ...rest }: RevealProps) {
const { ref, visible } = useReveal<HTMLDivElement>();
const { ref, visible, animated } = useReveal<HTMLDivElement>();
return (
<Box
@@ -18,10 +19,21 @@ export default function Reveal({ delay = 0, children, ...rest }: RevealProps) {
data-reveal={visible ? 'visible' : 'hidden'}
opacity={visible ? 1 : 0}
transform={visible ? 'none' : 'translateY(24px)'}
transitionProperty="opacity, transform"
transitionDuration="reveal"
transitionTimingFunction="smooth"
transitionDelay={`${delay}s`}
{...(animated
? {
transitionProperty: 'opacity, transform',
transitionDuration: 'reveal',
transitionTimingFunction: 'smooth',
transitionDelay: `${delay}s`,
}
: { transition: 'none' })}
// A preference that changes while elements are hidden shows them
// at once
_motionReduce={{
opacity: 1,
transform: 'none',
transition: 'none',
}}
{...rest}
>
{children}
@@ -105,11 +105,12 @@ export default function PortfolioGallery({
<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}
// The first row loads eagerly, with preloads, because it
// is in view and is the largest paint; later rows wait for
// the page and for proximity. An earlier measurement
// (2026-10-27, 0.80 eager against 0.91 deferred on mobile)
// was made while the grid still faded in; it is repeated
// before this is final.
onOpen={open}
onIntent={() => setLightboxRequested(true)}
/>