Design Systems, CSS Variables & Theme Switching with View Transitions
How semantic CSS variables keep visual consistency across projects and pair with the native View Transitions API for smooth light/dark theme switching without FOUC.
When multiple projects or teams share the same visual language, scattered hardcoded colors quickly become hard to maintain. These notes cover how CSS variables keep styles consistent and how this site handles theme switching.
Centralizing Tokens with CSS Variables
Defining semantic variables in :root and .dark lets components stay clean and makes theme adjustments predictable without runtime JavaScript overhead:
:root {
--bg-primary: #F8F9FA;
--text-primary: #121212;
--border-subtle: #E9ECEF;
}
.dark {
--bg-primary: #0B0C0E;
--text-primary: #EDEDED;
--border-subtle: #1E2024;
}Pairing these tokens with Figma variables and Storybook documentation keeps design and engineering aligned across projects.
Smooth Theme Switching with View Transitions API
To avoid an abrupt jump when toggling light/dark mode, the site wraps the DOM change in document.startViewTransition() with a graceful fallback:
export function toggleTheme() {
if (!document.startViewTransition) {
applyTheme();
return;
}
document.startViewTransition(() => {
applyTheme();
});
}Combined with a small inline script in <head> that applies the saved localStorage theme before first paint, reloading in dark mode avoids the classic white flash (FOUC).
Miguel Riquelme
Front-end Engineer. Software Engineering MBA student at USP/Esalq.