Hero from the CMS: markers HERO-HTML and HERO-PRELOAD, hero key in the data block, picture per ratio, baked fallback
The prerendered hero is plain HTML between the markers (first photo, h1, buttons), the slider takes over after hydration with the hero of the host block, the API or the build. The head preload is added to the finished HTML. Hero texts move into nl.json. The stand-in host and the e2e tests cover the hero. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,36 +7,154 @@ import {
|
||||
Image,
|
||||
VisuallyHidden,
|
||||
} from '@chakra-ui/react';
|
||||
import LinkButton from '../LinkButton';
|
||||
import CmsRegion from '../CmsRegion/CmsRegion';
|
||||
import HeroArrows from './HeroArrows';
|
||||
import HeroButton from './HeroButton';
|
||||
import HeroPauseButton from './HeroPauseButton';
|
||||
import HeroSocials from './HeroSocials';
|
||||
import useHeroSlider, { type HeroAnnouncement } from './useHeroSlider';
|
||||
import type { HeroContent, SocialLink } from '@/content/types';
|
||||
import useHero from './useHero';
|
||||
import type { SocialLink } from '@/content/types';
|
||||
import useHydrated from '@/hooks/useHydrated';
|
||||
import { t } from '@/i18n';
|
||||
import type { HeroData, HeroSlide } from '@/lib/hero';
|
||||
import { heroHtml, variantSrcset } from '@/lib/heroHtml';
|
||||
import { slidesToLoad } from '@/lib/slidesToLoad';
|
||||
|
||||
const SLIDE_DURATION = '6s';
|
||||
const SIZES = '100vw';
|
||||
const PORTRAIT = '(orientation: portrait)';
|
||||
|
||||
function announcementText(announcement: HeroAnnouncement, slide: string) {
|
||||
switch (announcement) {
|
||||
case 'slide':
|
||||
return slide;
|
||||
case 'paused':
|
||||
return 'Diavoorstelling gepauzeerd';
|
||||
return t('hero.paused');
|
||||
case 'playing':
|
||||
return 'Diavoorstelling speelt weer af';
|
||||
return t('hero.playing');
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
// Where the subject of the photo is; the picture is cropped around it
|
||||
const objectPosition = (slide: HeroSlide): string | undefined => {
|
||||
const { focus } = slide.image;
|
||||
return focus
|
||||
? `${Number((focus.x * 100).toFixed(2))}% ${Number((focus.y * 100).toFixed(2))}%`
|
||||
: undefined;
|
||||
};
|
||||
|
||||
type HeroProps = {
|
||||
content: HeroContent;
|
||||
content: HeroData;
|
||||
socials: SocialLink[];
|
||||
};
|
||||
|
||||
// The hero of the homepage. Until hydration it is the plain HTML between the
|
||||
// CMS markers (what the host may rewrite); after hydration the slider shows
|
||||
// the data of the host, the API or the build, whichever came first.
|
||||
export default function Hero({ content, socials }: HeroProps) {
|
||||
const { title, tagline, cta, slides } = content;
|
||||
const hero = useHero(content);
|
||||
return useHydrated() ? (
|
||||
<HeroSlider content={hero} socials={socials} />
|
||||
) : (
|
||||
<HeroPlain content={hero} socials={socials} />
|
||||
);
|
||||
}
|
||||
|
||||
// The page as the browser first gets it: the first photo with the title and
|
||||
// the buttons, written by lib/heroHtml and styled by the global CSS of the
|
||||
// theme (.cms-hero-*). The frame is the same as the slider's, so nothing
|
||||
// jumps when the slider takes over.
|
||||
function HeroPlain({ content, socials }: HeroProps) {
|
||||
return (
|
||||
<Box
|
||||
as="section"
|
||||
aria-label={t('hero.ariaLabel')}
|
||||
position="relative"
|
||||
isolation="isolate"
|
||||
h="100svh"
|
||||
minH="560px"
|
||||
overflow="hidden"
|
||||
bg="ink"
|
||||
>
|
||||
<CmsRegion name="HERO-HTML" html={heroHtml(content)}>
|
||||
{null}
|
||||
</CmsRegion>
|
||||
<Box aria-hidden position="absolute" inset={0} bgGradient="scrim" />
|
||||
<HeroSocials socials={socials} />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
// One photo: a picture with a source for portrait screens and, for the
|
||||
// slides after the first, AVIF as well (the first one is only WebP, because
|
||||
// the browser preloads exactly that file)
|
||||
function SlidePicture({
|
||||
slide,
|
||||
first,
|
||||
visible,
|
||||
scaled,
|
||||
onLoaded,
|
||||
}: {
|
||||
slide: HeroSlide;
|
||||
first: boolean;
|
||||
visible: boolean;
|
||||
scaled: boolean;
|
||||
onLoaded?: () => void;
|
||||
}) {
|
||||
const { image } = slide;
|
||||
const formats = first ? ['image/webp'] : ['image/avif', 'image/webp'];
|
||||
const tall = formats.map((type) => ({
|
||||
type,
|
||||
srcset: variantSrcset(image.variants, 'tall', type),
|
||||
}));
|
||||
const wide = variantSrcset(image.variants, 'wide');
|
||||
const wideAvif = first
|
||||
? ''
|
||||
: variantSrcset(image.variants, 'wide', 'image/avif');
|
||||
return (
|
||||
<chakra.picture display="block" w="100%" h="100%">
|
||||
{tall.map(
|
||||
({ type, srcset }) =>
|
||||
srcset && (
|
||||
<source
|
||||
key={type}
|
||||
media={PORTRAIT}
|
||||
srcSet={srcset}
|
||||
sizes={SIZES}
|
||||
type={type}
|
||||
/>
|
||||
),
|
||||
)}
|
||||
{wideAvif && (
|
||||
<source srcSet={wideAvif} sizes={SIZES} type="image/avif" />
|
||||
)}
|
||||
<Image
|
||||
src={image.src}
|
||||
srcSet={wide || undefined}
|
||||
sizes={wide ? SIZES : undefined}
|
||||
width={image.width}
|
||||
height={image.height}
|
||||
alt={slide.alt}
|
||||
fetchPriority={first ? 'high' : undefined}
|
||||
onLoad={onLoaded}
|
||||
onError={onLoaded}
|
||||
w="100%"
|
||||
h="100%"
|
||||
objectFit="cover"
|
||||
objectPosition={objectPosition(slide)}
|
||||
transform={scaled || visible ? 'scale(1)' : 'scale(1.06)'}
|
||||
transition="transform 9s linear"
|
||||
/>
|
||||
</chakra.picture>
|
||||
);
|
||||
}
|
||||
|
||||
function HeroSlider({ content, socials }: HeroProps) {
|
||||
const { title, tagline, buttons, slides, display } = content;
|
||||
const single = display === 'single';
|
||||
const {
|
||||
active,
|
||||
announcement,
|
||||
@@ -49,7 +167,7 @@ export default function Hero({ content, socials }: HeroProps) {
|
||||
prev,
|
||||
advance,
|
||||
handlers,
|
||||
} = useHeroSlider(slides.length);
|
||||
} = useHeroSlider(single ? 1 : slides.length);
|
||||
// De slides liggen op elkaar in beeld, dus loading="lazy" stelt niets uit.
|
||||
// Eerst alleen de actieve foto; zodra die binnen en getekend is (twee
|
||||
// frames later, dus na de eerste paint) ook de volgende en de vorige.
|
||||
@@ -59,14 +177,14 @@ export default function Hero({ content, socials }: HeroProps) {
|
||||
requestAnimationFrame(() =>
|
||||
requestAnimationFrame(() => setFirstPainted(true)),
|
||||
);
|
||||
const loaded = firstPainted
|
||||
? slidesToLoad(active, slides.length)
|
||||
: [active];
|
||||
const shown = single ? slides.slice(0, 1) : slides;
|
||||
const loaded = firstPainted ? slidesToLoad(active, shown.length) : [active];
|
||||
const current = shown[active];
|
||||
|
||||
return (
|
||||
<Box
|
||||
as="section"
|
||||
aria-label="Uitgelicht werk"
|
||||
aria-label={t('hero.ariaLabel')}
|
||||
aria-roledescription="carousel"
|
||||
position="relative"
|
||||
// Eigen stacking context: de z-indexen hieronder gelden alleen
|
||||
@@ -81,12 +199,15 @@ export default function Hero({ content, socials }: HeroProps) {
|
||||
>
|
||||
{/* Slides: crossfade met een langzame inzoom */}
|
||||
<Box position="absolute" inset={0}>
|
||||
{slides.map((slide, i) => (
|
||||
{shown.map((slide, i) => (
|
||||
<Box
|
||||
key={slide.id}
|
||||
role="group"
|
||||
aria-roledescription="slide"
|
||||
aria-label={`${i + 1} van ${slides.length}`}
|
||||
aria-label={t('hero.slideLabel', {
|
||||
position: i + 1,
|
||||
count: shown.length,
|
||||
})}
|
||||
aria-hidden={i !== active}
|
||||
position="absolute"
|
||||
inset={0}
|
||||
@@ -96,25 +217,14 @@ export default function Hero({ content, socials }: HeroProps) {
|
||||
transitionTimingFunction="smooth"
|
||||
>
|
||||
{loaded.includes(i) && (
|
||||
<Image
|
||||
src={slide.src}
|
||||
alt={slide.alt}
|
||||
fetchPriority={i === 0 ? 'high' : undefined}
|
||||
onLoad={
|
||||
<SlidePicture
|
||||
slide={slide}
|
||||
first={i === 0}
|
||||
visible={i === active}
|
||||
scaled={reducedMotion}
|
||||
onLoaded={
|
||||
firstPainted ? undefined : onFirstLoaded
|
||||
}
|
||||
onError={
|
||||
firstPainted ? undefined : onFirstLoaded
|
||||
}
|
||||
w="100%"
|
||||
h="100%"
|
||||
objectFit="cover"
|
||||
transform={
|
||||
reducedMotion || i === active
|
||||
? 'scale(1)'
|
||||
: 'scale(1.06)'
|
||||
}
|
||||
transition="transform 9s linear"
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
@@ -139,29 +249,51 @@ export default function Hero({ content, socials }: HeroProps) {
|
||||
{/* Tagline en naam samen in één h1: zo staan de zoekwoorden
|
||||
(wat en waar) in de belangrijkste kop van de pagina */}
|
||||
<Heading as="h1" mb={9} color="white">
|
||||
{tagline && (
|
||||
<>
|
||||
<chakra.span
|
||||
display="block"
|
||||
mb={4}
|
||||
fontFamily="body"
|
||||
textStyle="eyebrow"
|
||||
// Zelfde regelhoogte als lopende tekst, niet die van de kop
|
||||
lineHeight="1.5"
|
||||
color="white/80"
|
||||
>
|
||||
{tagline}
|
||||
</chakra.span>{' '}
|
||||
</>
|
||||
)}
|
||||
<chakra.span
|
||||
display="block"
|
||||
mb={4}
|
||||
fontFamily="body"
|
||||
textStyle="eyebrow"
|
||||
// Zelfde regelhoogte als lopende tekst, niet die van de kop
|
||||
lineHeight="1.5"
|
||||
color="white/80"
|
||||
textStyle="display"
|
||||
// Een nieuwe regel in de titel is een regelafbreking
|
||||
whiteSpace="pre-line"
|
||||
>
|
||||
{tagline}
|
||||
</chakra.span>{' '}
|
||||
<chakra.span display="block" textStyle="display">
|
||||
{title}
|
||||
</chakra.span>
|
||||
</Heading>
|
||||
<LinkButton to={cta.to} variant="light">
|
||||
{cta.label}
|
||||
</LinkButton>
|
||||
{buttons.length > 0 && (
|
||||
<Box
|
||||
display="flex"
|
||||
flexWrap="wrap"
|
||||
justifyContent="center"
|
||||
gap={4}
|
||||
>
|
||||
{buttons.map((button) => (
|
||||
<HeroButton
|
||||
key={`${button.label}|${button.target}`}
|
||||
label={button.label}
|
||||
target={button.target}
|
||||
/>
|
||||
))}
|
||||
</Box>
|
||||
)}
|
||||
</Container>
|
||||
|
||||
{slides.length > 1 && <HeroArrows onPrev={prev} onNext={next} />}
|
||||
{shown.length > 1 && <HeroArrows onPrev={prev} onNext={next} />}
|
||||
<HeroSocials socials={socials} />
|
||||
{autoplay && (
|
||||
{autoplay && shown.length > 1 && (
|
||||
<HeroPauseButton stopped={stopped} onToggle={toggleStopped} />
|
||||
)}
|
||||
|
||||
@@ -175,13 +307,22 @@ export default function Hero({ content, socials }: HeroProps) {
|
||||
>
|
||||
{announcementText(
|
||||
announcement,
|
||||
`Foto ${active + 1} van ${slides.length}: ${slides[active].alt}`,
|
||||
t(
|
||||
current.alt
|
||||
? 'hero.announcement'
|
||||
: 'hero.announcementPlain',
|
||||
{
|
||||
position: active + 1,
|
||||
count: shown.length,
|
||||
alt: current.alt,
|
||||
},
|
||||
),
|
||||
)}
|
||||
</VisuallyHidden>
|
||||
|
||||
{/* Voortgangsbalk: als de animatie klaar is, schuift de slider door.
|
||||
Door de key start hij opnieuw bij elke slidewissel. */}
|
||||
{autoplay && (
|
||||
{autoplay && shown.length > 1 && (
|
||||
<Box
|
||||
key={active}
|
||||
aria-hidden
|
||||
|
||||
Reference in New Issue
Block a user