Skip to content

ndg: add a theme toggle to the page header - #301

Merged
NotAShelf merged 1 commit into
feel-co:mainfrom
KiaraGrouwstra:theme-toggle-upstream
Oct 4, 2026
Merged

NotAShelf merged 1 commit into
feel-co:mainfrom
KiaraGrouwstra:theme-toggle-upstream

Conversation

@KiaraGrouwstra

@KiaraGrouwstra KiaraGrouwstra commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

This adds a button to the page header that selects the light or the dark theme.

toggle-header
  • A click selects the theme that is not shown. The page keeps the selection in localStorage and applies it before the first paint, so the page does not flash in the other theme.
  • When the selected theme is the same as the system theme, the page removes the selection and follows the system theme again. Thus the toggle has only two states for the reader, but a reader who never clicks it always gets the system theme.
  • The style sheet applies each prefers-color-scheme rule also when data-theme on <html> selects that theme, and does not apply it when data-theme selects the other theme.
  • The toggle also selects the matching <source> of a <picture> with a prefers-color-scheme media query, so light and dark screenshots follow the page theme.
  • The page sends an ndg:themechange event on document with { mode, resolved } when the theme changes, so other scripts can follow the theme.
  • The icons are a sun and a moon, made of simple SVG shapes in the template. They do not come from an icon set.

This change is independent of #300 (Mermaid diagrams). When both are merged, Mermaid can draw the diagrams again on ndg:themechange. I will add that change to the pull request that is merged second.

Tests: deno test -A passes, deno lint is clean and nix build .#ndg passes. I also checked the pages in a headless Chromium with a light and a dark system theme, before and after a click. deno fmt --check reports one file, which is the same on main.

Assisted-by: Claude:claude-opus-5-5

https://claude.ai/code/session_01RJvbghuj5GtCBiCjmBepNJ

A click selects the light or the dark theme. The page keeps the selection in `localStorage` and applies it before the first paint. When the selected theme is the same as the system theme, the page follows the system theme again.

The style sheet applies each `prefers-color-scheme` rule also when `data-theme` on `<html>` selects that theme, and does not apply it when `data-theme` selects the other theme. The toggle also selects the matching `<source>` of a `<picture>` with a `prefers-color-scheme` media query, and sends an `ndg:themechange` event on `document`.

Assisted-by: Claude:claude-opus-5-5
Claude-Session: https://claude.ai/code/session_01RJvbghuj5GtCBiCjmBepNJ
@NotAShelf
NotAShelf merged commit ef50e07 into feel-co:main Oct 4, 2026
14 checks passed
@NotAShelf

Copy link
Copy Markdown
Member

Simple enough that I'll go ahead and merge.

#300 needs a little bit of work, but I will have a review for it shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants