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:
@@ -0,0 +1,45 @@
|
||||
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 })]
|
||||
: [];
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// The phases of a Reveal. `settled` is what the server and the hydrating
|
||||
// render show: visible, nothing to animate. After hydration only an element
|
||||
// wholly below the viewport becomes `hidden`, and it becomes `revealed` once
|
||||
// it scrolls into view (BR-RVL-09).
|
||||
export type RevealPhase = 'settled' | 'hidden' | 'revealed';
|
||||
|
||||
export type RevealEvent = 'belowViewport' | 'inView' | 'intersected';
|
||||
|
||||
// An element whose top edge is at or below the bottom edge of the viewport was
|
||||
// not painted from the HTML, so it may be hidden without a flash. Anything
|
||||
// else, also an element above the viewport or one taller than it, stays.
|
||||
export function startsHidden(top: number, viewportHeight: number): boolean {
|
||||
return top >= viewportHeight;
|
||||
}
|
||||
|
||||
// One way only: settled stays settled, hidden becomes revealed, revealed
|
||||
// stays. Applying an event twice (StrictMode runs effects twice) gives the
|
||||
// same phase as once.
|
||||
export function nextRevealPhase(
|
||||
phase: RevealPhase,
|
||||
event: RevealEvent,
|
||||
): RevealPhase {
|
||||
if (phase === 'settled' && event === 'belowViewport') return 'hidden';
|
||||
if (phase === 'hidden' && event === 'intersected') return 'revealed';
|
||||
return phase;
|
||||
}
|
||||
Reference in New Issue
Block a user