सुलभता पैटर्न · इंटरैक्शन और मोशन
थीम स्विचर
तीन नेटिव रेडियो बटन लाइट, डार्क या सिस्टम चुनते हैं; चुनाव पेज के रूट पर एक एट्रिब्यूट सेट करता है, और बाकी काम स्टाइलशीट करती है। सिस्टम डिवाइस के साथ चलता है और उसके साथ बदलता है, color-scheme ब्राउज़र के अपने कंट्रोल और स्क्रॉलबार भी साथ ले आता है, और head में एक छोटी स्क्रिप्ट गलत थीम की चमक रोकती है।
- डब्ल्यूसीएजी मानदंड
- 7
- कीबोर्ड नियम
- 4
- जाँच का तरीक़ा
- axe, कीबोर्ड और इंस्पेक्टर
लाइव डेमो
आज़माएँ
इसे माउस, कीबोर्ड या स्क्रीन रीडर से इस्तेमाल करें। साथ का इंस्पेक्टर दिखाता है कि आपके इस्तेमाल के दौरान ब्राउज़र सहायक तकनीक को क्या बताता है: फ़ोकस, स्थिति में बदलाव और घोषणाएँ।
दिखावट
यह प्रीव्यू अपनी थीम ख़ुद रखता है, स्टेज के ऊपर वाले लाइट और डार्क बटनों से अलग।
आपका डिवाइस लाइट पर सेट है।
आपका ऑर्डर
रास्ते में हैमसाला चाय × 2, वड़ा पाव × 1
लगभग 12 मिनट में पहुँचेगा। राइडर को ट्रैक करें
- फ़िल्टर कॉफ़ी, इडली 28 Sep
- मसाला चाय × 3 21 Sep
- बन मस्का, अदरक वाली चाय 14 Sep
- पोहा, फ़िल्टर कॉफ़ी 7 Sep
- वड़ा पाव × 2 31 Aug
| पेयर | लाइट | डार्क |
|---|---|---|
बॉडी टेक्स्ट text / surface |
17.72:1 AAA | 16.97:1 AAA |
सहायक टेक्स्ट text-3 / surface |
6.42:1 AA | 6.91:1 AA |
लिंक accent-text / surface |
7.90:1 AAA | 8.89:1 AAA |
बटन लेबल on-accent / accent |
6.29:1 AA | 6.53:1 AA |
कंट्रोल के किनारे border-strong / surface |
4.50:1 AA | 5.46:1 AA |
<script>
const saved = localStorage.getItem("theme-choice");
if (saved) document.documentElement.dataset.theme = saved;
</script>
जावास्क्रिप्ट के बिना डेमो उतना ही काम करता है जितना उसका एचटीएमएल; इंस्पेक्टर के लिए जावास्क्रिप्ट चाहिए।
कीबोर्ड
जिन कुंजियों पर यह काम करता है
हर क्रिया पॉइंटर के बिना काम करती है।
| कुंजी | यह क्या करती है |
|---|---|
| Tab | चुनी गई थीम पर जाता है; अगला Tab समूह से निकलकर पेज के बाकी हिस्से पर जाता है। |
| Arrow RightयाArrow Down | अगली थीम चुनता है, सिस्टम से घूमकर लाइट पर, और पेज तुरंत बदल जाता है। |
| Arrow LeftयाArrow Up | पिछली थीम चुनता है, लाइट से घूमकर सिस्टम पर। |
| Space | अगर अभी कोई थीम नहीं चुनी है, तो फ़ोकस वाली थीम चुनता है। |
स्क्रीन रीडर
यह क्या घोषित करता है
मार्कअप की भूमिकाओं, नामों और स्थितियों से लिखा गया।
| कब | अपेक्षित घोषणा |
|---|---|
| Tab समूह तक पहुँचता है | थीम, समूह। आपका डिवाइस लाइट पर सेट है। सिस्टम, रेडियो बटन, चुना हुआ, 3 में से 3 |
| Arrow Right लाइट चुनता है | लाइट, रेडियो बटन, चुना हुआ, 3 में से 1। लाइट थीम चालू |
| Arrow Right डार्क चुनता है | डार्क, रेडियो बटन, चुना हुआ, 3 में से 2। डार्क थीम चालू |
| डार्क पर सेट डिवाइस पर सिस्टम चुना जाता है | सिस्टम, रेडियो बटन, चुना हुआ, 3 में से 3। थीम आपके डिवाइस के अनुसार: डार्क |
| सिस्टम चुने रहते डिवाइस बदलता है | कुछ नहीं: पेज बदलता है, लेकिन यहाँ किसी ने यह नहीं माँगा |
ये अपेक्षित घोषणाएँ हैं, रिकॉर्डिंग नहीं। स्क्रीन रीडर और ब्राउज़र के हिसाब से शब्द और क्रम बदलते हैं।
कोड
कोड कॉपी करें
वही फ़ाइलें जिन पर यह डेमो चलता है। स्टाइल एक प्रीफ़िक्स के साथ परोसी जाती हैं जो इस साइट की अपनी स्टाइल को डेमो से दूर रखता है; आप जो कॉपी करते हैं वह मूल है।
<div class="ap-theme-switcher" data-ap-theme-switcher data-theme="system">
<div class="ap-theme-switcher__appbar">
<span class="ap-theme-switcher__logo" aria-hidden="true"><svg viewBox="0 0 24 24" focusable="false"><path d="M5 8h11v6a5 5 0 0 1-5 5h-1a5 5 0 0 1-5-5Z"/><path d="M16 9h1.5a2.5 2.5 0 0 1 0 5H16M8 3.5c0 1 1 1 1 2M11.5 3.5c0 1 1 1 1 2"/></svg></span>
<span class="ap-theme-switcher__brand">Nukkad Café</span>
<span class="ap-theme-switcher__avatar" aria-hidden="true" translate="no">AR</span>
</div>
<div class="ap-theme-switcher__body">
<div class="ap-theme-switcher__settings">
<h3 class="ap-theme-switcher__name">Appearance</h3>
<p class="ap-theme-switcher__own">This preview keeps its own theme, separate from the Light and Dark buttons above the stage.</p>
<fieldset class="ap-theme-switcher__set" aria-describedby="theme-switcher-device">
<legend class="ap-theme-switcher__legend">Theme</legend>
<div class="ap-theme-switcher__options">
<label class="ap-theme-switcher__opt">
<input class="ap-theme-switcher__radio" type="radio" name="theme-switcher-theme" value="light" />
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="4"/><path d="M12 2.5v2M12 19.5v2M4.6 4.6 6 6M18 18l1.4 1.4M2.5 12h2M19.5 12h2M4.6 19.4 6 18M18 6l1.4-1.4"/></svg>
<span>Light</span>
</label>
<label class="ap-theme-switcher__opt">
<input class="ap-theme-switcher__radio" type="radio" name="theme-switcher-theme" value="dark" />
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5Z"/></svg>
<span>Dark</span>
</label>
<label class="ap-theme-switcher__opt">
<input class="ap-theme-switcher__radio" type="radio" name="theme-switcher-theme" value="system" checked />
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="3" y="4" width="18" height="12" rx="2"/><path d="M8 20h8M12 16v4"/></svg>
<span>System</span>
</label>
</div>
</fieldset>
<p class="ap-theme-switcher__device" id="theme-switcher-device" data-ap-device>Your device is set to light.</p>
<p class="ap-theme-switcher__sr" role="status" data-ap-theme-status></p>
</div>
<div class="ap-theme-switcher__order">
<div class="ap-theme-switcher__order-top">
<h4 class="ap-theme-switcher__order-name">Your order</h4>
<span class="ap-theme-switcher__chip"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="m5 12.5 4.5 4.5L19 7.5"/></svg>On its way</span>
</div>
<p class="ap-theme-switcher__items">Masala chai × 2, vada pav × 1</p>
<p class="ap-theme-switcher__eta">Arrives in about 12 minutes. <a href="#rider">Track the rider</a></p>
<div class="ap-field">
<label class="ap-label" for="theme-switcher-note">Note for the rider</label>
<input class="ap-input" id="theme-switcher-note" type="text" value="Gate 2, ring once" />
</div>
<div class="ap-theme-switcher__controls">
<label class="ap-theme-switcher__check"><input type="checkbox" checked /> Leave at the door</label>
<label class="ap-theme-switcher__tip">Rider tip
<select>
<option>None</option>
<option selected>₹20</option>
<option>₹50</option>
</select>
</label>
</div>
<div class="ap-theme-switcher__past" role="region" aria-label="Past orders" tabindex="0">
<ul>
<li><span>Filter coffee, idli</span> <span class="ap-theme-switcher__when" translate="no">28 Sep</span></li>
<li><span>Masala chai × 3</span> <span class="ap-theme-switcher__when" translate="no">21 Sep</span></li>
<li><span>Bun maska, ginger tea</span> <span class="ap-theme-switcher__when" translate="no">14 Sep</span></li>
<li><span>Poha, filter coffee</span> <span class="ap-theme-switcher__when" translate="no">7 Sep</span></li>
<li><span>Vada pav × 2</span> <span class="ap-theme-switcher__when" translate="no">31 Aug</span></li>
</ul>
</div>
</div>
</div>
<div class="ap-theme-switcher__contrast-wrap">
<table class="ap-theme-switcher__contrast">
<caption>Contrast in both themes, from the design tokens</caption>
<thead>
<tr>
<th scope="col">Pair</th>
<th scope="col" data-col="light">Light <span class="ap-theme-switcher__now" aria-hidden="true">Now</span></th>
<th scope="col" data-col="dark">Dark <span class="ap-theme-switcher__now" aria-hidden="true">Now</span></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><span>Body text</span> <code>text / surface</code></th>
<td data-col="light"><span translate="no">17.72:1</span> <span class="ap-theme-switcher__lvl" translate="no">AAA</span></td>
<td data-col="dark"><span translate="no">16.97:1</span> <span class="ap-theme-switcher__lvl" translate="no">AAA</span></td>
</tr>
<tr>
<th scope="row"><span>Secondary text</span> <code>text-3 / surface</code></th>
<td data-col="light"><span translate="no">6.42:1</span> <span class="ap-theme-switcher__lvl" translate="no">AA</span></td>
<td data-col="dark"><span translate="no">6.91:1</span> <span class="ap-theme-switcher__lvl" translate="no">AA</span></td>
</tr>
<tr>
<th scope="row"><span>Links</span> <code>accent-text / surface</code></th>
<td data-col="light"><span translate="no">7.90:1</span> <span class="ap-theme-switcher__lvl" translate="no">AAA</span></td>
<td data-col="dark"><span translate="no">8.89:1</span> <span class="ap-theme-switcher__lvl" translate="no">AAA</span></td>
</tr>
<tr>
<th scope="row"><span>Button label</span> <code>on-accent / accent</code></th>
<td data-col="light"><span translate="no">6.29:1</span> <span class="ap-theme-switcher__lvl" translate="no">AA</span></td>
<td data-col="dark"><span translate="no">6.53:1</span> <span class="ap-theme-switcher__lvl" translate="no">AA</span></td>
</tr>
<tr>
<th scope="row"><span>Control edges</span> <code>border-strong / surface</code></th>
<td data-col="light"><span translate="no">4.50:1</span> <span class="ap-theme-switcher__lvl" translate="no">AA</span></td>
<td data-col="dark"><span translate="no">5.46:1</span> <span class="ap-theme-switcher__lvl" translate="no">AA</span></td>
</tr>
</tbody>
</table>
</div>
<figure class="ap-theme-switcher__head">
<figcaption>No flash on load: in the head, before the stylesheets</figcaption>
<pre><code><script>
<span class="ap-theme-switcher__in">const saved = localStorage.getItem("theme-choice");</span>
<span class="ap-theme-switcher__in">if (saved) document.documentElement.dataset.theme = saved;</span>
</script></code></pre>
</figure>
</div>
/* Shared primitives: buttons and form fields used across the patterns.
Uses the --ap-* design tokens. */
.ap-btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 40px;
padding: 0 16px;
border: 1px solid var(--ap-border-strong);
border-radius: var(--ap-radius-sm);
background: var(--ap-surface);
color: var(--ap-text);
font: inherit;
font-weight: 600;
line-height: 1.2;
text-decoration: none;
white-space: nowrap;
cursor: pointer;
transition: background-color var(--ap-duration) var(--ap-ease), border-color var(--ap-duration) var(--ap-ease), box-shadow var(--ap-duration) var(--ap-ease);
}
.ap-btn:hover {
background: var(--ap-surface-2);
}
.ap-btn:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
.ap-btn:disabled,
.ap-btn[aria-disabled="true"] {
opacity: .55;
cursor: not-allowed;
}
.ap-btn--primary {
border-color: var(--ap-accent);
background: var(--ap-accent);
color: var(--ap-on-accent);
box-shadow: var(--ap-shadow-sm);
}
.ap-btn--primary:hover {
border-color: var(--ap-accent-hover);
background: var(--ap-accent-hover);
}
.ap-btn--danger {
border-color: var(--ap-danger);
background: var(--ap-danger);
color: var(--ap-surface);
}
.ap-btn--danger:hover {
filter: brightness(1.08);
}
.ap-btn--ghost {
border-color: transparent;
background: transparent;
}
.ap-btn--ghost:hover {
background: var(--ap-surface-2);
}
.ap-btn__icon {
width: 18px;
height: 18px;
flex-shrink: 0;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-field {
display: grid;
gap: 6px;
}
.ap-label {
color: var(--ap-text);
font-weight: 600;
font-size: .9375rem;
}
.ap-hint {
margin: 0;
color: var(--ap-text-3);
font-size: .875rem;
line-height: 1.45;
}
.ap-error {
display: flex;
align-items: flex-start;
gap: 6px;
margin: 0;
color: var(--ap-danger);
font-size: .875rem;
font-weight: 600;
line-height: 1.45;
}
.ap-input {
width: 100%;
min-height: 44px;
padding: 10px 12px;
border: 1px solid var(--ap-border-strong);
border-radius: var(--ap-radius-sm);
background: var(--ap-surface);
color: var(--ap-text);
font: inherit;
line-height: 1.4;
transition: border-color var(--ap-duration) var(--ap-ease), box-shadow var(--ap-duration) var(--ap-ease);
}
.ap-input::placeholder {
color: var(--ap-text-3);
}
.ap-input:hover {
border-color: var(--ap-text-2);
}
.ap-input:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 1px;
border-color: var(--ap-focus);
}
.ap-input[aria-invalid="true"] {
border-color: var(--ap-danger);
box-shadow: inset 4px 0 0 var(--ap-danger);
}
textarea.ap-input {
resize: vertical;
min-height: 88px;
}
@media (prefers-reduced-motion: reduce) {
.ap-btn,
.ap-input { transition: none; }
}
/* Theme switcher. Uses the --ap-* design tokens and the shared primitives. */
/* The demo stands in for a whole page, whose root carries data-theme. It
holds the token palette for both themes as light-dark() pairs, so it can
switch on its own inside this page, whatever theme the page around it
shows. color-scheme picks the side of each pair: data-theme="light" or
"dark" fixes it, and "system" allows both, so the device decides, live.
Both palettes come from the tokens stylesheet (Tokens tab), which always
declares them as --ap-light-* and --ap-dark-*. On a real page the tokens
stylesheet themes :root itself and this block is not needed. */
.ap-theme-switcher {
color-scheme: light dark;
--ap-bg: light-dark(var(--ap-light-bg), var(--ap-dark-bg));
--ap-surface: light-dark(var(--ap-light-surface), var(--ap-dark-surface));
--ap-surface-2: light-dark(var(--ap-light-surface-2), var(--ap-dark-surface-2));
--ap-surface-3: light-dark(var(--ap-light-surface-3), var(--ap-dark-surface-3));
--ap-border: light-dark(var(--ap-light-border), var(--ap-dark-border));
--ap-border-strong: light-dark(var(--ap-light-border-strong), var(--ap-dark-border-strong));
--ap-text: light-dark(var(--ap-light-text), var(--ap-dark-text));
--ap-text-2: light-dark(var(--ap-light-text-2), var(--ap-dark-text-2));
--ap-text-3: light-dark(var(--ap-light-text-3), var(--ap-dark-text-3));
--ap-accent: light-dark(var(--ap-light-accent), var(--ap-dark-accent));
--ap-accent-hover: light-dark(var(--ap-light-accent-hover), var(--ap-dark-accent-hover));
--ap-on-accent: light-dark(var(--ap-light-on-accent), var(--ap-dark-on-accent));
--ap-accent-text: light-dark(var(--ap-light-accent-text), var(--ap-dark-accent-text));
--ap-accent-soft: light-dark(var(--ap-light-accent-soft), var(--ap-dark-accent-soft));
--ap-accent-soft-text: light-dark(var(--ap-light-accent-soft-text), var(--ap-dark-accent-soft-text));
--ap-focus: light-dark(var(--ap-light-focus), var(--ap-dark-focus));
--ap-success: light-dark(var(--ap-light-success), var(--ap-dark-success));
--ap-success-soft: light-dark(var(--ap-light-success-soft), var(--ap-dark-success-soft));
}
.ap-theme-switcher[data-theme="light"] {
color-scheme: light;
}
.ap-theme-switcher[data-theme="dark"] {
color-scheme: dark;
}
.ap-theme-switcher {
width: min(100%, 780px);
margin-inline: auto;
overflow: hidden;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-lg);
background: var(--ap-bg);
color: var(--ap-text);
box-shadow: var(--ap-shadow-md);
container-type: inline-size;
}
/* ── The app's own bar ── */
.ap-theme-switcher__appbar {
display: flex;
align-items: center;
gap: 10px;
padding: 12px 18px;
border-bottom: 1px solid var(--ap-border);
background: var(--ap-surface);
}
.ap-theme-switcher__logo {
display: grid;
place-items: center;
width: 34px;
height: 34px;
border-radius: var(--ap-radius-sm);
background: var(--ap-accent-soft);
color: var(--ap-accent-soft-text);
}
.ap-theme-switcher__logo svg {
width: 20px;
height: 20px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-theme-switcher__brand {
font-weight: 700;
}
.ap-theme-switcher__avatar {
display: grid;
place-items: center;
width: 32px;
height: 32px;
margin-left: auto;
border-radius: var(--ap-radius-full);
background: var(--ap-surface-3);
color: var(--ap-text);
font-size: .75rem;
font-weight: 700;
}
.ap-theme-switcher__body {
display: grid;
grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr);
gap: 16px;
padding: 18px;
}
.ap-theme-switcher__settings,
.ap-theme-switcher__order {
min-width: 0;
padding: 18px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius);
background: var(--ap-surface);
}
/* ── The switcher: three radios drawn as one segmented control ── */
.ap-theme-switcher__name {
margin: 0;
font-size: 1.0625rem;
font-weight: 650;
}
.ap-theme-switcher__own {
margin: 4px 0 16px;
color: var(--ap-text-3);
font-size: .8125rem;
line-height: 1.5;
}
.ap-theme-switcher__set {
min-width: 0;
margin: 0;
padding: 0;
border: 0;
}
.ap-theme-switcher__legend {
margin-bottom: 8px;
padding: 0;
font-size: .875rem;
font-weight: 650;
}
.ap-theme-switcher__options {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 4px;
padding: 4px;
border: 1px solid var(--ap-border-strong);
border-radius: var(--ap-radius);
background: var(--ap-surface-2);
}
.ap-theme-switcher__opt {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 4px;
min-height: 60px;
padding: 6px 4px;
border-radius: var(--ap-radius-sm);
color: var(--ap-text-2);
font-size: .8125rem;
font-weight: 600;
text-align: center;
cursor: pointer;
}
.ap-theme-switcher__opt:hover {
color: var(--ap-text);
}
.ap-theme-switcher__radio {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
}
.ap-theme-switcher__opt svg {
width: 20px;
height: 20px;
fill: none;
stroke: currentColor;
stroke-width: 1.8;
stroke-linecap: round;
stroke-linejoin: round;
}
/* The chosen theme: raised, outlined in the accent, bolder; not color alone. */
.ap-theme-switcher__opt:has(:checked) {
background: var(--ap-surface);
color: var(--ap-text);
font-weight: 750;
box-shadow: 0 0 0 1.5px var(--ap-accent), var(--ap-shadow-sm);
}
.ap-theme-switcher__opt:has(:checked) svg {
color: var(--ap-accent-text);
}
.ap-theme-switcher__opt:has(:focus-visible) {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
.ap-theme-switcher__device {
margin: 10px 0 0;
color: var(--ap-text-3);
font-size: .8125rem;
}
/* ── A screen of the app, to see the theme on ── */
.ap-theme-switcher__order {
display: grid;
gap: 12px;
align-content: start;
}
.ap-theme-switcher__order-top {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.ap-theme-switcher__order-name {
margin: 0;
font-size: 1rem;
font-weight: 650;
}
.ap-theme-switcher__chip {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 3px 10px 3px 8px;
border-radius: var(--ap-radius-full);
background: var(--ap-success-soft);
color: var(--ap-success);
font-size: .75rem;
font-weight: 700;
}
.ap-theme-switcher__chip svg {
width: 14px;
height: 14px;
fill: none;
stroke: currentColor;
stroke-width: 2.4;
stroke-linecap: round;
stroke-linejoin: round;
}
.ap-theme-switcher__items,
.ap-theme-switcher__eta {
margin: 0;
}
.ap-theme-switcher__items {
font-weight: 600;
}
.ap-theme-switcher__eta {
margin-top: -6px;
color: var(--ap-text-2);
font-size: .875rem;
}
.ap-theme-switcher__eta a {
color: var(--ap-accent-text);
font-weight: 600;
text-decoration: underline;
text-underline-offset: 3px;
}
.ap-theme-switcher__eta a:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
border-radius: 2px;
}
/* Native controls left native: color-scheme themes them, accent-color tints them. */
.ap-theme-switcher__controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px 22px;
font-size: .9375rem;
}
.ap-theme-switcher__check,
.ap-theme-switcher__tip {
display: inline-flex;
align-items: center;
gap: 8px;
}
.ap-theme-switcher__check input {
width: 18px;
height: 18px;
margin: 0;
accent-color: var(--ap-accent);
}
.ap-theme-switcher__tip select {
min-height: 36px;
padding: 4px 8px;
font: inherit;
accent-color: var(--ap-accent);
}
.ap-theme-switcher__check input:focus-visible,
.ap-theme-switcher__tip select:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
.ap-theme-switcher__past {
height: 92px;
overflow: auto;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-sm);
}
.ap-theme-switcher__past:focus-visible {
outline: 2px solid var(--ap-focus);
outline-offset: 2px;
}
.ap-theme-switcher__past ul {
margin: 0;
padding: 2px 12px;
list-style: none;
}
.ap-theme-switcher__past li {
display: flex;
justify-content: space-between;
gap: 12px;
padding: 7px 0;
border-bottom: 1px solid var(--ap-border);
color: var(--ap-text-2);
font-size: .875rem;
}
.ap-theme-switcher__past li:last-child {
border-bottom: 0;
}
.ap-theme-switcher__when {
flex-shrink: 0;
color: var(--ap-text-3);
font-variant-numeric: tabular-nums;
}
/* ── Contrast, both themes, the one showing now marked ── */
.ap-theme-switcher__contrast-wrap {
margin: 0 18px 18px;
overflow: hidden;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius);
background: var(--ap-surface);
}
.ap-theme-switcher__contrast {
width: 100%;
border-collapse: collapse;
font-size: .875rem;
}
.ap-theme-switcher__contrast caption {
padding: 12px 14px;
font-weight: 650;
text-align: start;
}
.ap-theme-switcher__contrast th,
.ap-theme-switcher__contrast td {
padding: 8px 14px;
border-top: 1px solid var(--ap-border);
text-align: start;
vertical-align: top;
}
.ap-theme-switcher__contrast thead th {
color: var(--ap-text-3);
font-size: .75rem;
font-weight: 700;
letter-spacing: .04em;
text-transform: uppercase;
}
.ap-theme-switcher__contrast tbody th {
font-weight: 600;
}
.ap-theme-switcher__contrast code {
display: block;
margin-top: 2px;
color: var(--ap-text-3);
font-family: var(--ap-mono);
font-size: .75rem;
font-weight: 400;
}
.ap-theme-switcher__contrast td {
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.ap-theme-switcher__lvl {
display: inline-block;
margin-left: 4px;
padding: 0 6px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius-full);
background: var(--ap-surface);
color: var(--ap-text-2);
font-size: .6875rem;
font-weight: 700;
}
.ap-theme-switcher__now {
display: none;
margin-left: 4px;
padding: 0 6px;
border-radius: var(--ap-radius-full);
background: var(--ap-accent);
color: var(--ap-on-accent);
font-size: .6875rem;
letter-spacing: .04em;
}
.ap-theme-switcher[data-resolved="light"] [data-col="light"],
.ap-theme-switcher[data-resolved="dark"] [data-col="dark"] {
background: var(--ap-accent-soft);
color: var(--ap-accent-soft-text);
}
.ap-theme-switcher[data-resolved="light"] [data-col="light"] .ap-theme-switcher__now,
.ap-theme-switcher[data-resolved="dark"] [data-col="dark"] .ap-theme-switcher__now {
display: inline-block;
}
/* ── The head script that prevents a flash ── */
.ap-theme-switcher__head {
margin: 0 18px 18px;
border: 1px solid var(--ap-border);
border-radius: var(--ap-radius);
background: var(--ap-surface);
}
.ap-theme-switcher__head figcaption {
padding: 10px 14px;
border-bottom: 1px solid var(--ap-border);
color: var(--ap-text-2);
font-size: .8125rem;
font-weight: 650;
}
.ap-theme-switcher__head pre {
margin: 0;
padding: 12px 14px;
color: var(--ap-text);
font-family: var(--ap-mono);
font-size: .8125rem;
line-height: 1.6;
/* pre-line keeps the line breaks but drops the indentation of the page source. */
white-space: pre-line;
overflow-wrap: anywhere;
}
.ap-theme-switcher__in {
display: inline-block;
padding-left: 2ch;
}
/* Read by screen readers, not shown. */
.ap-theme-switcher__sr {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
@container (max-width: 600px) {
.ap-theme-switcher__body { grid-template-columns: minmax(0, 1fr); padding: 14px; }
.ap-theme-switcher__contrast-wrap,
.ap-theme-switcher__head { margin-inline: 14px; }
.ap-theme-switcher__contrast th,
.ap-theme-switcher__contrast td { padding-inline: 10px; }
}
@container (max-width: 460px) {
.ap-theme-switcher__contrast { font-size: .8125rem; }
.ap-theme-switcher__contrast th,
.ap-theme-switcher__contrast td { padding: 8px; }
.ap-theme-switcher__contrast td { white-space: normal; }
.ap-theme-switcher__contrast code { overflow-wrap: anywhere; }
.ap-theme-switcher__lvl { display: block; width: fit-content; margin: 4px 0 0; }
}
@media (forced-colors: active) {
.ap-theme-switcher__opt:has(:checked) { outline: 2px solid Highlight; }
}
/**
* Theme switcher: Light, Dark and System as native radio buttons.
*
* The choice is written to one attribute, data-theme, on the element that
* stands for the page's root; the stylesheet does the rest (here with
* light-dark() and color-scheme). System leaves the device in charge, and
* the page changes with it, live. A polite status confirms a choice made
* here, and stays quiet when the device changes the theme.
*
* Markup: [data-ap-theme-switcher] (the themed root, data-theme="light|dark|
* system") holding radio inputs name="theme-switcher-theme", a
* p[data-ap-device] that says what the device is set to, and a role=status
* element [data-ap-theme-status]. Add data-remember to the root to save the
* choice in localStorage ("theme-choice") and restore it on the next visit.
*
* On a real page, use document.documentElement as the target.
*/
const KEY = "theme-choice";
const CHOICES = ["light", "dark", "system"];
export function init(root) {
const target = root; // on a real page: document.documentElement
const radios = [...root.querySelectorAll("input[name='theme-switcher-theme']")];
const device = root.querySelector("[data-ap-device]");
const status = root.querySelector("[data-ap-theme-status]");
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
let speaking = 0;
const resolved = (choice) => (choice === "system" ? (prefersDark.matches ? "dark" : "light") : choice);
const checked = () => (radios.find((r) => r.checked) || radios[radios.length - 1]).value;
function remembered() {
if (!root.hasAttribute("data-remember")) return null;
try {
const saved = localStorage.getItem(KEY);
return CHOICES.includes(saved) ? saved : null;
} catch {
return null; // storage blocked: fall back to the default
}
}
function remember(choice) {
if (!root.hasAttribute("data-remember")) return;
try { localStorage.setItem(KEY, choice); } catch { /* storage blocked */ }
}
function apply(choice) {
target.dataset.theme = choice;
target.dataset.resolved = resolved(choice);
const text = prefersDark.matches ? "Your device is set to dark." : "Your device is set to light.";
if (device.textContent !== text) device.textContent = text;
}
function say(text) {
clearTimeout(speaking);
status.textContent = "";
speaking = setTimeout(() => { status.textContent = text; }, 60);
}
function onChange(event) {
if (!radios.includes(event.target)) return;
const choice = event.target.value;
apply(choice);
remember(choice);
if (choice === "light") say("Light theme on");
else if (choice === "dark") say("Dark theme on");
else say(prefersDark.matches ? "Theme follows your device: dark" : "Theme follows your device: light");
}
// The device changed (a schedule, or a setting elsewhere): follow it if
// System is chosen, and update the line about the device. Nobody here
// asked, so nothing is announced.
function onDevice() {
apply(checked());
}
const start = remembered() || checked();
for (const r of radios) r.checked = r.value === start;
apply(start);
root.addEventListener("change", onChange);
prefersDark.addEventListener("change", onDevice);
return () => {
clearTimeout(speaking);
root.removeEventListener("change", onChange);
prefersDark.removeEventListener("change", onDevice);
};
}
for (const root of document.querySelectorAll("[data-ap-theme-switcher]")) init(root);
/* Design tokens for the pattern components. Light by default, dark when the
system asks for it; set data-theme="dark" on :root to force dark. */
:root {
--ap-radius-sm: 8px;
--ap-radius: 12px;
--ap-radius-lg: 16px;
--ap-radius-full: 999px;
--ap-font: "Instrument Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--ap-mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
--ap-ease: cubic-bezier(.2, .8, .2, 1);
--ap-duration: 180ms;
--ap-target: 44px;
--ap-bg: #F4F4F5;
--ap-surface: #FFFFFF;
--ap-surface-2: #F4F4F5;
--ap-surface-3: #E4E4E7;
--ap-border: #E4E4E7;
--ap-border-strong: #76767F;
--ap-text: #18181B;
--ap-text-2: #3F3F46;
--ap-text-3: #5E5E66;
--ap-accent: #4F46E5;
--ap-accent-hover: #4338CA;
--ap-on-accent: #FFFFFF;
--ap-accent-text: #4338CA;
--ap-accent-soft: #EEF2FF;
--ap-accent-soft-text: #3730A3;
--ap-focus: #4F46E5;
--ap-danger: #B91C1C;
--ap-danger-soft: #FEF2F2;
--ap-success: #15803D;
--ap-success-soft: #F0FDF4;
--ap-warning: #A15C07;
--ap-warning-soft: #FEFCE8;
--ap-info: #1D4ED8;
--ap-info-soft: #EFF6FF;
--ap-scrim: rgb(9 9 11 / .48);
--ap-shadow-sm: 0 1px 2px rgb(9 9 11 / .06);
--ap-shadow-md: 0 1px 2px rgb(9 9 11 / .05), 0 6px 16px -4px rgb(9 9 11 / .1);
--ap-shadow-lg: 0 2px 6px rgb(9 9 11 / .06), 0 20px 40px -12px rgb(9 9 11 / .22);
--ap-light-bg: #F4F4F5;
--ap-light-surface: #FFFFFF;
--ap-light-surface-2: #F4F4F5;
--ap-light-surface-3: #E4E4E7;
--ap-light-border: #E4E4E7;
--ap-light-border-strong: #76767F;
--ap-light-text: #18181B;
--ap-light-text-2: #3F3F46;
--ap-light-text-3: #5E5E66;
--ap-light-accent: #4F46E5;
--ap-light-accent-hover: #4338CA;
--ap-light-on-accent: #FFFFFF;
--ap-light-accent-text: #4338CA;
--ap-light-accent-soft: #EEF2FF;
--ap-light-accent-soft-text: #3730A3;
--ap-light-focus: #4F46E5;
--ap-light-danger: #B91C1C;
--ap-light-danger-soft: #FEF2F2;
--ap-light-success: #15803D;
--ap-light-success-soft: #F0FDF4;
--ap-light-warning: #A15C07;
--ap-light-warning-soft: #FEFCE8;
--ap-light-info: #1D4ED8;
--ap-light-info-soft: #EFF6FF;
--ap-light-scrim: rgb(9 9 11 / .48);
--ap-light-shadow-sm: 0 1px 2px rgb(9 9 11 / .06);
--ap-light-shadow-md: 0 1px 2px rgb(9 9 11 / .05), 0 6px 16px -4px rgb(9 9 11 / .1);
--ap-light-shadow-lg: 0 2px 6px rgb(9 9 11 / .06), 0 20px 40px -12px rgb(9 9 11 / .22);
--ap-dark-bg: #09090B;
--ap-dark-surface: #18181B;
--ap-dark-surface-2: #27272A;
--ap-dark-surface-3: #3F3F46;
--ap-dark-border: #2E2E33;
--ap-dark-border-strong: #8E8E97;
--ap-dark-text: #FAFAFA;
--ap-dark-text-2: #D4D4D8;
--ap-dark-text-3: #A1A1AA;
--ap-dark-accent: #818CF8;
--ap-dark-accent-hover: #A5B4FC;
--ap-dark-on-accent: #0C0A1F;
--ap-dark-accent-text: #A5B4FC;
--ap-dark-accent-soft: #1E1B4B;
--ap-dark-accent-soft-text: #C7D2FE;
--ap-dark-focus: #A5B4FC;
--ap-dark-danger: #F87171;
--ap-dark-danger-soft: #2A1215;
--ap-dark-success: #4ADE80;
--ap-dark-success-soft: #0F2A1A;
--ap-dark-warning: #FACC15;
--ap-dark-warning-soft: #2A2410;
--ap-dark-info: #60A5FA;
--ap-dark-info-soft: #0F1D33;
--ap-dark-scrim: rgb(0 0 0 / .62);
--ap-dark-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-dark-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-dark-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--ap-bg: #09090B;
--ap-surface: #18181B;
--ap-surface-2: #27272A;
--ap-surface-3: #3F3F46;
--ap-border: #2E2E33;
--ap-border-strong: #8E8E97;
--ap-text: #FAFAFA;
--ap-text-2: #D4D4D8;
--ap-text-3: #A1A1AA;
--ap-accent: #818CF8;
--ap-accent-hover: #A5B4FC;
--ap-on-accent: #0C0A1F;
--ap-accent-text: #A5B4FC;
--ap-accent-soft: #1E1B4B;
--ap-accent-soft-text: #C7D2FE;
--ap-focus: #A5B4FC;
--ap-danger: #F87171;
--ap-danger-soft: #2A1215;
--ap-success: #4ADE80;
--ap-success-soft: #0F2A1A;
--ap-warning: #FACC15;
--ap-warning-soft: #2A2410;
--ap-info: #60A5FA;
--ap-info-soft: #0F1D33;
--ap-scrim: rgb(0 0 0 / .62);
--ap-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
}
:root[data-theme="dark"] {
--ap-bg: #09090B;
--ap-surface: #18181B;
--ap-surface-2: #27272A;
--ap-surface-3: #3F3F46;
--ap-border: #2E2E33;
--ap-border-strong: #8E8E97;
--ap-text: #FAFAFA;
--ap-text-2: #D4D4D8;
--ap-text-3: #A1A1AA;
--ap-accent: #818CF8;
--ap-accent-hover: #A5B4FC;
--ap-on-accent: #0C0A1F;
--ap-accent-text: #A5B4FC;
--ap-accent-soft: #1E1B4B;
--ap-accent-soft-text: #C7D2FE;
--ap-focus: #A5B4FC;
--ap-danger: #F87171;
--ap-danger-soft: #2A1215;
--ap-success: #4ADE80;
--ap-success-soft: #0F2A1A;
--ap-warning: #FACC15;
--ap-warning-soft: #2A2410;
--ap-info: #60A5FA;
--ap-info-soft: #0F1D33;
--ap-scrim: rgb(0 0 0 / .62);
--ap-shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
--ap-shadow-md: 0 1px 2px rgb(0 0 0 / .4), 0 8px 20px -6px rgb(0 0 0 / .5);
--ap-shadow-lg: 0 2px 8px rgb(0 0 0 / .45), 0 24px 48px -12px rgb(0 0 0 / .7);
}
डब्ल्यूसीएजी 2.2
यह किन मानदंडों को पूरा करता है
वे सफलता मानदंड जिनका यह पैटर्न ध्यान रखता है, और कैसे।
-
1.3.1 जानकारी और संबंध
स्तर A
fieldset और legend तीनों विकल्पों को थीम नाम के तहत समूह बनाते हैं, और डिवाइस वाली पंक्ति समूह का विवरण देती है।
-
1.4.3 कंट्रास्ट (न्यूनतम)
स्तर AA
हर टेक्स्ट जोड़ी दोनों थीम में 4.5:1 पूरा करती है; टेबल डिज़ाइन टोकन से मापे गए अनुपात दिखाती है।
-
1.4.11 गैर-टेक्स्ट कंट्रास्ट
स्तर AA
कंट्रोल के किनारे, फ़ोकस घेरा और चुने गए विकल्प का बॉर्डर दोनों थीम में 3:1 या उससे ज़्यादा रहते हैं।
-
2.1.1 कीबोर्ड
स्तर A
नेटिव रेडियो बटन Tab और ऐरो कुंजियों पर प्रतिक्रिया देते हैं, इसलिए स्विचर को अपनी कुंजी हैंडलिंग की ज़रूरत नहीं।
-
3.2.2 इनपुट पर
स्तर A
थीम चुनने से सिर्फ़ रंग बदलते हैं; फ़ोकस रेडियो बटन पर रहता है और पेज पर और कुछ नहीं हिलता।
-
4.1.2 नाम, भूमिका, मान
स्तर A
हर विकल्प लेबल वाला रेडियो इनपुट है, इसलिए उसका नाम, समूह में उसकी जगह और चुने होने की स्थिति ब्राउज़र से मिलती है।
-
4.1.3 स्टेटस मैसेज
स्तर AA
एक विनम्र स्टेटस मैसेज व्यक्ति के किए चुनाव की पुष्टि करता है, और जब डिवाइस अपने-आप थीम बदले तो चुप रहता है।
उपयोग
इसे कब इस्तेमाल करें
इसे अपनाएँ
- लाइट और डार्क डिज़ाइन वाली साइटें और ऐप, जहाँ लोग अपने डिवाइस की सेटिंग से अलग कुछ चाह सकते हैं।
- सेटिंग्स पेज और साइट हेडर; हेडर आम तौर पर यही तीन विकल्प एक मेनू बटन के पीछे दिखाता है।
कुछ और इस्तेमाल करें
- एक थीम वाली साइटें: अगर आप दोनों डिज़ाइन देते हैं, तो बिना कंट्रोल के prefers-color-scheme के साथ चलना काफ़ी है।
- डार्क मोड के लिए अकेला चालू-बंद टॉगल: वह "मेरे डिवाइस के साथ चलो" नहीं कह सकता, जो ज़्यादातर लोग चाहते हैं।
आम विफलताएँ
आमतौर पर यह कहाँ गलत होता है
गलत थीम की चमक
पेज लोड होने के बाद लागू किया गया चुनाव पहले डिफ़ॉल्ट दिखाता है, जो अँधेरे में आँखों में चुभता है। head में एक ब्लॉकिंग स्क्रिप्ट पहले पेंट से पहले एट्रिब्यूट सेट कर देती है।
सिस्टम का विकल्प नहीं
सिर्फ़ लाइट और डार्क पेज को बाँध देते हैं, इसलिए वह सूर्यास्त पर डिवाइस के साथ नहीं बदलता। यहाँ सिस्टम डिफ़ॉल्ट है और तुरंत बदलता है।
डार्क बैकग्राउंड पर लाइट कंट्रोल
color-scheme के बिना चेकबॉक्स, सेलेक्ट और स्क्रॉलबार डार्क पेज पर भी चमकीले रहते हैं। इसे सेट करने से ब्राउज़र अपने कंट्रोल मेल खाते हुए बनाता है।
सिर्फ़ एक थीम में जाँचा गया कंट्रास्ट
सफ़ेद पर पास होने वाला हल्का ग्रे लगभग काले पर विफल हो सकता है। यहाँ हर जोड़ी दोनों थीम में, एक ही टोकन से मापी गई है।
ऐसे बदलाव घोषित करना जो किसी ने नहीं माँगे
सूर्यास्त पर डिवाइस बदलने की वजह से "डार्क थीम चालू" पढ़ना व्यक्ति के काम में बाधा डालता है। सिर्फ़ यहाँ किया गया चुनाव घोषित होता है।
गलत स्थिति दिखाने वाला टॉगल
सूरज का आइकन किसी साइट पर "लाइट पर जाएँ" और किसी पर "लाइट चालू है" का मतलब देता है, जिससे सब उलझते हैं। रेडियो बटन टेक्स्ट में नाम के साथ दिखाते हैं कि कौन-सी थीम चुनी है।
नोट्स
इसे बनाना
- असली पेज पर स्क्रिप्ट document.documentElement पर data-theme सेट करती है, और टोकन स्टाइलशीट (टोकन टैब) पहले से :root[data-theme] और prefers-color-scheme पर बदलती है। यहाँ डेमो का अपना रूट उसकी जगह लेता है।
- डेमो की स्टाइलशीट दोनों पैलेट light-dark() जोड़ियों में रखती है और data-theme से color-scheme सेट करती है; सिस्टम पर दोनों स्कीम की अनुमति होती है और डिवाइस तुरंत तय करता है, बिना किसी स्क्रिप्ट के।
- स्क्रिप्ट matchMedia("(prefers-color-scheme: dark)") को सिर्फ़ इसलिए सुनती है ताकि डिवाइस वाली पंक्ति और सिस्टम की घोषणा सही रहें।
- चुनाव याद रखें चालू होने पर चुनाव localStorage में theme-choice के रूप में सहेजा जाता है। डेमो में दिखाई गई head स्क्रिप्ट कोई भी सीएसएस लोड होने से पहले वह कुंजी पढ़ती है, इसलिए पहला पेंट ही सही होता है।
- राइडर को ट्रैक करें लिंक किसी दूसरे पेज की जगह है; कॉपी किए गए कोड में यह एक सामान्य लिंक है।
स्रोत: मीडिया क्वेरीज़ लेवल 5: prefers-color-scheme · सीएसएस कलर एडजस्टमेंट: color-scheme प्रॉपर्टी · WAI-एआरआईए ऑथरिंग प्रैक्टिसेज़: रेडियो ग्रुप
आगे पढ़ें: डार्क मोड और एडेप्टिव रंग
लाइट और डार्क थीम में, डेस्कटॉप और फ़ोन की चौड़ाई पर axe से और कीबोर्ड से जाँचा गया। अभी हर स्क्रीन रीडर और ब्राउज़र जोड़ी के साथ नहीं परखा गया। सुधार बताएँ