Performance: Theme switching architecture
MEDIUM IMPACT
Theme switching affects page load speed and interaction responsiveness by controlling CSS variable usage and style recalculations.
:root {
--primary-color: #000;
--background-color: #fff;
}
[data-theme='dark'] {
--primary-color: #fff;
--background-color: #000;
}
body {
color: var(--primary-color);
background-color: var(--background-color);
}
// Switching theme by toggling data-theme attribute$primary-color: #000; $background-color: #fff; body { color: $primary-color; background-color: $background-color; } // To switch theme, reload or override many selectors with new variables
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| SASS variables with full CSS reload | Minimal DOM changes | Triggers full reflow | High paint cost due to layout recalculation | [X] Bad |
| CSS custom properties with attribute toggle | Single attribute change | No reflow, only paint | Low paint cost, fast repaint | [OK] Good |