import { lazy, Suspense } from 'react'; import { Box, VisuallyHidden } from '@chakra-ui/react'; import { DEFERRED_LOAD_MARGIN } from '@/components/DeferredImage/DeferredImage'; import PageSection from '@/components/PageSection/PageSection'; import Reveal from '@/components/Reveal/Reveal'; import SectionHeading from '@/components/SectionHeading/SectionHeading'; import type { HomeContent, Testimonial } from '@/content/types'; import useInView from '@/hooks/useInView'; import { visibleTestimonials } from '@/lib/visibleTestimonials'; import TestimonialSlide from './TestimonialSlide'; // The carousel (Chakra/Zag) is the heaviest code on the page and sits far // below the hero. It is loaded only when this section comes near the screen, // so the hero (the largest contentful paint) never waits for it: loading it // with the page cost ±0.02 Lighthouse performance on mobile. const TestimonialCarousel = lazy(() => import('./TestimonialCarousel')); const TITLE_ID = 'testimonials-title'; type TestimonialsProps = { content: HomeContent['testimonials']; testimonials: Testimonial[]; }; // "Lieve woorden": client reviews, one at a time (design .testimonials). // Without a valid review the section is left out; a single review is shown // without carousel controls. The heading is always rendered. export default function Testimonials({ content, testimonials, }: TestimonialsProps) { const shown = visibleTestimonials(testimonials); const { ref, inView } = useInView({ rootMargin: DEFERRED_LOAD_MARGIN, }); if (shown.length === 0) return null; return ( {shown.length === 1 ? ( ) : ( {inView ? ( } > ) : ( )} )} ); } // Stands in for the carousel until it has loaded: the longest review laid // out exactly like a slide, plus the room for the controls. From `lg` that // room is part of the slide; below `lg` the control bar follows the slides, so // a spacer of its height is added. That makes it as tall as the carousel at // every screen width, so nothing below moves when the carousel takes its // place, not even during a scroll to #contact that passes this section while // it loads. The other reviews are in the page too, visually hidden, so the // prerendered HTML holds every review text for search engines (BR-LZY-02). // They are aria-hidden and inert, so assistive technology reads each review // once, in the carousel. function CarouselPlaceholder({ testimonials, }: { testimonials: Testimonial[]; }) { const longest = testimonials.reduce((a, b) => b.quote.length > a.quote.length ? b : a, ); return ( {testimonials .filter((testimonial) => testimonial !== longest) .map((testimonial) => ( ))} {/* Below lg the control bar: 44px buttons under a 28px margin */} ); }