Prepare theming: PageHeader.Theme -> <html data-theme>, explicit dark block
Zero behavior change for every current consumer (Theme defaults to "", which resolves to the unchanged bare :root block). Adds the seam a future second theme slots into: its own :root[data-theme="..."] block in static/style.css, nothing else in this module changes. See README.md's expanded Theming section for the concrete mechanism.
This commit is contained in:
1 parent
adea139888
commit
8875062fc5
5 files changed
+123
-34
No files matched your search
@@ -62,21 +62,38 @@ forms, buttons, markdown, ...), and anything else specific to that tool.
|
|||||||
## Theming
|
## Theming
|
||||||
|
|
||||||
`static/style.css`'s palette is exposed as `--yw-*` CSS custom properties
|
`static/style.css`'s palette is exposed as `--yw-*` CSS custom properties
|
||||||
on `:root` (`--yw-bg`, `--yw-text`, `--yw-chrome-bg`, `--yw-accent`,
|
(`--yw-bg`, `--yw-text`, `--yw-chrome-bg`, `--yw-accent`, `--yw-nav-link`,
|
||||||
`--yw-nav-link`, `--yw-success-*`/`--yw-danger-*`, ...) rather than baked
|
`--yw-success-*`/`--yw-danger-*`, ...) rather than baked directly into
|
||||||
directly into each rule — it's a documented, reusable theme, not just an
|
each rule — a documented, reusable theme, not an implementation detail.
|
||||||
implementation detail. This is the *only* theme the module ships: fixed
|
|
||||||
dark, no `prefers-color-scheme`, no light mode. Don't add one here on a
|
The mechanism is prepared but only one theme ("dark") is actually
|
||||||
per-project basis (redefining `--yw-*` globally in one consumer would
|
defined today:
|
||||||
silently reskin every future consumer sharing this CSS file); a project
|
|
||||||
that genuinely needs a different look — e.g. a light/dark toggle, like
|
- `PageHeader.Theme` (see `layout.go`) renders as `<html data-theme="...">`.
|
||||||
`yaais` has — should own that itself rather than pushing multi-theme
|
`""` (every current consumer) omits the attribute, which resolves to
|
||||||
complexity into this module, since `yaais` shows that need doesn't
|
the bare `:root` block — today's exact look, unchanged.
|
||||||
actually require *this* module either (it's a client-rendered SPA, not a
|
- `static/style.css` also declares `:root[data-theme="dark"]`, byte-
|
||||||
server-rendered Go-template dashboard). If a real case for a second theme
|
identical to the bare `:root` block — `Theme: "dark"` is an explicit,
|
||||||
*in* this module shows up later, the `--yw-*` tokens are exactly the
|
equivalent opt-in, and the template a second theme copies.
|
||||||
seam to add one through — redefine them under a `[data-theme="..."]`
|
- **Adding a second theme** (e.g. "light"): add its own
|
||||||
selector the way `yaais`'s own stylesheet does.
|
`:root[data-theme="light"] { --yw-*: ...; }` block to
|
||||||
|
`static/style.css` with the same property list and new values. Nothing
|
||||||
|
else in this module changes. A consuming project opts in per-request
|
||||||
|
or per-user by setting `Theme: "light"` on the `PageHeader`s it
|
||||||
|
builds; projects that never set `Theme` are completely unaffected.
|
||||||
|
- This module does **not** read `prefers-color-scheme` or persist a
|
||||||
|
choice itself (no cookie/localStorage/JS) — that's each consuming
|
||||||
|
project's own concern (e.g. a cookie/session-backed preference,
|
||||||
|
resolved server-side into `Theme` before rendering), same as any other
|
||||||
|
`PageHeader` field.
|
||||||
|
- Don't redefine `--yw-*` globally in one consumer's own stylesheet to
|
||||||
|
get a per-project look — that silently reskins every other consumer
|
||||||
|
sharing this CSS file too. A project that genuinely needs a
|
||||||
|
self-contained different look — e.g. a light/dark toggle with its own
|
||||||
|
JS/localStorage, like `yaais` has — should own that itself rather than
|
||||||
|
pushing multi-theme complexity into this module, since `yaais` shows
|
||||||
|
that need doesn't actually require *this* module either (it's a
|
||||||
|
client-rendered SPA, not a server-rendered Go-template dashboard).
|
||||||
|
|
||||||
## Consuming this module
|
## Consuming this module
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// Package layout provides the shared server-rendered web UI chrome (base
|
// Package layout provides the shared server-rendered web UI chrome (base
|
||||||
// layout, navbar, sidebar, footer) used by every Yetano/Yak/Yamase
|
// layout, navbar, sidebar, footer) used by every Yetano/Yak/Yamase
|
||||||
// ops-tool admin dashboard: yasta, yasl, yaiam, yakwui, and yamase-web.
|
// ops-tool admin dashboard: yasta, yasl, yaiam, yakwui, yamase-web, and
|
||||||
|
// yamocir.
|
||||||
//
|
//
|
||||||
// Consumers embed PageHeader in their own per-page data struct and use
|
// Consumers embed PageHeader in their own per-page data struct and use
|
||||||
// NewRenderer to parse the shared layout templates together with their
|
// NewRenderer to parse the shared layout templates together with their
|
||||||
@@ -41,7 +42,7 @@ type NavItem struct {
|
|||||||
// fields simply render nothing (e.g. Username == "" hides the
|
// fields simply render nothing (e.g. Username == "" hides the
|
||||||
// username/logout group).
|
// username/logout group).
|
||||||
type PageHeader struct {
|
type PageHeader struct {
|
||||||
Title string
|
Title string
|
||||||
// AppName is the lowercase product name, e.g. "yasta", used in
|
// AppName is the lowercase product name, e.g. "yasta", used in
|
||||||
// <title> and the footer. Deliberately not "Name" -- a consuming
|
// <title> and the footer. Deliberately not "Name" -- a consuming
|
||||||
// project's own per-page data struct commonly embeds both PageHeader
|
// project's own per-page data struct commonly embeds both PageHeader
|
||||||
@@ -49,8 +50,20 @@ type PageHeader struct {
|
|||||||
// resource's name); two same-depth promoted "Name" fields make
|
// resource's name); two same-depth promoted "Name" fields make
|
||||||
// html/template's ".Name" an ambiguous selector that only fails at
|
// html/template's ".Name" an ambiguous selector that only fails at
|
||||||
// execution time, not at parse time.
|
// execution time, not at parse time.
|
||||||
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,
|
||||||
|
// rendered as <html data-theme="...">. "" omits the attribute, which
|
||||||
|
// resolves to the bare :root values -- today that's the only theme
|
||||||
|
// this module ships (dark), so leaving Theme unset is exactly today's
|
||||||
|
// behavior for every current consumer. Setting Theme: "dark" is
|
||||||
|
// equivalent (an explicit :root[data-theme="dark"] block exists with
|
||||||
|
// 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
|
||||||
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"
|
||||||
Subtitle string
|
Subtitle string
|
||||||
|
|||||||
@@ -85,6 +85,33 @@ func TestPageHeaderOverrides(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestThemeAttribute(t *testing.T) {
|
||||||
|
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewRenderer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
theme string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"unset resolves to the bare :root theme", "", `<html lang="en">`},
|
||||||
|
{"explicit dark", "dark", `<html lang="en" data-theme="dark">`},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := r.Page(&buf, "dashboard", PageHeader{Title: "Dashboard", Theme: c.theme}); err != nil {
|
||||||
|
t.Fatalf("Page: %v", err)
|
||||||
|
}
|
||||||
|
if out := buf.String(); !strings.Contains(out, c.want) {
|
||||||
|
t.Errorf("rendered output missing %q\n---\n%s", c.want, out)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestExecuteNamed(t *testing.T) {
|
func TestExecuteNamed(t *testing.T) {
|
||||||
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+46
-14
@@ -1,18 +1,21 @@
|
|||||||
/* Shared web UI chrome -- the dark-theme base layout, navbar, sidebar, and
|
/* Shared web UI chrome -- the base layout, navbar, sidebar, and footer
|
||||||
footer used by every ops-tool dashboard built on this module (yasta,
|
used by every ops-tool dashboard built on this module (yasta, yasl,
|
||||||
yasl, yaiam, yakwui, yamase-web, yamocir). Loaded after
|
yaiam, yakwui, yamase-web, yamocir). Loaded after bootstrap.min.css.
|
||||||
bootstrap.min.css. Page-content styling (cards, tables, badges,
|
Page-content styling (cards, tables, badges, forms, ...) is each
|
||||||
forms, ...) is each project's own concern and lives in that project's
|
project's own concern and lives in that project's own stylesheet,
|
||||||
own stylesheet, loaded alongside this one -- see
|
loaded alongside this one -- see PageHeader.ExtraHeadHTML.
|
||||||
PageHeader.ExtraHeadHTML. Fixed dark theme only -- no
|
|
||||||
prefers-color-scheme, no light mode (see README.md's Theming section
|
|
||||||
before assuming that's a gap to fill here).
|
|
||||||
|
|
||||||
The palette below is exposed as --yw-* custom properties, not just
|
The palette is exposed as --yw-* custom properties, not baked into
|
||||||
baked into each rule -- it's the shared "theme" every migrated
|
each rule -- a documented, reusable theme, not an implementation
|
||||||
project inherits, not an implementation detail. A project needing a
|
detail. Exactly one theme ships today ("dark"); PageHeader.Theme
|
||||||
genuinely different look (not just a different accent) should not
|
(see layout.go) renders as <html data-theme="...">, which is the seam
|
||||||
redefine these globally -- see README.md. */
|
a second theme slots into: add its own :root[data-theme="..."] block
|
||||||
|
below with the same property list, new values -- nothing else in this
|
||||||
|
module changes, and unset Theme (every current consumer) keeps
|
||||||
|
resolving to the bare :root block, i.e. today's exact look. A project
|
||||||
|
needing a genuinely different look (not just a different accent)
|
||||||
|
should not redefine these globally -- see README.md's Theming
|
||||||
|
section. */
|
||||||
|
|
||||||
:root {
|
:root {
|
||||||
--yw-bg: #1a1d23;
|
--yw-bg: #1a1d23;
|
||||||
@@ -39,6 +42,35 @@
|
|||||||
--bs-border-color: var(--yw-border);
|
--bs-border-color: var(--yw-border);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Theme: "dark" -- explicit opt-in via PageHeader.Theme = "dark",
|
||||||
|
byte-identical to the bare :root block above (which is what unset
|
||||||
|
Theme resolves to). Kept in sync manually -- there are only ever a
|
||||||
|
handful of themes here, a preprocessor would be overkill. */
|
||||||
|
:root[data-theme="dark"] {
|
||||||
|
--yw-bg: #1a1d23;
|
||||||
|
--yw-text: #d4d8e0;
|
||||||
|
--yw-border: #333;
|
||||||
|
--yw-chrome-bg: #14171e;
|
||||||
|
--yw-chrome-border: #272d3a;
|
||||||
|
--yw-accent: #4fc3f7;
|
||||||
|
--yw-accent-rgb: 79, 195, 247;
|
||||||
|
--yw-nav-link: #0b82b8;
|
||||||
|
--yw-nav-link-hover-text: #c8d4e4;
|
||||||
|
--yw-subtitle: #5a7a9a;
|
||||||
|
--yw-subtitle-sep: #333c4a;
|
||||||
|
--yw-footer-text: #4a5a72;
|
||||||
|
--yw-success-bg: #192e22;
|
||||||
|
--yw-success-border: #2a5c3a;
|
||||||
|
--yw-success-text: #7ecf98;
|
||||||
|
--yw-danger-bg: #2e1820;
|
||||||
|
--yw-danger-border: #5c2a32;
|
||||||
|
--yw-danger-text: #e08090;
|
||||||
|
|
||||||
|
--bs-body-bg: var(--yw-bg);
|
||||||
|
--bs-body-color: var(--yw-text);
|
||||||
|
--bs-border-color: var(--yw-border);
|
||||||
|
}
|
||||||
|
|
||||||
html {
|
html {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{{define "base"}}<!doctype html>
|
{{define "base"}}<!doctype html>
|
||||||
<html lang="{{if .Lang}}{{.Lang}}{{else}}en{{end}}">
|
<html lang="{{if .Lang}}{{.Lang}}{{else}}en{{end}}"{{if .Theme}} data-theme="{{.Theme}}"{{end}}>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|||||||
Reference in new issue
Block a user