Add light and system themes next to dark
PageHeader.Theme now has three values (ThemeDark, ThemeLight, ThemeSystem). "system" follows prefers-color-scheme. Unset Theme is still the dark palette, but every block now also sets color-scheme, so native controls and scrollbars match it. TestThemeBlocksInSync keeps the duplicated light values and the property lists in step. Persisting the choice stays the consumer's job (README, Theming). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
000bb42240
commit
6b40ff059c
4 files changed
+158
-72
No files matched your search
@@ -66,34 +66,41 @@ forms, buttons, markdown, ...), and anything else specific to that tool.
|
|||||||
`--yw-success-*`/`--yw-danger-*`, ...) rather than baked directly into
|
`--yw-success-*`/`--yw-danger-*`, ...) rather than baked directly into
|
||||||
each rule — a documented, reusable theme, not an implementation detail.
|
each rule — a documented, reusable theme, not an implementation detail.
|
||||||
|
|
||||||
The mechanism is prepared but only one theme ("dark") is actually
|
Three themes ship, selected by `PageHeader.Theme` (see `layout.go`),
|
||||||
defined today:
|
which renders as `<html data-theme="...">`:
|
||||||
|
|
||||||
- `PageHeader.Theme` (see `layout.go`) renders as `<html data-theme="...">`.
|
| `Theme` | Look |
|
||||||
`""` (every current consumer) omits the attribute, which resolves to
|
|---|---|
|
||||||
the bare `:root` block — today's exact look, unchanged.
|
| `""` (unset) | dark, the bare `:root` block, i.e. what every consumer had before themes existed |
|
||||||
- `static/style.css` also declares `:root[data-theme="dark"]`, byte-
|
| `layout.ThemeDark` (`"dark"`) | dark, the same values |
|
||||||
identical to the bare `:root` block — `Theme: "dark"` is an explicit,
|
| `layout.ThemeLight` (`"light"`) | light |
|
||||||
equivalent opt-in, and the template a second theme copies.
|
| `layout.ThemeSystem` (`"system"`) | follows the browser's `prefers-color-scheme` |
|
||||||
- **Adding a second theme** (e.g. "light"): add its own
|
|
||||||
`:root[data-theme="light"] { --yw-*: ...; }` block to
|
Every block also sets `color-scheme`, so native controls and scrollbars
|
||||||
`static/style.css` with the same property list and new values. Nothing
|
match the palette.
|
||||||
else in this module changes. A consuming project opts in per-request
|
|
||||||
or per-user by setting `Theme: "light"` on the `PageHeader`s it
|
- The light values appear twice in `static/style.css` (as
|
||||||
builds; projects that never set `Theme` are completely unaffected.
|
`:root[data-theme="light"]` and inside `@media (prefers-color-scheme:
|
||||||
- This module does **not** read `prefers-color-scheme` or persist a
|
light)` for `"system"`). `TestThemeBlocksInSync` fails if the two drift
|
||||||
choice itself (no cookie/localStorage/JS) — that's each consuming
|
apart or if a theme is missing a property.
|
||||||
project's own concern (e.g. a cookie/session-backed preference,
|
- `--yw-nav-link` is `#0b82b8` in every theme. It is a cross-project
|
||||||
resolved server-side into `Theme` before rendering), same as any other
|
design token (see the meta-repo `AGENTS.md`).
|
||||||
`PageHeader` field.
|
- This module does **not** persist a choice (no cookie, localStorage or
|
||||||
- Don't redefine `--yw-*` globally in one consumer's own stylesheet to
|
JS). That is the consuming project's job: keep the preference
|
||||||
get a per-project look — that silently reskins every other consumer
|
(e.g. in a cookie set by a POST form), resolve it server-side into
|
||||||
sharing this CSS file too. A project that genuinely needs a
|
`Theme` before rendering, and offer the switch itself (e.g. via
|
||||||
self-contained different look — e.g. a light/dark toggle with its own
|
`NavbarRightExtra`). Resolving server-side avoids a flash of the wrong
|
||||||
JS/localStorage, like `yaais` has — should own that itself rather than
|
theme without an inline script, so it works under a strict
|
||||||
pushing multi-theme complexity into this module, since `yaais` shows
|
`script-src 'self'` CSP. `yareco` is the reference consumer.
|
||||||
that need doesn't actually require *this* module either (it's a
|
- A consumer whose own stylesheet needs theme-dependent colors defines
|
||||||
client-rendered SPA, not a server-rendered Go-template dashboard).
|
its **own** custom properties with the same selector structure
|
||||||
|
(`:root, [data-theme="dark"], [data-theme="system"]` / `[data-theme="light"]`
|
||||||
|
/ the `prefers-color-scheme: light` media query). Don't redefine
|
||||||
|
`--yw-*` in one consumer's stylesheet: that reskins only that
|
||||||
|
project and makes it drift from everyone else sharing this file.
|
||||||
|
- **Adding another theme:** add its own `:root[data-theme="..."]` block
|
||||||
|
with the same property list, a constant in `layout.go`, and a case in
|
||||||
|
`TestThemeAttribute`.
|
||||||
|
|
||||||
## Consuming this module
|
## Consuming this module
|
||||||
|
|
||||||
@@ -140,12 +147,13 @@ concurrent-edit conflict during the initial rollout) and can produce a
|
|||||||
force-move an existing tag again for any future change — always cut a
|
force-move an existing tag again for any future change — always cut a
|
||||||
new one, even for a one-line fix.
|
new one, even for a one-line fix.
|
||||||
|
|
||||||
## PageHeader fields (current as of v0.1.5)
|
## PageHeader fields (current as of v0.1.9)
|
||||||
|
|
||||||
`Title`, `AppName` (lowercase product name for `<title>`/footer — not
|
`Title`, `AppName` (lowercase product name for `<title>`/footer — not
|
||||||
`Name`, which collides with a domain type's own `Name` field once
|
`Name`, which collides with a domain type's own `Name` field once
|
||||||
embedded in the same struct, as yakwui's viewmodel types do), `Lang`
|
embedded in the same struct, as yakwui's viewmodel types do), `Lang`
|
||||||
(`<html lang>`, defaults to `"en"`), `Brand`, `BrandInitial`, `Subtitle`,
|
(`<html lang>`, defaults to `"en"`), `Theme` (`<html data-theme>`, see
|
||||||
|
Theming), `Brand`, `BrandInitial`, `Subtitle`,
|
||||||
`BasePath`, `NavItems`/`ActiveNav`, `Flash`/`FlashKind`, `Username`,
|
`BasePath`, `NavItems`/`ActiveNav`, `Flash`/`FlashKind`, `Username`,
|
||||||
`LogoutPath` (a plain GET link — for a POST-form logout or any other
|
`LogoutPath` (a plain GET link — for a POST-form logout or any other
|
||||||
markup mismatch, render it yourself into `NavbarRightExtra` instead and
|
markup mismatch, render it yourself into `NavbarRightExtra` instead and
|
||||||
|
|||||||
@@ -29,6 +29,13 @@ var staticFS embed.FS
|
|||||||
// base/navbar/sidebar/footer templates.
|
// base/navbar/sidebar/footer templates.
|
||||||
const LayoutGlob = "templates/layout/*.tmpl"
|
const LayoutGlob = "templates/layout/*.tmpl"
|
||||||
|
|
||||||
|
// Values for PageHeader.Theme; each has its own block in static/style.css.
|
||||||
|
const (
|
||||||
|
ThemeDark = "dark"
|
||||||
|
ThemeLight = "light"
|
||||||
|
ThemeSystem = "system"
|
||||||
|
)
|
||||||
|
|
||||||
// NavItem is one sidebar entry.
|
// NavItem is one sidebar entry.
|
||||||
type NavItem struct {
|
type NavItem struct {
|
||||||
ID string // matches PageHeader.ActiveNav to mark this item active
|
ID string // matches PageHeader.ActiveNav to mark this item active
|
||||||
@@ -53,16 +60,11 @@ type PageHeader struct {
|
|||||||
AppName string
|
AppName string
|
||||||
Lang string // <html lang="...">; "" defaults to "en"
|
Lang string // <html lang="...">; "" defaults to "en"
|
||||||
// Theme selects which --yw-* token block static/style.css applies,
|
// Theme selects which --yw-* token block static/style.css applies,
|
||||||
// rendered as <html data-theme="...">. "" omits the attribute, which
|
// rendered as <html data-theme="...">: ThemeDark, ThemeLight or
|
||||||
// resolves to the bare :root values -- today that's the only theme
|
// ThemeSystem (follows prefers-color-scheme). "" omits the attribute,
|
||||||
// this module ships (dark), so leaving Theme unset is exactly today's
|
// which resolves to the bare :root values (dark), so a consumer that
|
||||||
// behavior for every current consumer. Setting Theme: "dark" is
|
// never sets Theme keeps its look. Persisting a user's choice is the
|
||||||
// equivalent (an explicit :root[data-theme="dark"] block exists with
|
// consumer's concern (see README.md's Theming section).
|
||||||
// the same values) -- it's the seam a future second theme (e.g.
|
|
||||||
// "light") would slot into: add its own :root[data-theme="..."]
|
|
||||||
// block to static/style.css, nothing else in this module changes,
|
|
||||||
// and consuming projects opt in per-request/per-user by setting this
|
|
||||||
// field (see README.md's Theming section).
|
|
||||||
Theme string
|
Theme string
|
||||||
Brand string // navbar brand text, e.g. "YASTA"
|
Brand string // navbar brand text, e.g. "YASTA"
|
||||||
BrandInitial string // single-letter favicon glyph, e.g. "y"
|
BrandInitial string // single-letter favicon glyph, e.g. "y"
|
||||||
|
|||||||
+43
-1
@@ -97,7 +97,9 @@ func TestThemeAttribute(t *testing.T) {
|
|||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{"unset resolves to the bare :root theme", "", `<html lang="en">`},
|
{"unset resolves to the bare :root theme", "", `<html lang="en">`},
|
||||||
{"explicit dark", "dark", `<html lang="en" data-theme="dark">`},
|
{"explicit dark", ThemeDark, `<html lang="en" data-theme="dark">`},
|
||||||
|
{"light", ThemeLight, `<html lang="en" data-theme="light">`},
|
||||||
|
{"system", ThemeSystem, `<html lang="en" data-theme="system">`},
|
||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
t.Run(c.name, func(t *testing.T) {
|
t.Run(c.name, func(t *testing.T) {
|
||||||
@@ -136,3 +138,43 @@ func TestPageUnknown(t *testing.T) {
|
|||||||
t.Fatal("expected error for unknown page")
|
t.Fatal("expected error for unknown page")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestThemeBlocksInSync guards the hand-kept duplication in
|
||||||
|
// static/style.css: every theme declares the same --yw-* properties, and
|
||||||
|
// the "system" media-query block is identical to "light".
|
||||||
|
func TestThemeBlocksInSync(t *testing.T) {
|
||||||
|
css, err := staticFS.ReadFile("static/style.css")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
block := func(selector string) string {
|
||||||
|
s := string(css)
|
||||||
|
i := strings.Index(s, selector+" {")
|
||||||
|
if i < 0 {
|
||||||
|
t.Fatalf("style.css: no block %q", selector)
|
||||||
|
}
|
||||||
|
s = s[i+len(selector)+2:]
|
||||||
|
return s[:strings.Index(s, "}")]
|
||||||
|
}
|
||||||
|
decls := func(b string) (names, full []string) {
|
||||||
|
for _, line := range strings.Split(b, "\n") {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
full = append(full, line)
|
||||||
|
names = append(names, strings.SplitN(line, ":", 2)[0])
|
||||||
|
}
|
||||||
|
return names, full
|
||||||
|
}
|
||||||
|
|
||||||
|
darkNames, _ := decls(block(`:root[data-theme="system"]`))
|
||||||
|
lightNames, light := decls(block(`:root[data-theme="light"]`))
|
||||||
|
_, system := decls(block(` :root[data-theme="system"]`))
|
||||||
|
if strings.Join(darkNames, ",") != strings.Join(lightNames, ",") {
|
||||||
|
t.Errorf("dark and light declare different properties:\ndark: %v\nlight: %v", darkNames, lightNames)
|
||||||
|
}
|
||||||
|
if strings.Join(light, "\n") != strings.Join(system, "\n") {
|
||||||
|
t.Errorf("light and prefers-color-scheme: light blocks differ:\nlight: %v\nsystem: %v", light, system)
|
||||||
|
}
|
||||||
|
}
|
||||||
+66
-32
@@ -7,17 +7,21 @@
|
|||||||
|
|
||||||
The palette is exposed as --yw-* custom properties, not baked into
|
The palette is exposed as --yw-* custom properties, not baked into
|
||||||
each rule -- a documented, reusable theme, not an implementation
|
each rule -- a documented, reusable theme, not an implementation
|
||||||
detail. Exactly one theme ships today ("dark"); PageHeader.Theme
|
detail. Three values for PageHeader.Theme (see layout.go), rendered
|
||||||
(see layout.go) renders as <html data-theme="...">, which is the seam
|
as <html data-theme="...">: "dark", "light", and "system" (follows
|
||||||
a second theme slots into: add its own :root[data-theme="..."] block
|
the browser's prefers-color-scheme). Unset Theme (every consumer
|
||||||
below with the same property list, new values -- nothing else in this
|
that never sets it) resolves to the bare :root block, i.e. dark --
|
||||||
module changes, and unset Theme (every current consumer) keeps
|
today's exact look. A consumer's own stylesheet that needs
|
||||||
resolving to the bare :root block, i.e. today's exact look. A project
|
theme-dependent colors mirrors the same selector structure for its
|
||||||
needing a genuinely different look (not just a different accent)
|
own custom properties instead of redefining these -- see README.md's
|
||||||
should not redefine these globally -- see README.md's Theming
|
Theming section. */
|
||||||
section. */
|
|
||||||
|
|
||||||
:root {
|
/* Dark: the default (bare :root), explicit "dark", and "system" unless
|
||||||
|
the media query below switches it to light. */
|
||||||
|
:root,
|
||||||
|
:root[data-theme="dark"],
|
||||||
|
:root[data-theme="system"] {
|
||||||
|
color-scheme: dark;
|
||||||
--yw-bg: #1a1d23;
|
--yw-bg: #1a1d23;
|
||||||
--yw-text: #d4d8e0;
|
--yw-text: #d4d8e0;
|
||||||
--yw-border: #333;
|
--yw-border: #333;
|
||||||
@@ -42,35 +46,65 @@
|
|||||||
--bs-border-color: var(--yw-border);
|
--bs-border-color: var(--yw-border);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Theme: "dark" -- explicit opt-in via PageHeader.Theme = "dark",
|
/* Light: explicit "light", and "system" on a light-preferring browser.
|
||||||
byte-identical to the bare :root block above (which is what unset
|
The two blocks are kept identical by hand -- CSS can't share one
|
||||||
Theme resolves to). Kept in sync manually -- there are only ever a
|
declaration block between a plain rule and a media query.
|
||||||
handful of themes here, a preprocessor would be overkill. */
|
--yw-nav-link stays #0b82b8 (a cross-project design token, see the
|
||||||
:root[data-theme="dark"] {
|
meta-repo AGENTS.md); it reads fine on both backgrounds. */
|
||||||
--yw-bg: #1a1d23;
|
:root[data-theme="light"] {
|
||||||
--yw-text: #d4d8e0;
|
color-scheme: light;
|
||||||
--yw-border: #333;
|
--yw-bg: #f4f6f9;
|
||||||
--yw-chrome-bg: #14171e;
|
--yw-text: #1f2530;
|
||||||
--yw-chrome-border: #272d3a;
|
--yw-border: #d3d9e2;
|
||||||
--yw-accent: #4fc3f7;
|
--yw-chrome-bg: #ffffff;
|
||||||
--yw-accent-rgb: 79, 195, 247;
|
--yw-chrome-border: #dde2ea;
|
||||||
|
--yw-accent: #0a72a3;
|
||||||
|
--yw-accent-rgb: 10, 114, 163;
|
||||||
--yw-nav-link: #0b82b8;
|
--yw-nav-link: #0b82b8;
|
||||||
--yw-nav-link-hover-text: #c8d4e4;
|
--yw-nav-link-hover-text: #0a4f72;
|
||||||
--yw-subtitle: #5a7a9a;
|
--yw-subtitle: #5a7088;
|
||||||
--yw-subtitle-sep: #333c4a;
|
--yw-subtitle-sep: #c3cad5;
|
||||||
--yw-footer-text: #4a5a72;
|
--yw-footer-text: #6a7a90;
|
||||||
--yw-success-bg: #192e22;
|
--yw-success-bg: #e5f4ea;
|
||||||
--yw-success-border: #2a5c3a;
|
--yw-success-border: #a6d4b4;
|
||||||
--yw-success-text: #7ecf98;
|
--yw-success-text: #1c5e33;
|
||||||
--yw-danger-bg: #2e1820;
|
--yw-danger-bg: #fbe9ec;
|
||||||
--yw-danger-border: #5c2a32;
|
--yw-danger-border: #e6b0b8;
|
||||||
--yw-danger-text: #e08090;
|
--yw-danger-text: #9b2335;
|
||||||
|
|
||||||
--bs-body-bg: var(--yw-bg);
|
--bs-body-bg: var(--yw-bg);
|
||||||
--bs-body-color: var(--yw-text);
|
--bs-body-color: var(--yw-text);
|
||||||
--bs-border-color: var(--yw-border);
|
--bs-border-color: var(--yw-border);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: light) {
|
||||||
|
:root[data-theme="system"] {
|
||||||
|
color-scheme: light;
|
||||||
|
--yw-bg: #f4f6f9;
|
||||||
|
--yw-text: #1f2530;
|
||||||
|
--yw-border: #d3d9e2;
|
||||||
|
--yw-chrome-bg: #ffffff;
|
||||||
|
--yw-chrome-border: #dde2ea;
|
||||||
|
--yw-accent: #0a72a3;
|
||||||
|
--yw-accent-rgb: 10, 114, 163;
|
||||||
|
--yw-nav-link: #0b82b8;
|
||||||
|
--yw-nav-link-hover-text: #0a4f72;
|
||||||
|
--yw-subtitle: #5a7088;
|
||||||
|
--yw-subtitle-sep: #c3cad5;
|
||||||
|
--yw-footer-text: #6a7a90;
|
||||||
|
--yw-success-bg: #e5f4ea;
|
||||||
|
--yw-success-border: #a6d4b4;
|
||||||
|
--yw-success-text: #1c5e33;
|
||||||
|
--yw-danger-bg: #fbe9ec;
|
||||||
|
--yw-danger-border: #e6b0b8;
|
||||||
|
--yw-danger-text: #9b2335;
|
||||||
|
|
||||||
|
--bs-body-bg: var(--yw-bg);
|
||||||
|
--bs-body-color: var(--yw-text);
|
||||||
|
--bs-border-color: var(--yw-border);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
html {
|
html {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user