@designtools/tokens 0.1.5 → 0.1.7

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @designtools/tokens
2
2
 
3
- Colour token generation from brand colours, as pure TypeScript. `generateTokens({ primary, neutral })` produces a `TokenSystem` — twelve-step OKLCH scales, contrast-checked semantic intents, surfaces, utilities, in light and dark — and `exportTokens(tokens, format)` serialises it, with CSS custom properties as the lead format and Tailwind v3/v4, DTCG, and Lovable as secondary outputs. No browser, DOM, or React.
3
+ Colour token generation from brand colours, as pure TypeScript. `generateTokens({ primary, neutral })` produces a `TokenSystem` — twelve-step OKLCH scales, contrast-checked semantic intents, surfaces, utilities, in light and dark — and `exportTokens(tokens, format)` serialises it, with CSS custom properties as the lead format and Tailwind v4/v3 and DTCG as secondary outputs. No browser, DOM, or React.
4
4
 
5
5
  Part of the `@designtools/*` suite; the `designtools-tokens` skill (in `skills/`) drives it at scaffold time.
6
6
 
@@ -10,7 +10,7 @@ Published, the CLI is `generate-tokens`:
10
10
 
11
11
  ```
12
12
  npx @designtools/tokens --primary "#4e9caf" # CSS custom properties to stdout
13
- npx @designtools/tokens --primary "#4e9caf" --vanilla --out tokens.css
13
+ npx @designtools/tokens --primary "#4e9caf" --format tailwind-v4 --out app/theme.css
14
14
  npx @designtools/tokens --primary "#4e9caf" --format all --out ./tokens
15
15
  ```
16
16
 
@@ -29,13 +29,215 @@ const tokens = generateTokens({ primary: "#4e9caf", neutral: "#9aa1af" });
29
29
  const css = exportTokens(tokens, "css", { colorFormat: "oklch" });
