Web accessibility (WCAG AA) and high-contrast dark mode
Why dark themes fail on disabled and unselected states, how to verify a contrast of at least 4.5:1 with unit tests, and how to support the keyboard and prefers-reduced-motion.
Fundamental · WCAG 2.2 · MUI · CSS · Vitest
Why dark themes fail on disabled and unselected states
When a dark theme is applied component by component, the usual thing is to style only the active state. Disabled, unselected or unchecked states inherit the library's default colours, designed for a light background: a translucent dark grey on an almost black background, in other words invisible text. The rule: if you customise the colour of one state, customise every other state in the same `sx`.
❌ Antipattern
<Button sx={{ bgcolor: '#14f195', color: '#0f172a' }} disabled={busy}>
{t('trade.open')}
</Button>✅ Stoic pattern
<Button
sx={{
bgcolor: '#14f195',
color: '#0f172a',
'&.Mui-disabled': { bgcolor: 'rgba(255,255,255,0.08)', color: '#94a3b8' },
}}
disabled={busy}
>
{t('trade.open')}
</Button>Key takeaways
- Every visual state (disabled, unselected, unchecked) needs its own explicit colour.
- Do not trust the library's default colours on a dark background.
- Check the state you were not styling, not just the one you have just changed.
A contrast of at least 4.5:1, verified with unit tests
WCAG AA requires a minimum contrast of 4.5:1 for normal text. Instead of relying on a visual review, the test reads the component's real stylesheet, extracts the colour declared for each selector and computes the ratio against the card background. If someone swaps a colour for a darker grey, the suite fails before it reaches production. The pages of this academy are protected exactly like this.
Example
import { getContrastRatio } from '@mui/material/styles';
import coursesCss from './Courses.css?raw';
it.each(['.courses-card-summary', '.courses-meta'])('keeps %s readable', selector => {
const color = cssColorOf(coursesCss, selector);
expect(getContrastRatio(color, '#1c2640')).toBeGreaterThanOrEqual(4.5);
});Key takeaways
- 4.5:1 for normal text; 3:1 for large text and graphical elements.
- A test that reads the real CSS turns contrast into an executable requirement.
- Measure against the card's real background, not against pure black.
Keyboard and prefers-reduced-motion
Everything that can be done with the mouse must be doable with the keyboard: real interactive elements (`button`, `a`), a logical tab order and a visible focus. Entrance animations and smooth scrolling are turned off when the user's system asks for reduced motion, because they make some people dizzy.
❌ Antipattern
<div className="card" onClick={open}>
{title}
</div>✅ Stoic pattern
<button type="button" className="card" onClick={open}>
{title}
</button>
/* Card.css */
.card:focus-visible { outline: 2px solid #22d3ee; }
@media (prefers-reduced-motion: reduce) {
.card { transition: none; }
}Key takeaways
- Use native elements: `button` and `a` come with focus and keyboard support built in.
- Never remove the outline without providing an alternative `:focus-visible` style.
- Respect `prefers-reduced-motion`: animation is an extra, not a requirement.