Thema-fundament: variant (B/E) x modus (licht/donker), tijdreis-knop en overgang
Vervangt rood/paars door tokens per variant en modus, voegt ModeToggle en icoon-knop VariantToggle toe, VariantRenderer met lazy E-chunk, dither-overgang (uit bij minder beweging), theme-init.js tegen themaflits en tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { Component } from 'react';
|
||||
import type { ErrorInfo, ReactNode } from 'react';
|
||||
|
||||
interface EBoundaryProps {
|
||||
children: ReactNode;
|
||||
/** Falls back to variant B and shows the alert (BR-U1-8). */
|
||||
onError: () => void;
|
||||
}
|
||||
|
||||
/** Error boundary around the lazy E variant: a crash in E never takes the whole site down. */
|
||||
export class EBoundary extends Component<EBoundaryProps, { failed: boolean }> {
|
||||
state = { failed: false };
|
||||
|
||||
static getDerivedStateFromError() {
|
||||
return { failed: true };
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, info: ErrorInfo) {
|
||||
console.error('Variant E crashed', error, info.componentStack);
|
||||
this.props.onError();
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.failed) {
|
||||
return null;
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { useTheme } from '../../theme/ThemeProvider';
|
||||
|
||||
/** Polite live region that announces variant changes to screen readers (BR-U1-7). */
|
||||
export function LiveRegion() {
|
||||
const { announcement } = useTheme();
|
||||
|
||||
return (
|
||||
<div role="status" aria-live="polite" className="sr-only" data-testid="live-region">
|
||||
{announcement}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState } from 'react';
|
||||
import { navLinks } from '../../features/landing/data/content';
|
||||
import { ThemeToggle } from '../ui/ThemeToggle';
|
||||
import { ModeToggle } from '../ui/ModeToggle';
|
||||
import { VariantToggle } from '../ui/VariantToggle';
|
||||
import { handleAnchorClick } from '../../utils/scrollToHash';
|
||||
|
||||
export function Nav() {
|
||||
@@ -21,7 +22,7 @@ export function Nav() {
|
||||
>
|
||||
SLP<span className="text-accent">.</span>Software
|
||||
</a>
|
||||
<ul className="flex items-center gap-7">
|
||||
<ul className="flex items-center gap-3 sm:gap-7">
|
||||
{navLinks.map((link) =>
|
||||
link.isCta ? (
|
||||
<li key={link.href} className="hidden sm:block">
|
||||
@@ -47,7 +48,10 @@ export function Nav() {
|
||||
),
|
||||
)}
|
||||
<li>
|
||||
<ThemeToggle />
|
||||
<ModeToggle />
|
||||
</li>
|
||||
<li>
|
||||
<VariantToggle />
|
||||
</li>
|
||||
<li className="sm:hidden">
|
||||
<button
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { Nav } from './Nav';
|
||||
import { Footer } from './Footer';
|
||||
import { VariantRenderer } from './VariantRenderer';
|
||||
import { TransitionOverlay } from './TransitionOverlay';
|
||||
import { LiveRegion } from './LiveRegion';
|
||||
import { VariantAlert } from './VariantAlert';
|
||||
import { UmamiAnalytics } from '../shared/UmamiAnalytics';
|
||||
import { SentryTestButton } from '../shared/SentryTestButton';
|
||||
|
||||
@@ -8,9 +12,20 @@ export function RootLayout({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<div className="font-sans min-h-screen bg-bg text-text">
|
||||
<UmamiAnalytics />
|
||||
<Nav />
|
||||
{children}
|
||||
<Footer />
|
||||
<VariantRenderer
|
||||
b={
|
||||
<>
|
||||
<Nav />
|
||||
<main id="main" tabIndex={-1} className="outline-none">
|
||||
{children}
|
||||
</main>
|
||||
<Footer />
|
||||
</>
|
||||
}
|
||||
/>
|
||||
<TransitionOverlay />
|
||||
<LiveRegion />
|
||||
<VariantAlert />
|
||||
<SentryTestButton />
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { usePrefersReducedMotion } from '../../shared/hooks/usePrefersReducedMotion';
|
||||
import { useTheme } from '../../theme/ThemeProvider';
|
||||
|
||||
/**
|
||||
* Pixel-dither overlay of the time-travel transition (BR-U1-6). Renders nothing while idle and
|
||||
* nothing at all under prefers-reduced-motion (BR-U1-9).
|
||||
*/
|
||||
export function TransitionOverlay() {
|
||||
const { transition } = useTheme();
|
||||
const reducedMotion = usePrefersReducedMotion();
|
||||
|
||||
if (reducedMotion || transition.phase === 'idle') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className="tt-overlay"
|
||||
data-phase={transition.phase}
|
||||
data-testid="transition-overlay"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { useTheme } from '../../theme/ThemeProvider';
|
||||
|
||||
/** Alert shown when the time-travel variant could not be loaded or crashed (BR-U1-8). */
|
||||
export function VariantAlert() {
|
||||
const { variantError, retryVariant, dismissVariantError } = useTheme();
|
||||
|
||||
if (!variantError) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
role="alert"
|
||||
data-testid="variant-alert"
|
||||
className="fixed bottom-4 left-1/2 z-[110] flex w-[min(92vw,28rem)] -translate-x-1/2 flex-wrap items-center gap-3 rounded border border-accent-line bg-surface px-4 py-3 text-sm text-text shadow-lg"
|
||||
>
|
||||
<span className="flex-1">De tijdreis kon niet worden geladen.</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={retryVariant}
|
||||
data-testid="variant-alert-retry"
|
||||
className="rounded border border-line px-3 py-1.5 font-medium hover:border-accent-line"
|
||||
>
|
||||
Opnieuw proberen
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={dismissVariantError}
|
||||
aria-label="Melding sluiten"
|
||||
data-testid="variant-alert-dismiss"
|
||||
className="rounded px-2 py-1.5 text-muted hover:text-text"
|
||||
>
|
||||
Sluiten
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { lazy, Suspense } from 'react';
|
||||
import type { ReactNode } from 'react';
|
||||
import { useTheme } from '../../theme/ThemeProvider';
|
||||
import { EBoundary } from './EBoundary';
|
||||
|
||||
// Variant E is its own chunk (FR-6): visitors who never use the easter egg never download it.
|
||||
const LandingE = lazy(() => import('../../variants/e/LandingE'));
|
||||
|
||||
/**
|
||||
* Renders the eager B shell or the lazy E landing depending on the current variant. The
|
||||
* transition (see ThemeProvider) preloads the E chunk before the swap, so the Suspense fallback is
|
||||
* only a safety net.
|
||||
*/
|
||||
export function VariantRenderer({ b }: { b: ReactNode }) {
|
||||
const { variant, reportVariantError } = useTheme();
|
||||
|
||||
if (variant === 'b') {
|
||||
return <>{b}</>;
|
||||
}
|
||||
|
||||
return (
|
||||
<EBoundary onError={reportVariantError}>
|
||||
<Suspense fallback={null}>
|
||||
<LandingE />
|
||||
</Suspense>
|
||||
</EBoundary>
|
||||
);
|
||||
}
|
||||
@@ -14,7 +14,6 @@ function renderNav() {
|
||||
describe('Nav', () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
document.documentElement.className = '';
|
||||
});
|
||||
|
||||
it('renders all nav links including the CTA', () => {
|
||||
@@ -25,14 +24,24 @@ describe('Nav', () => {
|
||||
expect(screen.getByTestId('nav-cta-link')).toHaveTextContent('Start project');
|
||||
});
|
||||
|
||||
it('renders the theme toggle with a Dutch accessible label', () => {
|
||||
it('renders the mode toggle and the icon-only variant toggle with Dutch labels', () => {
|
||||
renderNav();
|
||||
expect(screen.getByRole('button', { name: 'Wissel kleurthema' })).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Donkere weergave' })).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByRole('button', { name: 'Tijdreis: bekijk de site zoals vroeger' }),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles the theme when the toggle button is clicked', () => {
|
||||
it('puts the variant toggle right after the mode toggle', () => {
|
||||
renderNav();
|
||||
const toggle = screen.getByTestId('theme-toggle-button');
|
||||
const mode = screen.getByTestId('mode-toggle-button');
|
||||
const variant = screen.getByTestId('variant-toggle-button');
|
||||
expect(mode.compareDocumentPosition(variant) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
||||
});
|
||||
|
||||
it('toggles the mode when the mode toggle is clicked', () => {
|
||||
renderNav();
|
||||
const toggle = screen.getByTestId('mode-toggle-button');
|
||||
expect(toggle).toHaveAttribute('aria-pressed', 'false');
|
||||
fireEvent.click(toggle);
|
||||
expect(toggle).toHaveAttribute('aria-pressed', 'true');
|
||||
@@ -58,11 +67,10 @@ describe('Nav', () => {
|
||||
renderNav();
|
||||
fireEvent.click(screen.getByTestId('nav-mobile-toggle'));
|
||||
const mobileMenu = screen.getByTestId('nav-mobile-menu');
|
||||
|
||||
const link = mobileMenu.querySelector('a[href="#pakketten"]');
|
||||
expect(mobileMenu).toBeInTheDocument();
|
||||
const link = mobileMenu.querySelector('a');
|
||||
expect(link).not.toBeNull();
|
||||
fireEvent.click(link as HTMLAnchorElement);
|
||||
|
||||
expect(screen.queryByTestId('nav-mobile-menu')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { VariantRenderer } from '../VariantRenderer';
|
||||
import { TransitionOverlay } from '../TransitionOverlay';
|
||||
import { LiveRegion } from '../LiveRegion';
|
||||
import { VariantAlert } from '../VariantAlert';
|
||||
import { EBoundary } from '../EBoundary';
|
||||
import { ThemeProvider } from '../../../theme/ThemeProvider';
|
||||
import { VariantToggle } from '../../ui/VariantToggle';
|
||||
import { clearMatchMedia, mockMatchMedia } from '../../../test/matchMedia';
|
||||
import { REDUCED_MOTION_QUERY } from '../../../shared/hooks/usePrefersReducedMotion';
|
||||
|
||||
function renderApp(loadVariant: (v: 'b' | 'e') => Promise<unknown> = () => Promise.resolve()) {
|
||||
return render(
|
||||
<ThemeProvider loadVariant={loadVariant}>
|
||||
<VariantRenderer
|
||||
b={
|
||||
<>
|
||||
<VariantToggle />
|
||||
<main id="main" tabIndex={-1}>
|
||||
Moderne weergave
|
||||
</main>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
<TransitionOverlay />
|
||||
<LiveRegion />
|
||||
<VariantAlert />
|
||||
</ThemeProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe('VariantRenderer and transition UI', () => {
|
||||
beforeEach(() => window.localStorage.clear());
|
||||
afterEach(() => {
|
||||
clearMatchMedia();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('renders B first', () => {
|
||||
renderApp();
|
||||
expect(screen.getByText('Moderne weergave')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('heading', { name: 'Tijdreis' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the lazy E landing after the switch and announces it (FR-6, BR-U1-7)', async () => {
|
||||
mockMatchMedia({ [REDUCED_MOTION_QUERY]: true });
|
||||
renderApp();
|
||||
fireEvent.click(screen.getByTestId('variant-toggle-button'));
|
||||
expect(await screen.findByRole('heading', { name: 'Tijdreis' })).toBeInTheDocument();
|
||||
expect(screen.queryByText('Moderne weergave')).not.toBeInTheDocument();
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('live-region')).toHaveTextContent('Tijdreis-weergave geladen.'),
|
||||
);
|
||||
|
||||
// The way back to B exists inside E.
|
||||
fireEvent.click(screen.getAllByTestId('variant-toggle-button')[0]);
|
||||
expect(await screen.findByText('Moderne weergave')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the dither overlay during the transition (BR-U1-6)', async () => {
|
||||
mockMatchMedia();
|
||||
renderApp();
|
||||
expect(screen.queryByTestId('transition-overlay')).not.toBeInTheDocument();
|
||||
fireEvent.click(screen.getByTestId('variant-toggle-button'));
|
||||
expect(screen.getByTestId('transition-overlay')).toHaveAttribute('data-phase', 'cover');
|
||||
expect(screen.getByTestId('transition-overlay')).toHaveAttribute('aria-hidden', 'true');
|
||||
await waitFor(
|
||||
() => expect(screen.queryByTestId('transition-overlay')).not.toBeInTheDocument(),
|
||||
{
|
||||
timeout: 2000,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('renders no overlay at all under reduced motion (BR-U1-9)', async () => {
|
||||
mockMatchMedia({ [REDUCED_MOTION_QUERY]: true });
|
||||
renderApp();
|
||||
fireEvent.click(screen.getByTestId('variant-toggle-button'));
|
||||
expect(screen.queryByTestId('transition-overlay')).not.toBeInTheDocument();
|
||||
await screen.findByRole('heading', { name: 'Tijdreis' });
|
||||
expect(screen.queryByTestId('transition-overlay')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('stays in B with an alert when E cannot be loaded, and retry recovers (BR-U1-8)', async () => {
|
||||
mockMatchMedia({ [REDUCED_MOTION_QUERY]: true });
|
||||
const load = vi
|
||||
.fn<(v: 'b' | 'e') => Promise<unknown>>()
|
||||
.mockRejectedValueOnce(new Error('offline'))
|
||||
.mockResolvedValue(undefined);
|
||||
renderApp(load);
|
||||
fireEvent.click(screen.getByTestId('variant-toggle-button'));
|
||||
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent(
|
||||
'De tijdreis kon niet worden geladen.',
|
||||
);
|
||||
expect(screen.getByText('Moderne weergave')).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(screen.getByTestId('variant-alert-retry'));
|
||||
expect(await screen.findByRole('heading', { name: 'Tijdreis' })).toBeInTheDocument();
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('lets the visitor dismiss the alert', async () => {
|
||||
mockMatchMedia({ [REDUCED_MOTION_QUERY]: true });
|
||||
renderApp(() => Promise.reject(new Error('offline')));
|
||||
fireEvent.click(screen.getByTestId('variant-toggle-button'));
|
||||
await screen.findByRole('alert');
|
||||
fireEvent.click(screen.getByTestId('variant-alert-dismiss'));
|
||||
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('EBoundary', () => {
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
it('calls onError and renders nothing when the child throws (BR-U1-8)', () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const onError = vi.fn();
|
||||
function Bomb(): never {
|
||||
throw new Error('boom');
|
||||
}
|
||||
const { container } = render(
|
||||
<EBoundary onError={onError}>
|
||||
<Bomb />
|
||||
</EBoundary>,
|
||||
);
|
||||
expect(onError).toHaveBeenCalledTimes(1);
|
||||
expect(container).toBeEmptyDOMElement();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user