Files
AndreaSlopFotografie/web/src/components/home/Testimonials/TestimonialCarousel.tsx
T
SluijsensandClaude Sonnet 5.5 107647f7a5 Local only: Cormorant Garamond and Mulish instead of Marcellus and Jost
Not pushed: Bryan compares it with the current test version first.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 17:15:04 +01:00

136 lines
5.1 KiB
TypeScript

import { useState } from 'react';
import {
Box,
Carousel,
chakra,
Flex,
Text,
useCarousel,
useMediaQuery,
useRecipe,
VisuallyHidden,
} from '@chakra-ui/react';
import { Icon } from '@iconify/react/offline';
import leftOutlined from '@iconify-icons/ant-design/left-outlined';
import rightOutlined from '@iconify-icons/ant-design/right-outlined';
import type { Testimonial } from '@/content/types';
import { slideCounter } from '@/lib/slideCounter';
import { slideLabel } from '@/lib/slideLabel';
import TestimonialSlide from './TestimonialSlide';
// The carousel itself. Chakra's carousel (Zag) handles paging, snapping,
// looping, swiping and hiding off-screen slides. The buttons and arrow keys
// are our own: the built-in ones always scroll smoothly from JavaScript,
// which CSS cannot switch off for visitors who prefer reduced motion; ours
// pass the API's `instant` flag instead (BR-REV-06). The review only changes
// by those buttons and keys or by swiping: nothing moves by itself.
export default function TestimonialCarousel({
testimonials,
}: {
testimonials: Testimonial[];
}) {
const count = testimonials.length;
const [reducedMotion] = useMediaQuery(['(prefers-reduced-motion: reduce)']);
// Empty on load; filled only by a change, so nothing is read out then
const [announcement, setAnnouncement] = useState('');
const carousel = useCarousel({
slideCount: count,
loop: true,
allowMouseDrag: true,
// The slides follow each other directly, as in the design
spacing: '0px',
translations: { item: slideLabel },
onPageChange: ({ page }) => setAnnouncement(slideLabel(page, count)),
});
const control = useRecipe({ key: 'carouselControl' });
const previous = () => carousel.scrollPrev(reducedMotion);
const next = () => carousel.scrollNext(reducedMotion);
return (
<Carousel.RootProvider
value={carousel}
aria-label="Reviews"
aria-roledescription="carrousel"
gap={0}
// The control bar lies over the bottom right of the slides
position="relative"
maxW={{ lg: '1120px' }}
mx="auto"
>
{/* Our own live region announces changes, so the item group's
built-in one is switched off to avoid hearing it twice */}
<Carousel.ItemGroup aria-live="off">
{testimonials.map((testimonial, index) => (
<Carousel.Item key={testimonial.id} index={index}>
<TestimonialSlide
testimonial={testimonial}
withControls
near
/>
</Carousel.Item>
))}
</Carousel.ItemGroup>
{/* From lg the bar sits in the room under the card, in line with
the card's text; below lg it follows the slides, centred.
The 25% and 44px match the card's padding. */}
<Flex
position={{ lg: 'absolute' }}
bottom={{ lg: 0 }}
right={{ lg: 0 }}
w={{ lg: '56%' }}
ps={{ lg: '25%' }}
pe={{ lg: '44px' }}
mt={{ base: 7, lg: 0 }}
justifyContent="center"
alignItems="center"
gap={3}
onKeyDown={(event) => {
if (event.key === 'ArrowLeft') previous();
else if (event.key === 'ArrowRight') next();
else return;
event.preventDefault();
}}
>
<chakra.button
type="button"
aria-label="Vorige review"
data-testid="testimonials-prev"
css={control({ kind: 'arrow' })}
onClick={previous}
>
<Icon icon={leftOutlined} aria-hidden />
</chakra.button>
{/* Visible text only: the live region below says it aloud */}
<Text
aria-hidden="true"
data-testid="testimonials-counter"
minW="56px"
textAlign="center"
fontFamily="heading"
fontSizeAdjust="0.43"
fontSize="16px"
letterSpacing="0.08em"
color="ink.soft"
>
{slideCounter(carousel.page, count)}
</Text>
<chakra.button
type="button"
aria-label="Volgende review"
data-testid="testimonials-next"
css={control({ kind: 'arrow' })}
onClick={next}
>
<Icon icon={rightOutlined} aria-hidden />
</chakra.button>
</Flex>
<Box aria-live="polite" data-testid="testimonials-status">
<VisuallyHidden>{announcement}</VisuallyHidden>
</Box>
</Carousel.RootProvider>
);
}