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 ( {/* Our own live region announces changes, so the item group's built-in one is switched off to avoid hearing it twice */} {testimonials.map((testimonial, index) => ( ))} {/* 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. */} { if (event.key === 'ArrowLeft') previous(); else if (event.key === 'ArrowRight') next(); else return; event.preventDefault(); }} > {/* Visible text only: the live region below says it aloud */} {announcement} ); }