· 3 min read
A theme toggle that doesn't flash
Added a blocking inline <script> in app.html that reads localStorage.theme and sets data-theme on <html> before the first paint.
What we did
- Added a blocking inline
<script>inapp.htmlthat readslocalStorage.themeand setsdata-themeon<html>before the first paint. - Built
ThemeToggle.svelte:$statefor the theme,onMountto sync the button with the current theme, andtoggle()to update the state, the attribute andlocalStorage. - Put the toggle in the header. Tested on the built site: the choice persists across pages and reloads without a flash.
What I learned
- The flash: prerendered HTML has no
data-theme, so the first paint follows the OS. SvelteKit's JS runs later, as module scripts after parsing, and switches the theme. - A plain inline
<script>in<head>blocks parsing, so it runs before<body>is painted. Component code runs after hydration, which is too late. localStorageis outside data: validate it ('light' | 'dark'), and wrap access intrybecause it can throw when storage is blocked.- Prerendering runs with no visitor, so
themestarts asundefinedand the button is disabled untilonMount. Starting it as'light'would bake the wrongaria-pressedinto the HTML for dark-OS visitors. - The inline script decides the colors.
onMountonly syncs the button with what the script already did. - Accessible toggle: a
<button>with a fixed label andaria-pressed. A label that flips between "Light" and "Dark" is ambiguous: is it the current state or the action? - The OS theme is already detected with no JS, through
color-scheme: light dark. JS only handles the user's override.
Decisions
- One button, not a three-way control. If the site ever gets a settings panel, "Use system setting" goes there. I read the 2026 debate before deciding:
- Lea Verou argues for two visible states (the system theme and its opposite), and later that most sites don't need a toggle at all.
- Bramus makes the case for showing all three states (system, light, dark).
- Kilian Valkhof's older post argues the toggle must have a way back to "system".
- I went with Lea's model. The button switches between the system theme and its opposite. Clicking back to the theme that matches the OS deletes the override (both
data-themeandlocalStorage), so the site follows the OS again. One button, and there's still a way back to "system". - A
matchMediachangelistener keeps the button in sync if the OS theme changes while the page is open.
Problems & fixes
- My first browser test of the new version showed the old behavior. A preview server from an earlier test was still holding the port and serving the old build. When a test result makes no sense, check what's actually being served.
- The OS-change test failed at first because the
changeevent fires a moment after the switch, and the test read the button too early. It passed after waiting 100 ms.
Links
- Lea Verou: Dark mode toggles: two states are enough
- Lea Verou: The best dark mode toggle is probably none
- Bramus: The case for tri-state dark mode toggles
- Kilian Valkhof: Your dark mode toggle is broken
- MDN: aria-pressed
Next
- Self-host fonts.