@designtools/tokens 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +336 -116
- package/dist/{chunk-757P7AQA.js → chunk-MCLZSLHE.js} +389 -102
- package/dist/cli.js +201 -13
- package/dist/index.d.ts +110 -10
- package/dist/index.js +15 -1
- package/package.json +37 -3
package/README.md
CHANGED
|
@@ -1,26 +1,208 @@
|
|
|
1
1
|
# @designtools/tokens
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**One brand colour in, a complete design-token system out.** A CLI that turns `#4e9caf` into ~220 CSS custom properties — colour ramps, semantic intents, surfaces, borders — in light and dark mode, ready to drop into a project.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```
|
|
6
|
+
npx @designtools/tokens --primary "#4e9caf" --out tokens.css
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Not a palette generator with extra steps. Most tools rotate hue and step lightness in HSL, then leave contrast to you — which is how you end up with ramps that look even in a swatch grid and fall apart in a UI.
|
|
10
|
+
|
|
11
|
+
This one works in **OKLCH**, so equal steps are equal *perceptual* steps and the ramps stay balanced without hand-tweaking. Lightness is a fraction of the headroom either side of your colour rather than a fixed position, so a pale tint and a near-black both get a usable scale. Chroma follows its own easing curve, so light tints stay clean instead of going muddy while the interactive range stays saturated.
|
|
12
|
+
|
|
13
|
+
And **every foreground is measured, not assumed.** For each background it generates, it walks the scale computing real contrast — WCAG or APCA — and picks the shade closest to a comfortable target among those clearing your floor. `--report` prints what it achieved, so accessibility is something you can assert in CI rather than hope for:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
light mode (wcag, target aa)
|
|
17
|
+
AA primary/default 5.12:1
|
|
18
|
+
- neutral/default 4.30:1
|
|
19
|
+
score: 93% (14/15 pairings)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
No config file, no install, no account, no model. Pure arithmetic — the same input always produces byte-identical output. [Why the output holds up](#why-the-output-holds-up) has the detail.
|
|
23
|
+
|
|
24
|
+
## What it generates
|
|
25
|
+
|
|
26
|
+
From a single seed colour:
|
|
27
|
+
|
|
28
|
+
**Colour ramps** — a twelve-step OKLCH scale (`25, 50, 100 … 900, 950`) for each colour. `primary` and `neutral` come from your seeds; `success`/`green`, `destructive`/`red` and `warning`/`yellow` are derived from the primary so they sit in the same colour world rather than clashing with it.
|
|
29
|
+
|
|
30
|
+
```css
|
|
31
|
+
--color-primary-25: oklch(97.34% 0.0065 215.79);
|
|
32
|
+
--color-primary-500: oklch(65.07% 0.0815 215.79); /* your seed, exactly */
|
|
33
|
+
--color-primary-950: oklch(17.50% 0.0766 215.79);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Semantic intents** — what you actually write in components. Each of `primary`, `neutral`, `success`, `destructive`, `warning` gets six slots, plus a `muted` pair:
|
|
37
|
+
|
|
38
|
+
```css
|
|
39
|
+
--primary --primary-foreground
|
|
40
|
+
--primary-subdued --primary-subdued-foreground
|
|
41
|
+
--primary-highlight --primary-highlight-foreground
|
|
42
|
+
```
|
|
6
43
|
|
|
7
|
-
|
|
44
|
+
Every `-foreground` is chosen by **contrast search**, not a fixed mapping — the generator measures each candidate against its background and picks one that clears your target (WCAG AA by default, or APCA).
|
|
8
45
|
|
|
9
|
-
|
|
46
|
+
**Surfaces and utilities** — `--canvas`, `--surface`, `--overlay` with paired foregrounds, plus `--border`, `--input`, `--ring`, `--shadow`.
|
|
10
47
|
|
|
48
|
+
**Light and dark**, derived independently from the same ramps rather than one being a transform of the other, with contrast re-checked per mode. Output is keyed on `[data-theme]` with a `prefers-color-scheme` fallback, so it works with or without a theme toggle:
|
|
49
|
+
|
|
50
|
+
```css
|
|
51
|
+
:root { /* the ramps */ }
|
|
52
|
+
:root, [data-theme="light"] { /* light intents */ }
|
|
53
|
+
[data-theme="dark"] { /* dark intents */ }
|
|
54
|
+
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { … } }
|
|
11
55
|
```
|
|
12
|
-
|
|
13
|
-
|
|
56
|
+
|
|
57
|
+
## Try it
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# print CSS custom properties to stdout
|
|
61
|
+
npx @designtools/tokens --primary "#4e9caf"
|
|
62
|
+
|
|
63
|
+
# write a file
|
|
64
|
+
npx @designtools/tokens --primary "#4e9caf" --out src/tokens.css
|
|
65
|
+
|
|
66
|
+
# a Tailwind v4 theme
|
|
67
|
+
npx @designtools/tokens --primary "#4e9caf" --format tailwind-v4 --out src/theme.css
|
|
68
|
+
|
|
69
|
+
# add your own scales
|
|
70
|
+
npx @designtools/tokens --primary "#4e9caf" --color accent=#8b5cf6 --color info=#0ea5e9
|
|
71
|
+
|
|
72
|
+
# just the ramps, as JSON
|
|
73
|
+
npx @designtools/tokens --primary "#4e9caf" --only ramp --format json
|
|
74
|
+
|
|
75
|
+
# every format at once
|
|
14
76
|
npx @designtools/tokens --primary "#4e9caf" --format all --out ./tokens
|
|
15
77
|
```
|
|
16
78
|
|
|
17
|
-
|
|
79
|
+
Any CSS colour works as a seed: `#4e9caf`, `rgb(78 156 175)`, `hsl(193 39% 50%)`, `oklch(65% 0.08 216)`, `rebeccapurple`.
|
|
80
|
+
|
|
81
|
+
## Why the output holds up
|
|
82
|
+
|
|
83
|
+
### Lightness adapts to the colour you gave it
|
|
84
|
+
|
|
85
|
+
Steps are a fraction of the *headroom* above and below your seed rather than fixed positions, so a pale tint and a near-black both get a usable ramp instead of one end collapsing.
|
|
86
|
+
|
|
87
|
+
Chromatic and achromatic seeds get different curves — anything under 0.01 chroma counts as achromatic. Colour gets eased transitions at the extremes and vivid mid-tones; greys get a flatter spread tuned for surface layering and text contrast.
|
|
88
|
+
|
|
89
|
+
### Chroma has its own curve
|
|
90
|
+
|
|
91
|
+
Held near-neutral at the light end so tints stay clean, at full strength through the interactive range. Measured on `#4e9caf`:
|
|
92
|
+
|
|
93
|
+
| shade | 25 | 100 | 300 | 400 | 500–800 | 900 | 950 |
|
|
94
|
+
|---|---|---|---|---|---|---|---|
|
|
95
|
+
| chroma vs. seed | 8% | 38% | 87% | 96% | **100%** | 96% | 94% |
|
|
96
|
+
|
|
97
|
+
Hue is held constant across the whole scale — no Bezold–Brücke correction, no artistic rotation — so shade 900 is unmistakably the same colour as shade 100.
|
|
98
|
+
|
|
99
|
+
### Foregrounds are searched, not mapped
|
|
100
|
+
|
|
101
|
+
For every background it generates, the tool computes real contrast against each candidate shade and picks the one **closest to a comfortable target among those clearing your floor** — not the first that scrapes past, which would hand you the least legible passing option every time. If nothing in the colour's own scale qualifies it falls back to the neutral scale, then to black or white.
|
|
102
|
+
|
|
103
|
+
- **WCAG 2.x** (default) — `(L₁ + 0.05) / (L₂ + 0.05)`, with floors of 3:1 (`--target aa-large`), 4.5:1 (`aa`) and 7:1 (`aaa`). Widely adopted and legally referenced, but it treats dark-on-light and light-on-dark identically and has no concept of text size.
|
|
104
|
+
- **APCA** (`--contrast apca`) — models how contrast is actually perceived, so the same two colours score differently depending on which one is the text. Bands at Lc 45 (large text), Lc 60 (larger content), Lc 75 (body text), Lc 90 (fluent reading).
|
|
105
|
+
|
|
106
|
+
### Dark mode is derived, not inverted
|
|
107
|
+
|
|
108
|
+
Both modes come from the same ramps via different shade mappings, and the contrast search re-runs for each — so a dark foreground is chosen against dark backgrounds rather than inherited from a flipped light palette.
|
|
109
|
+
|
|
110
|
+
### Status colours belong to your palette
|
|
111
|
+
|
|
112
|
+
Success, destructive and warning are derived from your primary rather than bolted on: the generator reads the primary's chroma and lightness and builds them at matching energy on fixed hues (145°, 25°, 75°), clamped so red still reads as danger and amber still reads as caution. They sit in your colour world instead of looking borrowed from Bootstrap.
|
|
113
|
+
|
|
114
|
+
## For AI agents
|
|
115
|
+
|
|
116
|
+
This is deliberately a **dumb, deterministic tool** — no model, no network, no hidden state — which is what makes it safe to hand to an agent:
|
|
117
|
+
|
|
118
|
+
- **Deterministic.** Same flags always produce the same bytes, so a re-run is a no-op and a diff means something really changed.
|
|
119
|
+
- **stdout by default**, `--out` to write. Pipes cleanly.
|
|
120
|
+
- **Fails loudly.** Unknown values are rejected with the valid set, rather than silently ignored.
|
|
121
|
+
- **`--check` for CI.** Regenerates in memory, compares against the file on disk, exits non-zero on drift — no temp dirs, no diffing ritual.
|
|
122
|
+
- **`--report` for a11y assertions.** Prints the measured contrast of every pairing with a score.
|
|
123
|
+
- **`--ramps` + `--merge` for partial regeneration.** Change one colour in a file a human has also edited, without rewriting their work. See [Regenerating part of a token file](#regenerating-part-of-a-token-file).
|
|
124
|
+
- **`--only` for tier-level slicing.** Keep generated colour in a different file from anything hand-tuned.
|
|
125
|
+
- **`--seed` / `--stdin`** to replay a whole configuration from JSON instead of assembling flags.
|
|
126
|
+
|
|
127
|
+
Generated files carry a provenance header naming the version and a hash of the inputs, so an agent can tell "the generator was upgraded" apart from "a human edited this":
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
/* Generated by @designtools/tokens v0.4.0 (seed e06984b1) - do not edit by hand */
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# in CI: fail if tokens.css no longer matches its inputs
|
|
135
|
+
npx @designtools/tokens --primary "#4e9caf" --out tokens.css --check
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
check: tokens.css up to date
|
|
140
|
+
check: tokens.css differs — seed e06984b1 on disk, 7b5a14d2 now. The inputs changed; regenerate.
|
|
141
|
+
check: tokens.css differs — same generator and seed, so the file was edited by hand.
|
|
142
|
+
check: tokens.css differs — generated by v0.4.0, running v0.5.0. Expected after a generator upgrade.
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Regenerating part of a token file
|
|
146
|
+
|
|
147
|
+
The problem this solves: an agent asked to "change the reds" regenerates the whole file and wipes out every hand-edit someone made to the primary.
|
|
148
|
+
|
|
149
|
+
Scope the run to a **colour family** — one colour plus everything computed against it — and merge it in:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# regenerate just the reds, in place, leaving the rest of the file alone
|
|
153
|
+
generate-tokens --primary "#4e9caf" --color destructive=#ff0000 \
|
|
154
|
+
--ramps destructive --out tokens.css --merge
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
merged into tokens.css: 33 updated, 0 added, 9 already current
|
|
159
|
+
slice references 1 token defined elsewhere:
|
|
160
|
+
--color-neutral-950
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Adding a colour works the same way, and touches nothing that already existed:
|
|
18
164
|
|
|
165
|
+
```bash
|
|
166
|
+
generate-tokens --primary "#4e9caf" --color accent=#8b5cf6 \
|
|
167
|
+
--ramps accent --out tokens.css --merge
|
|
19
168
|
```
|
|
20
|
-
|
|
169
|
+
|
|
170
|
+
### Why a family, and why merge
|
|
171
|
+
|
|
172
|
+
A family is the right unit because **the contrast search runs inside it**. Change the ramp and the foregrounds change with it — they are chosen *against* those values. Regenerate `destructive` and this happens:
|
|
173
|
+
|
|
174
|
+
| | background | foreground | ratio |
|
|
175
|
+
|---|---|---|---|
|
|
176
|
+
| before | dark red | near-white | 5.28:1 ✅ |
|
|
177
|
+
| after | brighter red | **near-black** | 4.72:1 ✅ |
|
|
178
|
+
| ramp pasted in on its own | brighter red | stale near-white | **3.66:1 ❌** |
|
|
179
|
+
|
|
180
|
+
Splice only the twelve steps and you keep a foreground that no longer passes — silently, because nothing re-checks it.
|
|
181
|
+
|
|
182
|
+
`--merge` exists for the same reason. A family is not one contiguous block: its ramp sits in `:root` and its semantics repeat in the light block, the dark block **and** the `prefers-color-scheme` fallback. That last one is the copy a hand-splice forgets, leaving system-dark on the old colour while the toggle shows the new one. `--merge` rewrites all four and leaves every other line byte-identical.
|
|
183
|
+
|
|
184
|
+
Surfaces and utilities belong to no single colour, so a family-scoped slice never touches them.
|
|
185
|
+
|
|
186
|
+
### Checking a part-generated file
|
|
187
|
+
|
|
188
|
+
A full `--check` is useless once a file has hand-edited regions — it reports drift forever. Scope the check the same way and it only compares what this run generates:
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
generate-tokens --primary "#4e9caf" --color destructive=#ff0000 \
|
|
192
|
+
--ramps destructive --out tokens.css --check
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
check: tokens.css up to date for destructive
|
|
21
197
|
```
|
|
22
198
|
|
|
23
|
-
|
|
199
|
+
So an agent's contract is *scope, merge, verify* — and a bad merge is caught rather than shipped.
|
|
200
|
+
|
|
201
|
+
### Aliases move together
|
|
202
|
+
|
|
203
|
+
`destructive`/`red`, `success`/`green` and `warning`/`yellow` are two names for one scale, so overriding either moves both. Without that, `--color destructive=…` would leave `bg-red-500` on the old colour and the two names would silently disagree inside the same file.
|
|
204
|
+
|
|
205
|
+
## Programmatic use
|
|
24
206
|
|
|
25
207
|
```ts
|
|
26
208
|
import { generateTokens, exportTokens } from "@designtools/tokens";
|
|
@@ -29,21 +211,43 @@ const tokens = generateTokens({ primary: "#4e9caf", neutral: "#9aa1af" });
|
|
|
29
211
|
const css = exportTokens(tokens, "css", { colorFormat: "oklch" });
|
|
30
212
|
```
|
|
31
213
|
|
|
214
|
+
`generateTokens` returns a structured `TokenSystem`; `exportTokens` serialises it. Everything the CLI does is available here. No browser, DOM, or React — it runs anywhere.
|
|
215
|
+
|
|
216
|
+
One option has to be passed to both: **`seedShade`**. The semantic bridge is expressed as offsets from the anchor, so an export that does not know where the seed sits emits `var()` references the resolved values in `tokens` disagree with. Pass the same value to each, as the CLI does:
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const seedShade = 800;
|
|
220
|
+
const tokens = generateTokens({ primary: "#1e3a5f" }, { seedShade });
|
|
221
|
+
const css = exportTokens(tokens, "css", { colorFormat: "oklch", seedShade });
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Output formats
|
|
225
|
+
|
|
226
|
+
| `--format` | File under `--format all` | What it is |
|
|
227
|
+
|---|---|---|
|
|
228
|
+
| `css` *(default)* | `tokens.css` | CSS custom properties. The lead format — works in any project. Mode blocks carry both `[data-theme]` attributes and `.dark`/`.light` classes, matching `tailwind-v4`. |
|
|
229
|
+
| `tailwind-v4` | `tailwind.css` | A complete `@theme static` block. See [Tailwind v4](#tailwind-v4). |
|
|
230
|
+
| `tailwind-v3` | `tailwind.config.js` | `theme.extend.colors` config. |
|
|
231
|
+
| `dtcg` | `tokens.dtcg.json` | W3C Design Token Community Group JSON. |
|
|
232
|
+
| `json` | `tokens.json` | The raw `TokenSystem`, for downstream tooling. |
|
|
233
|
+
|
|
234
|
+
`--color-format` controls how literal values are printed: `oklch` (default), `hex`, `rgb`, `hsl`.
|
|
235
|
+
|
|
32
236
|
## CLI reference
|
|
33
237
|
|
|
34
|
-
`--primary` is the only required input, and even that can come from
|
|
238
|
+
`--primary` is the only required input, and even that can come from `--seed`.
|
|
35
239
|
|
|
36
240
|
### Seeds
|
|
37
241
|
|
|
38
242
|
| Flag | Default | What it does |
|
|
39
243
|
|---|---|---|
|
|
40
|
-
| `--primary <colour>` | — |
|
|
244
|
+
| `--primary <colour>` | — | Your brand colour. Required unless `--seed` supplies it. |
|
|
41
245
|
| `--neutral <colour>` | `#747474` | Neutral seed. Drives greys, surfaces, borders. |
|
|
42
|
-
| `--color <name>=<colour>` | — | An extra named scale. Repeatable
|
|
43
|
-
| `--seed <file.json>` | — | Replay a saved configuration. Flags given alongside it win, so a seed is
|
|
44
|
-
| `--stdin` | off | Read
|
|
246
|
+
| `--color <name>=<colour>` | — | An extra named scale. Repeatable. |
|
|
247
|
+
| `--seed <file.json>` | — | Replay a saved configuration. Flags given alongside it win, so a seed is defaults rather than a lock. |
|
|
248
|
+
| `--stdin` | off | Read that same JSON from standard input. |
|
|
45
249
|
|
|
46
|
-
A seed file
|
|
250
|
+
A seed file:
|
|
47
251
|
|
|
48
252
|
```json
|
|
49
253
|
{
|
|
@@ -65,129 +269,139 @@ A seed file is a `SavedConfig` — the shape the web tool persists — so the tw
|
|
|
65
269
|
| Flag | Values | Default | What it does |
|
|
66
270
|
|---|---|---|---|
|
|
67
271
|
| `--contrast` | `wcag` · `apca` | `wcag` | Which algorithm picks foregrounds. |
|
|
68
|
-
| `--target` | `aa-large` · `aa` · `aaa` | `aa` | Minimum the foreground search
|
|
69
|
-
| `--lightness` | `fixed` · `perceptual` | `fixed` | How lightness is
|
|
70
|
-
| `--seed-shade` | `25`…`950` | `500` | Which step
|
|
272
|
+
| `--target` | `aa-large` · `aa` · `aaa` | `aa` | Minimum the foreground search must clear. |
|
|
273
|
+
| `--lightness` | `fixed` · `perceptual` | `fixed` | How lightness is spread across the steps. |
|
|
274
|
+
| `--seed-shade` | `25`…`950` | `500` | Which step your seed colour lands on. |
|
|
71
275
|
| `--shades` | `tailwind` · `full` · a list | `full` | Which steps to emit. |
|
|
72
|
-
| `--overrides <file.json>` | — | — |
|
|
276
|
+
| `--overrides <file.json>` | — | — | Per-token shade overrides. |
|
|
73
277
|
|
|
74
|
-
#### `--seed-shade` — where
|
|
278
|
+
#### `--seed-shade` — where your brand colour sits
|
|
75
279
|
|
|
76
|
-
|
|
280
|
+
Everything is derived from the headroom above and below the seed, so where it sits matters. Brand colours arrive as whatever they are: pin a deep navy to the middle of the ramp and there is almost nothing left underneath, so the dark end collapses; pin a pale tint there and the light end has nowhere to go.
|
|
77
281
|
|
|
78
|
-
```
|
|
282
|
+
```bash
|
|
79
283
|
generate-tokens --primary "#1e3a5f" --seed-shade 800 # deep navy belongs near the bottom
|
|
80
284
|
generate-tokens --primary "#fde68a" --seed-shade 200 # pale amber belongs near the top
|
|
81
285
|
```
|
|
82
286
|
|
|
83
|
-
|
|
287
|
+
Your colour is still reproduced exactly, just at the step you name. The ramp keeps its shape — gentle at the light end, larger jumps at the dark end for text contrast — and peak chroma follows the seed.
|
|
84
288
|
|
|
85
|
-
|
|
289
|
+
**The semantics follow the anchor too.** `--primary`, its subdued and highlight variants, and `--ring` are positions relative to the seed, not fixed steps — so `bg-primary` is your brand colour wherever you anchor it. Before 0.4.0 they were pinned to 500, which meant anchoring a deep navy at 800 left `--primary` pointing at a pale mid-tone the brand never chose, with no way to reach the real one through `bg-primary` at all.
|
|
290
|
+
|
|
291
|
+
`muted`, `border`, `input` and `shadow` stay where they are: they read the neutral ramp, not your brand, and re-anchoring the whole UI's greys is a different thing from placing a brand colour.
|
|
86
292
|
|
|
87
|
-
|
|
293
|
+
`--report` prints where the bridge landed, so you can check rather than infer:
|
|
88
294
|
|
|
89
295
|
```
|
|
296
|
+
semantic bridge (seed anchored at 800)
|
|
297
|
+
light default 800 subdued 300 highlight 950 ring 700
|
|
298
|
+
dark default 900 subdued 500 highlight 700 ring 950
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
#### `--shades` — which steps exist
|
|
302
|
+
|
|
303
|
+
The default is 25–950 (12 steps). Tailwind's stock ramps are 50–950 (11, no 25), and matching that makes the Tailwind override exact:
|
|
304
|
+
|
|
305
|
+
```bash
|
|
90
306
|
generate-tokens --primary "#4e9caf" --shades tailwind # 50–950
|
|
91
307
|
generate-tokens --primary "#4e9caf" --shades 100,500,900 # a coarse three-step ramp
|
|
92
308
|
```
|
|
93
309
|
|
|
94
|
-
Some tokens name specific steps — `muted` and light `canvas` both sit on 25
|
|
310
|
+
Some tokens name specific steps — `muted` and light `canvas` both sit on 25 — so dropping a step could leave a `var()` pointing at nothing. References snap to the nearest emitted step instead, and every substitution is reported, because snapping quietly changes a design decision:
|
|
95
311
|
|
|
96
312
|
```
|
|
97
|
-
|
|
98
|
-
--shades 50,100,200,300,400,500,600,700,800,900,950 drops steps some tokens name; snapped to nearest:
|
|
313
|
+
--shades 50,100,…,950 drops steps some tokens name; snapped to nearest:
|
|
99
314
|
muted (light): 25 -> 50
|
|
100
315
|
canvas (light): 25 -> 50
|
|
101
316
|
```
|
|
102
317
|
|
|
103
|
-
|
|
318
|
+
A shade set needs at least three steps, and `--seed-shade` must be one of them.
|
|
104
319
|
|
|
105
320
|
### Selection
|
|
106
321
|
|
|
107
322
|
| Flag | Values | What it does |
|
|
108
323
|
|---|---|---|
|
|
109
|
-
| `--only <sections>` | `ramp` · `primitives` · `semantic` · `surface` · `utility` | Emit only these
|
|
324
|
+
| `--only <sections>` | `ramp` · `primitives` · `semantic` · `surface` · `utility` | Emit only these tiers. Comma-separated or repeated. |
|
|
325
|
+
| `--ramps <names>` | any colour family | Emit only these colour families. See [Regenerating part of a token file](#regenerating-part-of-a-token-file). |
|
|
110
326
|
|
|
111
|
-
-
|
|
112
|
-
-
|
|
113
|
-
-
|
|
114
|
-
-
|
|
115
|
-
-
|
|
327
|
+
- **`ramp`** — the colour ramps alone. This is "hand me a colour, hand me back scales".
|
|
328
|
+
- **`primitives`** — the ramps plus `--color-black` and `--color-white`.
|
|
329
|
+
- **`semantic`** — `--primary`, `--destructive-subdued-foreground`, `--muted`, …
|
|
330
|
+
- **`surface`** — `--canvas`, `--surface`, `--overlay` and their foregrounds.
|
|
331
|
+
- **`utility`** — `--border`, `--input`, `--ring`, `--shadow`.
|
|
116
332
|
|
|
117
|
-
|
|
333
|
+
Beyond convenience, this keeps generated colour separable from files a project hand-tunes, so re-theming does not clobber them:
|
|
118
334
|
|
|
119
|
-
```
|
|
335
|
+
```bash
|
|
120
336
|
# scaffold: everything, once
|
|
121
337
|
generate-tokens --primary "#4e9caf" --out tokens.css
|
|
122
338
|
|
|
123
339
|
# re-theme: colour only, leaving hand-tuned files alone
|
|
124
340
|
generate-tokens --primary "#c0392b" --only primitives,semantic --out tokens.colour.css
|
|
125
|
-
|
|
126
|
-
# just the ramps, for a palette reference
|
|
127
|
-
generate-tokens --primary "#4e9caf" --only ramp --format json
|
|
128
341
|
```
|
|
129
342
|
|
|
130
|
-
Supported by `css`, `tailwind-v4` and `json`;
|
|
343
|
+
Supported by `css`, `tailwind-v4` and `json`; other formats reject it rather than silently ignoring it. **A partial slice is not self-contained** — `--only semantic` references primitives it does not itself define, so ship it alongside the primitive slice.
|
|
131
344
|
|
|
132
345
|
### Output
|
|
133
346
|
|
|
134
|
-
| Flag |
|
|
135
|
-
|
|
136
|
-
| `--format
|
|
137
|
-
| `--color-format` | `oklch` · `hex` · `rgb` · `hsl
|
|
138
|
-
| `--out <path>` |
|
|
139
|
-
| `--replace-palette` |
|
|
140
|
-
| `--
|
|
141
|
-
| `--
|
|
142
|
-
|
|
143
|
-
|
|
347
|
+
| Flag | Default | What it does |
|
|
348
|
+
|---|---|---|
|
|
349
|
+
| `--format <fmt>` | `css` | See [Output formats](#output-formats). |
|
|
350
|
+
| `--color-format <fmt>` | `oklch` | `oklch` · `hex` · `rgb` · `hsl`. |
|
|
351
|
+
| `--out <path>` | stdout | File to write, or a directory when `--format all`. |
|
|
352
|
+
| `--no-replace-palette` | off | `tailwind-v4` only — keep Tailwind's stock palette. |
|
|
353
|
+
| `--merge` | off | Rewrite only the declarations this run generates into `--out`, preserving every other line. |
|
|
354
|
+
| `--no-provenance` | off | Omit the `Generated by …` header. |
|
|
355
|
+
| `--vanilla` | — | Deprecated no-op, warns. The shadcn/ui aliases it switched off no longer exist. |
|
|
356
|
+
| `--replace-palette` | — | Deprecated no-op, warns. Clearing the palette is the default; use `--no-replace-palette` to opt out. |
|
|
144
357
|
|
|
145
358
|
### Verification
|
|
146
359
|
|
|
147
360
|
| Flag | What it does |
|
|
148
361
|
|---|---|
|
|
149
|
-
| `--check` |
|
|
150
|
-
| `--report` | Print the measured contrast
|
|
151
|
-
|
|
152
|
-
`--check` replaces the regenerate-to-a-temp-dir-and-diff ritual, and reads the provenance header so it can tell three different failures apart:
|
|
362
|
+
| `--check` | Compare against what is already at `--out`. Writes nothing; exits non-zero on drift. With `--ramps` or `--merge`, only the declarations this run generates are compared. |
|
|
363
|
+
| `--report` | Print where the semantic bridge landed, peak chroma per ramp, and measured contrast for every pairing — semantic, surface and utility — with a score. Stderr only; never changes the exit code. |
|
|
153
364
|
|
|
154
365
|
```
|
|
155
|
-
|
|
156
|
-
check: tokens.css differs — generated by v0.1.6, running v0.2.0. Expected after a generator upgrade; regenerate to adopt it.
|
|
157
|
-
check: tokens.css differs — seed e06984b1 on disk, 7b5a14d2 now. The inputs changed; regenerate.
|
|
158
|
-
check: tokens.css differs — same generator and seed, so the file was edited by hand.
|
|
159
|
-
```
|
|
366
|
+
$ generate-tokens --primary "#4e9caf" --report --out tokens.css
|
|
160
367
|
|
|
161
|
-
|
|
368
|
+
semantic bridge (seed anchored at 500)
|
|
369
|
+
light default 500 subdued 50 highlight 700 ring 400
|
|
370
|
+
dark default 600 subdued 200 highlight 400 ring 700
|
|
162
371
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
372
|
+
ramp chroma (peak C per family)
|
|
373
|
+
primary 0.0815 at 500
|
|
374
|
+
grey forest 0.0247 at 500
|
|
375
|
+
note: forest carries too little chroma to read as a marking
|
|
376
|
+
at small sizes. Picking harder will not fix it; a palette can be short of a colour.
|
|
167
377
|
|
|
168
378
|
light mode (wcag, target aa)
|
|
169
379
|
AA primary/default 5.12:1
|
|
170
380
|
- neutral/default 4.30:1
|
|
171
|
-
|
|
172
|
-
|
|
381
|
+
score: 93% (14/15 pairings)
|
|
382
|
+
|
|
383
|
+
light surfaces and utilities (wcag, not counted in the score)
|
|
384
|
+
AAA canvas/foreground 13.05:1
|
|
385
|
+
- ring/canvas 1.82:1 (non-text, 1.4.11)
|
|
386
|
+
border/canvas 1.29:1 (advisory: 3:1 only where it bounds a control)
|
|
173
387
|
```
|
|
174
388
|
|
|
389
|
+
Three things worth knowing about that output. **Chroma** is reported because a seed can be chromatic by the numbers and still land as grey on the page — every candidate is a real token, nothing errors, and the result just looks weak. **Surfaces and utilities** are measured but not scored, so the score stays comparable across versions; the focus ring is held to WCAG 1.4.11's 3:1 rather than the body-text bar, and `border` is reported without a verdict because round a card it is decoration and round a text input it is the control's boundary. **Nothing here changes the exit code** — `--check` is the gate.
|
|
390
|
+
|
|
175
391
|
## Tailwind v4
|
|
176
392
|
|
|
177
|
-
On Tailwind v4 a token only becomes a usable class if it sits inside a `@theme` block under a namespace Tailwind owns
|
|
393
|
+
On Tailwind v4 a token only becomes a usable class if it sits inside a `@theme` block under a namespace Tailwind owns — variables in a plain `:root` block are just CSS and generate nothing. The `tailwind-v4` format emits a complete, self-contained theme, so there is no bridge to hand-write:
|
|
178
394
|
|
|
179
395
|
```css
|
|
180
396
|
@theme static {
|
|
181
|
-
/*
|
|
182
|
-
--color-primary-500: oklch(65.07% 0.0815 215.79);
|
|
183
|
-
|
|
184
|
-
/* Semantics — registers the utility; re-bound per mode below */
|
|
185
|
-
--color-primary: var(--primary);
|
|
397
|
+
--color-*: initial; /* clear Tailwind's stock palette */
|
|
398
|
+
--color-primary-500: oklch(65.07% 0.0815 215.79); /* primitives: real values */
|
|
399
|
+
--color-primary: var(--primary); /* semantics: registers the utility */
|
|
186
400
|
}
|
|
187
401
|
|
|
188
402
|
:root, [data-theme="light"] {
|
|
189
403
|
--primary: var(--color-primary-500);
|
|
190
|
-
--color-primary: var(--primary); /* re-bound — see below */
|
|
404
|
+
--color-primary: var(--primary); /* re-bound per mode — see below */
|
|
191
405
|
}
|
|
192
406
|
[data-theme="dark"], .dark {
|
|
193
407
|
--primary: var(--color-primary-600);
|
|
@@ -195,70 +409,76 @@ On Tailwind v4 a token only becomes a usable class if it sits inside a `@theme`
|
|
|
195
409
|
}
|
|
196
410
|
```
|
|
197
411
|
|
|
198
|
-
|
|
412
|
+
You get `bg-primary`, `text-primary-foreground`, `bg-canvas`, `border-border` **and** `bg-primary-500`, in both modes.
|
|
199
413
|
|
|
200
|
-
|
|
414
|
+
Three details carry the weight, each verified against a real Tailwind compile:
|
|
201
415
|
|
|
202
|
-
|
|
416
|
+
**`@theme static`, never plain `@theme`.** Tailwind tree-shakes a plain theme block down to only the tokens some utility references — silently, with no build error. On a default palette that drops **51 of 179** colour tokens, including every `red`/`green`/`yellow` ramp, so `bg-red-500` quietly falls back to Tailwind's stock red.
|
|
203
417
|
|
|
204
|
-
**
|
|
418
|
+
**Semantic aliases appear in the theme block *and* every mode block.** The `@theme` entry registers the name so the utility exists. But custom properties inherit their *computed* value, so an alias declared only on `:root` captures the light value and will not re-resolve inside a scoped `[data-theme="dark"]` subtree — a dark card on a light page would keep the light colour. (`<html class="dark">` works either way, being the same element, which is why this is easy to miss.)
|
|
205
419
|
|
|
206
|
-
|
|
420
|
+
**The stock palette is cleared by default.** `--color-*: initial` means any colour class the file does not define fails to compile rather than rendering off-brand. The alternative is worse than it looks: if a project does not own `--color-slate-900`, then `bg-slate-900` yields Tailwind's stock slate — off-brand, compiles cleanly, reads as deliberate in review, and survives a re-theme. Better a build error you fix.
|
|
207
421
|
|
|
208
|
-
|
|
422
|
+
Tailwind's `transparent`, `current` and `inherit` are not part of the `--color-*` namespace, so `bg-transparent` and `text-current` keep working. Pass `--no-replace-palette` when adopting this into an existing Tailwind codebase that still uses stock hues — otherwise every `bg-slate-*` stops compiling at once.
|
|
209
423
|
|
|
210
|
-
|
|
424
|
+
## Overriding what it generates
|
|
211
425
|
|
|
212
|
-
|
|
426
|
+
Every generated block is **unlayered**, and the mode blocks carry class hooks as well as attributes:
|
|
213
427
|
|
|
214
428
|
```css
|
|
215
|
-
|
|
429
|
+
:root /* primitives */
|
|
430
|
+
:root, [data-theme="light"] /* light */
|
|
431
|
+
[data-theme="dark"], .dark /* dark */
|
|
432
|
+
@media (prefers-color-scheme: dark) {
|
|
433
|
+
:root:not([data-theme="light"]):not(.light) /* system dark */
|
|
434
|
+
}
|
|
216
435
|
```
|
|
217
436
|
|
|
218
|
-
|
|
437
|
+
Two consequences, both of which have cost real projects real time:
|
|
219
438
|
|
|
220
|
-
|
|
439
|
+
**An override inside `@layer` loses**, whatever the import order — unlayered CSS beats any layer. Nothing errors; the override simply does not apply, and it reads as correct when you open the file.
|
|
221
440
|
|
|
222
|
-
0
|
|
441
|
+
**An unlayered override can still lose on specificity.** The system-dark selector is `(0,3,0)`; a plain `:root` is `(0,1,0)`. An override written on `:root` alone applies in light mode and silently loses in dark, which one project shipped as a warm brown ground served to every dark-mode visitor — verified as correct twice, because headless browsers default to light. Match the selector:
|
|
223
442
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
| `tailwind-v4` primitives renamed | `--primary-500` → `--color-primary-500`, matching the css export. Update any direct references. |
|
|
231
|
-
| Output carries a provenance header | Expected. `--no-provenance` opts out. |
|
|
232
|
-
| A seed outside 15–98% lightness no longer inverts the ramp | Bug fix. A near-black or near-white seed was reproduced exactly at its anchor while its neighbours were clamped, so 500 could come out darker than 600. The range now widens to admit the seed; the tight side collapses onto it instead. Only affects seeds outside that range. |
|
|
443
|
+
```css
|
|
444
|
+
:root,
|
|
445
|
+
:root:not([data-theme="light"]):not(.light) {
|
|
446
|
+
--canvas: var(--color-ink);
|
|
447
|
+
}
|
|
448
|
+
```
|
|
233
449
|
|
|
234
|
-
|
|
450
|
+
Then check **both** colour schemes, not whichever your tool defaults to.
|
|
235
451
|
|
|
236
|
-
|
|
452
|
+
**A `--color-<name>-<step>` ramp step cannot be overridden this way at all.** Tailwind inlines a ramp step's literal value into every utility it emits, so by the time your declaration exists there is no `var()` left to intercept — the CSS is present, in the right place, satisfying the cascade, and does nothing. Semantic tokens are the opposite: utilities reach them through `var()`, so they override cleanly. If you need a ramp step to change, change the seed and regenerate, or point the call sites at a token you do own.
|
|
237
453
|
|
|
238
|
-
|
|
239
|
-
src/ types, oklch, primitives, semantic, generate, export, seed, index (barrel), cli
|
|
240
|
-
test/ parity tests, export invariants, fixture cases, golden fixtures, fixture regenerator
|
|
241
|
-
```
|
|
454
|
+
Generated files carry this contract in their own header, built from the selectors that file actually emitted.
|
|
242
455
|
|
|
243
|
-
|
|
456
|
+
If you find yourself hand-writing a colour beside the generated file, that is the signal to add a seed instead — `--color name=value` gives you a full scale with contrast-paired foregrounds, and `--seed-shade` puts it on the step the semantics read. A hand-written brand layer forks the colour system in a way `--check` cannot see: it passes, while the page renders from somewhere else entirely.
|
|
244
457
|
|
|
245
|
-
##
|
|
458
|
+
## Guarantees
|
|
246
459
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
```
|
|
460
|
+
- **Deterministic.** Same inputs and version produce byte-identical output. No randomness, clocks, or environment reads. (One caveat: the *hue* of a fully-desaturated colour in `hsl` output is mathematically meaningless and not stable across engines; every other value is exact.)
|
|
461
|
+
- **Two test tiers.** Golden fixtures pin the bytes; structural invariants pin what a fixture cannot — chiefly that every `var(--x)` an export emits resolves to a `--x:` the same output defines. Bytes can be stable and still be incoherent CSS.
|
|
462
|
+
- **Two dependencies**, both pure JS: `culori` (colour maths) and `apca-w3` (APCA contrast).
|
|
463
|
+
- **ESM**, Node 18+.
|
|
464
|
+
|
|
465
|
+
## Development
|
|
254
466
|
|
|
255
|
-
|
|
467
|
+
Part of the [designtools](https://github.com/andflett/designtools) monorepo.
|
|
256
468
|
|
|
257
|
-
|
|
469
|
+
```bash
|
|
470
|
+
npx tsx packages/tokens/src/cli.ts --primary "#4e9caf" # run from source
|
|
471
|
+
npm -w packages/tokens run build # tsup -> dist
|
|
472
|
+
npm -w packages/tokens run typecheck
|
|
473
|
+
npm -w packages/tokens run fixtures # regenerate goldens
|
|
474
|
+
npm test # vitest
|
|
475
|
+
```
|
|
258
476
|
|
|
259
|
-
|
|
477
|
+
```
|
|
478
|
+
src/ types, oklch, primitives, semantic, generate, export, seed, shades, index, cli
|
|
479
|
+
test/ fixture parity, export invariants, shade behaviour, public API
|
|
480
|
+
```
|
|
260
481
|
|
|
261
|
-
|
|
262
|
-
- **Structural invariants** in `test/export-invariants.test.ts` — properties a fixture cannot assert, because a fixture only pins that bytes have not changed, and bytes can be stable and still be incoherent CSS. Chief among them: every `var(--x)` an export emits must resolve to a `--x:` the same output defines.
|
|
482
|
+
## Licence
|
|
263
483
|
|
|
264
|
-
|
|
484
|
+
CC-BY-NC-4.0
|