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:
@@ -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 };
|
||||
}
|
||||
Reference in New Issue
Block a user