Part of the jedee wiki — An LLM wiki written by an AI, Claude Code, based on the methods and techniques used by this site.
The theme toggle
Dark mode reaches a page two ways. prefers-color-scheme reports the operating system’s setting and handles most visitors with no interface at all. A toggle exists for everyone else — someone whose system is dark but who wants this one site light — and the moment a site has one it has three states to represent rather than two: light, dark, and follow the system, which is not the same as either of the others.
Two problems arrive with the toggle, and both are more awkward than the button itself.
The flash of the wrong theme. A chosen theme is stored on the client, usually in localStorage, and reading it is JavaScript. If that script runs after the page has painted, the visitor sees the default theme for a frame and then a jump. The only reliable fix is a small blocking inline script in the head that applies the theme before first paint — one of the few cases where a render-blocking script is the correct answer rather than an oversight.
Motion. A control that morphs one shape into another is exactly the decorative animation prefers-reduced-motion exists for. The state change still has to read clearly with the animation off, which means the reduced-motion path needs to be a designed alternative rather than animation: none.
In jedee
One button, one SVG: a sun that morphs into a moon when the theme goes dark. Adapted from Adam Argyle’s sun-and-moon theme switch (MIT). Four files carry it — partials/theme-toggle.njk, local/theme-toggle.css (the button), local/sun-and-moon.css (the icon and its animation), and scripts/bundle/theme-toggle.js.
Diverges from Eleventy Excellent, which ships its toggle. This replaced an earlier two-icon version that stacked both glyphs and showed whichever was inactive.
There is no moon shape
The moon is the sun’s disc with a circle masked out of it:
<mask class="moon" id="theme-toggle-moon-mask">
<rect x="0" y="0" width="100%" height="100%" fill="white" />
<circle cx="24" cy="10" r="6" fill="black" />
</mask>
<circle class="sun" cx="12" cy="12" r="6" mask="url(#theme-toggle-moon-mask)" fill="currentColor" />In light mode the mask circle sits at cx="24", outside the disc, so the mask does nothing. Going dark it slides left and bites the crescent while the disc grows and the eight ray lines fade out.


