A landscape photo overlaps a card with the shoot type, quote and names. The controls are two arrows with an X / Y counter; the dots are gone and nothing moves by itself. A change slides the whole slide horizontally, also on a swipe. Reviews get an optional imagePosition for the crop, normalised by normalizeImagePosition; slideCounter writes the counter. Both helpers have property tests next to example tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
110 lines
4.3 KiB
TypeScript
110 lines
4.3 KiB
TypeScript
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<HTMLDivElement>({
|
|
rootMargin: DEFERRED_LOAD_MARGIN,
|
|
});
|
|
if (shown.length === 0) return null;
|
|
|
|
return (
|
|
<PageSection
|
|
labelledBy={TITLE_ID}
|
|
tone="alt"
|
|
data-testid="testimonials"
|
|
>
|
|
<Reveal>
|
|
<SectionHeading id={TITLE_ID} title={content.title} />
|
|
</Reveal>
|
|
<Reveal>
|
|
{shown.length === 1 ? (
|
|
<TestimonialSlide testimonial={shown[0]} />
|
|
) : (
|
|
<Box ref={ref}>
|
|
{inView ? (
|
|
<Suspense
|
|
fallback={
|
|
<CarouselPlaceholder testimonials={shown} />
|
|
}
|
|
>
|
|
<TestimonialCarousel testimonials={shown} />
|
|
</Suspense>
|
|
) : (
|
|
<CarouselPlaceholder testimonials={shown} />
|
|
)}
|
|
</Box>
|
|
)}
|
|
</Reveal>
|
|
</PageSection>
|
|
);
|
|
}
|
|
|
|
// 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 (
|
|
<Box maxW={{ lg: '1120px' }} mx="auto">
|
|
<TestimonialSlide testimonial={longest} withControls />
|
|
{testimonials
|
|
.filter((testimonial) => testimonial !== longest)
|
|
.map((testimonial) => (
|
|
<VisuallyHidden key={testimonial.id} asChild>
|
|
<Box aria-hidden="true" inert>
|
|
<TestimonialSlide testimonial={testimonial} />
|
|
</Box>
|
|
</VisuallyHidden>
|
|
))}
|
|
{/* Below lg the control bar: 44px buttons under a 28px margin */}
|
|
<Box
|
|
display={{ base: 'block', lg: 'none' }}
|
|
mt={7}
|
|
h="44px"
|
|
data-requires-js
|
|
/>
|
|
</Box>
|
|
);
|
|
}
|