technicaldark-mode 3 min read
How dark mode works in Saastro Theme
Technical deep-dive into the anti-FOUC dark mode system.
Saastro ·
The problem
Flash of Unstyled Content (FOUC) happens when the page loads in light mode before JavaScript applies the user’s dark mode preference. This causes a visible white flash.
The solution
Saastro Theme uses an inline script in <head> that runs before any content renders:
- Checks
localStoragefor a saved preference - Falls back to
prefers-color-scheme: darkmedia query - Applies the
darkclass to<html>immediately
Because the script is inline and synchronous, it executes before the browser paints — eliminating the flash entirely.
Toggle behavior
The ToggleTheme component cycles through three states:
- Light — forces light mode
- Dark — forces dark mode
- System — follows OS preference
The preference is saved to localStorage so it persists across visits.