--- url: "https://web.dev/articles/building/a-theme-switch-component" titulek: "Building a theme switch component | Articles | web.dev" h1: "Building a theme switch component Stay organized with collections Save and categorize content based on your preferences." popis: "A foundational overview of how to build an adaptive and accessible theme switch component." jazyk: "en" kanonicka: "https://web.dev/articles/building/a-theme-switch-component" drobecky: ["Articles", "Building a theme switch component"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:35:36" znaku: 19745 slov: 2781 kodovani: "iso-8859-2" sha256: "50a0d442c7f1cfe94fa20c01428385ae478696b48e270416a7d58aacb0efcac6" --- Building a theme switch component Stay organized with collections Save and categorize content based on your preferences. A foundational overview of how to build an adaptive and accessible theme switch component. Adam Argyle In this post I want to share thinking on a way to build a dark and light theme switch component. [Try the demo](https://gui-challenges.web.app/theme-switch/dist/). [Demo](https://gui-challenges.web.app/theme-switch/dist/) button size increased for easy visibility If you prefer video, here's a YouTube version of this post: Overview A website may provide settings for controlling the color scheme instead of relying entirely on the system preference. This means that users may browse in a mode other than their system preferences. For example, a user's system is in a light theme, but the user prefers the website to display in the dark theme. There are several web engineering considerations when building this feature. For example, the browser should be made aware of the preference as soon as possible to prevent page color flashes, and the control needs to first sync with the system then allow client-side stored exceptions. Diagram shows a preview of JavaScript page load and document interaction events to overall show there's 4 paths to setting the theme Markup A [`` `aria-label` and `aria-live` polite To indicate to screen readers that changes to `aria-label` should be announced, add [`aria-live="polite"`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA/Attributes/aria-live#:%7E:text=on%20that%20region.-,polite,-Indicates%20that%20updates) to the button. `` This markup addition signals to screen readers to politely, instead of [`aria-live="assertive"`](https://developer.mozilla.org/docs/Web/Accessibility/ARIA/ARIA_Live_Regions#:%7E:text=aria%2Dlive%3D%22assertive%22%20should%20only%20be%20used%20for%20time%2Dsensitive/critical%20notifications%20that%20absolutely%20require%20the%20user%27s%20immediate%20attention.), tell the user what changed. In the case of this button, it will announce "light" or "dark" depending on what the `aria-label` has become. The scalable vector graphic (SVG) icon [SVG](https://developer.mozilla.org/docs/Web/SVG) provides a way to create high-quality, scalable shapes with minimal markup. Interacting with the button can trigger new visual states for the vectors, making SVG great for icons. The following SVG markup goes inside the `