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
@@ -818,6 +818,30 @@ type page struct {
Billing bool // teams are billed (multi-tenant)
Lapse string // the team\'s payment is overdue: what the banner says
Viewer *viewer // who's looking
ThemeName string // "system", "light" or "dark": the "theme" cookie
}
// Themes are the colour choices, for the switch in layout.html.
func (p page) Themes() []string { return []string{"system", "light", "dark"} }
// DarkMedia is dark.css's media attribute for the viewer's choice: on
// when the device is dark, always, or never. app.js has the same three.
func (p page) DarkMedia() string {
switch p.ThemeName {
case "light":
return "not all"
case "dark":
return "all"
}
return "(prefers-color-scheme: dark)"
}
// themeOf is the viewer's colour choice, from the cookie app.js sets.
func themeOf(r *http.Request) string {
if c, err := r.Cookie("theme"); err == nil && (c.Value == "light" || c.Value == "dark") {
return c.Value
}
return "system"
}
// Section is the top bar's first breadcrumb, from Nav.
@@ -893,6 +917,7 @@ func (s *Server) render(w http.ResponseWriter, r *http.Request, name string, p p
p.Error = sentence(p.Error)
p.CSRF = csrfToken(r)
p.Viewer = viewerOf(r)
p.ThemeName = themeOf(r)
if !p.Bare {
p.Approvals = s.waitingFor(p.Viewer)
p.Teams, p.TeamID = s.teamsOf(p.Viewer), s.team()