Theme toggle
Sun to moon: detached rays pull in one by one, the sun winds up and the moon draws in. Plus five ways to move instead.
- [ 0 deps ]No JavaScript and no packages. Just HTML and CSS you paste into your project.
- state: aria-pressedYour code changes aria-pressed (or data-state) on the element; the CSS animates the change. That attribute is the only thing you wire up.
- early releaseReady to use, but names of classes and settings may still change before version 1.0. Pin the version you copy.
- all modern browsersUses only CSS that Chrome, Safari, Firefox and Edge have all supported for at least 2.5 years (Baseline widely available).
- 1.16 kB · moderateThe CSS is 1191 bytes after gzip, which is how browsers download it. Lightweight is under 1 kB, moderate under 2.5 kB, heavy above.
Rounded ends and corners.
<button class="u-theme-toggle" type="button" aria-pressed="false" aria-label="Dark mode">
<svg class="u-theme-toggle__svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" aria-hidden="true" focusable="false">
<g class="u-theme-toggle__sun">
<circle class="u-theme-toggle__core" cx="12" cy="12" r="4" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:0" x1="12" y1="5" x2="12" y2="2.75" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:1" x1="16.95" y1="7.05" x2="18.54" y2="5.46" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:2" x1="19" y1="12" x2="21.25" y2="12" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:3" x1="16.95" y1="16.95" x2="18.54" y2="18.54" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:4" x1="12" y1="19" x2="12" y2="21.25" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:5" x1="7.05" y1="16.95" x2="5.46" y2="18.54" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:6" x1="5" y1="12" x2="2.75" y2="12" pathLength="1" />
<line class="u-theme-toggle__ray" style="--i:7" x1="7.05" y1="7.05" x2="5.46" y2="5.46" pathLength="1" />
</g>
<g class="u-theme-toggle__night">
<path class="u-theme-toggle__moon" d="M11.05 4.35A8.2 8.2 0 1 0 20.16 13.28A6.6 6.6 0 0 1 11.05 4.35Z" pathLength="1" />
</g>
</svg>
</button>/*
U-002 · Theme toggle
Sun to moon. Detached rays pull in one by one, the sun winds up and the moon draws in with a spring.
State: aria-pressed="true" = dark mode on (twin: data-state="on")
Variants: data-variant="draw" (default) | "spin" | "rise" | "flip" | "orbit" | "swap"
Tune: --u-theme-toggle-duration whole change (default 800ms)
--u-theme-toggle-size icon size (default 24px; hit area stays ≥ 40px)
--u-theme-toggle-color line colour (default currentColor)
--u-icon-stroke (default 1.5 here) / --u-icon-cap / --u-icon-join
*/
.u-theme-toggle {
--_d: var(--u-theme-toggle-duration, 800ms);
--_in: var(--u-ease-spring, cubic-bezier(0.34, 1.56, 0.64, 1));
--_out: var(--u-ease-out, cubic-bezier(0.16, 1, 0.3, 1));
--_io: var(--u-ease-in-out, cubic-bezier(0.65, 0, 0.35, 1));
--_size: var(--u-theme-toggle-size, 24px);
display: inline-grid;
place-items: center;
inline-size: max(40px, var(--_size));
block-size: max(40px, var(--_size));
padding: 0;
border: 0;
background: none;
color: var(--u-theme-toggle-color, currentColor);
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.u-theme-toggle:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
.u-theme-toggle__svg {
inline-size: var(--_size);
block-size: var(--_size);
stroke-width: var(--u-icon-stroke, 1.5);
stroke-linecap: var(--u-icon-cap, round);
stroke-linejoin: var(--u-icon-join, round);
overflow: visible;
transition: transform var(--_d) var(--_out);
}
.u-theme-toggle__sun,
.u-theme-toggle__night,
.u-theme-toggle__core,
.u-theme-toggle__moon { transform-box: view-box; transform-origin: 12px 12px; }
.u-theme-toggle__ray,
.u-theme-toggle__core,
.u-theme-toggle__moon { stroke-dasharray: 1 1; }
/* ---------- draw (default) ---------- */
/* Back to light: the moon undraws, then the sun draws back, then the rays grow out one by one. */
.u-theme-toggle__moon {
stroke-dashoffset: 1; opacity: 0; transform: rotate(-40deg);
transition: stroke-dashoffset calc(var(--_d) * 0.3) var(--_out), transform calc(var(--_d) * 0.3) var(--_out), opacity 0s linear calc(var(--_d) * 0.3);
}
.u-theme-toggle__core {
stroke-dashoffset: 0; opacity: 1; transform: rotate(0deg);
transition: stroke-dashoffset calc(var(--_d) * 0.35) var(--_out) calc(var(--_d) * 0.3), transform calc(var(--_d) * 0.45) var(--_in) calc(var(--_d) * 0.3), opacity 0s linear calc(var(--_d) * 0.3);
}
.u-theme-toggle__ray {
stroke-dashoffset: 0; opacity: 1;
transition: stroke-dashoffset calc(var(--_d) * 0.25) var(--_out) calc(var(--_d) * 0.55 + var(--i, 0) * 20ms), opacity 0s linear calc(var(--_d) * 0.55 + var(--i, 0) * 20ms);
}
/* Into dark: rays pull in, the sun winds up and is fully gone before the moon starts drawing. */
.u-theme-toggle:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__ray {
stroke-dashoffset: 1; opacity: 0;
transition: stroke-dashoffset calc(var(--_d) * 0.22) var(--_out) calc(var(--i, 0) * 20ms), opacity 0s linear calc(var(--_d) * 0.22 + var(--i, 0) * 20ms);
}
.u-theme-toggle:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__core {
stroke-dashoffset: -1; opacity: 0; transform: rotate(-90deg);
transition: stroke-dashoffset calc(var(--_d) * 0.3) var(--_out) calc(var(--_d) * 0.12), transform calc(var(--_d) * 0.3) var(--_out) calc(var(--_d) * 0.12), opacity 0s linear calc(var(--_d) * 0.42);
}
.u-theme-toggle:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__moon {
stroke-dashoffset: 0; opacity: 1; transform: rotate(0deg);
transition: stroke-dashoffset calc(var(--_d) * 0.42) var(--_out) calc(var(--_d) * 0.48), transform calc(var(--_d) * 0.52) var(--_in) calc(var(--_d) * 0.48), opacity 0s linear calc(var(--_d) * 0.48);
}
/* ---------- spin: the same change inside one full turn ---------- */
.u-theme-toggle[data-variant="spin"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__svg {
transform: rotate(360deg);
transition: transform var(--_d) var(--_in);
}
/* ---------- variants that move whole drawings instead of drawing them ---------- */
.u-theme-toggle:is([data-variant="rise"], [data-variant="flip"], [data-variant="orbit"], [data-variant="swap"]) :is(.u-theme-toggle__ray, .u-theme-toggle__core, .u-theme-toggle__moon) {
stroke-dashoffset: 0; opacity: 1; transform: none; transition: none;
}
.u-theme-toggle:is([data-variant="rise"], [data-variant="orbit"]) .u-theme-toggle__svg { overflow: hidden; }
/* rise: the sun sets below the edge, the moon drops in from above */
.u-theme-toggle[data-variant="rise"] .u-theme-toggle__sun {
transform: translateY(0); opacity: 1;
transition: transform calc(var(--_d) * 0.6) var(--_in) calc(var(--_d) * 0.3), opacity calc(var(--_d) * 0.3) linear calc(var(--_d) * 0.3);
}
.u-theme-toggle[data-variant="rise"] .u-theme-toggle__night {
transform: translateY(-14px); opacity: 0;
transition: transform calc(var(--_d) * 0.4) var(--_out), opacity calc(var(--_d) * 0.3) linear;
}
.u-theme-toggle[data-variant="rise"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__sun {
transform: translateY(14px); opacity: 0;
transition: transform calc(var(--_d) * 0.45) var(--_out), opacity calc(var(--_d) * 0.3) linear calc(var(--_d) * 0.15);
}
.u-theme-toggle[data-variant="rise"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__night {
transform: translateY(0); opacity: 1;
transition: transform calc(var(--_d) * 0.6) var(--_in) calc(var(--_d) * 0.3), opacity calc(var(--_d) * 0.25) linear calc(var(--_d) * 0.3);
}
/* flip: a coin flip, sun on the front, moon on the back */
.u-theme-toggle[data-variant="flip"] .u-theme-toggle__svg {
transform: perspective(80px) rotateY(0deg);
transition: transform var(--_d) var(--_io);
}
.u-theme-toggle[data-variant="flip"] .u-theme-toggle__night { transform: scaleX(-1); opacity: 0; transition: opacity 0s linear calc(var(--_d) * 0.5); }
.u-theme-toggle[data-variant="flip"] .u-theme-toggle__sun { opacity: 1; transition: opacity 0s linear calc(var(--_d) * 0.5); }
.u-theme-toggle[data-variant="flip"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__svg { transform: perspective(80px) rotateY(180deg); }
.u-theme-toggle[data-variant="flip"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__sun { opacity: 0; }
.u-theme-toggle[data-variant="flip"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__night { opacity: 1; }
/* orbit: the sun swings away along an arc and the moon swings in behind it */
.u-theme-toggle[data-variant="orbit"] :is(.u-theme-toggle__sun, .u-theme-toggle__night) { transform-origin: 12px 34px; }
.u-theme-toggle[data-variant="orbit"] .u-theme-toggle__sun { transform: rotate(0deg); transition: transform var(--_d) var(--_in); }
.u-theme-toggle[data-variant="orbit"] .u-theme-toggle__night { transform: rotate(80deg); transition: transform var(--_d) var(--_in); }
.u-theme-toggle[data-variant="orbit"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__sun { transform: rotate(-80deg); }
.u-theme-toggle[data-variant="orbit"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__night { transform: rotate(0deg); }
/* swap: the sun shrinks and turns away, the moon pops in with a spring */
.u-theme-toggle[data-variant="swap"] .u-theme-toggle__sun {
transform: scale(1) rotate(0deg); opacity: 1;
transition: transform calc(var(--_d) * 0.6) var(--_in) calc(var(--_d) * 0.25), opacity calc(var(--_d) * 0.3) linear calc(var(--_d) * 0.25);
}
.u-theme-toggle[data-variant="swap"] .u-theme-toggle__night {
transform: scale(0.3) rotate(-90deg); opacity: 0;
transition: transform calc(var(--_d) * 0.35) var(--_out), opacity calc(var(--_d) * 0.25) linear;
}
.u-theme-toggle[data-variant="swap"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__sun {
transform: scale(0.3) rotate(90deg); opacity: 0;
transition: transform calc(var(--_d) * 0.35) var(--_out), opacity calc(var(--_d) * 0.25) linear;
}
.u-theme-toggle[data-variant="swap"]:is([aria-pressed="true"], [data-state="on"]) .u-theme-toggle__night {
transform: scale(1) rotate(0deg); opacity: 1;
transition: transform calc(var(--_d) * 0.6) var(--_in) calc(var(--_d) * 0.25), opacity calc(var(--_d) * 0.3) linear calc(var(--_d) * 0.25);
}
@media (prefers-reduced-motion: reduce) {
.u-theme-toggle, .u-theme-toggle * { transition: none !important; }
}Coming soon. The uinimate package isn't on npm yet. Copy the HTML and CSS for now; this is how it will work:
npm i uinimate
// once, in your app's entry or main stylesheet
import "uinimate/components/theme-toggle/theme-toggle.css";Three steps, no install.
- 01
Add the CSS
Copy the CSS tab into your stylesheet, or save it as
theme-toggle.cssand link it. It works on its own; our design tokens are optional. - 02
Paste the markup
Copy the HTML tab where the theme toggle should go. Your playground choices (variant, size, colour…) are already in it.
- 03
Flip the state
Set
aria-pressedto"true"or"false"from your code. That's the whole wiring. Pick your framework below.
Use on a <button> with a fixed aria-label such as "Dark mode"; aria-pressed="true" means dark mode is on. Screen readers announce it as a toggle.