Adds the Lifestyle fotografie section to the homepage
Adds the first block below the hero: heading, intro and a staggered gallery of four live photos. Brings in the shared section building blocks (HomeSection, SectionHeading, Reveal) and DeferredImage, which holds back below-the-fold photos until the page has loaded so they do not compete with the hero for bandwidth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -121,6 +121,29 @@ describe('app', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('homepage', () => {
|
||||
it('shows the sections below the hero in page order', async () => {
|
||||
const { container } = await renderApp('/');
|
||||
const hero = await screen.findByRole('region', {
|
||||
name: 'Uitgelicht werk',
|
||||
});
|
||||
const sections = [
|
||||
screen.getByRole('region', { name: 'Lifestyle fotografie' }),
|
||||
];
|
||||
|
||||
let previous: Element = hero;
|
||||
for (const section of sections) {
|
||||
expect(
|
||||
previous.compareDocumentPosition(section) &
|
||||
Node.DOCUMENT_POSITION_FOLLOWING,
|
||||
).toBeTruthy();
|
||||
previous = section;
|
||||
}
|
||||
expect(screen.getAllByRole('heading', { level: 1 })).toHaveLength(1);
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
});
|
||||
|
||||
describe('pagina-register', () => {
|
||||
it('bevat precies de routes van de route tree', async () => {
|
||||
const { router } = await renderApp('/');
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 92 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 141 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 137 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 110 KiB |
@@ -0,0 +1,53 @@
|
||||
import { act, screen } from '@testing-library/react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import DeferredImage from './DeferredImage';
|
||||
|
||||
const image = {
|
||||
src: '/foto.jpg',
|
||||
alt: 'Een foto',
|
||||
width: 800,
|
||||
height: 1200,
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
FakeIntersectionObserver.reset();
|
||||
});
|
||||
|
||||
describe('DeferredImage', () => {
|
||||
it('loads straight away without IntersectionObserver once the page has loaded', async () => {
|
||||
await renderWithProviders(<DeferredImage image={image} />);
|
||||
const img = screen.getByRole('img', { name: 'Een foto' });
|
||||
expect(img).toHaveAttribute('src', '/foto.jpg');
|
||||
expect(img).toHaveAttribute('width', '800');
|
||||
expect(img).toHaveAttribute('height', '1200');
|
||||
expect(img).toHaveAttribute('loading', 'lazy');
|
||||
});
|
||||
|
||||
it('waits until the image comes near the viewport', async () => {
|
||||
vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver);
|
||||
await renderWithProviders(<DeferredImage image={image} />);
|
||||
const img = screen.getByRole('img', { name: 'Een foto' });
|
||||
const [observer] = FakeIntersectionObserver.instances;
|
||||
|
||||
expect(img).not.toHaveAttribute('src');
|
||||
expect(observer.options?.rootMargin).toBe('600px 0px');
|
||||
|
||||
act(() => observer.trigger(true));
|
||||
expect(img).toHaveAttribute('src', '/foto.jpg');
|
||||
});
|
||||
|
||||
it('waits for the page load event, even when already in view', async () => {
|
||||
vi.spyOn(document, 'readyState', 'get').mockReturnValue('interactive');
|
||||
await renderWithProviders(<DeferredImage image={image} />);
|
||||
const img = screen.getByRole('img', { name: 'Een foto' });
|
||||
expect(img).not.toHaveAttribute('src');
|
||||
|
||||
act(() => {
|
||||
window.dispatchEvent(new Event('load'));
|
||||
});
|
||||
expect(img).toHaveAttribute('src', '/foto.jpg');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Image, type ImageProps } from '@chakra-ui/react';
|
||||
import usePageLoaded from '@/hooks/usePageLoaded';
|
||||
import useInView from '@/hooks/useInView';
|
||||
import type { ContentImage } from '@/content/types';
|
||||
|
||||
// How far below the viewport an image starts loading
|
||||
const LOAD_MARGIN = '600px 0px';
|
||||
|
||||
type DeferredImageProps = Omit<ImageProps, 'src' | 'alt'> & {
|
||||
image: ContentImage;
|
||||
};
|
||||
|
||||
// A below-the-fold image that only starts downloading once the page has
|
||||
// finished loading and the image is near the viewport. Native
|
||||
// loading="lazy" is not enough: Chrome fetches lazy images up to ±1250px
|
||||
// below the screen straight away, so photos right under the hero compete
|
||||
// with it for bandwidth and delay the largest contentful paint.
|
||||
// Width and height reserve the space, so nothing shifts when it loads.
|
||||
export default function DeferredImage({ image, ...rest }: DeferredImageProps) {
|
||||
const pageLoaded = usePageLoaded();
|
||||
const { ref, inView } = useInView<HTMLImageElement>({
|
||||
rootMargin: LOAD_MARGIN,
|
||||
});
|
||||
|
||||
return (
|
||||
<Image
|
||||
ref={ref}
|
||||
src={pageLoaded && inView ? image.src : undefined}
|
||||
alt={image.alt}
|
||||
htmlWidth={image.width}
|
||||
htmlHeight={image.height}
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import HomeSection from './HomeSection';
|
||||
|
||||
describe('HomeSection', () => {
|
||||
it('is a region named by its heading', async () => {
|
||||
const { container } = await renderWithProviders(
|
||||
<HomeSection labelledBy="about-title" data-testid="about">
|
||||
<h2 id="about-title">Over mij</h2>
|
||||
</HomeSection>,
|
||||
);
|
||||
expect(screen.getByRole('region', { name: 'Over mij' })).toBe(
|
||||
screen.getByTestId('about'),
|
||||
);
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('passes the anchor id through for every tone, width and spacing', async () => {
|
||||
await renderWithProviders(
|
||||
<HomeSection
|
||||
labelledBy="contact-title"
|
||||
id="contact"
|
||||
tone="alt"
|
||||
width="narrow"
|
||||
spacing="tight"
|
||||
>
|
||||
<h2 id="contact-title">Contact</h2>
|
||||
</HomeSection>,
|
||||
);
|
||||
expect(screen.getByRole('region', { name: 'Contact' })).toHaveAttribute(
|
||||
'id',
|
||||
'contact',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Box, Container } from '@chakra-ui/react';
|
||||
|
||||
type HomeSectionProps = {
|
||||
// Id of the heading that names this section for assistive technology
|
||||
labelledBy: string;
|
||||
id?: string;
|
||||
tone?: 'paper' | 'alt';
|
||||
width?: 'default' | 'narrow';
|
||||
spacing?: 'default' | 'tight';
|
||||
'data-testid'?: string;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
// Frame of a homepage section (design .section): vertical rhythm, an
|
||||
// optional alternate background and the content column.
|
||||
export default function HomeSection({
|
||||
labelledBy,
|
||||
id,
|
||||
tone = 'paper',
|
||||
width = 'default',
|
||||
spacing = 'default',
|
||||
'data-testid': testId,
|
||||
children,
|
||||
}: HomeSectionProps) {
|
||||
return (
|
||||
<Box
|
||||
as="section"
|
||||
id={id}
|
||||
aria-labelledby={labelledBy}
|
||||
data-testid={testId}
|
||||
bg={tone === 'alt' ? 'paper.alt' : 'paper'}
|
||||
py={spacing === 'tight' ? 'sectionYTight' : 'sectionY'}
|
||||
>
|
||||
<Container maxW={width === 'narrow' ? 'contentNarrow' : 'content'}>
|
||||
{children}
|
||||
</Container>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import Reveal from './Reveal';
|
||||
|
||||
describe('Reveal', () => {
|
||||
it('renders its content visibly when IntersectionObserver is missing', async () => {
|
||||
await renderWithProviders(
|
||||
<Reveal data-testid="reveal">
|
||||
<p>Inhoud</p>
|
||||
</Reveal>,
|
||||
);
|
||||
expect(screen.getByText('Inhoud')).toBeVisible();
|
||||
expect(screen.getByTestId('reveal')).toHaveAttribute(
|
||||
'data-reveal',
|
||||
'visible',
|
||||
);
|
||||
});
|
||||
|
||||
it('passes element props through, e.g. to render a figure', async () => {
|
||||
await renderWithProviders(
|
||||
<Reveal as="figure" delay={0.16} data-testid="reveal">
|
||||
<span>Foto</span>
|
||||
</Reveal>,
|
||||
);
|
||||
expect(screen.getByTestId('reveal').tagName).toBe('FIGURE');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Box, type BoxProps } from '@chakra-ui/react';
|
||||
import useReveal from '@/hooks/useReveal';
|
||||
|
||||
type RevealProps = BoxProps & {
|
||||
// Seconds to wait before fading in, to stagger items in a row
|
||||
delay?: number;
|
||||
};
|
||||
|
||||
// Fades its content in and up once it scrolls into view (design .reveal).
|
||||
// The content is always in the DOM and readable by assistive technology;
|
||||
// only its opacity and position change.
|
||||
export default function Reveal({ delay = 0, children, ...rest }: RevealProps) {
|
||||
const { ref, visible } = useReveal<HTMLDivElement>();
|
||||
|
||||
return (
|
||||
<Box
|
||||
ref={ref}
|
||||
data-reveal={visible ? 'visible' : 'hidden'}
|
||||
opacity={visible ? 1 : 0}
|
||||
transform={visible ? 'none' : 'translateY(24px)'}
|
||||
transitionProperty="opacity, transform"
|
||||
transitionDuration="reveal"
|
||||
transitionTimingFunction="smooth"
|
||||
transitionDelay={`${delay}s`}
|
||||
{...rest}
|
||||
>
|
||||
{children}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import SectionHeading from './SectionHeading';
|
||||
|
||||
describe('SectionHeading', () => {
|
||||
it('renders the label, an h2 with its id and the lead', async () => {
|
||||
const { container } = await renderWithProviders(
|
||||
<SectionHeading
|
||||
id="prices-title"
|
||||
eyebrow="Pakketten"
|
||||
title="Prijzen"
|
||||
lead="Kies wat bij je past."
|
||||
/>,
|
||||
);
|
||||
const heading = screen.getByRole('heading', { level: 2 });
|
||||
expect(heading).toHaveTextContent('Prijzen');
|
||||
expect(heading).toHaveAttribute('id', 'prices-title');
|
||||
expect(screen.getByText('Pakketten')).toBeInTheDocument();
|
||||
expect(screen.getByText('Kies wat bij je past.')).toBeInTheDocument();
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('works with only a title', async () => {
|
||||
const { container } = await renderWithProviders(
|
||||
<SectionHeading id="t" title="Instagram" align="start" />,
|
||||
);
|
||||
expect(
|
||||
screen.getByRole('heading', { level: 2, name: 'Instagram' }),
|
||||
).toBeInTheDocument();
|
||||
expect(container.querySelectorAll('p')).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Box, Heading, Text } from '@chakra-ui/react';
|
||||
|
||||
type SectionHeadingProps = {
|
||||
// Referenced by the section's aria-labelledby
|
||||
id: string;
|
||||
title: string;
|
||||
eyebrow?: string;
|
||||
lead?: string;
|
||||
align?: 'center' | 'start';
|
||||
};
|
||||
|
||||
// Heading block of a homepage section (design .section__head): optional
|
||||
// small label, the h2 and an optional intro sentence.
|
||||
export default function SectionHeading({
|
||||
id,
|
||||
title,
|
||||
eyebrow,
|
||||
lead,
|
||||
align = 'center',
|
||||
}: SectionHeadingProps) {
|
||||
const centered = align === 'center';
|
||||
|
||||
return (
|
||||
<Box
|
||||
maxW="620px"
|
||||
mx={centered ? 'auto' : undefined}
|
||||
mb="sectionHeadGap"
|
||||
textAlign={centered ? 'center' : 'start'}
|
||||
>
|
||||
{eyebrow && (
|
||||
// accent.deep instead of the design's accent: meets 4.5:1 (NFR-HP-01)
|
||||
<Text textStyle="eyebrow" color="accent.deep" mb={4}>
|
||||
{eyebrow}
|
||||
</Text>
|
||||
)}
|
||||
<Heading as="h2" id={id} textStyle="sectionTitle">
|
||||
{title}
|
||||
</Heading>
|
||||
{lead && (
|
||||
<Text textStyle="lead" color="ink.soft" mt={2}>
|
||||
{lead}
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { screen, within } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import homeContent from '@/content/homeContent';
|
||||
import { axeResults } from '@/test/axe';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import IntroGallery from './IntroGallery';
|
||||
|
||||
describe('IntroGallery', () => {
|
||||
it('shows the heading, the lead and the four photos', async () => {
|
||||
const { container } = await renderWithProviders(
|
||||
<IntroGallery content={homeContent.intro} />,
|
||||
);
|
||||
const section = screen.getByRole('region', {
|
||||
name: 'Lifestyle fotografie',
|
||||
});
|
||||
expect(
|
||||
within(section).getByRole('heading', { level: 2 }),
|
||||
).toHaveTextContent('Lifestyle fotografie');
|
||||
expect(
|
||||
within(section).getByText(homeContent.intro.lead),
|
||||
).toBeInTheDocument();
|
||||
|
||||
const images = within(section).getAllByRole('img');
|
||||
expect(images).toHaveLength(4);
|
||||
images.forEach((image, index) => {
|
||||
const photo = homeContent.intro.photos[index];
|
||||
expect(image).toHaveAttribute('alt', photo.alt);
|
||||
expect(image).toHaveAttribute('loading', 'lazy');
|
||||
expect(image).toHaveAttribute('width', String(photo.width));
|
||||
expect(image).toHaveAttribute('height', String(photo.height));
|
||||
});
|
||||
expect(await axeResults(container)).toHaveNoViolations();
|
||||
});
|
||||
|
||||
it('shows every photo as a figure, in order', async () => {
|
||||
await renderWithProviders(<IntroGallery content={homeContent.intro} />);
|
||||
homeContent.intro.photos.forEach((photo, index) => {
|
||||
const figure = screen.getByTestId(
|
||||
`intro-gallery-photo-${index + 1}`,
|
||||
);
|
||||
expect(figure.tagName).toBe('FIGURE');
|
||||
expect(within(figure).getByRole('img')).toHaveAttribute(
|
||||
'alt',
|
||||
photo.alt,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('still renders the heading when there are no photos', async () => {
|
||||
await renderWithProviders(
|
||||
<IntroGallery content={{ ...homeContent.intro, photos: [] }} />,
|
||||
);
|
||||
expect(
|
||||
screen.getByRole('heading', { name: 'Lifestyle fotografie' }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.queryAllByRole('img')).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
import { Box, Grid } from '@chakra-ui/react';
|
||||
import DeferredImage 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 } from '@/content/types';
|
||||
|
||||
const TITLE_ID = 'intro-title';
|
||||
|
||||
// Every other photo sits lower, so the row is not a rigid block (design
|
||||
// .intro-gallery figure:nth-child(2|4))
|
||||
const OFFSET = 'clamp(1.5rem, 4vw, 3.5rem)';
|
||||
|
||||
// Delay between photos fading in, as in the design's .reveal:nth-child
|
||||
const STAGGER = 0.08;
|
||||
|
||||
type IntroGalleryProps = {
|
||||
content: HomeContent['intro'];
|
||||
};
|
||||
|
||||
// "Lifestyle fotografie": heading with intro and a gallery of four photos
|
||||
// (design .intro-gallery). 4 columns on desktop, 2 on tablet, 1 on phones.
|
||||
export default function IntroGallery({ content }: IntroGalleryProps) {
|
||||
return (
|
||||
<HomeSection labelledBy={TITLE_ID} data-testid="intro-gallery">
|
||||
<Reveal>
|
||||
<SectionHeading
|
||||
id={TITLE_ID}
|
||||
title={content.title}
|
||||
lead={content.lead}
|
||||
/>
|
||||
</Reveal>
|
||||
<Grid
|
||||
templateColumns={{
|
||||
base: '1fr',
|
||||
sm: 'repeat(2, 1fr)',
|
||||
lg: 'repeat(4, 1fr)',
|
||||
}}
|
||||
gap="gridGap"
|
||||
alignItems="start"
|
||||
>
|
||||
{content.photos.map((photo, index) => (
|
||||
<Reveal
|
||||
key={photo.src}
|
||||
as="figure"
|
||||
delay={index * STAGGER}
|
||||
mt={index % 2 === 1 ? { base: 0, sm: OFFSET } : 0}
|
||||
data-testid={`intro-gallery-photo-${index + 1}`}
|
||||
>
|
||||
<Box
|
||||
overflow="hidden"
|
||||
// Shown until the deferred photo arrives
|
||||
bg="paper.deep"
|
||||
css={{
|
||||
'&:hover img': { transform: 'scale(1.05)' },
|
||||
}}
|
||||
>
|
||||
<DeferredImage
|
||||
image={photo}
|
||||
w="100%"
|
||||
h="auto"
|
||||
aspectRatio="3 / 4"
|
||||
objectFit="cover"
|
||||
transitionProperty="transform"
|
||||
transitionDuration="fade"
|
||||
transitionTimingFunction="smooth"
|
||||
_motionReduce={{ transition: 'none' }}
|
||||
/>
|
||||
</Box>
|
||||
</Reveal>
|
||||
))}
|
||||
</Grid>
|
||||
</HomeSection>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { HomeContent } from './types';
|
||||
|
||||
// Images
|
||||
import intro1 from '../assets/home/intro-1.jpg';
|
||||
import intro2 from '../assets/home/intro-2.jpg';
|
||||
import intro3 from '../assets/home/intro-3.jpg';
|
||||
import intro4 from '../assets/home/intro-4.jpg';
|
||||
|
||||
// Static content of the homepage sections until the CMS API exists; the API
|
||||
// will then deliver this object.
|
||||
const homeContent: HomeContent = {
|
||||
intro: {
|
||||
title: 'Lifestyle fotografie',
|
||||
lead: 'Jouw favoriete momenten, vastgelegd op een natuurlijke en ongedwongen manier.',
|
||||
photos: [
|
||||
{
|
||||
src: intro1,
|
||||
alt: 'Babyhandje in de hand van de moeder, op een gebreid dekentje',
|
||||
width: 800,
|
||||
height: 1200,
|
||||
},
|
||||
{
|
||||
src: intro2,
|
||||
alt: 'Moeder drukt haar pasgeboren baby in een gebreid truitje tegen zich aan',
|
||||
width: 800,
|
||||
height: 1200,
|
||||
},
|
||||
{
|
||||
src: intro3,
|
||||
alt: 'Zwangere vrouw op blote voeten in het zand bij zonsondergang',
|
||||
width: 800,
|
||||
height: 1200,
|
||||
},
|
||||
{
|
||||
src: intro4,
|
||||
alt: 'Aanstaande vader omarmt zijn zwangere partner in de duinen',
|
||||
width: 800,
|
||||
height: 1200,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
export default homeContent;
|
||||
@@ -21,6 +21,24 @@ export interface SiteContent {
|
||||
email: EmailAddress;
|
||||
}
|
||||
|
||||
// An image as the API will deliver it. width and height are the intrinsic
|
||||
// size, so the browser can reserve space before the image loads.
|
||||
export interface ContentImage {
|
||||
src: string;
|
||||
alt: string;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
// Content of the homepage sections below the hero
|
||||
export interface HomeContent {
|
||||
intro: {
|
||||
title: string;
|
||||
lead: string;
|
||||
photos: ContentImage[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface HeroSlide {
|
||||
id: string;
|
||||
src: string;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
type UseInViewOptions = {
|
||||
threshold?: number;
|
||||
rootMargin?: string;
|
||||
// Skip observing and report "in view" right away
|
||||
skip?: boolean;
|
||||
};
|
||||
|
||||
// Tells whether an element has come into (or near) the viewport, once.
|
||||
// Without IntersectionObserver the element counts as in view right away, so
|
||||
// content is never left hidden or unloaded.
|
||||
export default function useInView<T extends Element>({
|
||||
threshold = 0,
|
||||
rootMargin = '0px',
|
||||
skip = false,
|
||||
}: UseInViewOptions = {}) {
|
||||
const ref = useRef<T>(null);
|
||||
const supported = typeof IntersectionObserver !== 'undefined';
|
||||
const [intersected, setIntersected] = useState(false);
|
||||
const immediate = skip || !supported;
|
||||
|
||||
useEffect(() => {
|
||||
const element = ref.current;
|
||||
if (!element || immediate || intersected) return;
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
if (entries.some((entry) => entry.isIntersecting)) {
|
||||
setIntersected(true);
|
||||
observer.disconnect();
|
||||
}
|
||||
},
|
||||
{ threshold, rootMargin },
|
||||
);
|
||||
observer.observe(element);
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, [immediate, intersected, threshold, rootMargin]);
|
||||
|
||||
return { ref, inView: immediate || intersected };
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import usePageLoaded from './usePageLoaded';
|
||||
|
||||
describe('usePageLoaded', () => {
|
||||
it('is true when the page had already loaded', () => {
|
||||
const { result } = renderHook(() => usePageLoaded());
|
||||
expect(result.current).toBe(true);
|
||||
});
|
||||
|
||||
it('turns true on the load event', () => {
|
||||
vi.spyOn(document, 'readyState', 'get').mockReturnValue('interactive');
|
||||
const { result } = renderHook(() => usePageLoaded());
|
||||
expect(result.current).toBe(false);
|
||||
|
||||
act(() => {
|
||||
window.dispatchEvent(new Event('load'));
|
||||
});
|
||||
expect(result.current).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
// True once the window's load event has fired (or had already fired).
|
||||
export default function usePageLoaded() {
|
||||
const [loaded, setLoaded] = useState(
|
||||
() => document.readyState === 'complete',
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (loaded) return;
|
||||
const onLoad = () => setLoaded(true);
|
||||
window.addEventListener('load', onLoad, { once: true });
|
||||
return () => window.removeEventListener('load', onLoad);
|
||||
}, [loaded]);
|
||||
|
||||
return loaded;
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { act, render, screen } from '@testing-library/react';
|
||||
import { ChakraProvider } from '@chakra-ui/react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { FakeIntersectionObserver as FakeObserver } from '@/test/fakeIntersectionObserver';
|
||||
import system from '../theme';
|
||||
import useReveal from './useReveal';
|
||||
|
||||
function Probe() {
|
||||
const { ref, visible } = useReveal<HTMLDivElement>();
|
||||
return (
|
||||
<div ref={ref} data-testid="probe">
|
||||
{visible ? 'visible' : 'hidden'}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const renderProbe = () =>
|
||||
render(
|
||||
<ChakraProvider value={system}>
|
||||
<Probe />
|
||||
</ChakraProvider>,
|
||||
);
|
||||
|
||||
const mockReducedMotion = () =>
|
||||
vi.spyOn(window, 'matchMedia').mockImplementation(
|
||||
(query: string) =>
|
||||
({
|
||||
matches: query.includes('reduce'),
|
||||
media: query,
|
||||
onchange: null,
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
addListener: () => {},
|
||||
removeListener: () => {},
|
||||
dispatchEvent: () => false,
|
||||
}) as MediaQueryList,
|
||||
);
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
FakeObserver.reset();
|
||||
});
|
||||
|
||||
describe('useReveal', () => {
|
||||
it('is visible right away without IntersectionObserver', () => {
|
||||
renderProbe();
|
||||
expect(screen.getByTestId('probe')).toHaveTextContent('visible');
|
||||
});
|
||||
|
||||
it('is visible right away when the visitor prefers reduced motion', () => {
|
||||
vi.stubGlobal('IntersectionObserver', FakeObserver);
|
||||
mockReducedMotion();
|
||||
renderProbe();
|
||||
expect(screen.getByTestId('probe')).toHaveTextContent('visible');
|
||||
expect(FakeObserver.instances).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('stays hidden until the element scrolls into view, then stops observing', () => {
|
||||
vi.stubGlobal('IntersectionObserver', FakeObserver);
|
||||
renderProbe();
|
||||
const probe = screen.getByTestId('probe');
|
||||
const [observer] = FakeObserver.instances;
|
||||
|
||||
expect(probe).toHaveTextContent('hidden');
|
||||
expect(observer.observed).toEqual([probe]);
|
||||
expect(observer.options).toEqual({
|
||||
threshold: 0.12,
|
||||
rootMargin: '0px 0px -60px 0px',
|
||||
});
|
||||
|
||||
act(() => observer.trigger(false));
|
||||
expect(probe).toHaveTextContent('hidden');
|
||||
|
||||
act(() => observer.trigger(true));
|
||||
expect(probe).toHaveTextContent('visible');
|
||||
expect(observer.disconnected).toBe(true);
|
||||
expect(FakeObserver.instances).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('disconnects when the element unmounts before it was seen', () => {
|
||||
vi.stubGlobal('IntersectionObserver', FakeObserver);
|
||||
const { unmount } = renderProbe();
|
||||
unmount();
|
||||
expect(FakeObserver.instances[0].disconnected).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { useMediaQuery } from '@chakra-ui/react';
|
||||
import useInView from './useInView';
|
||||
|
||||
// Tells whether an element has scrolled into view, once, using the design's
|
||||
// initReveal trigger: a little of the element must be on screen, and not
|
||||
// right at the bottom edge. When the visitor prefers reduced motion the
|
||||
// element counts as visible right away.
|
||||
export default function useReveal<T extends Element>() {
|
||||
const [reducedMotion] = useMediaQuery(
|
||||
['(prefers-reduced-motion: reduce)'],
|
||||
{ ssr: false },
|
||||
);
|
||||
const { ref, inView } = useInView<T>({
|
||||
threshold: 0.12,
|
||||
rootMargin: '0px 0px -60px 0px',
|
||||
skip: reducedMotion,
|
||||
});
|
||||
|
||||
return { ref, visible: inView };
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import { createFileRoute } from '@tanstack/react-router';
|
||||
import Hero from '../components/Hero/Hero';
|
||||
import IntroGallery from '../components/home/IntroGallery/IntroGallery';
|
||||
import heroContent from '../content/heroContent';
|
||||
import homeContent from '../content/homeContent';
|
||||
import siteContent from '../content/siteContent';
|
||||
import { pageHead } from '../seo';
|
||||
|
||||
@@ -9,6 +11,12 @@ export const Route = createFileRoute('/')({
|
||||
component: Index,
|
||||
});
|
||||
|
||||
// The homepage: the hero, then the sections in the order of LAYOUT.md
|
||||
function Index() {
|
||||
return <Hero content={heroContent} socials={siteContent.socials} />;
|
||||
return (
|
||||
<>
|
||||
<Hero content={heroContent} socials={siteContent.socials} />
|
||||
<IntroGallery content={homeContent.intro} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// A controllable IntersectionObserver for tests: install it with
|
||||
// vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver) and call
|
||||
// trigger() to simulate an element coming into view.
|
||||
export class FakeIntersectionObserver {
|
||||
static instances: FakeIntersectionObserver[] = [];
|
||||
observed: Element[] = [];
|
||||
disconnected = false;
|
||||
readonly callback: IntersectionObserverCallback;
|
||||
readonly options?: IntersectionObserverInit;
|
||||
|
||||
constructor(
|
||||
callback: IntersectionObserverCallback,
|
||||
options?: IntersectionObserverInit,
|
||||
) {
|
||||
this.callback = callback;
|
||||
this.options = options;
|
||||
FakeIntersectionObserver.instances.push(this);
|
||||
}
|
||||
|
||||
observe(element: Element) {
|
||||
this.observed.push(element);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.disconnected = true;
|
||||
}
|
||||
|
||||
trigger(isIntersecting: boolean) {
|
||||
this.callback(
|
||||
[{ isIntersecting } as IntersectionObserverEntry],
|
||||
this as unknown as IntersectionObserver,
|
||||
);
|
||||
}
|
||||
|
||||
static reset() {
|
||||
FakeIntersectionObserver.instances = [];
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,13 @@ const config = defineConfig({
|
||||
},
|
||||
spacing: {
|
||||
gutter: { value: 'clamp(1.25rem, 4vw, 3rem)' }, // zijmarge van de pagina
|
||||
// Vertical rhythm of homepage sections (design --section-y)
|
||||
sectionY: { value: 'clamp(4.5rem, 9vw, 8.5rem)' },
|
||||
sectionYTight: { value: 'clamp(2.7rem, 5.4vw, 5.1rem)' },
|
||||
// Space below a section heading (design .section__head)
|
||||
sectionHeadGap: { value: 'clamp(2.5rem, 5vw, 4.5rem)' },
|
||||
// Gap between grid items such as photos and cards (design --gap)
|
||||
gridGap: { value: 'clamp(1rem, 2.2vw, 2rem)' },
|
||||
},
|
||||
easings: {
|
||||
smooth: { value: 'cubic-bezier(.22, .61, .36, 1)' },
|
||||
@@ -78,6 +85,7 @@ const config = defineConfig({
|
||||
durations: {
|
||||
interaction: { value: '0.45s' }, // hover, focus
|
||||
fade: { value: '1.2s' }, // overvloeien van foto's
|
||||
reveal: { value: '0.8s' }, // fade-in of sections on scroll
|
||||
},
|
||||
// Stapelvolgorde van de vaste shell-onderdelen, van laag naar hoog
|
||||
zIndex: {
|
||||
@@ -133,6 +141,8 @@ const config = defineConfig({
|
||||
},
|
||||
// Maximale breedte van de content (Container en de to-top-knop)
|
||||
content: { value: '{sizes.8xl}' },
|
||||
// Narrow content column, e.g. the contact form (design --container-narrow)
|
||||
contentNarrow: { value: '820px' },
|
||||
},
|
||||
spacing: {
|
||||
// Zelfde waarde voor content die onder de balk begint (pt="header")
|
||||
@@ -160,6 +170,22 @@ const config = defineConfig({
|
||||
lineHeight: '1',
|
||||
},
|
||||
},
|
||||
// The h2 of a homepage section (SectionHeading)
|
||||
sectionTitle: {
|
||||
value: {
|
||||
fontFamily: 'heading',
|
||||
fontSize: { base: '1.85rem', md: '2.125rem', xl: '2.5rem' },
|
||||
lineHeight: '1.15',
|
||||
},
|
||||
},
|
||||
// Intro sentence below a section heading
|
||||
lead: {
|
||||
value: {
|
||||
fontSize: '1.25rem',
|
||||
fontWeight: '300',
|
||||
lineHeight: '1.6',
|
||||
},
|
||||
},
|
||||
// De h1 van een gewone pagina (PageIntro, StatusPage)
|
||||
pageTitle: {
|
||||
value: {
|
||||
|
||||
Reference in New Issue
Block a user