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>
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import type { ContentImage } from '../content/types.ts';
|
|
import { preloadImage, type HeadLink } from '../seo.ts';
|
|
|
|
// Columns of the portfolio grid per screen width, narrowest first: the values
|
|
// of PortfolioGrid's gridTemplateColumns (base, md, lg)
|
|
export const GRID_COLUMNS = [
|
|
{ media: null, columns: 1 },
|
|
{ media: '(min-width: 48em)', columns: 2 },
|
|
{ media: '(min-width: 64em)', columns: 3 },
|
|
] as const;
|
|
|
|
type GridColumns = readonly {
|
|
media: string | null;
|
|
columns: number;
|
|
}[];
|
|
|
|
// 'always': in the first row at every width. { media }: in the first row from
|
|
// that width on. null: never in the first row.
|
|
export type FirstRow = 'always' | { media: string } | null;
|
|
|
|
// Whether the tile at this index is in the first row of the grid, and from
|
|
// which width on. The column count is CSS only, so the build cannot know the
|
|
// width: it names the media query instead.
|
|
export function firstRow(
|
|
index: number,
|
|
columns: GridColumns = GRID_COLUMNS,
|
|
): FirstRow {
|
|
if (index < columns[0].columns) return 'always';
|
|
const breakpoint = columns.find((entry) => index < entry.columns);
|
|
return breakpoint?.media ? { media: breakpoint.media } : null;
|
|
}
|
|
|
|
// The preloads React cannot write itself: the first-row tiles that sit in a
|
|
// <picture>, each bound to the query of its <source>
|
|
export function firstRowPreloads(
|
|
items: readonly { thumb: ContentImage }[],
|
|
columns: GridColumns = GRID_COLUMNS,
|
|
): HeadLink[] {
|
|
return items.flatMap((item, index) => {
|
|
const row = firstRow(index, columns);
|
|
return row && row !== 'always'
|
|
? [preloadImage(item.thumb, { media: row.media })]
|
|
: [];
|
|
});
|
|
}
|