Demo content from demo/developer-guide (4d096b5)

This commit is contained in:
f451-admin 2026-10-06 08:46:05 +02:00
parent 619ecadaa6
commit 893d29d13e
2 changed files with 161 additions and 0 deletions

159
theme-templates/index.md Normal file
View file

@ -0,0 +1,159 @@
---
id: theme-templates
title: Theme templates
description: How theme templates, switches, cross-token rules and theme stylesheets work in the code, and how to add a built-in template.
tags: [architecture, theming, howto]
lang: 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** (12): how a block is built — `table-style`, `callout-style`,
`chip-style`, …
- **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`, the root layout writes them on `<html>`. The settings page's
preview carrier does the same on the preview element.
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.
```css
[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.
1. **Mockup.** Add the HTML mockup (or a theme CSS) and a line to `SOURCES` in
`scripts/themes-from-mockups.ts` (`slug`, `name`, `path`). The script reads
the token **values** from the file.
2. **`SWITCHES` entry.** Construction cannot be read from values: add the slug
to `SWITCHES` with **all 17 switches** set explicitly, defaults included
(so a copy of the template shows every switch). Take shared values from
`SHARED`; the five frame switches differ per template.
If the mockup sets the lower `--space-*` steps above the defaults,
`keepScaleMonotonic` raises the higher ones — check the console output for
`adjusted` and `dropped` lines.
3. **Generate:**
`pnpm --filter @f451/design-tokens themes:from-mockups`. It writes
`packages/design-tokens/themes/<slug>.yaml` and
`src/builtin-themes.data.ts`. Do not edit either by hand.
4. **Tests** in `packages/design-tokens/test/builtin-themes.test.ts`: add the
slug to `SLUGS`, 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.
## 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)` in `packages/design-tokens/src/stylesheet.ts`
is a small tokenizer, not a CSS parser: it skips comments and strings, reads
every `url(…)` (quoted or not, escapes decoded) and the forbidden keywords.
It returns `{ ok, problems[] }`, each problem with `code` (`css_import`,
`css_url`, `css_forbidden`), `line` and `message`. Size (`css_too_large`) and
empty or non-text bodies (`css_not_text`) are checked by the callers on bytes.
`rewriteFontUrls(css, base)` points `url(fonts/<name>.woff2)` at the font
route.
- **Routes** (`apps/api/src/routes/theme-stylesheet.ts`): public GETs
`/api/theme/stylesheet` and `/api/spaces/:space/theme/stylesheet`
(`text/css`, `nosniff`, ETag = blob sha, 304) and `…/theme/fonts/:name`; `PUT`
and `DELETE` on the stylesheet routes commit with the **caller's own provider
token** and the theme write gates, `422` with `errors[]` 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 bytes `wOF2`,
1 MB each, 4 MB per repository.
- **Cache and webhook.** `theme/stylesheet-cache.ts` keeps 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.css` or
`_meta/fonts/`.
- **Layout.** `GET /api/theme/resolved` returns `stylesheets: string[]` — the
URLs in order (instance, then space, `?v=<sha>` for cache busting), only for
valid files. The root layout links them after `F451_CUSTOM_STYLESHEET`. The
query `?ohne-stylesheet` on the appearance page leaves them out for that
view.
No design check applies to the file: no contrast measurement, no `checkRules`.