9.2 KiB
| id | title | description | tags | lang | |||
|---|---|---|---|---|---|---|---|
| theme-templates | Theme templates | How theme templates, switches, cross-token rules and theme stylesheets work in the code, and how to add a built-in template. |
|
en |
Theme templates
The operator's view of theming (files, use:, settings page) is in the
Admin Guide, page Theming. This page is for the code behind it:
packages/design-tokens (catalog, resolver, rules, stylesheet check),
apps/api (routes, caches) and apps/web (settings page, layout).
Layers and use
A theme is resolved token by token and per mode (resolveTheme in
packages/design-tokens/src/theme.ts) from a list of layers:
default ← instance ← space ← user
Array order is the precedence; a token a later layer leaves out is inherited.
Each layer remembers where a value came from (origin: source plus the
template slug if any), which is what the origin marks on the settings page
show.
use: never reaches the resolver. The caller turns a template into the
values of its own layer before mixing: the file's own values lie on top of
the template's, the result is one layer with template set. That is why a
template cannot cycle: one level deep is enforced when the file is parsed
(parseThemeFile(…, { allowUse: false }) for template files), and the
resolver has no loop and no lookup by name. use: <slug> names a template of
the layer's own repository, use: instance/<slug> one of the instance (only
in a space theme).
Switches
A switch is a catalog token with range: { kind: 'choice', values: […] } and
emit: 'attribute'. Two groups, both in catalog.ts:
- Bausteine (13): how a block is built —
table-style,callout-style,chip-style, …, andmarginalia(margin|list): footnotes as margin notes or as an end list. Built-ins:marginfor Editorial, Fokus and Klar & Warm;listfor System / Raster, Werkbank and Rotecodefraktion. - Rahmen (5): the page frame —
topbar,page-head,pane-controls,rail-scroll,status-bar. Several change markup, not only styling.
A switch creates no CSS variable. toAttributes(resolved) returns a
data-<name> attribute for every switch whose value differs from Editorial
(the first value of values); GET /api/theme/resolved delivers them as
attributes (only the deviations) and as switches (the full set, which the
Server Components read for the frame); the root layout writes attributes on
<html>. The settings page's preview carrier does the same on the preview
element.
Migration. Files from before 1.2.5 are read in the new form:
heading-number: counter(sec) '.' becomes numeral, and heading-number-sub
becomes heading-depth, each with the warning value_migrated; the next save
writes the new form.
Note tokens. layout-note-w (10–20 rem) and layout-note-gap (0–2 rem)
were locked from 1.2.5 until margin notes arrived in 1.2.8
(#63); they are
corridor tokens again and only take effect with marginalia: margin.
The stylesheets branch on the attribute and follow one pattern: one rule per
value sets private --_… custom properties on the carrier; the block
reads them with the Editorial value as fallback.
[data-button-primary='accent'] {
--_btn-p-bg: var(--color-accent);
--_btn-p-fg: var(--color-accent-contrast);
}
.btn-primary {
background: var(--_btn-p-bg, var(--color-text));
color: var(--_btn-p-fg, var(--color-bg));
}
The fallback is why a page with no attribute (the default) needs no extra
rule. See app/styles/40-schaltflaeche.css and 43-flaeche.css. Switches
that change markup (the Rahmen group) are read by the Server Components from
the same resolved set, which is why they show after saving and not in the
program preview.
Cross-token rules: checkRules
checkRules(resolved) in packages/design-tokens/src/rules.ts checks the
resolved set, never a single file — a space file that only sets
--measure-wide must pass against the --measure it inherits. It returns
RuleViolation[] (rule, tokens, message, with both tokens and their
layers named). The rules:
| Rule | Meaning |
|---|---|
space-monotonic |
the --space-* scale does not decrease |
weight-gap |
--weight-strong at least 100 above --weight-text |
measure-order |
--measure does not exceed --measure-wide |
pane-controls-needs-topbar |
--pane-controls: topbar needs --topbar: on |
A value checkRules cannot read (calc(), var(), mixed units) is left
alone. Saving refuses a violation; reading falls back (the pane controls to
edges).
Add a built-in template
Built-in templates are generated, not written by hand.
- Mockup. Add the HTML mockup (or a theme CSS) and a line to
SOURCESinscripts/themes-from-mockups.ts(slug,name,path). The script reads the token values from the file. SWITCHESentry. Construction cannot be read from values: add the slug toSWITCHESwith all 18 switches set explicitly, defaults included (so a copy of the template shows every switch). Take shared values fromSHARED; the five frame switches differ per template. If the mockup sets the lower--space-*steps above the defaults,keepScaleMonotonicraises the higher ones — check the console output foradjustedanddroppedlines.- Generate:
pnpm --filter @f451/design-tokens themes:from-mockups. It writespackages/design-tokens/themes/<slug>.yamlandsrc/builtin-themes.data.ts. Do not edit either by hand. - Tests in
packages/design-tokens/test/builtin-themes.test.ts: add the slug toSLUGS, and add a line for it to carries the construction of its mockup and carries the frame of its mockup. The per-template tests (parses without errors or warnings, contrast check, passes the cross-token rules on the resolved set, sets every switch) run for it automatically. The count of templates is also fixed elsewhere (the instance library in the api tests, the settings list); let the test run tell you where.
Built-ins cannot be overwritten or deleted through the api; their slugs are reserved.
Adopting and saving templates in the settings page
The pure functions live in apps/web/lib/theme-editor.ts, so they are testable
without the page:
adoptTemplate(draft, template)replaces the draft'sbase,lightanddarkvalues with the template's.usestays, so the template remains selected; it works for built-in and own templates alike.embedTemplate(draft, template)merges the template under the draft's own values and dropsuse— a template saved from "Fokus plus two colours" really contains Fokus.templateFile(draft, name, template)builds the file for Save as template …:embedTemplateif a template is chosen, thenname, with nouseand nobrand.templateState(data, draft, templates)says what applies, for the select and the line below it: the draft's own template, "Own settings (no template)", and "Underneath" — template "X" (instance), own settings (instance), or the Editorial default, taken fromdata.belowLayers.
Theme stylesheet
A scope (instance or space) may have _meta/theme.css and
_meta/fonts/*.woff2. No pointer in theme.yaml: file present means active.
- Check.
checkStylesheet(text)inpackages/design-tokens/src/stylesheet.tsis a small tokenizer, not a CSS parser: it skips comments and strings, reads everyurl(…)(quoted or not, escapes decoded) and the forbidden keywords. It returns{ ok, problems[] }, each problem withcode(css_import,css_url,css_forbidden),lineandmessage. Size (css_too_large) and empty or non-text bodies (css_not_text) are checked by the callers on bytes.rewriteFontUrls(css, base)pointsurl(fonts/<name>.woff2)at the font route. - Routes (
apps/api/src/routes/theme-stylesheet.ts): public GETs/api/theme/stylesheetand/api/spaces/:space/theme/stylesheet(text/css,nosniff, ETag = blob sha, 304) and…/theme/fonts/:name;PUTandDELETEon the stylesheet routes commit with the caller's own provider token and the theme write gates,422witherrors[]on a violation. The space GET checks read access itself and answers 404 for no file and no access alike; there is no fallback to the instance file. - Reading from the repo (
apps/api/src/theme/stylesheet.ts): the same check; a file that fails is ignored with a log warning, the state tells the settings page why. Fonts: name[a-z0-9-]{1,40}.woff2, magic byteswOF2, 1 MB each, 4 MB per repository. - Cache and webhook.
theme/stylesheet-cache.tskeeps one entry per scope for 5 minutes; saving through the api invalidates it, and the push webhook (routes/webhooks.ts) does when a push touches_meta/theme.cssor_meta/fonts/. - Layout.
GET /api/theme/resolvedreturnsstylesheets: string[]— the URLs in order (instance, then space,?v=<sha>for cache busting), only for valid files. The root layout links them afterF451_CUSTOM_STYLESHEET. The query?ohne-stylesheeton the appearance page leaves them out for that view.
No design check applies to the file: no contrast measurement, no checkRules.