Files
terminarandClaude Opus 5.5 33dfebab9e Modern style as the new default; style axis, slate palette, nav groups and icons
A third appearance axis, PageHeader.Style (<html data-style>), with
StyleModern -- the default when Style is empty -- and StyleClassic, the
look up to v0.4.x. The modern style is one [data-style="modern"] block
at the end of style.css over the classic rules: navbar without a rule,
the sidebar as a floating rounded card with group headings, icons and
a pill for the active entry, rounder and quieter Bootstrap components.
In it inactive sidebar links use the text color; #0b82b8 now only
colors them in the classic style (owner decision 2026-11-28).

- appearance menu: "Style"/"Stil" section, cookie yw_style
- palette slate (dark slate and lavender)
- NavItem.Group (sidebar group headings) and NavItem.Icon (names from
  the new NavIcons set, shown only in the modern style)
- cmd/webui-demo: a sample page for comparing styles, palettes and modes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 11:11:28 +01:00

201 lines
8.2 KiB
Go

// Package layout provides the shared server-rendered web UI chrome (base
// layout, navbar, sidebar, footer) used by every Yetano/Yak/Yamase
// ops-tool admin dashboard: yasta, yasl, yaiam, yakwui, yamase-web, and
// yamocir.
//
// Consumers embed PageHeader in their own per-page data struct and use
// NewRenderer to parse the shared layout templates together with their
// own page-specific templates. Page-content styling (cards, tables,
// badges, forms, ...) stays each project's own concern; only the chrome
// that base/navbar/sidebar/footer actually render is shared here.
package layout
import (
"embed"
"fmt"
"html/template"
"io"
"io/fs"
"net/http"
)
//go:embed templates/layout
var templateFS embed.FS
//go:embed static
var staticFS embed.FS
// LayoutGlob is the glob NewRenderer uses to pull in the shared
// base/navbar/sidebar/footer templates.
const LayoutGlob = "templates/layout/*.tmpl"
// Values for PageHeader.Theme, the color mode; see static/style.css.
const (
ThemeDark = "dark"
ThemeLight = "light"
ThemeSystem = "system"
)
// NavItem is one sidebar entry.
type NavItem struct {
ID string // matches PageHeader.ActiveNav to mark this item active
Label string
Href string // BasePath-relative, e.g. "/tasks"
// Group, when set, puts a small heading above the first item of a
// run of items with the same Group ("Account", "Downloads", ...).
Group string
// Icon names one of NavIcons ("dashboard", "settings", ...); unknown
// names render nothing. Only StyleModern shows icons, so setting it
// leaves the classic look unchanged.
Icon string
}
// SidebarEntry is one NavItem as the sidebar template renders it.
type SidebarEntry struct {
NavItem
Active bool
GroupHead string // the group heading to render before this item, if any
IconSVG template.HTML // NavIcons[Icon]
}
// SidebarEntries resolves NavItems for the sidebar template: active
// state, group headings (once per run of equal Group values) and icons.
func (h PageHeader) SidebarEntries() []SidebarEntry {
out := make([]SidebarEntry, len(h.NavItems))
prev := ""
for i, n := range h.NavItems {
e := SidebarEntry{NavItem: n, Active: n.ID == h.ActiveNav, IconSVG: navIcon(n.Icon)}
if n.Group != "" && n.Group != prev {
e.GroupHead = n.Group
}
prev = n.Group
out[i] = e
}
return out
}
// PageHeader holds the fields every page's own data struct should embed
// so the shared base/navbar/sidebar/footer templates can render
// regardless of which project or page is being rendered. Zero-valued
// fields simply render nothing (e.g. Username == "" hides the
// username/logout group).
type PageHeader struct {
Title string
// AppName is the lowercase product name, e.g. "yasta", used in
// <title> and the footer. Deliberately not "Name" -- a consuming
// project's own per-page data struct commonly embeds both PageHeader
// and a domain type that has its own Name field (e.g. a Kubernetes
// resource's name); two same-depth promoted "Name" fields make
// html/template's ".Name" an ambiguous selector that only fails at
// execution time, not at parse time.
AppName string
Lang string // <html lang="...">; "" defaults to "en"
// Theme is the color mode, rendered as <html data-theme="...">:
// ThemeDark, ThemeLight or ThemeSystem (follows
// prefers-color-scheme). "" omits the attribute, which is dark, so a
// consumer that never sets Theme keeps its look.
Theme string
// Palette is the color set, rendered as <html data-palette="...">:
// PaletteDefault, PaletteGraphite or PaletteContrast; "" is
// PaletteDefault. ApplyAppearance fills Theme and Palette from the
// visitor's cookies (see README.md's Theming section).
Palette string
// Style is the shape of the chrome, rendered as <html
// data-style="...">: StyleModern (also "") or StyleClassic.
// ApplyAppearance fills it from the visitor's cookie.
Style string
// AppearancePath is the BasePath-relative POST route of
// AppearanceHandler, e.g. "/appearance"; set, the navbar shows the
// theme/palette menu. AppearanceBack is the BasePath-relative page
// the menu returns to (ApplyAppearance sets it).
AppearancePath string
AppearanceBack string
// AppearanceFormHTML is extra markup inside the menu's POST form,
// e.g. a hidden CSRF token field for a consumer whose middleware
// demands one on every POST.
AppearanceFormHTML template.HTML
Brand string // navbar brand text, e.g. "YASTA"
BrandInitial string // single-letter favicon glyph, e.g. "y"
Subtitle string
BasePath string
NavItems []NavItem
ActiveNav string
Flash string
FlashKind string // FlashOK, FlashError or FlashInfo (flash.go); meaningless if Flash == ""
Username string
LogoutPath string // BasePath-relative, e.g. "/logout"; rendered as a POST form (never a GET link); "" hides it
NavbarBeforeBrand template.HTML // extra markup rendered before the brand link, e.g. a mobile sidebar toggle
NavbarLeftExtra template.HTML // extra markup rendered after Subtitle in the navbar's left group
NavbarRightExtra template.HTML // extra markup rendered after Username/Logout in the navbar's right group
ExtraHeadHTML template.HTML // extra <link>/<script> tags appended to <head>
ExtraBodyHTML template.HTML // extra <script> tags appended before </body>
// BodyAttrs/SidebarAttrs are raw extra attributes (include their own
// leading space, e.g. ` x-data="{ open: true }"`) spliced into
// <body...> and the sidebar's <nav...> tag -- an escape hatch for a
// consuming project's own client-side framework state (e.g. yakwui's
// Alpine.js collapsible sidebar), which the shared markup has no
// generic opinion about. template.HTMLAttr, not template.HTML --
// html/template's contextual autoescaper requires the attribute-list
// sentinel type in this position or it silently drops the value.
BodyAttrs template.HTMLAttr
SidebarAttrs template.HTMLAttr
}
// Renderer holds one independently parsed *template.Template per page:
// the shared layout files plus that one page's own file. Parsing each
// page separately -- rather than one shared template.Template built from
// every file -- keeps each page's "content" block (every page defines
// one) scoped to that page instead of colliding in a single global
// template namespace.
type Renderer struct {
pages map[string]*template.Template
}
// NewRenderer parses, for each name in pageNames, the shared layout
// templates plus pagesFS's <pageDir>/<name><pageSuffix> into its own
// Renderer entry. funcs must supply everything the templates call,
// including "buildInfo" (used by the shared footer).
func NewRenderer(pagesFS fs.FS, pageDir string, pageNames []string, pageSuffix string, funcs template.FuncMap) (*Renderer, error) {
r := &Renderer{pages: make(map[string]*template.Template, len(pageNames))}
for _, name := range pageNames {
t := template.New("base").Funcs(funcs)
t, err := t.ParseFS(templateFS, LayoutGlob)
if err != nil {
return nil, fmt.Errorf("layout: parsing shared layout templates: %w", err)
}
t, err = t.ParseFS(pagesFS, pageDir+"/"+name+pageSuffix)
if err != nil {
return nil, fmt.Errorf("layout: parsing page %q: %w", name, err)
}
r.pages[name] = t
}
return r, nil
}
// Page renders the full page (layout + content) for the given page name.
func (r *Renderer) Page(w io.Writer, page string, data any) error {
return r.ExecuteNamed(w, page, "base", data)
}
// ExecuteNamed renders the named block (not necessarily "base") from
// page's parsed template set -- e.g. an SSE handler re-rendering just a
// page's own fragment block on every tick instead of the full page.
func (r *Renderer) ExecuteNamed(w io.Writer, page, name string, data any) error {
t, ok := r.pages[page]
if !ok {
return fmt.Errorf("layout: unknown page %q", page)
}
return t.ExecuteTemplate(w, name, data)
}
// StaticHandler serves the shared vendored static assets
// (bootstrap.min.css, bootstrap.bundle.min.js, and the shared chrome
// stylesheet) under prefix.
func StaticHandler(prefix string) http.Handler {
sub, err := fs.Sub(staticFS, "static")
if err != nil {
panic(err) // static is embedded at build time; this cannot fail at runtime
}
return http.StripPrefix(prefix, http.FileServerFS(sub))
}