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
+171
@@ -0,0 +1,171 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Values for PageHeader.Palette; each has its own block in
|
||||
// static/style.css. "" is PaletteDefault.
|
||||
const (
|
||||
PaletteDefault = "default"
|
||||
PaletteGraphite = "graphite" // grey and violet, after nessajina.tv
|
||||
PaletteContrast = "contrast" // high contrast
|
||||
)
|
||||
|
||||
// Cookies AppearanceHandler sets and ApplyAppearance reads. Scoped to
|
||||
// the consumer's BasePath, so two dashboards on one host keep their own
|
||||
// choice.
|
||||
const (
|
||||
ThemeCookie = "yw_theme"
|
||||
PaletteCookie = "yw_palette"
|
||||
)
|
||||
|
||||
// Choice is one entry of the appearance menu.
|
||||
type Choice struct {
|
||||
ID string
|
||||
Label string // English
|
||||
LabelDE string
|
||||
Symbol string
|
||||
}
|
||||
|
||||
// Themes and Palettes are the choices ApplyAppearance and
|
||||
// AppearanceHandler accept, in menu order.
|
||||
var (
|
||||
Themes = []Choice{
|
||||
{ThemeSystem, "System", "System", "◐"},
|
||||
{ThemeLight, "Light", "Hell", "☀"},
|
||||
{ThemeDark, "Dark", "Dunkel", "☾"},
|
||||
}
|
||||
Palettes = []Choice{
|
||||
{PaletteDefault, "Yetano", "Yetano", ""},
|
||||
{PaletteGraphite, "Graphite", "Graphit", ""},
|
||||
{PaletteContrast, "High contrast", "Hoher Kontrast", ""},
|
||||
}
|
||||
)
|
||||
|
||||
func known(choices []Choice, id string) bool {
|
||||
for _, c := range choices {
|
||||
if c.ID == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ApplyAppearance overrides h.Theme and h.Palette with the visitor's
|
||||
// choice from the cookies AppearanceHandler sets (unknown values are
|
||||
// ignored, so the consumer's own defaults stay), and records the
|
||||
// current page as AppearanceBack for the menu's return trip. Call it
|
||||
// after BasePath is set. Resolving this on the server lets the page
|
||||
// arrive in the right colors without an inline script.
|
||||
func (h *PageHeader) ApplyAppearance(r *http.Request) {
|
||||
if c, err := r.Cookie(ThemeCookie); err == nil && known(Themes, c.Value) {
|
||||
h.Theme = c.Value
|
||||
}
|
||||
if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) {
|
||||
h.Palette = c.Value
|
||||
}
|
||||
back := url.URL{Path: strings.TrimPrefix(r.URL.Path, h.BasePath), RawQuery: r.URL.RawQuery}
|
||||
h.AppearanceBack = back.String()
|
||||
}
|
||||
|
||||
// AppearanceHandler stores a theme and/or palette choice from a POST
|
||||
// form (fields "theme", "palette", "back") in cookies and redirects to
|
||||
// back, a BasePath-relative path inside this UI (anything else goes to
|
||||
// the start page, so it is no open redirect). Register it for POST only
|
||||
// at BasePath + PageHeader.AppearancePath; other methods get 405.
|
||||
//
|
||||
// No CSRF token: a foreign page could at worst change the colors.
|
||||
func AppearanceHandler(basePath string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
theme, palette := r.FormValue("theme"), r.FormValue("palette")
|
||||
if (theme == "" && palette == "") ||
|
||||
(theme != "" && !known(Themes, theme)) ||
|
||||
(palette != "" && !known(Palettes, palette)) {
|
||||
http.Error(w, "unknown theme or palette", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
set := func(name, value string) {
|
||||
http.SetCookie(w, &http.Cookie{Name: name, Value: value, Path: basePath + "/",
|
||||
MaxAge: 365 * 24 * 3600, HttpOnly: true, SameSite: http.SameSiteLaxMode})
|
||||
}
|
||||
if theme != "" {
|
||||
set(ThemeCookie, theme)
|
||||
}
|
||||
if palette != "" {
|
||||
set(PaletteCookie, palette)
|
||||
}
|
||||
http.Redirect(w, r, basePath+localPath(r.FormValue("back")), http.StatusSeeOther)
|
||||
})
|
||||
}
|
||||
|
||||
// localPath returns back if it is a path (with query) inside this UI,
|
||||
// otherwise "/".
|
||||
func localPath(back string) string {
|
||||
u, err := url.Parse(back)
|
||||
if err != nil || u.Scheme != "" || u.Host != "" || u.User != nil ||
|
||||
!strings.HasPrefix(back, "/") || strings.HasPrefix(back, "//") ||
|
||||
strings.ContainsAny(back, "\\\r\n") {
|
||||
return "/"
|
||||
}
|
||||
return back
|
||||
}
|
||||
|
||||
// AppearanceItem is one button of the rendered appearance menu.
|
||||
type AppearanceItem struct {
|
||||
ID, Label, Symbol string
|
||||
Current bool
|
||||
}
|
||||
|
||||
// AppearanceMenuData is what the navbar template renders the menu from.
|
||||
type AppearanceMenuData struct {
|
||||
Current AppearanceItem // the active theme, shown on the closed menu
|
||||
ThemeHeading, PaletteHeading string
|
||||
Themes, Palettes []AppearanceItem
|
||||
}
|
||||
|
||||
// AppearanceMenu builds the menu's data, labeled in German when Lang
|
||||
// starts with "de", otherwise in English. The navbar template calls it.
|
||||
func (h PageHeader) AppearanceMenu() AppearanceMenuData {
|
||||
de := strings.HasPrefix(h.Lang, "de")
|
||||
items := func(choices []Choice, current string) []AppearanceItem {
|
||||
out := make([]AppearanceItem, len(choices))
|
||||
for i, c := range choices {
|
||||
label := c.Label
|
||||
if de {
|
||||
label = c.LabelDE
|
||||
}
|
||||
out[i] = AppearanceItem{ID: c.ID, Label: label, Symbol: c.Symbol, Current: c.ID == current}
|
||||
}
|
||||
return out
|
||||
}
|
||||
theme, palette := h.Theme, h.Palette
|
||||
if theme == "" {
|
||||
theme = ThemeDark
|
||||
}
|
||||
if palette == "" {
|
||||
palette = PaletteDefault
|
||||
}
|
||||
m := AppearanceMenuData{
|
||||
Themes: items(Themes, theme),
|
||||
Palettes: items(Palettes, palette),
|
||||
ThemeHeading: "Theme",
|
||||
PaletteHeading: "Palette",
|
||||
}
|
||||
if de {
|
||||
m.ThemeHeading, m.PaletteHeading = "Modus", "Farben"
|
||||
}
|
||||
for _, t := range m.Themes {
|
||||
if t.Current {
|
||||
m.Current = t
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
Reference in new issue
Block a user