Adds the Lieve woorden section to the homepage
Shows client reviews one at a time with the framed photo, a large quotation mark, the shoot type and the names. Uses Chakra's carousel for paging, looping and swiping with our own controls, so visitors who prefer reduced motion get instant changes, and loads the carousel only when the section comes near the screen so the hero does not wait for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
Box,
|
||||
Carousel,
|
||||
chakra,
|
||||
Flex,
|
||||
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 { 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, dots 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).
|
||||
export default function TestimonialCarousel({
|
||||
testimonials,
|
||||
}: {
|
||||
testimonials: Testimonial[];
|
||||
}) {
|
||||
const count = testimonials.length;
|
||||
const [reducedMotion] = useMediaQuery(
|
||||
['(prefers-reduced-motion: reduce)'],
|
||||
{ ssr: false },
|
||||
);
|
||||
// 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,
|
||||
// Room between slides, so the next one never peeks in
|
||||
spacing: '4rem',
|
||||
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}
|
||||
>
|
||||
{/* 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) => (
|
||||
// The photo's sand block sticks out 1.5rem to the left
|
||||
// and below (FramedImage); the item group clips
|
||||
// everything outside a slide, so each slide makes room
|
||||
<Carousel.Item
|
||||
key={testimonial.id}
|
||||
index={index}
|
||||
ps={{ base: 0, sm: '1.5rem' }}
|
||||
pb={{ base: 0, sm: '1.5rem' }}
|
||||
>
|
||||
<TestimonialSlide testimonial={testimonial} />
|
||||
</Carousel.Item>
|
||||
))}
|
||||
</Carousel.ItemGroup>
|
||||
|
||||
<Flex
|
||||
justifyContent="center"
|
||||
alignItems="center"
|
||||
gap={6}
|
||||
mt="clamp(2.5rem, 5vw, 4rem)"
|
||||
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>
|
||||
<Flex gap={1}>
|
||||
{testimonials.map((testimonial, index) => (
|
||||
<chakra.button
|
||||
key={testimonial.id}
|
||||
type="button"
|
||||
aria-label={`Review ${index + 1}`}
|
||||
aria-current={
|
||||
carousel.page === index ? 'true' : undefined
|
||||
}
|
||||
data-testid={`testimonials-dot-${index + 1}`}
|
||||
css={control({ kind: 'dot' })}
|
||||
onClick={() =>
|
||||
carousel.scrollTo(index, reducedMotion)
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</Flex>
|
||||
<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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import testimonials from '@/content/testimonials';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import TestimonialSlide from './TestimonialSlide';
|
||||
|
||||
const review = testimonials[1];
|
||||
|
||||
describe('TestimonialSlide', () => {
|
||||
it('shows the photo, shoot, quote and names', async () => {
|
||||
const { container } = await renderWithProviders(
|
||||
<TestimonialSlide testimonial={review} />,
|
||||
);
|
||||
expect(screen.getByRole('img')).toHaveAccessibleName(review.image.alt);
|
||||
expect(screen.getByText(review.shoot)).toBeInTheDocument();
|
||||
|
||||
const quote = container.querySelector('blockquote');
|
||||
expect(quote).toHaveTextContent(review.quote);
|
||||
expect(container.querySelector('figcaption')).toHaveTextContent(
|
||||
review.author,
|
||||
);
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('keeps the decorative quotation mark away from assistive technology', async () => {
|
||||
const { container } = await renderWithProviders(
|
||||
<TestimonialSlide testimonial={review} />,
|
||||
);
|
||||
const mark = [
|
||||
...container.querySelectorAll('[aria-hidden="true"]'),
|
||||
].find((element) => element.textContent === '“');
|
||||
expect(mark).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { Blockquote, Box, Grid, Text } from '@chakra-ui/react';
|
||||
import FramedImage from '@/components/FramedImage/FramedImage';
|
||||
import type { Testimonial } from '@/content/types';
|
||||
|
||||
type TestimonialSlideProps = {
|
||||
testimonial: Testimonial;
|
||||
};
|
||||
|
||||
// One review (design .testimonial): framed photo next to a large quotation
|
||||
// mark, the shoot type, the quote and the names. Stacked below `lg`.
|
||||
export default function TestimonialSlide({
|
||||
testimonial,
|
||||
}: TestimonialSlideProps) {
|
||||
return (
|
||||
<Grid
|
||||
data-testid={`testimonial-${testimonial.id}`}
|
||||
templateColumns={{ base: '1fr', lg: '5fr 7fr' }}
|
||||
gap="clamp(2rem, 6vw, 5.5rem)"
|
||||
alignItems="center"
|
||||
h="100%"
|
||||
>
|
||||
<Box maxW={{ base: '420px', lg: 'none' }} w="100%">
|
||||
<FramedImage image={testimonial.image} aspectRatio="4 / 5" />
|
||||
</Box>
|
||||
<Box>
|
||||
<Text
|
||||
aria-hidden="true"
|
||||
fontFamily="heading"
|
||||
fontSize="clamp(5rem, 9vw, 7.5rem)"
|
||||
lineHeight="1"
|
||||
h="0.55em"
|
||||
color="accent"
|
||||
>
|
||||
“
|
||||
</Text>
|
||||
{/* accent.deep instead of the design's accent: meets 4.5:1 (NFR-HP-01) */}
|
||||
<Text textStyle="eyebrow" color="accent.deep" mb={4}>
|
||||
{testimonial.shoot}
|
||||
</Text>
|
||||
<Blockquote.Root variant="plain" px={0} gap={7}>
|
||||
<Blockquote.Content
|
||||
fontFamily="heading"
|
||||
fontSize="clamp(1.35rem, 2.2vw, 1.75rem)"
|
||||
lineHeight="1.5"
|
||||
color="ink"
|
||||
>
|
||||
{testimonial.quote}
|
||||
</Blockquote.Content>
|
||||
<Blockquote.Caption
|
||||
textStyle="eyebrow"
|
||||
color="ink.soft"
|
||||
_before={{ content: '"— "' }}
|
||||
>
|
||||
{testimonial.author}
|
||||
</Blockquote.Caption>
|
||||
</Blockquote.Root>
|
||||
</Box>
|
||||
</Grid>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
import { act, fireEvent, screen, within } from '@testing-library/react';
|
||||
import type { UseCarouselProps } from '@chakra-ui/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import homeContent from '@/content/homeContent';
|
||||
import testimonials from '@/content/testimonials';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import { visibleTestimonials } from '@/lib/visibleTestimonials';
|
||||
import Testimonials from './Testimonials';
|
||||
|
||||
// jsdom has no layout, so the carousel (Zag) cannot compute pages and
|
||||
// cannot really move; paging, looping and swiping are Zag's own and are
|
||||
// checked in a real browser (see the unit's code summary). These tests check
|
||||
// our wiring: which API calls our controls make, and what is announced.
|
||||
const carousel = vi.hoisted(() => ({
|
||||
props: undefined as UseCarouselProps | undefined,
|
||||
calls: [] as Array<[string, ...unknown[]]>,
|
||||
}));
|
||||
|
||||
vi.mock('@chakra-ui/react', async (importOriginal) => {
|
||||
const chakra = await importOriginal<typeof import('@chakra-ui/react')>();
|
||||
return {
|
||||
...chakra,
|
||||
useCarousel: (props: UseCarouselProps) => {
|
||||
const api = chakra.useCarousel(props);
|
||||
carousel.props = props;
|
||||
const record =
|
||||
(name: string, real: (...args: never[]) => void) =>
|
||||
(...args: unknown[]) => {
|
||||
carousel.calls.push([name, ...args]);
|
||||
(real as (...args: unknown[]) => void)(...args);
|
||||
};
|
||||
return {
|
||||
...api,
|
||||
scrollNext: record('scrollNext', api.scrollNext),
|
||||
scrollPrev: record('scrollPrev', api.scrollPrev),
|
||||
scrollTo: record('scrollTo', api.scrollTo),
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
carousel.calls = [];
|
||||
vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver);
|
||||
Element.prototype.scrollTo = () => {};
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
FakeIntersectionObserver.reset();
|
||||
// @ts-expect-error -- jsdom has no Element.scrollTo of its own
|
||||
delete Element.prototype.scrollTo;
|
||||
});
|
||||
|
||||
const content = homeContent.testimonials;
|
||||
|
||||
// Renders the section and brings it into view, so the lazily loaded
|
||||
// carousel appears
|
||||
async function renderTestimonials(items = testimonials) {
|
||||
const result = await renderWithProviders(
|
||||
<Testimonials content={content} testimonials={items} />,
|
||||
);
|
||||
act(() => {
|
||||
FakeIntersectionObserver.instances.forEach((observer) =>
|
||||
observer.trigger(true),
|
||||
);
|
||||
});
|
||||
if (visibleTestimonials(items).length > 1) {
|
||||
await screen.findByRole('region', { name: 'Reviews' });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const status = () => screen.getByTestId('testimonials-status');
|
||||
const button = (name: string) => screen.getByRole('button', { name });
|
||||
|
||||
const preferReducedMotion = () =>
|
||||
vi.spyOn(window, 'matchMedia').mockImplementation(
|
||||
(query: string) =>
|
||||
({
|
||||
matches: query.includes('reduce'),
|
||||
media: query,
|
||||
onchange: null,
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
addListener: () => {},
|
||||
removeListener: () => {},
|
||||
dispatchEvent: () => false,
|
||||
}) as MediaQueryList,
|
||||
);
|
||||
|
||||
describe('Testimonials', () => {
|
||||
it('shows the heading at once but loads the carousel only near the screen', async () => {
|
||||
await renderWithProviders(
|
||||
<Testimonials content={content} testimonials={testimonials} />,
|
||||
);
|
||||
expect(
|
||||
screen.getByRole('region', { name: 'Lieve woorden' }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.queryByRole('region', { name: 'Reviews' })).toBeNull();
|
||||
|
||||
act(() => {
|
||||
FakeIntersectionObserver.instances.forEach((observer) =>
|
||||
observer.trigger(true),
|
||||
);
|
||||
});
|
||||
expect(
|
||||
await screen.findByRole('region', { name: 'Reviews' }),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows a looping carousel of the reviews with Dutch labels', async () => {
|
||||
const { container } = await renderTestimonials();
|
||||
const section = screen.getByRole('region', { name: 'Lieve woorden' });
|
||||
const region = within(section).getByRole('region', {
|
||||
name: 'Reviews',
|
||||
});
|
||||
expect(region).toHaveAttribute('aria-roledescription', 'carrousel');
|
||||
expect(carousel.props).toMatchObject({
|
||||
slideCount: 2,
|
||||
loop: true,
|
||||
allowMouseDrag: true,
|
||||
});
|
||||
expect(button('Vorige review')).toBeInTheDocument();
|
||||
expect(button('Volgende review')).toBeInTheDocument();
|
||||
expect(button('Review 1')).toHaveAttribute('aria-current', 'true');
|
||||
expect(button('Review 2')).not.toHaveAttribute('aria-current');
|
||||
const slides = container.querySelectorAll(
|
||||
'[aria-roledescription="slide"]',
|
||||
);
|
||||
expect(slides[0]).toHaveAttribute('aria-label', 'Review 1 van 2');
|
||||
expect(slides[1]).toHaveAttribute('aria-label', 'Review 2 van 2');
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('announces nothing on load and only through its own live region', async () => {
|
||||
const { container } = await renderTestimonials();
|
||||
expect(status()).toHaveTextContent('');
|
||||
const live = [...container.querySelectorAll('[aria-live]')].filter(
|
||||
(element) => element.getAttribute('aria-live') !== 'off',
|
||||
);
|
||||
expect(live).toEqual([status()]);
|
||||
});
|
||||
|
||||
it('announces a page change as "Review n van m"', async () => {
|
||||
await renderTestimonials();
|
||||
act(() => {
|
||||
carousel.props?.onPageChange?.({ page: 1, pageSnapPoint: 0 });
|
||||
});
|
||||
expect(status()).toHaveTextContent('Review 2 van 2');
|
||||
});
|
||||
|
||||
it('moves with the arrows, the dots and the arrow keys', async () => {
|
||||
await renderTestimonials();
|
||||
fireEvent.click(button('Volgende review'));
|
||||
fireEvent.click(button('Vorige review'));
|
||||
fireEvent.click(button('Review 2'));
|
||||
fireEvent.keyDown(button('Review 2'), { key: 'ArrowRight' });
|
||||
fireEvent.keyDown(button('Review 2'), { key: 'ArrowLeft' });
|
||||
fireEvent.keyDown(button('Review 2'), { key: 'Enter' });
|
||||
expect(carousel.calls).toEqual([
|
||||
['scrollNext', false],
|
||||
['scrollPrev', false],
|
||||
['scrollTo', 1, false],
|
||||
['scrollNext', false],
|
||||
['scrollPrev', false],
|
||||
]);
|
||||
});
|
||||
|
||||
it('scrolls instantly for visitors who prefer reduced motion', async () => {
|
||||
preferReducedMotion();
|
||||
await renderTestimonials();
|
||||
fireEvent.click(button('Volgende review'));
|
||||
fireEvent.click(button('Review 1'));
|
||||
fireEvent.keyDown(button('Review 1'), { key: 'ArrowLeft' });
|
||||
expect(carousel.calls).toEqual([
|
||||
['scrollNext', true],
|
||||
['scrollTo', 0, true],
|
||||
['scrollPrev', true],
|
||||
]);
|
||||
});
|
||||
|
||||
it('shows a single review without carousel controls', async () => {
|
||||
await renderTestimonials([testimonials[0]]);
|
||||
expect(
|
||||
screen.getByTestId(`testimonial-${testimonials[0].id}`),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button')).toBeNull();
|
||||
});
|
||||
|
||||
it('leaves the section out when no review is valid', async () => {
|
||||
await renderTestimonials([{ ...testimonials[0], quote: '' }]);
|
||||
expect(
|
||||
screen.queryByRole('region', { name: 'Lieve woorden' }),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
import { lazy, Suspense } from 'react';
|
||||
import { Box } from '@chakra-ui/react';
|
||||
import { DEFERRED_LOAD_MARGIN } from '@/components/DeferredImage/DeferredImage';
|
||||
import HomeSection from '@/components/HomeSection/HomeSection';
|
||||
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';
|
||||
|
||||
// Keeps about the space of the carousel while it loads, so the page below
|
||||
// does not jump when it arrives (measured: ±920px on phones and tablets,
|
||||
// ±570px at 1024px, ±790px at 1440px)
|
||||
const PLACEHOLDER_HEIGHT = { base: '920px', lg: '570px', xl: '790px' };
|
||||
|
||||
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 (
|
||||
<HomeSection
|
||||
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={<Box minH={PLACEHOLDER_HEIGHT} />}
|
||||
>
|
||||
<TestimonialCarousel testimonials={shown} />
|
||||
</Suspense>
|
||||
) : (
|
||||
<Box minH={PLACEHOLDER_HEIGHT} />
|
||||
)}
|
||||
</Box>
|
||||
)}
|
||||
</Reveal>
|
||||
</HomeSection>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user