01The toggle isn't the hard part
Flipping a dark class on <html> and inverting a handful of colors takes an afternoon. Making dark mode hold up across a growing product, with new components, third-party widgets, charts and data tables with status colors, takes a system.
The failure mode is always the same: someone hardcodes #1f2937 instead of a token, it looks fine in isolation, and six months later it's the one panel that stays white in dark mode and nobody remembers why.
02Building it as tokens, not overrides
-
01
Name colors by role, not value
--surface,--text-muted,--border. Never--gray-200. A role-based name can be redefined per theme without touching a single component. -
02
Redefine tokens, not components
Dark mode should only ever touch the token layer. If a component file needs a
dark:override, a token is missing — that's the bug to fix, not the override to add. -
03
Support three states, not two
Light, dark, and system, where system means "no explicit choice, follow the OS." Treating "no preference" as its own state avoids fighting
prefers-color-schemelater. -
04
Keep status colors semantic too
Success, warning and danger colors need their own light/dark pairs. A green that passes contrast on white paper can fail badly on near-black backgrounds.
-
05
Set the theme before first paint
Read the stored preference in an inline script in
<head>, before any stylesheet renders, so the page never flashes the wrong theme on load. -
06
Audit contrast per theme, not once
A token pair that passes WCAG AA in light mode isn't guaranteed to pass in dark mode. Check both directions, especially for muted text and disabled states.
03Third-party components are the real test
Your own components are easy to theme correctly because you control every color. The test is a component library, chart package or embedded widget that ships its own default palette and doesn't know your tokens exist.
The fix is the same as everywhere else: map the library's theming API (CSS custom properties, a theme preset or a config object) to your own semantic tokens once, centrally. Never patch an individual instance's colors; the next instance of that same component will just break the same way again.
04A theme service built on signals
The three-state toggle (light, dark, system) is a natural fit for signals: the user's choice is a signal, the OS preference is another, and isDark is a computed of the two. A single effect writes the class to <html> and persists the choice.
PrimeNG follows the same class through darkModeSelector: '.app-dark', so library components switch with your tokens. A tiny inline script in index.html sets the class before Angular boots, so there's no light flash on load.
type ThemeChoice = 'light' | 'dark' | 'system';
@Injectable({ providedIn: 'root' })
export class ThemeService {
private doc = inject(DOCUMENT);
private media = this.doc.defaultView!.matchMedia('(prefers-color-scheme: dark)');
private systemDark = signal(this.media.matches);
choice = signal<ThemeChoice>(
(localStorage.getItem('theme') as ThemeChoice | null) ?? 'system',
);
isDark = computed(() =>
this.choice() === 'system' ? this.systemDark() : this.choice() === 'dark',
);
constructor() {
this.media.addEventListener('change', (e) => this.systemDark.set(e.matches));
effect(() => {
this.doc.documentElement.classList.toggle('app-dark', this.isDark());
localStorage.setItem('theme', this.choice());
});
}
}
The takeaway
Dark mode that lasts is a token architecture decision made once, not a CSS override added per component. Name colors by role, redefine only the token layer per theme, and map every third-party library into that same system — and new screens will be dark-mode-correct by default, not by remembering to check.