Files
projax/web/theme.go
mAi 5dcacff520 feat(phase 4b): dark/light theme toggle + file-upload permanently out-of-scope
## Slice A — explicit dark/light toggle

projax now ships with two palettes and a 1y cookie to remember the choice.
Dark is the new default; ☀ button in the header nav flips to light and
writes projax_theme=light. Server reads the cookie via themeFromRequest(r)
and injects Theme + ThemeColor into every template via the centralised
render(w, r, …) path, so first paint never flashes the wrong theme. Inline
JS in layout.tmpl handles the toggle without a server roundtrip.

Every panel colour now lives in a CSS variable under
:root[data-theme=dark|light]; the only hardcoded hex values left are
inside those two :root blocks. A future palette tweak is one edit, not
30 selectors. Graph node colours, kind-badges, highlights and warn/ok/bad
all have parallel dark/light values picked for contrast.

Standalone SVG download bakes the light palette inline because the
downloaded asset has no parent :root providing vars — m's existing
snapshots stay print-friendly regardless of his current cookie.

Login page keeps its embedded dark CSS — it's the gateway, intentionally
always dark.

Tests: TestThemeDefaultIsDark, TestThemeCookieRoundTrips,
TestThemeCookieUnknownFallsBackToDark, TestThemeTogglePagesShareSameTheme,
TestThemeToggleScriptPresent, TestThemeColorMetaHelper. Full suite green.

## Slice B — file-upload permanently out of scope (m, 2026-05-17)

docs/design.md moves "File uploads / in-projax storage" from the §3c
parked list to a permanent "Out of scope (decided 2026-05-17)" clause
with the rationale: PER is the cross-reference index, not the file
system. docs/standards/per.md gains the same explicit clause so future
shifts working from the PER standard see the constraint where they
look. Memory note filed so future workers don't re-propose multipart
uploads, attachments tables, or documents buckets.

## docs/design.md §13 Theming

Documents the toggle approach, cookie semantics, palette table, the
standalone-SVG carve-out, the login-page exception, and the 4b
out-of-scope (prefers-color-scheme detection, per-page overrides,
transitions on swap).
2026-05-17 18:14:08 +02:00

48 lines
1.2 KiB
Go

package web
import (
"net/http"
"strings"
)
// themeCookieName is the cookie projax uses to persist the user's chosen
// theme. Read by the layout on render, written by the inline toggle script
// in the header nav. Not HttpOnly because the toggle is JS-driven; a
// compromise here at worst flips someone's colour scheme.
const themeCookieName = "projax_theme"
const (
themeDark = "dark"
themeLight = "light"
)
// themeFromRequest reads the projax_theme cookie. Unknown / missing values
// fall back to the default (dark). Whitespace + case are normalised so a
// hand-typed cookie still works.
func themeFromRequest(r *http.Request) string {
if r == nil {
return themeDark
}
c, err := r.Cookie(themeCookieName)
if err != nil {
return themeDark
}
v := strings.ToLower(strings.TrimSpace(c.Value))
switch v {
case themeLight:
return themeLight
case themeDark:
return themeDark
}
return themeDark
}
// themeColorForMeta returns the colour value for the `<meta name="theme-color">`
// tag. Apple Safari uses this to tint the iOS status bar in the installed PWA.
func themeColorForMeta(theme string) string {
if theme == themeLight {
return "#f0efe8"
}
return "#161616"
}