# Design tokens

`theme.tokens` loads a [W3C Design Tokens (DTCG)](https://tr.designtokens.org/format/) `.json` file
or a CSS variables `.css` file into the `theme` section of `recit.config` (`.ts`, `.js`, `.mjs` or
`.json`). The path is resolved relative to the configuration file: the config loader adds it to the
config's dependencies and the deck dev server reloads it on change; for a film, a webstory or a
book, restart `recit dev` after changing the file.

```json title="tokens.json"
{
  "$description": "Lorem ipsum design tokens.",
  "color": {
    "$type": "color",
    "background": { "$value": "#ffffff" },
    "ink": { "$value": "#1a1d24" },
    "action": { "$value": "#2563eb" },
    "accent": { "$value": "{color.spot.one}" },
    "spot": {
      "one": { "$value": "#0d9488" },
      "two": { "$value": "#d97706" }
    }
  },
  "font": {
    "$type": "fontFamily",
    "body": { "$value": ["Lorem Sans", "system-ui", "sans-serif"] },
    "heading": { "$value": "Lorem Display, serif" }
  },
  "radius": {
    "$type": "dimension",
    "sm": { "$value": "4px" },
    "lg": { "$value": "8px" }
  },
  "shadow": {
    "$type": "shadow",
    "card": {
      "$value": {
        "color": "rgba(26, 29, 36, 0.08)",
        "offsetX": "0px",
        "offsetY": "2px",
        "blur": "8px"
      }
    }
  }
}
```

```ts title="recit.config.ts"
import { defineConfig } from "recit/config";

export default defineConfig({
  colorMode: "light",
  theme: {
    tokens: "./tokens.json",
    colors: { primary: "#1d4ed8" },
  },
});
```

## Precedence and light mode

- **Any key written in `theme` overrides the token.** `theme.colors`, `theme.dark` and `theme.fonts`
  win key by key over the mapped tokens (`colors.primary` above replaces `color.action` while
  `color.background`, `color.ink` and `color.accent` still come from `tokens.json` or `tokens.css`),
  and `theme.radius` overrides a mapped radius token.
- **Without dark tokens in the token file, set `colorMode: "light"`.** Stories open in dark mode by
  default (`colorMode: "dark"`): when `tokens.json` (no root `dark` group) or `tokens.css` (no dark
  selector or media block) carries only light tokens, set `colorMode: "light"` beside `theme:` so
  decks and other stories open in light mode.

## Token mapping (`DESIGN_TOKEN_MAP`)

For each `theme` key, Récit checks the token paths below in order (`DESIGN_TOKEN_MAP` from
`@recit/core`) and picks the first match whose `$type` fits:

| Design token path (first match wins)                                                                                                                                 | Key of `theme`                   | Expected `$type` |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | ---------------- |
| `color.primary`, `color.action`, `color.brand`                                                                                                                       | `theme.colors.primary`           | `color`          |
| `color.primaryForeground`, `color.primary-foreground`, `color.onPrimary`, `color.on-primary`, `color.onAction`, `color.on-action`, `color.onBrand`, `color.on-brand` | `theme.colors.primaryForeground` | `color`          |
| `color.accent`, `color.secondary`                                                                                                                                    | `theme.colors.accent`            | `color`          |
| `color.accentForeground`, `color.accent-foreground`, `color.onAccent`, `color.on-accent`, `color.onSecondary`, `color.on-secondary`                                  | `theme.colors.accentForeground`  | `color`          |
| `color.background`, `color.surface.0`, `color.canvas`, `color.bg`                                                                                                    | `theme.colors.background`        | `color`          |
| `color.foreground`, `color.text`, `color.ink`                                                                                                                        | `theme.colors.foreground`        | `color`          |
| `color.card`, `color.surface`, `color.surface.1`                                                                                                                     | `theme.colors.card`              | `color`          |
| `color.cardForeground`, `color.card-foreground`, `color.onCard`, `color.on-card`, `color.onSurface`, `color.on-surface`                                              | `theme.colors.cardForeground`    | `color`          |
| `color.border`, `color.stroke`, `color.line`, `color.divider`                                                                                                        | `theme.colors.border`            | `color`          |
| `color.muted`, `color.subtle`, `color.surface.2`                                                                                                                     | `theme.colors.muted`             | `color`          |
| `color.mutedForeground`, `color.muted-foreground`, `color.textMuted`, `color.text-muted`, `color.inkMuted`, `color.ink-muted`                                        | `theme.colors.mutedForeground`   | `color`          |
| `dark.<color path>` (`dark.color.primary`, `dark.color.action`, `dark.color.background`, `dark.color.ink`…)                                                          | `theme.dark.<key>`               | `color`          |
| `radius.lg`, `radius.default`, `radius`, `borderRadius.lg`, `borderRadius.default`, `borderRadius`                                                                   | `theme.radius`                   | `dimension`      |
| `font.sans`, `font.body`, `font.family.sans`, `font.family.body`, `fontFamily.sans`, `fontFamily.body`, `font`                                                       | `theme.fonts.sans`               | `fontFamily`     |
| `font.heading`, `font.display`, `font.family.heading`, `font.family.display`, `fontFamily.heading`, `fontFamily.display`                                             | `theme.fonts.heading`            | `fontFamily`     |
| `font.mono`, `font.code`, `font.family.mono`, `font.family.code`, `fontFamily.mono`, `fontFamily.code`                                                               | `theme.fonts.mono`               | `fontFamily`     |
| `font.lettering`, `font.family.lettering`, `fontFamily.lettering`                                                                                                    | `theme.fonts.lettering`          | `fontFamily`     |

Nested groups inherit `$type`, `{group.token}` aliases (including chained aliases) are resolved with
cycle detection, `dimension` tokens accept CSS length strings (`"8px"`, `"0.5rem"`) or
`{ value, unit }` objects (`px` or `rem`), and `fontFamily` tokens accept a CSS font-family string
or an array of family names.

In TypeScript, `designTokensFromCss(css, { file })`, `themeFromDesignTokens(json, { map })` and
`mergeThemeTokens(theme, tokens)` are also exported from `@recit/core` when converting CSS tokens or
using a custom mapping table, and `themeFromTokensSource(source, path)` reads the text of a
`theme.tokens` file, CSS or JSON by its extension, as `recit` and the site framework do.

## CSS variables

When `theme.tokens` ends with `.css`, Récit parses the stylesheet with `designTokensFromCss` into
the equivalent DTCG structure and maps it through the same `DESIGN_TOKEN_MAP` table:

- **Name correspondence.** Custom properties declared on `:root` or `html` (including
  `:where(:root)` and rules inside `@layer`) map hyphenated names to `DESIGN_TOKEN_MAP` paths and
  infer `$type` (`color`, `dimension` or `fontFamily`) from the matched row:
  - Colors: `--color-primary` → `color.primary`, `--color-action` → `color.action`,
    `--color-on-primary` → `color.onPrimary`, `--color-primary-foreground` →
    `color.primaryForeground`, `--color-accent` → `color.accent`, `--color-on-accent` →
    `color.onAccent`, `--color-background` / `--color-surface-0` / `--color-bg` → `color.background`
    / `color.surface.0` / `color.bg`, `--color-foreground` / `--color-text` / `--color-ink` →
    `color.foreground` / `color.text` / `color.ink`, `--color-card` / `--color-surface` /
    `--color-surface-1` → `color.card` / `color.surface` / `color.surface.1`, `--color-on-card` /
    `--color-on-surface` → `color.onCard` / `color.onSurface`, `--color-border` / `--color-stroke` →
    `color.border` / `color.stroke`, `--color-muted` / `--color-subtle` / `--color-surface-2` →
    `color.muted` / `color.subtle` / `color.surface.2`, `--color-text-muted` /
    `--color-muted-foreground` → `color.textMuted` / `color.mutedForeground`.
  - Radius: `--radius-lg` → `radius.lg`, `--radius-default` → `radius.default`, `--radius` →
    `radius`, `--border-radius-lg` → `borderRadius.lg`, `--border-radius` → `borderRadius`.
  - Fonts: `--font-sans` / `--font-body` / `--font-family-sans` / `--font` → `font.sans` /
    `font.body` / `font.family.sans` / `font`, `--font-heading` / `--font-display` → `font.heading`
    / `font.display`, `--font-mono` / `--font-code` → `font.mono` / `font.code`, `--font-lettering`
    → `font.lettering`.
- **Dark mode.** Custom properties declared inside `[data-mantine-color-scheme="dark"]` (including
  `:where(:root[data-mantine-color-scheme="dark"])`), `.dark`, `[data-theme="dark"]`, or `:root` /
  `html` inside `@media (prefers-color-scheme: dark)` go into the root `dark` group
  (`dark.color.*`), filling `theme.dark` just like DTCG JSON.
- **`var()` aliases.** A value `var(--x)` that points to another custom property in the file
  (including an unmapped primitive such as `--palette-blue: #2563eb`) becomes a DTCG `{a.b}` alias
  (`{palette.blue}`), with chained alias resolution and cycle detection.
- **Unknown properties and errors.** Unmapped custom properties not referenced by a mapped token are
  ignored. An empty custom property value or unreadable CSS throws an `Error` naming the file and
  line number.

```css title="tokens.css"
:root {
  --palette-teal: #0d9488;
  --color-background: #ffffff;
  --color-ink: #1a1d24;
  --color-action: #2563eb;
  --color-accent: var(--palette-teal);
  --radius-lg: 8px;
  --font-body: "Lorem Sans", system-ui, sans-serif;
  --font-heading: "Lorem Display", serif;
}

[data-mantine-color-scheme="dark"] {
  --color-background: #0b1020;
  --color-ink: #f9fafb;
  --color-action: #60a5fa;
}
```

```ts title="recit.config.ts"
import { defineConfig } from "recit/config";

export default defineConfig({
  theme: {
    tokens: "./tokens.css",
  },
});
```

## In a site

`theme.tokens` also works in a site's `recit.config.ts` (`@recit/site`). The site config loader
resolves the path relative to the config file, watches it for reloads in `dev`, and passes the
resolved `theme` to the client through the site config while stripping `tokens` so the file path
never enters the client bundle:

- **Colors and radius.** Mapped light colors fill `theme.light` and the root `dark` group fills
  `theme.dark` for the closed list of site token keys (`background`, `foreground`, `card`,
  `cardForeground`, `primary`, `primaryForeground`, `muted`, `mutedForeground`, `border`), while a
  mapped `radius` token fills `theme.light.radius`. `accent` (and `accentForeground`) from tokens
  does not go to the site, because `--accent` in `@recit/site` is the pale hover surface rather than
  a vivid spot color. Any key written directly in `theme.light` or `theme.dark` wins over the token.
- **Light-only token files.** When `tokens.json` has no `dark` group (or `tokens.css` has no dark
  block), set `site: { colorMode: "light" }` for a light-only site; top-level `colorMode` in
  `recit.config` applies to stories, not to the site.
- **Fonts are not copied.** A site's `theme.fonts` requires a stylesheet `url` (or a predefined
  Google Fonts name) to load the font files, so `fontFamily` tokens from `tokens.json` or
  `tokens.css` are ignored by the site loader; configure site fonts in `theme.fonts` or `@import`
  them in `styles.css`.

## What is not read

- **Spot and chart colors.** Extra palette entries such as `color.spot.*` are validated when typed
  as `color`, and ignored when mapping to `theme`: chart role and scale colors live under
  [`theme.chart`](/theme/colors#chart-colors). `shadow` tokens and unmapped dimensions (such as
  `radius.sm`) are likewise validated and ignored.
- **Object color values.** DTCG structured color objects (`{ colorSpace, components }`) are not
  read; write colors as CSS strings (hex, `rgb()`, `hsl()` or `oklch()`).
