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:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user