Palettes, light-dark() tokens, Bootstrap bridge and a theme menu
Every color token is now one light-dark(<light>, <dark>) pair; the mode (data-theme) only sets color-scheme, so "system" no longer needs a duplicated media-query block. RGB triplets and the url() icons Bootstrap swaps in dark mode pick their value via the --yw-if-light/--yw-if-dark switches. Bootstrap's theme-dependent --bs-* variables and the components that hard-code its primary blue are mapped onto --yw-*, so tables, forms, links, buttons and alerts follow the theme. The navbar drops navbar-dark, which pinned its colors to white in light mode. New data-palette axis (PageHeader.Palette): default, graphite (grey/violet, after nessajina.tv) and contrast. The default palette keeps the #0b82b8 nav-link token; the others use their own. appearance.go: ApplyAppearance reads the yw_theme/yw_palette cookies, AppearanceHandler stores a choice (POST only, local redirects only), and PageHeader.AppearancePath shows a script-free <details> menu in the navbar. TestPaletteTokens replaces TestThemeBlocksInSync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d0e49b2365
commit
3af449e9f9
8 files changed
+893
-179
No files matched your search
@@ -21,7 +21,7 @@ archived on 2026-10-23 (nothing imported them).
|
||||
|
||||
Shared here: the base HTML skeleton, navbar, sidebar (generic nav-item
|
||||
list), footer, and the CSS rules that style exactly those elements
|
||||
(`static/style.css` — theme root variables, navbar, `.sidebar .nav-link`
|
||||
(`static/style.css` — theme tokens and the Bootstrap bridge, navbar, `.sidebar .nav-link`
|
||||
incl. the `#0b82b8` inactive-link color, footer, flash-banner alerts).
|
||||
|
||||
Stays in each consuming project: page-specific templates
|
||||
@@ -30,46 +30,80 @@ forms, buttons, markdown, ...), and anything else specific to that tool.
|
||||
|
||||
## Theming
|
||||
|
||||
`static/style.css`'s palette is exposed as `--yw-*` CSS custom properties
|
||||
(`--yw-bg`, `--yw-text`, `--yw-chrome-bg`, `--yw-accent`, `--yw-nav-link`,
|
||||
`--yw-success-*`/`--yw-danger-*`, ...) rather than baked directly into
|
||||
each rule — a documented, reusable theme, not an implementation detail.
|
||||
Two independent axes, both set on `<html>` and resolved on the server:
|
||||
|
||||
Three themes ship, selected by `PageHeader.Theme` (see `layout.go`),
|
||||
which renders as `<html data-theme="...">`:
|
||||
| Axis | `PageHeader` field | Attribute | Values |
|
||||
|---|---|---|---|
|
||||
| Mode | `Theme` | `data-theme` | `layout.ThemeDark` (also `""`, unset), `ThemeLight`, `ThemeSystem` (follows `prefers-color-scheme`) |
|
||||
| Colors | `Palette` | `data-palette` | `layout.PaletteDefault` (also `""`), `PaletteGraphite` (grey/violet, after nessajina.tv), `PaletteContrast` (high contrast) |
|
||||
|
||||
| `Theme` | Look |
|
||||
|---|---|
|
||||
| `""` (unset) | dark, the bare `:root` block, i.e. what every consumer had before themes existed |
|
||||
| `layout.ThemeDark` (`"dark"`) | dark, the same values |
|
||||
| `layout.ThemeLight` (`"light"`) | light |
|
||||
| `layout.ThemeSystem` (`"system"`) | follows the browser's `prefers-color-scheme` |
|
||||
A consumer that sets neither keeps the original dark look.
|
||||
|
||||
Every block also sets `color-scheme`, so native controls and scrollbars
|
||||
match the palette.
|
||||
How `static/style.css` does it:
|
||||
|
||||
- The light values appear twice in `static/style.css` (as
|
||||
`:root[data-theme="light"]` and inside `@media (prefers-color-scheme:
|
||||
light)` for `"system"`). `TestThemeBlocksInSync` fails if the two drift
|
||||
apart or if a theme is missing a property.
|
||||
- `--yw-nav-link` is `#0b82b8` in every theme. It is a cross-project
|
||||
design token (see the meta-repo `AGENTS.md`).
|
||||
- This module does **not** persist a choice (no cookie, localStorage or
|
||||
JS). That is the consuming project's job: keep the preference
|
||||
(e.g. in a cookie set by a POST form), resolve it server-side into
|
||||
`Theme` before rendering, and offer the switch itself (e.g. via
|
||||
`NavbarRightExtra`). Resolving server-side avoids a flash of the wrong
|
||||
theme without an inline script, so it works under a strict
|
||||
`script-src 'self'` CSP. `yareco` is the reference consumer.
|
||||
- A consumer whose own stylesheet needs theme-dependent colors defines
|
||||
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`.
|
||||
- **Every color token is written once**, as `--yw-x: light-dark(<light>,
|
||||
<dark>)`. The mode only sets `color-scheme` (`dark`, `light`, or
|
||||
`light dark` for system), so no mode repeats another's values and
|
||||
"system" needs no duplicated media-query block. Needs a browser from
|
||||
2024 or later (`light-dark()`, `color-mix()`).
|
||||
- **Triplets and images** (`--yw-*-rgb` for Bootstrap's `rgba(var(...),
|
||||
a)`, and the few `url()` icons Bootstrap swaps in dark mode) can't use
|
||||
`light-dark()`. They pick with the mode switches `--yw-if-light`/
|
||||
`--yw-if-dark`: `var(--yw-if-light, <light>) var(--yw-if-dark,
|
||||
<dark>)`.
|
||||
- **A palette is one `:root[data-palette="..."]` block** that lists only
|
||||
what it changes from the default (bare `:root`) block.
|
||||
- **Bootstrap follows the palette:** its theme-dependent `--bs-*`
|
||||
variables (body, emphasis, secondary/tertiary, links, borders, the
|
||||
`*-subtle`/`*-emphasis` colors, form validation) and the components
|
||||
that hard-code its primary blue (`.btn-primary`, `.list-group`,
|
||||
`.form-check-input:checked`, focus rings, ...) are mapped onto
|
||||
`--yw-*`. Bootstrap's own `data-bs-theme` is not used (it has no
|
||||
"system" mode without a script); a consumer's local
|
||||
`data-bs-theme="dark"` on one element still works.
|
||||
- `TestPaletteTokens` checks that every palette in `layout.Palettes` has
|
||||
a block, every color is one `light-dark()` pair, every `-rgb` triplet
|
||||
matches its color, and a palette only overrides default tokens.
|
||||
- `--yw-nav-link` is `#0b82b8` in the default palette in both modes (the
|
||||
cross-project design token, see the meta-repo `AGENTS.md`). The other
|
||||
palettes use their own nav-link color: blue would clash with
|
||||
graphite's violet, and it lacks contrast on black.
|
||||
|
||||
Tokens a consumer's own stylesheet can use: `--yw-bg`, `--yw-surface`,
|
||||
`--yw-surface-2`, `--yw-text`, `--yw-text-strong`, `--yw-muted`,
|
||||
`--yw-border`, `--yw-chrome-bg`/`-border`, `--yw-brand`, `--yw-accent`,
|
||||
`--yw-link`/`-hover`, `--yw-primary`, `--yw-on-primary`,
|
||||
`--yw-{success,danger,warning,info}-{bg,border,text}`, plus the `-rgb`
|
||||
triplets of bg/surface/text/accent/link/primary. For its own
|
||||
theme-dependent colors a consumer writes `light-dark()` as well, never
|
||||
redefines `--yw-*` (that would reskin only that project). The older
|
||||
pattern, separate `[data-theme="..."]` blocks, keeps working.
|
||||
|
||||
### Theme/palette menu
|
||||
|
||||
The module can store the visitor's choice itself, without any script
|
||||
(works under a strict `script-src 'self'` CSP):
|
||||
|
||||
```go
|
||||
mux.Handle("POST /appearance", layout.AppearanceHandler(basePath))
|
||||
...
|
||||
hdr := layout.PageHeader{BasePath: basePath, AppearancePath: "/appearance", ...}
|
||||
hdr.ApplyAppearance(r) // Theme/Palette from the cookies, AppearanceBack = current page
|
||||
```
|
||||
|
||||
`AppearancePath` set, the navbar shows a `<details>` dropdown (labels in
|
||||
German when `Lang` starts with `de`) with one POST form whose buttons
|
||||
submit `theme` or `palette`. `AppearanceHandler` stores the choice in
|
||||
the cookies `yw_theme`/`yw_palette` (path `BasePath/`, a year,
|
||||
`HttpOnly`, `SameSite=Lax`) and redirects to `back`, but only to a path
|
||||
inside the UI. Any other method gets 405. `ApplyAppearance` ignores unknown cookie
|
||||
values, so whatever the consumer set before stays the default. A
|
||||
consumer with its own switch (e.g. `yareco`'s) can keep it and just set
|
||||
`Theme`/`Palette`.
|
||||
|
||||
**Adding a palette:** a `:root[data-palette="..."]` block in
|
||||
`static/style.css`, a constant and an entry in `Palettes` in
|
||||
`appearance.go`. `TestPaletteTokens` reports anything missing.
|
||||
|
||||
## Consuming this module
|
||||
|
||||
@@ -116,7 +150,7 @@ concurrent-edit conflict during the initial rollout) and can produce a
|
||||
force-move an existing tag again for any future change — always cut a
|
||||
new one, even for a one-line fix.
|
||||
|
||||
## PageHeader fields (current as of v0.1.10)
|
||||
## PageHeader fields (current as of v0.2.0)
|
||||
|
||||
`Title`, `AppName` (lowercase product name for `<title>`/footer — not
|
||||
`Name`, which collides with a domain type's own `Name` field once
|
||||
@@ -124,7 +158,8 @@ embedded in the same struct, as yakwui's viewmodel types do), `Lang`
|
||||
(`<html lang>`, defaults to `"en"`), `Theme` (`<html data-theme>`, see
|
||||
Theming), `Brand`, `BrandInitial`, `Subtitle`,
|
||||
`BasePath`, `NavItems`/`ActiveNav`, `Flash`/`FlashKind`, `Username`,
|
||||
`LogoutPath` (since v0.1.10 a `<form method="post">` with a submit
|
||||
`Palette`, `AppearancePath`/`AppearanceBack` (since v0.2.0, see
|
||||
Theming), `LogoutPath` (since v0.1.10 a `<form method="post">` with a submit
|
||||
button styled like the former link, never a GET link: a logout reachable
|
||||
by GET can be triggered from any foreign page via an `<img>`/link and by
|
||||
link prefetchers; the consumer registers the route as `POST` only and
|
||||
|
||||
Reference in new issue
Block a user