Light and dark: a switch in the top bar

Three choices: as the device is (the default), light, or dark, kept in
a "theme" cookie per browser. The dark colours move to dark.css, linked
with a media attribute from the cookie, so the server renders the page
in the chosen colours with no flash (the CSP allows no inline script).
app.js sets the cookie and switches the media at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 06:44:38 -07:00
co-authored by Claude Opus 5.5
parent a7162acb03
commit 19476edea2
6 changed files with 128 additions and 37 deletions
+25
View File
@@ -797,3 +797,28 @@ func TestAboutRunning(t *testing.T) {
}
}
}
func TestThemeCookieSetsDarkStylesheet(t *testing.T) {
_, h := newTestServer(t, nil)
for cookie, want := range map[string]string{
"": `media="(prefers-color-scheme: dark)"`,
"theme=light": `media="not all"`,
"theme=dark": `media="all"`,
"theme=garbage": `media="(prefers-color-scheme: dark)"`,
} {
body := do(h, http.MethodGet, "/", nil, map[string]string{"Cookie": cookie}).Body.String()
if !strings.Contains(body, `href="/static/dark.css" `+want) {
t.Errorf("cookie %q: want dark.css with %s", cookie, want)
}
name := "system"
if cookie == "theme=light" || cookie == "theme=dark" {
name = strings.TrimPrefix(cookie, "theme=")
}
if !strings.Contains(body, `data-theme-set="`+name+`" aria-pressed="true"`) {
t.Errorf("cookie %q: %s isn't shown as chosen", cookie, name)
}
}
if rec := do(h, http.MethodGet, "/static/dark.css", nil, nil); rec.Code != http.StatusOK {
t.Errorf("dark.css: %d", rec.Code)
}
}