motion-components 0.7: A Theme Toggle With a Circular Wipe
Version 0.7 of motion-components adds two new elements:
You can see the toggle now: the theme menu in the header of this site is a
Install
The toggle sets two things on the
Four appearances
The
| Appearance | What it shows | Best for |
|---|---|---|
| One button with the sun or moon icon | A site header with little space | |
| A segmented control with one option for each theme | Settings pages | |
| A switch with “Light” and “Dark” labels on each side | Forms and preference panels | |
| A button that opens a drop-down list | A header that also offers “System” |
Add the
The optional page wipe
By default, the theme changes at once. A wipe animates the whole page, and on most sites that is too much for a control people use often. Add the
The wipe uses the View Transitions API. The browser takes a picture of the page before the change. Then the toggle changes the theme, and the browser takes a second picture. The toggle shows the new picture inside a circle and makes the circle grow.
The circle starts at the center of the control. It grows until it covers the farthest corner of the window. Motion drives the circle with its
If the user clicks again during a wipe, the toggle stops the running wipe and starts a new one. Two wipes never run at the same time. Browsers without the View Transitions API change the theme at once, with no error.
The icon is its own element
The morph between sun and moon lives in
When
Compatible with dark-mode-toggle
The attributes and events follow
| Feature | dark-mode-toggle | motion-theme-toggle |
|---|---|---|
| Yes | Yes | |
| Yes | Yes | |
| Switching stylesheets that use a | Yes | Yes |
| A third “System” option | No | Yes |
| A drop-down menu appearance | No | Yes |
| A custom target element instead of | No | Yes, with |
| Animated icon | No | Yes |
| Optional circular wipe | No | Yes, with |
With
Prevent a flash of the wrong theme
Web components start after the browser paints the page for the first time. For a short moment, the page can show the wrong theme. To prevent this, put this small script in the
Accessibility
Every appearance works with the keyboard and with screen readers.
| Appearance | Role for screen readers | Keys |
|---|---|---|
| A switch with an on and off state | Space or Enter | |
| A button that names the current mode | Space or Enter | |
| A group of radio buttons with a label | Arrow keys | |
| A menu button with a list of options | Arrow keys, Home, End, Enter, Escape |
If the user turns on the reduced motion setting of the OS, the theme and the icon change at once. There is no morph and no wipe.
Motion 13
The library now depends on Motion 13 instead of Motion 11. The attributes and events of the existing components did not change. If your own code also imports
What comes next
A later version will make the attribute names more consistent across all 40 components. For example, every time value will use seconds. The release notes will list every renamed attribute.
Read the motion-theme-toggle documentation for every attribute, event and CSS custom property.