Design theme and state variants
Expose theme choices as data
A theme is a set of visual values applied consistently to a region. CSS custom properties such as --surface cascade and can be overridden by selectors. React can choose a theme by updating an attribute instead of rewriting every color. Use App.jsx with theme.css below; the second JSX example is an alternative App.jsx using the same CSS.
import React, {useState} from 'react';import './theme.css';export default function App() { const [dark, setDark] = useState(false); return <main className="theme" data-theme={dark ? 'dark' : 'light'}> <h1>Gallery settings</h1><button aria-pressed={dark} onClick={() => setDark(v => !v)}>Dark theme</button> <p>Current theme: {dark ? 'Dark' : 'Light'}</p></main>;}.theme { --surface: #fff; --ink: #172554; --accent: #1d4ed8; background: var(--surface); color: var(--ink); padding: 1rem; }.theme[data-theme="dark"] { --surface: #172554; --ink: #fff; --accent: #bfdbfe; }.theme button { color: var(--ink); background: var(--surface); border: 2px solid var(--accent); padding: .5rem; }.theme button:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }.status { border-inline-start: 4px solid var(--accent); padding: .5rem; transition: background-color 200ms; }.status[data-state="ready"] { font-weight: bold; }@media (prefers-reduced-motion: reduce) { .status { transition: none; } }The light theme starts with a white surface and dark text. The dark attribute overrides the custom properties for the region; descendants inherit the token values. The toggle has a stable accessible name and aria-pressed reflects selection. Use an explicit allowlist of theme names, not arbitrary CSS strings. A custom property can also be set with style={{"--accent": "#1d4ed8"}} using a trusted fixed value. The sample deliberately omits persistence and OS theme detection to keep state ownership clear.
Make state understandable without color
import React, {useState} from 'react';import './theme.css';function stateLabel(ready) { return ready ? 'Ready to visit' : 'Not ready'; }export default function App() { const [ready, setReady] = useState(false); return <main className="theme" data-theme="light"><h1>Visit checklist</h1> <button aria-pressed={ready} onClick={() => setReady(v => !v)}>Ready</button> <p className="status" data-state={ready ? 'ready' : 'waiting'} role="status">{stateLabel(ready)}</p></main>;}The status text changes from Not ready to Ready to visit and becomes bold. Color alone is not the status signal. The stylesheet includes a visible focus outline and disables the decorative transition for users requesting reduced motion. That preference does not automatically make every animation accessible. Check contrast, zoom, keyboard navigation and announcements in the actual app. Design tokens are a convention, not a React API; keep variants finite and document them.
Practice
Add a third approved token for spacing. Verify it is inherited within the theme and does not change a sibling outside the theme. Test both themes and reduced-motion preference in an approved browser. Explain why a CSS test cannot prove a React event handler works.