The mockup is src/wiki/_sources/theme-toggle.html. It links the site’s compiled global.css, so the easings, the durations, the mask geometry and both palettes in the picture are this stylesheet’s rather than a copy, and each frame is a real transition paused and seeked with getAnimations(). The glyph is drawn far larger than its shipped 24px, where the mask circle’s whole travel is about three pixels. The first tile is the light resting state, before the click; the other four are past it, which is why they wear the dark page color and the moon’s blue-gray from their first millisecond — when the figure was shot, neither the icon color nor the background was in any transition list, so both snapped while only the shape animated. Since 2026-10-26 the button fades its color with transition: color, and the icon is no longer orange or blue-gray; the colors are the subject further down.
⚠️ A transition only exists where a value changes. The first version rendered each frame with data-theme="dark" already on it, so nothing ever transitioned and getAnimations() had nothing to seek — all five frames came out as the finished moon. They have to be built in the light state, flipped, and then frozen. Reading a computed value in between is what forces the recalc that creates the transitions.
The reduced-motion split
Everything keys off data-theme on <html>. The end states always apply; only the animation between them lives inside @media (prefers-reduced-motion: no-preference). A reduced-motion user gets an instant swap, never a broken half-state — this is EE’s “opt in to motion” idiom rather than the reset’s global duration clamp.
[data-theme='dark'] .sun-and-moon > .sun { transform: scale(1.75); }
[data-theme='dark'] .sun-and-moon > .sun-beams { opacity: 0; }
@supports (cx: 1px) {
[data-theme='dark'] .sun-and-moon > .moon > circle { transform: translateX(0); cx: 17px; }
}The mask circle animates its cx attribute where CSS geometry properties are supported, which keeps the mask geometry honest; older browsers get a translateX fallback of the same distance. Timings are staggered: beams fold in fast (0.15s, −25° rotation), disc grows in 0.25s, mask circle slides last (0.25s delay, 0.5s); going light the disc springs back on an elastic easing.
Argyle’s original pulls its easings from Open Props via a remote @import. Here the four cubic-beziers are local custom properties on the button, so nothing is fetched at runtime.
The page follows in a circle
Since 2026-11-17 the page no longer snaps from one theme to the other on a click: the new theme grows as a circle out of the button and covers the screen in half a second. It is a same-document view transition. revealTheme() in theme-toggle.js hands the theme change to document.startViewTransition(), the browser keeps a picture of the page in the old theme, and theme-toggle.css clips the live new theme to a circle centred on the button with clip-path: circle(). The script measures the button and the distance to the farthest corner of the screen, and puts the centre and radius on <html> as --theme-reveal-x, --theme-reveal-y and --theme-reveal-r.
Three other ways were weighed and turned down. A transition on color and background-color has to reach every element, stutters on long pages such as /activities/, gets mixed up with the links’ hover fades, and still lets images, shadows and fixed colors snap. Registering the color tokens with @property so they can fade makes every element recalculate on every frame, which is worse. A plain crossfade uses the same mechanism as the circle and costs the same; the circle was chosen because it starts where the click was.
What the view transition costs, and why it stays cheap:
- The picture is the screen, not the page. The browser captures the viewport, so the circle runs as smoothly on
/activities/as on a short page. The theme change itself still recalculates the whole page once, as it always did: measured in headless Chrome, the circle starts 30–140ms after the click there, against about 40ms on the start page. - The icon gets a separate layer. Inside the page picture the sun-to-moon morph would freeze. The button takes
view-transition-name: theme-toggleduring the reveal, its old picture is hidden and the new one is the live button, so the morph plays on top of the circle. - A 3D object gets its own layer too. In Safari the 3D pamphlet on the Bethink Yourselves! note jumped while the circle passed it: its box and transform never changed, so the still picture of the page drew its stacked sheets differently from the live page. Chrome showed no jump, recorded frame by frame.
pamphlet.cssgives it the icon’s treatment,view-transition-name: pamphletduring the reveal, old picture hidden, new one not animated, so it is always drawn live. Its shadow and lighting now switch at the click instead of with the circle. WebKit’s view transitions could not be recorded here, so the fix was checked in Safari by eye. - ⚠️ The rules are scoped to
data-theme-reveal. Page-to-page navigation uses the same::view-transition-*pseudo-elements for its crossfade (global-styles.css). The script sets the attribute on<html>for the length of the reveal and removes it when the transition finishes, so the circle never leaks into a navigation. A second click before the first finishes skips the first transition, and the attribute stays until the last one is done. - Clicks go to the picture for half a second. While a view transition runs, the page underneath does not take pointer input. At 0.5s this is not noticeable, but ⚠️ the button still hears a
pointerleavewhen the picture covers it, though the pointer never moved. That used to clear the tooltip’s dismissal, so the tooltip popped back up under a still pointer when the circle finished; the button now ignores apointerleavewhiledata-theme-revealis set.
It does nothing for visitors who ask for reduced motion: the script checks prefers-reduced-motion: no-preference before starting the transition, and the CSS sits inside the same media query. A browser without startViewTransition (support: Chrome and Edge 111, Safari 18, Firefox 144) gets the instant switch. A change coming from the system setting or from another open tab stays instant too; only a click on the button has a place for the circle to start from.
No flash of the wrong theme
scripts/bundle/theme-toggle.js is minified into _includes/scripts/ and inlined into <head> by js-inline.njk, so it runs before the body renders:
function getColorPreference() {
if (localStorage.getItem(storageKey)) return localStorage.getItem(storageKey);
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
reflectPreference(); // set early so no page flashesThe choice lives in localStorage under theme-preference and is applied as data-theme on document.firstElementChild — the attribute all theme CSS, including the icon morph, selects on. With no saved choice it follows the system setting, and a listener on that media query keeps the page in sync if the OS switches while the page is open. A storage listener does the same for other open tabs: when one tab saves or clears the choice, the others follow it at once instead of on their next load. ⚠️ Picking the scheme the system already shows clears the saved choice instead of saving it; saving it would pin the site to that scheme for good, so it would no longer switch when the system does. setPreference() also rewrites both <meta name="theme-color"> tags from the two colors in src/_data/meta.js. Each tag has a media query, so the system setting picks which one counts, and only setting both makes the browser bar follow the toggle. A color-scheme: light or dark rule on each data-theme value in global-styles.css carries the choice to what the browser draws itself: scrollbars and form controls. ⚠️ The <audio> player is left out of this: it stays dark in both themes, because Chrome’s light player is nearly the page colour.
Accessibility details worth copying
- The accessible name is a visually-hidden label; the SVG is
aria-hidden. State rides onaria-pressed, where pressed = dark mode active. - The button is wrapped in an is-land island,
on:idleand hidden by CSS until the island upgrades (is-land:not(:defined) .theme-toggle { display: none; }) — a toggle that can’t run shouldn’t show. The theme itself doesn’t depend on the button; the inline script sets it. - The hit area stays at the WCAG 2.5.8 minimum of 24×24px on purpose. A bigger target would be empty space pushing the icon away from the nav. The glyph takes the header’s shared icon size,
--theme-toggle-size: var(--site-header-icon-size, var(--size-step-min-2)), the same as the logomark and the search magnifier.
Why the icon can’t be yellow
Since 2026-10-26 the icon is neither orange nor blue. Like the rest of the header chrome it rests at --color-text-header-dim in both themes and brightens to --color-text-strong on hover and keyboard focus; only the logomark keeps the accent orange. The rest of this section is how the earlier colors were chosen, kept because the contrast reasoning still holds for any colored icon in this header.
The sun was --color-accent-orange (#d0621e), the same orange as the breadcrumb logomark at the other end of the header, and hover only deepened it — color-mix(in oklab, var(--color-accent-orange) 82%, var(--color-text)). Mixing toward the text color rather than a fixed second color is what lets one declaration work in both themes: it darkens on the light page and lightens on the dark one, which is the direction that preserves contrast each way.
The moon was not orange — a night-sky object rather than a warm one. It was --color-text cooled by 12% --color-blue-vivid (#b9c3cd), with the accent orange kept for its hover. So the two glyphs did not share a color, and the dark override was where that was expressed.
The tint is a nod to perception rather than physics. The lunar surface is gray-brown with an albedo close to worn asphalt, and moonlight is reflected sunlight, so spectrally it is faintly warm. We see night as blue because of the Purkinje effect: in dim light vision shifts to the rods, whose peak sensitivity lies further toward blue-green. An icon should follow the perception.
Two practical notes. There is no light blue to reach for — the ramp is three dark values (#245375, #2f536e, #3d8ecc), and the lightest reads as a link blue, an interface color that would compete with the orange logomark a few centimetres to its left in the same header. And the mix has to come from blue-vivid: --color-accent-blue resolves to the desaturated #2f536e in dark, so 12% of it barely registers (chroma 0.010 against 0.018). Contrast is not the constraint here the way it was for the sun — every mix from 6% to 30% lands between 8.9 and 10.6 on the dark background, so the percentage is purely an aesthetic dial.
mix into --color-text | hex | on #141619 | chroma |
|---|---|---|---|
none (#c8cacc) | #c8cacc | 11.03 | 0.004 |
| 6% blue-vivid | #c0c7cc | 10.58 | 0.011 |
| 12% blue-vivid | #b9c3cd | 10.14 | 0.018 |
| 20% blue-vivid | #aebfcd | 9.58 | 0.027 |
| 30% blue-vivid | #a1b9cd | 8.91 | 0.039 |
Fixing the logomark to match found two live contrast failures on every page but the start page, where the trail’s home crumb takes --color-logo-icon rather than the aria-current orange: that token was --color-base-dark (#bbbfca) in light, measuring 1.67, and --color-orange-700 (#8c2100) in dark, measuring 2.01. Both are now the one accent orange, and the aria-current special case is gone with them — the home crumb no longer differs from anywhere else.
A yellower, lighter sun was the design intent and is not reachable. The sun only ever appears in the light theme, on #f4f4f2, and WCAG 1.4.11 Non-text Contrast wants 3:1 for a graphic you need in order to operate a control — which an icon-only button’s icon is, notwithstanding its aria-hidden (that attribute governs the accessibility tree, not what a sighted user has to see). Measured against that background, the palette’s lighter oranges are nowhere close:
on #f4f4f2 | on #141619 | |
|---|---|---|
orange-200 #ffdec3 | 1.16 | 14.23 |
orange-300 #ffb78a | 1.54 | 10.70 |
orange-400 #f68f5a | 2.12 | 7.76 |
orange-500 #d0621e | 3.50 | 4.70 |

orange-200 to orange-500 on the light page. Only the last, at 3.50:1, is enough for a control.This is a general constraint, not a palette flaw: a light background and a yellow object cannot both be true at 3:1, which is why yellow suns live on dark headers. orange-500 is the lightest shade that clears the bar in the theme where the sun is shown.
The eight states as shipped, measured on the resolved colors against each theme’s page color (re-measured 2026-10-28, after the 10-26 recolor). The icon now clears 4.5:1 in every state, the logomark clears 3:1:
| light rest | light hover | dark rest | dark hover | |
|---|---|---|---|---|
| logomark | 3.50 | 3.94 | 4.70 | 5.56 |
| toggle icon | 4.63 | 6.97 | 5.24 | 16.46 |
Current state, not target state
Because the CSS selects on [data-theme='dark'] and the icon’s resting form is the sun, the icon always shows the theme you are in. The old two-icon version did the opposite — it advertised what a click would give you. Dropping that convention makes the state indicator and the control the same object, and aria-pressed tells assistive tech the same story.
The label
The accessible name is a visually-hidden span, not the SVG, which is aria-hidden; aria-pressed carries the state. Since 2026-09-02 there is also a visible tooltip on hover and keyboard focus — see Tooltips — and the two strings deliberately say different things.
The name is a fixed “Toggle dark mode”. It has to be: on a toggle button the state lives in aria-pressed, and a name that flips with it announces the state twice in two vocabularies. The tooltip is decoration, exposed to nobody, so it is free to say what a click will do — “Show dark mode” in light, “Show light mode” in dark, set by reflectToggleState() in the same breath as aria-pressed, so a theme change from any source (the button, the system preference listener, first load) updates both.
The markup carries no data-tooltip; the script adds it. That keeps a wrong string from ever existing, and costs nothing, because the button is already hidden until its island upgrades. The title attribute that used to serve this purpose was removed, or the browser would draw its native tooltip beside the styled one.
Raw source: src/_raw/dev-notes/How the theme toggle works.md, src/_raw/dev-notes/How the Safari arrow and pamphlet fixes were found.md