Get started
Documentation menu

Guides

Design tokens

Let editors change colours, typefaces and corners, with the real site as the preview.

With siteTools.design, the CMS gets a Website design editor: one control per token, contrast warnings, and a preview of your real site rendered with the unsaved values. Nothing changes for visitors until the tokens are saved and the site is rebuilt.

This website uses it: every colour of the night and day themes, both typefaces, three corner radii and the page width are tokens.

1. A token module

Write one module that lists your tokens. The editor builds its controls, the schema and the CSS from that list. Its exports:

Export What it is
DESIGN_TOKENS The list. Each token has a key (its name in the file), css (the custom property, such as --brand), type (color, font or length), group, label, help and default; a length also has min and max.
DESIGN_GROUPS The groups the editor shows, by key: { brand: 'Brand colours', … }.
DESIGN_FONTS The typefaces a font token can choose, by key: { label, stack }.
designSchema A Zod schema of all tokens, each with its default.
DEFAULT_DESIGN Every token’s default.
parseDesignFile(data) The tokens from the saved file’s data ({ tokens: { … } }), falling back to the defaults.
contrastIssues(design) The pairs of colours whose contrast is too low, for the editor’s warnings (label, ratio, min).
DESIGN_URL Where the deployed site serves the saved tokens as JSON, for example /admin/design.json.
DESIGN_FILE The tokens’ file, for example src/content/settings/design.yml.

The quickest start is to copy this website’s module and change the list. Build CSS only from validated values (hex colours, whole numbers in a range, names from fixed lists), so token data can never inject CSS.

2. Turn the editor on

// astro.config.mjs
visualEditor({
  siteTools: { design: './src/lib/design-tokens.ts' },
});

Add the file to Sveltia, with the design-tokens field type:

- name: settings
  label: Site settings
  files:
    - name: design
      label: Website design
      file: src/content/settings/design.yml
      format: yaml
      editor:
        preview: false
      fields:
        - { name: tokens, label: Design tokens, widget: design-tokens }

3. Use the tokens

  • In your layout, write the tokens as custom properties on :root, and take unsaved ones from the design prop the preview passes:

    ---
    // designCss turns tokens into `html:root { --brand: …; }`; getDesign reads the saved file.
    // Both are your own helpers (see this website's module and src/lib/content.ts).
    const tokensCss = designCss(Astro.props.design ?? (await getDesign()));
    ---
    <style is:inline set:html={tokensCss}></style>
  • In your CSS, use only var(--brand), var(--radius) and so on, and declare every token’s default in your global stylesheet, so the site looks right before the tokens load.

  • Serve the saved tokens at DESIGN_URL, so the CMS can read them on the deployed site:

    // src/pages/admin/design.json.ts
    export const GET: APIRoute = async () => new Response(JSON.stringify({ tokens: await getDesign() }), { headers: { 'content-type': 'application/json' } });

CMS styling

siteTools.brand (it needs design) adds CMS styling: the CMS’s name, logo, sign-in text and a theme, including one made from your website’s tokens. Sveltia plans theming of its own; this option goes away when that ships.