Lesson 4 of 8

Theme switcher

Goal

Three states, not two

01

Explanation

A theme has three states, not two: light, dark and follow the system. Most switchers forget the third one, which forces users into a choice they never asked to make.

The approach is to make every color a CSS variable and let the default state follow prefers-color-scheme. When users pick a theme themselves, you write it to data-theme on the root element, and it takes precedence over the default.

Also set color-scheme with each theme, so the browser colors form fields and scrollbars to match.

02

Your task

Make the button cycle through the three states. On each click it should set data-theme, update its label and announce its state with aria-pressed.

Checks

6 checks
  1. Starts by following the system, with no data-theme
  2. The first click picks light
  3. aria-pressed is true after picking a theme
  4. The second click picks dark and actually changes the colors
  5. The third click goes back to the system
  6. The button text changes with each state
03

Hints

0/3
Track lessons0 of 8 done

Build from scratch, on your own, the components you'll find on almost every website, such as tabs, dialogs, tables, toasts and a carousel.

Advanced level, 8 lessons, 2 hours in total.

index.html
Preview
index.html100%
–/6

Press “Run and check” to grade your code.

6 checks listed under “Your task”.

Track lessons0 of 8 done

Build from scratch, on your own, the components you'll find on almost every website, such as tabs, dialogs, tables, toasts and a carousel.

Advanced level, 8 lessons, 2 hours in total.