30
30
  ```
31
31
 
32
- Pass `--vanilla` (or `compat: false`) to omit the shadcn/ui aliases from the `css` and `tailwind-v4` outputs — the right default for Base UI / vanilla-CSS stacks. Full input/output contracts, options, and export formats live in `skills/designtools-tokens/SKILL.md`.
32
+ ## CLI reference
33
+
34
+ `--primary` is the only required input, and even that can come from a seed file.
35
+
36
+ ### Seeds
37
+
38
+ | Flag | Default | What it does |
39
+ |---|---|---|
40
+ | `--primary <colour>` | — | Brand colour: hex, `rgb()`, `hsl()`, `oklch()`, or a CSS named colour. Required unless `--seed` supplies it. |
41
+ | `--neutral <colour>` | `#747474` | Neutral seed. Drives greys, surfaces, borders. |
42
+ | `--color <name>=<colour>` | — | An extra named scale. Repeatable: `--color accent=#8b5cf6 --color info=#0ea5e9`. |
43
+ | `--seed <file.json>` | — | Replay a saved configuration. Flags given alongside it win, so a seed is a set of defaults rather than a lock. |
44
+ | `--stdin` | off | Read the same JSON document from standard input instead of a file. |
45
+
46
+ A seed file is a `SavedConfig` — the shape the web tool persists — so the two can hand work to each other:
47
+
48
+ ```json
49
+ {
50
+ "primaryColor": "#7c3aed",
51
+ "neutralColor": "#747474",
52
+ "additionalColors": [{ "name": "accent", "color": "#f59e0b" }],
53
+ "colorFormat": "hex",
54
+ "contrastAlgorithm": "apca",
55
+ "contrastTarget": "aaa",
56
+ "lightnessStrategy": "perceptual",
57
+ "seedShade": 500,
58
+ "shades": [25, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950],
59
+ "shadeOverrides": {}
60
+ }
61
+ ```
62
+
63
+ ### Generation
64
+
65
+ | Flag | Values | Default | What it does |
66
+ |---|---|---|---|
67
+ | `--contrast` | `wcag` · `apca` | `wcag` | Which algorithm picks foregrounds. |
68
+ | `--target` | `aa-large` · `aa` · `aaa` | `aa` | Minimum the foreground search has to clear. |
69
+ | `--lightness` | `fixed` · `perceptual` | `fixed` | How lightness is distributed across the twelve steps. |
70
+ | `--seed-shade` | `25`…`950` | `500` | Which step the seed colour lands on. |
71
+ | `--shades` | `tailwind` · `full` · a list | `full` | Which steps to emit. |
72
+ | `--overrides <file.json>` | — | — | `{ shadeOverrides, surfaceShadeOverrides, utilityShadeOverrides }`, same shape as the web tool. Replaces any overrides carried by `--seed`. |
73
+
74
+ #### `--seed-shade` — where the brand colour sits
75
+
76
+ The seed used to be pinned to 500, and everything else is derived from the headroom above and below it. Brand colours arrive from client guidelines and Figma files as whatever they are: pin a deep navy to 500 and there is almost no room left underneath, so 600–950 collapse into each other; pin a pale tint to 500 and the light end has nowhere to go.
77
+
78
+ ```
79
+ generate-tokens --primary "#1e3a5f" --seed-shade 800 # deep navy belongs near the bottom
80
+ generate-tokens --primary "#fde68a" --seed-shade 200 # pale amber belongs near the top
81
+ ```
82
+
83
+ The seed is still reproduced exactly, just at the step you name. Each side's easing curve is resampled onto its new step count, so the ramp keeps its shape — gentle at the light end, larger jumps at the dark end for text contrast — rather than becoming one uniform gradient. The chroma envelope moves with the anchor too, so peak chroma stays on the seed instead of leaving a visible dip at it.
84
+
85
+ #### `--shades` — which steps exist
86
+
87
+ Ours is 25–950 (12 steps); Tailwind's stock ramps are 50–950 (11, no 25). Since the generated ramps deliberately override Tailwind's, matching its shade set makes the override exact rather than one step wider.
88
+
89
+ ```
90
+ generate-tokens --primary "#4e9caf" --shades tailwind # 50–950
91
+ generate-tokens --primary "#4e9caf" --shades 100,500,900 # a coarse three-step ramp
92
+ ```
93
+
94
+ Some tokens name specific steps — `muted` and light `canvas` both sit on 25, borders on `neutral-200` — so dropping a step would otherwise leave a `var()` pointing at nothing. References are snapped to the nearest emitted step, measured in ramp positions with ties going darker, and every substitution is reported:
95
+
96
+ ```
97
+ $ generate-tokens --primary "#4e9caf" --shades tailwind --out tokens.css
98
+ --shades 50,100,200,300,400,500,600,700,800,900,950 drops steps some tokens name; snapped to nearest:
99
+ muted (light): 25 -> 50
100
+ canvas (light): 25 -> 50
101
+ ```
102
+
103
+ Snapping keeps output coherent but does change a design decision, which is why it is reported rather than done quietly. The contrast search that picks foregrounds is restricted to emitted steps for the same reason. A shade set needs at least three steps, and `--seed-shade` has to be one of them.
104
+
105
+ ### Selection
106
+
107
+ | Flag | Values | What it does |
108
+ |---|---|---|
109
+ | `--only <sections>` | `ramp` · `primitives` · `semantic` · `surface` · `utility` | Emit only these slices. Comma-separated or repeated. |
110
+
111
+ - `ramp` — the twelve-step colour ramps alone. This is "hand me a colour, hand me back scales".
112
+ - `primitives` — the ramps plus `--color-black` and `--color-white`.
113
+ - `semantic` — intent tokens: `--primary`, `--destructive-subdued-foreground`, `--muted`, …
114
+ - `surface` — `--canvas`, `--surface`, `--overlay` and their foregrounds.
115
+ - `utility` — `--border`, `--input`, `--ring`, `--shadow`.
116
+
117
+ Why it matters beyond convenience: it keeps generated colour separable from files a project hand-tunes, so a re-theme regenerates the colour without clobbering anything else.
118
+
119
+ ```
120
+ # scaffold: everything, once
121
+ generate-tokens --primary "#4e9caf" --out tokens.css
122
+
123
+ # re-theme: colour only, leaving hand-tuned files alone
124
+ 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
+ ```
129
+
130
+ Supported by `css`, `tailwind-v4` and `json`; the other formats reject it rather than silently ignoring it. **A partial slice is not self-contained** — `--only semantic` emits `var(--color-*)` references to primitives it does not itself define, so ship it alongside the primitive slice.
131
+
132
+ ### Output
133
+
134
+ | Flag | Values | Default | What it does |
135
+ |---|---|---|---|
136
+ | `--format` | `css` · `tailwind-v4` · `tailwind-v3` · `dtcg` · `json` · `all` | `css` | Serialiser. `all` writes every format into `--out` as a directory. |
137
+ | `--color-format` | `oklch` · `hex` · `rgb` · `hsl` | `oklch` | How literal colour values are printed. |
138
+ | `--out <path>` | — | stdout | File to write, or a directory when `--format all`. |
139
+ | `--replace-palette` | flag | off | `tailwind-v4` only — see [Overriding Tailwind's palette](#overriding-tailwinds-palette). |
140
+ | `--no-provenance` | flag | off | Omit the `Generated by …` header. |
141
+ | `--vanilla` | flag | — | Accepted and ignored; removed in 0.3. See [Migrating from 0.1](#migrating-from-01). |
142
+
143
+ File names under `--format all`: `tokens.css`, `tailwind.css`, `tailwind.config.js`, `tokens.dtcg.json`, `tokens.json`.
144
+
145
+ ### Verification
146
+
147
+ | Flag | What it does |
148
+ |---|---|
149
+ | `--check` | Regenerate in memory and compare against what is already at `--out`. Writes nothing. Exit 0 if identical, 1 if not. Requires `--out`. |
150
+ | `--report` | Print the measured contrast of every semantic pairing to stderr, with a score. Changes no output. |
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:
153
+
154
+ ```
155
+ check: tokens.css up to date
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
+ ```
160
+
161
+ That distinction is the point. Without it, every drift check in every project fails identically the day the generator is upgraded, and a real hand-edit looks exactly like a version bump.
162
+
163
+ `--report` surfaces contrast the generator already measures but never showed:
164
+
165
+ ```
166
+ $ generate-tokens --primary "#4e9caf" --report --format css > tokens.css
167
+
168
+ light mode (wcag, target aa)
169
+ AA primary/default 5.12:1
170
+ - neutral/default 4.30:1
171
+ AAA destructive/subdued 7.41:1
172
+ score: 87% (13/15 pairings)
173
+ ```
174
+
175
+ ## Tailwind v4
176
+
177
+ 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` export emits a complete, self-contained theme, so no hand-written bridge is needed:
178
+
179
+ ```css
180
+ @theme static {
181
+ /* Primitives — real values, mode-invariant */
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);
186
+ }
187
+
188
+ :root, [data-theme="light"] {
189
+ --primary: var(--color-primary-500);
190
+ --color-primary: var(--primary); /* re-bound — see below */
191
+ }
192
+ [data-theme="dark"], .dark {
193
+ --primary: var(--color-primary-600);
194
+ --color-primary: var(--primary);
195
+ }
196
+ ```
197
+
198
+ Three details carry the weight:
199
+
200
+ **`@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. Measured 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 instead of the project's.
201
+
202
+ **Semantic aliases appear in both the theme block and every mode block.** The `@theme` entry registers the name so the utility exists at all. But custom properties inherit their *computed* value, so `--color-primary: var(--primary)` declared only on `:root` captures the light value and would not re-resolve inside a scoped `[data-theme="dark"]` subtree — a dark card on a light page would keep the light colour. Re-declaring per mode costs one line per token in a generated file. (`<html class="dark">` happens to work either way, since it is the same element, which is why this is easy to miss.)
203
+
204
+ **Primitives carry real values and use the same names as the plain-CSS export**, so the two formats stay interchangeable.
205
+
206
+ Verified against a real Tailwind v4 compile and in a browser: `bg-primary` and `bg-primary-500` both compile, unreferenced tokens survive into the built CSS, and `bg-primary` inside a scoped `[data-theme="dark"]` wrapper computes differently from one outside it while `bg-primary-500` stays fixed.
207
+
208
+ ### Overriding Tailwind's palette
209
+
210
+ The generated ramps deliberately override Tailwind's stock ones, so `text-red-500` is the project's red. The instinct is to withhold primitives so people are pushed toward semantics, but that is the wrong failure mode: if a project does not own `--color-red-500`, then `text-red-500` yields Tailwind's stock red — off-brand, compiles cleanly, looks deliberate in review, and survives a re-theme. Emitting them makes the accident harmless.
211
+
212
+ That covers only the ramps the generator emits, leaving ~17 stock hues, so `bg-slate-900` stays off-brand and available. `--replace-palette` clears the whole namespace:
213
+
214
+ ```css
215
+ @theme static { --color-*: initial; }
216
+ ```
217
+
218
+ Any stock colour class then fails to compile rather than compiling wrong. It is off by default because it would break a project adopting the generator into an existing Tailwind codebase.
219
+
220
+ ## Migrating from 0.1
221
+
222
+ 0.2 changes generated output. Existing projects will see their drift checks fail once; regenerate to adopt.
223
+
224
+ | Change | What to do |
225
+ |---|---|
226
+ | shadcn/ui aliases (`--background`, `--card`, `--secondary`, `--sidebar-*`, `--chart-*`) are gone | Map them yourself if you need them. Vanilla is now the only behaviour. |
227
+ | `--vanilla` is a no-op | Remove it. Accepted for 0.2, removed in 0.3. `ExportOptions.compat` is gone. |
228
+ | `--format lovable` removed | Dropped with the aliases — it was a shadcn-shaped `globals.css`. `exportToLovable` is no longer exported. |
229
+ | `tailwind-v4` restructured | Regenerate. The dark selector is now `[data-theme="dark"], .dark` rather than `.dark` alone, and there is now a `prefers-color-scheme` fallback matching the css export. |
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. |
233
+
234
+ The plain-CSS, `tailwind-v3` and `dtcg` outputs are otherwise unchanged apart from the header and the removed aliases.
33
235
 
34
236
  ## Layout
35
237
 
36
238
  ```
37
- src/ types, oklch, primitives, semantic, generate, export, index (barrel), cli
38
- test/ parity tests, fixture cases, golden fixtures, fixture regenerator
239
+ src/ types, oklch, primitives, semantic, generate, export, seed, index (barrel), cli
240
+ test/ parity tests, export invariants, fixture cases, golden fixtures, fixture regenerator
39
241
  ```
40
242
 
41
243
  Runtime dependencies: `culori` (colour maths) and `apca-w3` (APCA contrast), both pure JS. Built with tsup to ESM plus a compiled `generate-tokens` bin; `dist/` is produced at build/publish time and is not committed.
@@ -47,11 +249,16 @@ npm -w packages/tokens run build # tsup -> dist (ESM + generate-tokens bi
47
249
  npm -w packages/tokens run typecheck
48
250
  npm -w packages/tokens run cli -- --primary "#4e9caf"
49
251
  npm -w packages/tokens run fixtures # regenerate goldens — only when a behaviour change is intended
50
- npm test # parity tests run in the repo's vitest suite
252
+ npm test # parity + invariant tests run in the repo's vitest suite
51
253
  ```
52
254
 
53
255
  ## Determinism and parity
54
256
 
55
- Same seeds and options always produce byte-identical output — pure arithmetic, no randomness, clocks, or environment reads. `test/` pins that: the generator must reproduce the golden fixtures in `test/fixtures/`, which were generated from the original Colour Token Generator web-tool source. Regenerate them (`run fixtures`) only when a behaviour change is intended.
257
+ Same seeds and options always produce byte-identical output for a given version — pure arithmetic, no randomness, clocks, or environment reads. Consumers' drift checks depend on it.
258
+
259
+ Two test tiers pin that:
260
+
261
+ - **Golden fixtures** in `test/fixtures/` — regenerate (`run fixtures`) only when a behaviour change is intended.
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.
56
263
 
57
- `src/` (except `index.ts` and `cli.ts`) is a verbatim copy of that web tool's library, so the two stay in step; `src/export.ts` is the one deliberate exception, carrying the `compat` toggle behind `--vanilla`, with its own fixtures. This package is the canonical source.
264
+ This package is the canonical source for the generator. It previously tracked a separate web-tool library byte-for-byte, with a test asserting the copies had not drifted; that constraint is retired, so the core files change here first.