CMS: één bannerlijst met Locatie, SLP-thema, thema-schakelaar, documentatie
- Banner-admin: één lijst met Locatie-badge (Boven/Onder), Locatie-keuzeveld in het formulier, "Live" en "Wordt niet getoond" per locatie, aan/uit per rij - Thema slpsoftware: Archivo, bordeaux #7A1414 (WCAG AA licht en donker), <SLP/>-logo, favicon, mail-logo; VITE_THEME in de SLP-builds (test en productie-config) - Thema-schakelaar: eerste klik wisselt altijd het getoonde thema (klasse op <html> als bron van waarheid, gedeelde status, startscript valideert opslag) + tests - Docs: README, docs/cms-themas.md, docs/deploy-en-testomgevingen.md, docs/privacy-gegevens-contactformulier.md, aidlc-docs (offering-banners, bug theme-toggle-first-click) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -48,3 +48,57 @@ describe('useTheme', () => {
|
||||
expect(document.documentElement.classList.contains('light')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('first click', () => {
|
||||
const media = (dark: boolean) => vi.spyOn(window, 'matchMedia').mockReturnValue({ matches: dark } as MediaQueryList);
|
||||
const shown = () => (document.documentElement.classList.contains('dark') ? 'dark' : 'light');
|
||||
|
||||
it.each([
|
||||
['light', 'dark'],
|
||||
['dark', 'light'],
|
||||
])('flips the shown theme straight away when the OS prefers %s and nothing is stored or applied', (os, expected) => {
|
||||
media(os === 'dark');
|
||||
const { result } = renderHook(() => useTheme());
|
||||
|
||||
expect(shown()).toBe(os);
|
||||
act(() => { result.current.toggleTheme(); });
|
||||
|
||||
expect(shown()).toBe(expected);
|
||||
expect(result.current.theme).toBe(expected);
|
||||
});
|
||||
|
||||
it('flips from what is shown even when the stored value is unusable', () => {
|
||||
localStorage.setItem('cms-theme', 'system');
|
||||
media(true);
|
||||
document.documentElement.className = 'system';
|
||||
const { result } = renderHook(() => useTheme());
|
||||
|
||||
expect(shown()).toBe('dark');
|
||||
act(() => { result.current.toggleTheme(); });
|
||||
expect(shown()).toBe('light');
|
||||
});
|
||||
|
||||
it('follows the shown theme when it differs from the stored one', () => {
|
||||
localStorage.setItem('cms-theme', 'light');
|
||||
document.documentElement.className = 'dark';
|
||||
const { result } = renderHook(() => useTheme());
|
||||
|
||||
expect(result.current.theme).toBe('dark');
|
||||
act(() => { result.current.toggleTheme(); });
|
||||
expect(shown()).toBe('light');
|
||||
expect(localStorage.getItem('cms-theme')).toBe('light');
|
||||
});
|
||||
|
||||
it('keeps two toggles on the page in step', () => {
|
||||
localStorage.setItem('cms-theme', 'light');
|
||||
const a = renderHook(() => useTheme());
|
||||
const b = renderHook(() => useTheme());
|
||||
|
||||
act(() => { a.result.current.toggleTheme(); });
|
||||
expect(b.result.current.theme).toBe('dark');
|
||||
|
||||
act(() => { b.result.current.toggleTheme(); });
|
||||
expect(shown()).toBe('light');
|
||||
expect(a.result.current.theme).toBe('light');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,26 +1,62 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { useCallback, useSyncExternalStore } from 'react';
|
||||
|
||||
type Theme = 'light' | 'dark';
|
||||
|
||||
const STORAGE_KEY = 'cms-theme';
|
||||
|
||||
function getInitialTheme(): Theme {
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function isTheme(value: string | null): value is Theme {
|
||||
return value === 'light' || value === 'dark';
|
||||
}
|
||||
|
||||
function preferredTheme(): Theme {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored === 'light' || stored === 'dark') return stored;
|
||||
if (isTheme(stored)) return stored;
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
function apply(theme: Theme) {
|
||||
document.documentElement.classList.remove('light', 'dark');
|
||||
document.documentElement.classList.add(theme);
|
||||
}
|
||||
|
||||
/**
|
||||
* The theme that is actually shown, which is what the <html> class says: the stylesheet only
|
||||
* knows that class. The stored value and the OS preference are only the way to pick a theme when
|
||||
* the page carries none (or an unusable one); in that case it is applied right away, so the state
|
||||
* we report and what the user sees can never differ. A first click then always flips what is shown.
|
||||
*/
|
||||
function currentTheme(): Theme {
|
||||
const classes = document.documentElement.classList;
|
||||
if (classes.contains('dark')) return 'dark';
|
||||
if (classes.contains('light')) return 'light';
|
||||
|
||||
const theme = preferredTheme();
|
||||
apply(theme);
|
||||
return theme;
|
||||
}
|
||||
|
||||
function subscribe(listener: () => void) {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
const [theme, setTheme] = useState<Theme>(getInitialTheme);
|
||||
// One source of truth shared by every toggle on the page (desktop sidebar, mobile overlay).
|
||||
const theme = useSyncExternalStore(subscribe, currentTheme, () => 'light' as Theme);
|
||||
|
||||
const toggleTheme = useCallback(() => {
|
||||
setTheme((current) => {
|
||||
const next: Theme = current === 'dark' ? 'light' : 'dark';
|
||||
const next: Theme = currentTheme() === 'dark' ? 'light' : 'dark';
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, next);
|
||||
document.documentElement.classList.remove('light', 'dark');
|
||||
document.documentElement.classList.add(next);
|
||||
return next;
|
||||
});
|
||||
} catch {
|
||||
// Storage can be unavailable (private mode); the switch itself still works.
|
||||
}
|
||||
apply(next);
|
||||
listeners.forEach((notify) => notify());
|
||||
}, []);
|
||||
|
||||
return { theme, isDark: theme === 'dark', toggleTheme };
|
||||
|
||||
Reference in New Issue
Block a user