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()
+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)
}
}
+5 -37
View File
@@ -44,43 +44,7 @@
color-scheme: light;
}
/* Dark: brain's readability pass, body text at about 13:1 rather than a
glaring near-white on near-black. */
@media (prefers-color-scheme: dark) {
:root {
--background: 240 6% 10%;
--foreground: 0 0% 92%;
--card: 240 5% 13%;
--primary: 245 60% 60%;
--primary-foreground: 0 0% 100%;
--secondary: 240 4% 19%;
--muted: 240 4% 19%;
--muted-foreground: 240 5% 64.9%;
--accent: 245 30% 20%;
--accent-foreground: 245 60% 75%;
--destructive: 0 70% 62%;
--border: 240 4% 19%;
--input: 240 4% 25%;
--ring: 245 60% 60%;
--sidebar: 240 6% 8%;
--sidebar-foreground: 0 0% 85%;
--ok: 142 55% 55%;
--ok-bg: 142 30% 14%;
--warn: 38 90% 65%;
--warn-bg: 35 40% 13%;
--warn-border: 35 50% 30%;
--bad-bg: 0 40% 14%;
--bad-border: 0 45% 32%;
--teal: 160 60% 55%;
--teal-bg: 160 30% 14%;
--coral: 20 80% 65%;
--coral-bg: 20 40% 14%;
--shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.15), 0 1px 2px -1px rgb(0 0 0 / 0.15);
--shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.2), 0 2px 4px -2px rgb(0 0 0 / 0.15);
--shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.25), 0 4px 6px -4px rgb(0 0 0 / 0.15);
color-scheme: dark;
}
}
/* The dark colours are in dark.css. */
/* --- Base --- */
* { box-sizing: border-box; border-color: hsl(var(--border)); }
@@ -283,6 +247,10 @@ button:disabled, button:disabled:hover { opacity: 0.5; cursor: not-allowed; box-
button.small { padding: 0.28rem 0.75rem; font-size: 0.82rem; }
.icon-button { padding: 0.35rem; background: transparent; color: hsl(var(--foreground)); box-shadow: none; }
.icon-button:hover { background: hsl(var(--accent)); box-shadow: none; }
.theme-switch { display: flex; gap: 0.15rem; margin-left: auto; padding: 0.15rem; border: 1px solid hsl(var(--border)); border-radius: var(--radius); background: hsl(var(--background)); }
.theme-switch .icon-button { color: hsl(var(--muted-foreground)); border-radius: var(--radius-sm); }
.theme-switch .icon-button[aria-pressed="true"] { background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); }
.bare-shell .theme-switch { margin: 0.75rem auto 0; width: max-content; }
.actions { display: flex; gap: 0.75rem; align-items: center; margin: 1rem 0 1.5rem; flex-wrap: wrap; }
.danger-zone { margin-top: 2rem; }
+21
View File
@@ -29,7 +29,28 @@
}
});
// Light, dark, or as the device is (layout.html's theme switch). The
// choice is a cookie so the server renders the page in it, with no flash;
// here it takes effect at once by switching dark.css's media.
var darkMedia = { system: "(prefers-color-scheme: dark)", light: "not all", dark: "all" };
function setTheme(name) {
if (!(name in darkMedia)) return;
document.cookie = name === "system"
? "theme=; path=/; max-age=0; SameSite=Lax"
: "theme=" + name + "; path=/; max-age=31536000; SameSite=Lax";
var css = document.getElementById("dark-css");
if (css) css.media = darkMedia[name];
document.querySelectorAll("[data-theme-set]").forEach(function (b) {
b.setAttribute("aria-pressed", String(b.getAttribute("data-theme-set") === name));
});
}
document.addEventListener("click", function (e) {
var theme = e.target.closest("[data-theme-set]");
if (theme) {
setTheme(theme.getAttribute("data-theme-set"));
return;
}
// The menu on narrow screens.
if (e.target.closest("[data-nav-toggle]")) {
document.body.classList.toggle("nav-open");
+38
View File
@@ -0,0 +1,38 @@
/* Supermarket's dark colours: brain's readability pass, body text at about
13:1 rather than a glaring near-white on near-black. layout.html links
this with a media attribute from the viewer's choice (the "theme"
cookie): "(prefers-color-scheme: dark)" to follow the device, "all" for
dark, "not all" for light. app.js changes it when they choose. */
:root {
--background: 240 6% 10%;
--foreground: 0 0% 92%;
--card: 240 5% 13%;
--primary: 245 60% 60%;
--primary-foreground: 0 0% 100%;
--secondary: 240 4% 19%;
--muted: 240 4% 19%;
--muted-foreground: 240 5% 64.9%;
--accent: 245 30% 20%;
--accent-foreground: 245 60% 75%;
--destructive: 0 70% 62%;
--border: 240 4% 19%;
--input: 240 4% 25%;
--ring: 245 60% 60%;
--sidebar: 240 6% 8%;
--sidebar-foreground: 0 0% 85%;
--ok: 142 55% 55%;
--ok-bg: 142 30% 14%;
--warn: 38 90% 65%;
--warn-bg: 35 40% 13%;
--warn-border: 35 50% 30%;
--bad-bg: 0 40% 14%;
--bad-border: 0 45% 32%;
--teal: 160 60% 55%;
--teal-bg: 160 30% 14%;
--coral: 20 80% 65%;
--coral-bg: 20 40% 14%;
--shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.15), 0 1px 2px -1px rgb(0 0 0 / 0.15);
--shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.2), 0 2px 4px -2px rgb(0 0 0 / 0.15);
--shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.25), 0 4px 6px -4px rgb(0 0 0 / 0.15);
color-scheme: dark;
}
+14
View File
@@ -10,6 +10,7 @@
<link rel="icon" type="image/png" href="/static/favicon.png">
<link rel="apple-touch-icon" href="/static/apple-touch-icon.png">
<link rel="stylesheet" href="/static/app.css">
<link rel="stylesheet" href="/static/dark.css" media="{{.DarkMedia}}" id="dark-css">
<script src="/static/htmx.min.js" defer></script>
<script src="/static/app.js" defer></script>
</head>
@@ -23,6 +24,7 @@
{{template "content" .}}
</main>
<p class="version">Supermarket {{.Version}}</p>
{{template "theme-switch" .}}
</div>
{{else}}
<div class="shell">
@@ -124,6 +126,7 @@
{{with .Section}}{{if ne . $.Title}}<span>{{.}}</span><span class="sep" aria-hidden="true">/</span>{{end}}{{end}}
<span class="here">{{.Title}}</span>
</nav>
{{template "theme-switch" .}}
</header>
<main>
{{with .Viewer}}{{if .Viewing}}<div class="card warn viewing" role="status"><p><strong>Viewing this team as the platform's admin</strong>, read-only, until {{.Session.ViewUntil.Local.Format "15:04"}}: {{.Session.ViewReason}}</p>
@@ -139,6 +142,14 @@
</body>
</html>
{{/* Light, dark, or as the device is: {{template "theme-switch" .}}. The
choice is the "theme" cookie, for this browser; app.js sets it. */}}
{{define "theme-switch"}}
<div class="theme-switch" role="group" aria-label="Colours">
{{range .Themes}}<button type="button" class="icon-button" data-theme-set="{{.}}" aria-pressed="{{if eq . $.ThemeName}}true{{else}}false{{end}}" title="{{if eq . "system"}}As your device is{{else if eq . "light"}}Light{{else}}Dark{{end}}">{{template "icon" .}}<span class="visually-hidden">{{if eq . "system"}}As your device is{{else if eq . "light"}}Light{{else}}Dark{{end}}</span></button>{{end}}
</div>
{{end}}
{{/* Settings pages share this frame: a heading, and the tabs on the left
(grouped, as in brain). Call with the page: {{template "settings-start" $}}
… {{template "settings-end"}}. */}}
@@ -227,6 +238,9 @@
{{- else if eq . "backups"}}<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/><path d="M12 7v5l4 2"/>
{{- else if eq . "chevron"}}<path d="m6 9 6 6 6-6"/>
{{- else if eq . "menu"}}<path d="M4 12h16"/><path d="M4 6h16"/><path d="M4 18h16"/>
{{- else if eq . "system"}}<rect width="20" height="14" x="2" y="3" rx="2"/><path d="M8 21h8"/><path d="M12 17v4"/>
{{- else if eq . "light"}}<circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="m17.66 17.66 1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="m6.34 17.66-1.41 1.41"/><path d="m19.07 4.93-1.41 1.41"/>
{{- else if eq . "dark"}}<path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/>
{{- end}}</svg>{{end}}
{{define "seo-sites"}}