theme

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package theme handles theme discovery, loading, token resolution, and CSS generation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDarkTokens

func DefaultDarkTokens() map[string]string

DefaultDarkTokens returns the hardcoded default dark-mode token overrides. These mirror the semantic remaps previously in dark.css, making all dark-mode values reachable via theme.dark_overrides in sarde.yaml.

func DefaultTokens

func DefaultTokens() map[string]string

DefaultTokens returns the hardcoded default light-mode tokens. This is layer 0 — the absolute fallback ensuring every expected CSS variable exists even with no theme.yaml.

func DeriveTokens

func DeriveTokens(tokens map[string]string) map[string]string

DeriveTokens auto-generates variant tokens from the accent color. When accent is a valid hex or oklch color, it derives:

  • accent-hover: 10% darker
  • accent-high: 20% lighter (for dark mode emphasis)
  • accent-low: rgba() at 10% opacity (for subtle backgrounds)
  • accent-text: lightness-capped variant for accent-colored text, guaranteed readable (4.5:1) on light surfaces

Derivation is skipped if a key already exists (user overrides win). Unparseable accent values are skipped gracefully.

func GenerateCSS

func GenerateCSS(lightTokens, darkTokens map[string]string) string

GenerateCSS produces CSS custom property blocks for light and dark modes. All token keys are prefixed with --sd- and sorted alphabetically.

func GenerateLightDarkCSS

func GenerateLightDarkCSS(lightTokens, darkTokens map[string]string) string

GenerateLightDarkCSS produces a @supports block that uses the CSS light-dark() function for tokens that have both a light and dark value. This is emitted as progressive enhancement — browsers that support light-dark() use it, others ignore the @supports block and fall back to the legacy two-block output.

func GenerateStyleTag

func GenerateStyleTag(lightTokens, darkTokens map[string]string) htmltemplate.HTML

GenerateStyleTag wraps the generated CSS in a <style> element. It emits both the legacy two-block output (baseline) and a light-dark() @supports block (progressive enhancement for modern browsers).

func KnownTokens

func KnownTokens() map[string]bool

KnownTokens returns the set of all valid --sd-* token names (without the prefix). This must stay in sync with tokens.css — the TestKnownTokens_MatchesCSS test enforces this.

func ResolveDarkTokens

func ResolveDarkTokens(defaults map[string]string, t *Theme, presetName string, overrides map[string]string) map[string]string

ResolveDarkTokens merges dark-mode tokens through the 4-layer cascade.

func ResolveTokens

func ResolveTokens(defaults map[string]string, t *Theme, presetName string, overrides map[string]string) map[string]string

ResolveTokens merges light-mode tokens through the 4-layer cascade. Order (last wins): defaults → theme.Tokens → preset.Tokens → overrides.

func SuggestToken

func SuggestToken(unknown string, known map[string]bool) string

SuggestToken returns the closest known token name if the edit distance is ≤ 2.

func ValidateOverrides

func ValidateOverrides(label string, overrides map[string]string, known map[string]bool) error

ValidateOverrides checks that all keys in overrides are known token names. Returns an error listing unknown tokens with did-you-mean suggestions.

Types

type Preset

type Preset struct {
	Name       string            `yaml:"name"`
	Tokens     map[string]string `yaml:"tokens"`
	DarkTokens map[string]string `yaml:"dark_tokens"`
}

Preset represents a theme preset variation (e.g., "ocean", "forest").

type Theme

type Theme struct {
	Name        string            `yaml:"name"`
	Slug        string            `yaml:"slug"`
	Version     string            `yaml:"version"`
	Author      string            `yaml:"author"`
	Description string            `yaml:"description"`
	License     string            `yaml:"license"`
	Tokens      map[string]string `yaml:"tokens"`
	DarkTokens  map[string]string `yaml:"dark_tokens"`
	Presets     map[string]Preset `yaml:"presets"`
}

Theme represents a loaded theme with its configuration and token definitions.

func LoadFromDir

func LoadFromDir(dir string) (*Theme, error)

LoadFromDir loads a theme from a filesystem directory. Returns (nil, nil) if theme.yaml does not exist.

func LoadFromFS

func LoadFromFS(fsys fs.FS, path string) (*Theme, error)

LoadFromFS loads a theme from an fs.FS (typically the embedded filesystem). path is the directory within the FS containing theme.yaml. Returns (nil, nil) if theme.yaml does not exist (zero-config behavior).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL