Theme toggle
How people choose light or dark: products follow the system setting, settings pages offer "Appearance: System / Light / Dark", and the header of a product that ships a dark theme carries a 48px dark_mode icon button.
When to use
- Appearance in settings (
.oa-appearance): on the settings or account page of every product that ships a dark theme, as one setting among the others (Forms and settings, template 6) (canonical §10.28). - The header toggle (
button.oa-icon-btn,dark_mode): in the site header of every product that ships a dark theme, after the language switcher, at every width (D-CSN-56). A product that is light-only has none. It is an icon, never a word. - The footer theme button (
button.oa-theme-switch): the same control with its words, "Dark theme", beside the language switcher in the footer's legal bar of the same products (D-CSN-57). In a header the controls are icons; in a footer they are full buttons with labels. Both buttons show the same state.
When not to use
| Need | Use instead |
|---|---|
| A product that has not passed its dark review | Nothing: it sets data-theme="light" on <html> and offers no Appearance setting until it ships dark |
| The Games projector stage | Nothing: the stage page sets data-theme="dark" on its root (canonical §2.9) |
| A dark band or panel inside a light page | The inverse panel; there is no subtree theme switch (D-ARC-07) |
| Any other on/off setting | A switch, in Checkbox, radio, and switch |
Anatomy
Appearance (the default):
- Label: "Appearance", a visible
label-mdline above the group, which names it througharia-labelledby. - Group (
fieldset.oa-segmented.oa-appearance): the segmented button. - Segments (
button.oa-segmented__btn,aria-pressed,value): "System" (value="system"), "Light" (value="light"), "Dark" (value="dark"), each holding thecheckthat shows on the chosen one.
The header toggle:
- Button (
button.oa-icon-btn,aria-pressed): a standard icon button, 48 × 48. - Icon:
dark_mode, 24px; FILL 1 while pressed.
Variants
| Variant | Use for | Key tokens |
|---|---|---|
| Appearance (default) | The settings page: three segments | --color-outline (group edge), --color-surface, --color-primary-container with --color-on-primary-container (the chosen segment), --color-on-surface |
| Header toggle | The header's end cluster, beside the language switcher | --color-on-surface-variant (off); --color-primary-container with --color-on-primary-container (on) |
Sizes
| Part | Height | Padding | Type style | Icon |
|---|---|---|---|---|
| Appearance segment | 48px, --size-button-md (h-12) |
24px inline (px-6) |
label-lg 16/24 600 |
check 20px, --size-icon-md |
| Appearance group | 48px plus its 1px edge | none | none | none |
| Appearance label | its line | 8px above the group (--space-stack-tight) |
label-md 14/20 600 |
none |
| Header toggle | 48 × 48px, --size-button-md (size-12) |
none | none | dark_mode 24px, --size-icon-lg |
Below 640px the Appearance group fills its row and its three segments stack in a column (oa-segmented--stack), because three labels side by side do not fit a 320px window in English, Arabic, or French; from 640px it is one row that hugs its labels (canonical §10.8).
Tokens
| Part | Property | Token (Tailwind) |
|---|---|---|
| Group | edge, corner, fill | 1px --color-outline (border-outline), --radius-full (rounded-full), --color-surface (bg-surface) |
| Group | width, direction | 100% below 640px (w-full), a column with 16px corners below 640px (max-sm:flex-col max-sm:rounded-box, the stacked segmented recipe), its content in one row from 640px (sm:w-fit) |
| Segment | label | --color-on-surface, --text-label-lg with its companions (text-label-lg) |
| Segment | divider | 1px --color-outline on the inline start (border-s border-outline) |
| Chosen segment | fill, label, cue | --color-primary-container (bg-primary-container), --color-on-primary-container, the 20px check |
| Label | type | --text-label-md (text-label-md), --color-on-surface |
| Header toggle | icon | --color-on-surface-variant; pressed --color-primary-container with --color-on-primary-container |
| State layer | opacity | --opacity-state-hover (0.08), --opacity-state-pressed (0.12), over --duration-short-3 with --ease-standard; a segment's layer covers its own divider, the toggle's its 1px border |
| Focus | ring | the base ring; segments take --size-focus-inset inside the group |
States
| State | What changes |
|---|---|
| Enabled | Appearance: "System" chosen by default, in primary-container with its check. Toggle: off, no fill. |
| Hover | The state layer at 8% of the label color (pointer devices only). |
| Focus-visible | Segments: the ring inset 4px inside the group. Toggle: the base ring around the circle. |
| Pressed | The layer at 12%, at once. |
| Selected | Segment: aria-pressed="true", primary-container, the check. Toggle: aria-pressed="true" while dark, primary-container and the filled glyph; its name stays "Dark theme". |
| Disabled, error, loading | None. The choice applies at once and is saved in the background. |
Behavior
The default: follow the system. Themes switch on <html> only. With no data-theme the tokens follow prefers-color-scheme; data-theme="light" or data-theme="dark" (or the class light or dark) forces one. Components never write theme media queries; the tokens switch by themselves (canonical §0.5, D-ARC-07).
Appearance, the script contract.
- A press on a segment sets
aria-pressed="true"on it and"false"on the other two. - It applies at once: "System" removes
data-themefrom<html>; "Light" and "Dark" set it. - It saves the choice with the account; signed-out pages keep it in
localStorageunderoa-theme("light"or"dark"; "System" removes the key). Wrap storage intry, because private windows may refuse it. - On the next visit the server renders
data-themefrom the account, so the page never flashes the other theme; without an account, a small inline script in<head>(with the page's CSP nonce) readsoa-themebefore the first paint. - Where a product ships
<meta name="theme-color">with a dark twin, the script updates it from--color-surface, never from a literal.
const KEY = 'oa-theme'
function applyAppearance(choice) { // 'system' | 'light' | 'dark'
const root = document.documentElement
if (choice === 'system') root.removeAttribute('data-theme')
else root.setAttribute('data-theme', choice)
try { choice === 'system' ? localStorage.removeItem(KEY) : localStorage.setItem(KEY, choice) } catch {}
}The header toggle. A press switches between Dark and Light, overriding the system, and saves the choice the same way; "System" in settings returns to the system setting. The name stays "Dark theme"; aria-pressed says whether it is on. It stays in the bar at every width, as the Global ID circle does: at 360px Global Jobs' bar would hold the S lock-up, the toggle, the circle, and the menu button in 310px of 328px (canonical §10.14, D-CSN-56).
Motion. None: the theme changes at once. Never animate the whole page's colors.
Accessibility
- Appearance. A
<fieldset>named by the visible "Appearance" (aria-labelledby); three<button type="button">witharia-pressed, so screen readers say which one is on. The chosen one shows the check, a cue that is not color (canonical §10.0). - Header toggle.
<button type="button">witharia-label="Dark theme"andaria-pressed; the name never changes with the state (canonical §10.2); the plain tooltip carries the same words on pointer devices. - Targets. 48px segments; the 48 × 48 toggle.
- Keyboard. Tab reaches each segment; Enter or Space chooses it.
- Contrast. Labels in
on-surfaceandon-primary-container(13.22:1 or more in light); the group'soutlineedge 3:1 or more. - Forced colors. The check stays as the chosen segment's cue; the ring stays an outline.
- Reduced motion. Nothing moves; the theme changes at once in every case.
RTL and languages
- In Arabic the segments run from the right: System, Light, Dark; the check sits at each label's inline start.
dark_modenever mirrors. - French labels run longer ("Système", "Clair", "Sombre"); the group stacks below 640px and the labels never wrap.
Content
- The setting is "Appearance"; the choices are "System", "Light", and "Dark", in that order. The header toggle is "Dark theme". Sentence case, no colon, no "mode" (canonical §10.28, D-CSN-40, D-CSN-56).
| English | Arabic (native review) | French (native review) | |
|---|---|---|---|
| Setting | Appearance | المظهر | Apparence |
| Choices | System · Light · Dark | النظام · فاتح · داكن | Système · Clair · Sombre |
| Header toggle | Dark theme | المظهر الداكن | Thème sombre |
The English strings are canonical; the Arabic and French ones are new and need native review before release (canonical §12.6).
Do and don't
| Do | Don't |
|---|---|
Follow the system, offer System / Light / Dark in settings, and carry the dark_mode icon button in the header where the product ships a dark theme. |
Put a theme toggle in the header of a light-only product, or give it a word label. |
| Keep "System" as the first choice and the default. | Store Light or Dark before the person chooses. |
Render data-theme on the server from the account. |
Let the page flash the wrong theme while a script runs. |
Keep the name "Dark theme" and change aria-pressed. |
Swap the name between "Switch to dark theme" and "Switch to light theme" (Tamakkan today). |
Use Material Symbols dark_mode. |
Use a ◐ glyph or another Unicode symbol (Tamakkan today). |
Keep a product light (data-theme="light") until its dark review passes. |
Offer "Dark" in a product whose screens were never reviewed in dark. |
Code
HTML (reference CSS)
Appearance in settings (.oa-appearance on the segmented group of segmented.css):
<div class="oa-stack oa-stack--tight">
<p class="oa-text-label-md" id="bk-appearance">Appearance</p>
<fieldset class="oa-segmented oa-segmented--stack oa-appearance" aria-labelledby="bk-appearance">
<button type="button" class="oa-segmented__btn" aria-pressed="true" value="system"><span class="oa-icon oa-segmented__check" aria-hidden="true" translate="no">check</span>System</button>
<button type="button" class="oa-segmented__btn" aria-pressed="false" value="light"><span class="oa-icon oa-segmented__check" aria-hidden="true" translate="no">check</span>Light</button>
<button type="button" class="oa-segmented__btn" aria-pressed="false" value="dark"><span class="oa-icon oa-segmented__check" aria-hidden="true" translate="no">check</span>Dark</button>
</fieldset>
</div>The header toggle:
<button type="button" class="oa-icon-btn" aria-label="Dark theme" aria-pressed="false"><span class="oa-icon" aria-hidden="true" translate="no">dark_mode</span></button>In Arabic (native review):
<div class="oa-stack oa-stack--tight" dir="rtl" lang="ar">
<p class="oa-text-label-md" id="bk-appearance-ar">المظهر</p>
<fieldset class="oa-segmented oa-segmented--stack oa-appearance" aria-labelledby="bk-appearance-ar">
<button type="button" class="oa-segmented__btn" aria-pressed="true" value="system"><span class="oa-icon oa-segmented__check" aria-hidden="true" translate="no">check</span>النظام</button>
<button type="button" class="oa-segmented__btn" aria-pressed="false" value="light"><span class="oa-icon oa-segmented__check" aria-hidden="true" translate="no">check</span>فاتح</button>
<button type="button" class="oa-segmented__btn" aria-pressed="false" value="dark"><span class="oa-icon oa-segmented__check" aria-hidden="true" translate="no">check</span>داكن</button>
</fieldset>
</div><button type="button" class="oa-icon-btn" aria-label="المظهر الداكن" aria-pressed="false"><span class="oa-icon" aria-hidden="true" translate="no">dark_mode</span></button>Tailwind CSS v4
<!-- Appearance: the segmented recipe, filling the row and stacked below 640px: add the stacked classes of segmented-button.md to the fieldset and the segments -->
<div class="flex flex-col gap-2">
<p id="appearance" class="text-label-md text-on-surface">Appearance</p>
<fieldset aria-labelledby="appearance" class="m-0 flex w-full min-w-0 max-w-full overflow-hidden rounded-full border border-outline bg-surface p-0 sm:w-fit max-sm:flex-col max-sm:rounded-box">
<button type="button" value="system" aria-pressed="true" class="relative isolate inline-flex h-12 flex-1 items-center justify-center gap-2 border-s border-outline px-6 text-label-lg text-on-surface whitespace-nowrap cursor-pointer first-of-type:border-s-0 first-of-type:rounded-s-full last-of-type:rounded-e-full focus-visible:-outline-offset-4 aria-pressed:bg-primary-container aria-pressed:text-on-primary-container transition-[background-color,color] duration-short-3 ease-standard before:pointer-events-none before:absolute before:inset-0 not-first-of-type:before:-start-px before:-z-1 before:rounded-[inherit] before:bg-current before:opacity-0 before:transition-opacity before:duration-short-3 before:ease-standard before:content-[''] hover:before:opacity-(--opacity-state-hover) active:before:opacity-(--opacity-state-pressed) active:before:duration-0"><Icon name="check" class="hidden text-(length:--size-icon-md) [font-variation-settings:'FILL'_1] in-aria-pressed:inline-block" />System</button>
<!-- Light (value="light"), Dark (value="dark"); the chosen one shows the 20px check -->
</fieldset>
</div>
<!-- The header toggle, in a product that ships a dark theme (D-CSN-56). The footer twin (D-CSN-57) is the language switcher pill with dark_mode and the words "Dark theme": <button type="button" aria-pressed="false" class="…the language switcher recipe… aria-pressed:bg-primary-container aria-pressed:text-on-primary-container">. -->
<button type="button" aria-label="Dark theme" aria-pressed="false" class="relative isolate inline-flex size-12 items-center justify-center rounded-full border border-transparent text-on-surface-variant aria-pressed:bg-primary-container aria-pressed:text-on-primary-container before:pointer-events-none before:absolute before:-inset-px before:-z-1 before:rounded-[inherit] before:bg-current before:opacity-0 before:transition-opacity before:duration-short-3 before:ease-standard before:content-[''] hover:before:opacity-(--opacity-state-hover) active:before:opacity-(--opacity-state-pressed) active:before:duration-0">
<Icon name="dark_mode" class="text-(length:--size-icon-lg) in-aria-pressed:[font-variation-settings:'FILL'_1]" />
</button>In Tailwind, dark: variants match only an explicit data-theme="dark" or .dark, never the system setting; components need none, because the tokens switch (canonical §0.5). shadcn/ui: ToggleGroup becomes the segmented group.
React
Prop (AppearanceSetting) |
Type | Default | Description |
|---|---|---|---|
| value | 'system' | 'light' | 'dark' |
'system' |
The saved choice. |
| onChange | (value) => void |
required | Applies the choice at once and saves it with the account (or oa-theme). |
| label | string | "Appearance" | The visible label, in the page's language. |
| labels | { system: string; light: string; dark: string } |
"System", "Light", "Dark" | The segments, in the page's language. |
Prop (ThemeToggle, only if declined) |
Type | Default | Description |
|---|---|---|---|
| pressed | boolean | required | True while the dark theme is on. |
| onPressedChange | (pressed: boolean) => void |
required | Sets Dark or Light and saves it. |
| label | string | "Dark theme" | Never changes with the state. |
<AppearanceSetting value={appearance} onChange={setAppearance} label={t('settings.appearance')} />