import { useEffect, useRef, useState } from 'react'; import { Box, chakra, Image, VisuallyHidden } from '@chakra-ui/react'; import HeroArrows from './HeroArrows'; import HeroPauseButton from './HeroPauseButton'; import HeroSocials from './HeroSocials'; import useHeroSlider, { type HeroAnnouncement } from './useHeroSlider'; import useHero from './useHero'; import type { SocialLink } from '@/content/types'; import useHydrated from '@/hooks/useHydrated'; import { t } from '@/i18n'; import { regionMarkers } from '@/lib/cmsHtml'; 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 t('hero.paused'); case 'playing': 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: HeroData; socials: SocialLink[]; }; // The hero of the homepage. The plain HTML between the CMS markers (first // photo, title, buttons, styled by .cms-hero-* in the theme) is what the // browser paints first and it is never replaced: React leaves it alone, so the // photo the browser already has stays the largest paint of the page (replacing // it at hydration made the LCP wait for the hydration). After hydration the // slider is laid around it: the other photos on top of the first, the arrows, // the pause button and so on, from the hero of the host, the API or the build. export default function Hero({ content, socials }: HeroProps) { const { hero, regionHero } = useHero(content); return ( ); } // One photo after the first: a picture with a source for portrait screens // and AVIF where there is one (the first photo is only WebP, because the // browser preloads exactly that file) function SlidePicture({ slide, visible, scaled, }: { slide: HeroSlide; visible: boolean; scaled: boolean; }) { const { image } = slide; const tall = ['image/avif', 'image/webp'].map((type) => ({ type, srcset: variantSrcset(image.variants, 'tall', type), })); const wide = variantSrcset(image.variants, 'wide'); const wideAvif = variantSrcset(image.variants, 'wide', 'image/avif'); return ( {tall.map( ({ type, srcset }) => srcset && ( ), )} {wideAvif && ( )} {slide.alt} ); } type FrameProps = HeroProps & { regionHtml: string; hydrated: boolean }; function HeroFrame({ content, socials, regionHtml, hydrated }: FrameProps) { const { slides, display } = content; const shown = display === 'single' ? slides.slice(0, 1) : slides; const { active, announcement, reducedMotion, autoplay, paused, stopped, toggleStopped, next, prev, advance, handlers, } = useHeroSlider(shown.length); const section = useRef(null); // De slides liggen op elkaar in beeld, dus loading="lazy" stelt niets uit. // Eerst alleen de eerste foto; zodra die binnen en getekend is (twee // frames later, dus na de eerste paint) ook de volgende en de vorige. // Verder weg laden we pas als de slider in de buurt komt (BR-HERO-01). const [firstPainted, setFirstPainted] = useState(false); // The first photo is part of the plain HTML, so React has no onLoad on it: // listen to it here useEffect(() => { const image = section.current?.querySelector('.cms-hero-image'); if (!image || firstPainted) return; const painted = () => requestAnimationFrame(() => requestAnimationFrame(() => setFirstPainted(true)), ); if (image.complete && image.naturalWidth > 0) { painted(); return; } image.addEventListener('load', painted); image.addEventListener('error', painted); return () => { image.removeEventListener('load', painted); image.removeEventListener('error', painted); }; }, [firstPainted]); // The first photo is hidden from screen readers while another one is on // top of it, and it zooms (data-zoom, see .cms-hero-image) while it is the // active one useEffect(() => { const picture = section.current?.querySelector('.cms-hero-picture'); if (active === 0) { picture?.removeAttribute('aria-hidden'); picture?.setAttribute('data-zoom', ''); } else { picture?.setAttribute('aria-hidden', 'true'); picture?.removeAttribute('data-zoom'); } }, [active]); const loaded = firstPainted ? slidesToLoad(active, shown.length) : [active]; const current = shown[active]; return ( {/* Eerste foto, titel en knoppen: gewone HTML tussen de CMS-markers. Dezelfde tekst als bij het prerenderen, dus React laat hem staan. */}
{/* De volgende slides liggen boven de eerste: crossfade met een langzame inzoom */} {hydrated && ( {shown.map((slide, i) => i === 0 ? null : ( {loaded.includes(i) && ( )} ), )} )} {/* Scrim */} {hydrated && shown.length > 1 && ( )} {hydrated && autoplay && shown.length > 1 && ( )} {/* Meldt een wissel of pauze door de bezoeker; staat er altijd, want een live-gebied moet al bestaan voordat de tekst verandert. Leeg na een automatische wissel, zodat er geen oude melding blijft. */} {hydrated && ( {announcementText( announcement, t( current.alt ? 'hero.announcement' : 'hero.announcementPlain', { position: active + 1, count: shown.length, alt: current.alt, }, ), )} )} {/* Voortgangsbalk: als de animatie klaar is, schuift de slider door. Door de key start hij opnieuw bij elke slidewissel. */} {hydrated && autoplay && shown.length > 1 && ( )} ); }