Files
slp-modular-cms/aidlc-docs/bugs/theme-toggle-first-click/bug-fix.md
T
SluijsensandClaude Sonnet 5.5 321234dfa6 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>
2026-11-15 00:14:10 +01:00

44 lines
3.4 KiB
Markdown

# Bug Fix: the first click on the theme toggle does nothing
- **Slug**: theme-toggle-first-click
- **Status**: Complete (pending deploy; built outside the AI-DLC flow, documented afterwards)
- **Issue**: None (reported by Bryan)
- **Branch**: feature/contact-forms-module
- **Started**: unknown
- **Documentation Language**: English
- **Conversation Language**: Dutch (user language)
## Report
In the admin, the first click on the light/dark toggle sometimes changed nothing visible; only the second click switched the theme.
## Root Cause
The `useTheme` hook (`frontend/src/hooks/useTheme.ts`) kept its own state, initialised from `localStorage` (`cms-theme`) or else from the OS preference (`prefers-color-scheme`). What is actually shown is decided by the class (`light` / `dark`) on `<html>`, which the start script in `frontend/index.html` sets before React runs. The two were not the same source: when the hook's state and the class on `<html>` differed, the first click flipped the hook's state and applied *that* theme, which could be the theme already shown, so nothing visible happened.
Two ways they could differ, both visible in the old code:
1. The start script in `index.html` took any stored value as a class name (`s ? s : ...`), while the hook only accepted `light` or `dark`. A stored value such as `system` put the class `system` on `<html>` (no theme at all) while the hook fell back to the OS preference.
2. Every toggle (desktop sidebar, mobile overlay) called `useTheme()` and so had its own state; flipping one left the others stale, so the next click on another toggle could re-apply the theme already shown.
**Not reproduced in a real browser.** The cause was derived from reading the old code; the only evidence is that the new tests fail against the old hook (reported by the developer; not re-run for this document). Which of the two cases (or another) happened in the browser session that was reported is not known.
## The Fix
- `useTheme.ts`: one source of truth, the class on `<html>`, read through `useSyncExternalStore`. If the page carries neither class, the stored value or OS preference is applied right away, so the reported state and what is shown cannot differ. `toggleTheme` flips what is shown now, stores it (inside `try/catch`, since storage can be unavailable), applies it and notifies every subscriber, so all toggles on the page share one state.
- `index.html`: the start script accepts only `light` or `dark` from storage; anything else falls back to the OS preference.
## Verification
**Path used**: Automated test framework (`frontend/src/hooks/useTheme.test.ts`, new block `first click`, 4 tests): with the OS preferring light and with it preferring dark and nothing stored or applied, the first click flips the shown theme; an unusable stored value (`system`) with class `system` on `<html>` still flips from what is shown; when the shown theme differs from the stored one the toggle follows the shown one and stores the new value; two toggles on one page stay in step.
**Before the fix**: according to the developer, the new tests fail against the old hook. Not re-run for this document. **After the fix**: not re-run for this document either.
**Not verified**: behaviour in a real browser (no manual reproduction before or after the fix).
## Result
- **PR(s)**: None
- **Commit(s)**: commit "CMS: één bannerlijst met Locatie, SLP-thema, thema-schakelaar" on feature/contact-forms-module (2026-11-15)
- **Completed**: 2026-11-15 (documentation)