· 4 min read
A sun/moon toggle with a circular reveal
Planned Phase 4.5 (Identity & motion): technical look (terminal, monospace, blueprints, code), dark first, a mascot, and each route with its own composition…
What we did
- Planned Phase 4.5 (Identity & motion): technical look (terminal, monospace, blueprints, code), dark first, a mascot, and each route with its own composition and trick. Theme and tokens stay shared.
- Turned the theme toggle into an icon: an SVG sun whose rays fade out while a masked circle bites out a crescent moon.
- The new theme now grows out of the button in a circle, using a view transition with the type
theme. - Added a
--darktoken (1 in dark, 0 in light) intokens.css. The icon computes its transforms from it. - Tested on the built site at 320px and desktop, in both themes, with reduced motion. Lighthouse a11y is still 100 in both themes.
- Rebuilt the toggle as a plain HTML/CSS/JS CodePen with a control panel (duration, easing, edge blur, blend mode) to play with the reveal.
- Brought the soft edge back to the project:
ThemeToggletakesduration(ms) andblur(px) props. The defaults (600, 0) keep the old look.
What I learned
light-dark()only works for colors. For anything else that depends on the theme, a 0/1 number in a custom property works withcalc():scale(calc(1 + 0.75 * var(--dark))).- The icon reads
--dark, notaria-pressed. The CSS applies beforeonMount, so the icon is right from the first paint, even with JS off. If it readaria-pressed, dark-OS visitors would see a sun that turns into a moon on every page load. startViewTransition(): the browser takes a picture of the old page, runsupdate(), takes a picture of the new state, then animates between them with::view-transition-old(root)and::view-transition-new(root). The reveal keeps the old one still and clips the new one with a growingcircle().- Transition types label a transition, and the CSS only matches its own with
:active-view-transition-type(theme). Without it, the circle would also play on route transitions later. flushSyncis a Svelte function that applies pending DOM updates right away instead of in the next batch, so they land before the browser's "after" picture. Today it's insurance: the visible change comes fromdata-theme, which we set directly.- The two pictures are blended for a cross-fade by default (
mix-blend-mode: plus-lighter), so the reveal setsmix-blend-mode: normal. clip-path: circle()always has a hard edge. Amask-image: radial-gradient(...)can fade: solid up toradius - blur, transparent atradius.- Custom properties are untyped text, so animating one just jumps from start to end.
@propertyregisters it as a<length>, and then keyframes can tween it. - The transition's pseudo-elements hang off
:root, not off the component. So the props can't be CSS props on the component (<ThemeToggle --x="…" />, likeHero): the click handler writes them on:root, next to--reveal-x/y/r.
Problems & fixes
- The prerendered button had
aria-pressed="false"for everyone, even thoughthemestarts asundefined. Svelte rendersfalseon anaria-*attribute as the string"false"; onlyundefinedleaves it out. Fixed witharia-pressed={theme ? theme === 'dark' : undefined}. - With the mask, the old theme flashed at the end. The transition stays up a frame or so after its animation ends, and in that gap
--reveal-radiuswent back to0px, so the mask hid the new picture. Fixed withanimation: … forwards. (clip-pathnever had this: without the animation it goes back tonone, which shows everything.) - In the CodePen, the SVG was 0×0 and nothing animated. The new CodePen editor works with files and didn't load the CSS and JS ones, so
index.htmlhas to link them. - In the CodePen, blend modes other than
normalflashed at the end: the last frame was old and new blended, then the page snapped to the plain new theme. Fixed there by fading the old picture out after the reveal. The project only usesnormal, so it doesn't need that fix.
Links
- MDN: View Transition API
- MDN: :active-view-transition-type()
- MDN: SVG
<mask> - MDN: @property
- MDN: mask-image
- Svelte: flushSync
- Adam Argyle: Building a theme switch component
- nerdy.dev, the inspiration for the reveal
Next
- The dark-first technical palette and type.