@designtools/tokens 0.11.1 → 0.12.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.
@@ -2,7 +2,7 @@
2
2
  * The studio's plain logic, browser-safe: a project's generate-tokens command read into a seed, a
3
3
  * seed turned into the generator's inputs, and the command that regenerates from a seed.
4
4
  */
5
- import { type BrandColors, type GenerationOptions, type SeedFile, type TokenSystem } from "@designtools/tokens";
5
+ import { type BrandColors, type GenerationOptions, type GroundFill, type SeedFile, type TokenSystem } from "@designtools/tokens";
6
6
  /** Flags that say where and how the output is written, kept when the colour moves into a seed. */
7
7
  export interface OutputFlags {
8
8
  format?: string;
@@ -43,6 +43,20 @@ export declare function generateFromSeed(seed: SeedFile): TokenSystem;
43
43
  * tokens file uses, so added after it, it re-themes the page.
44
44
  */
45
45
  export declare function studioCss(seed: SeedFile, tokens?: TokenSystem): string;
46
+ /** Where a ground's text and its lines and panels come from, as it stands: its own choice, else every ground's, else the generator's */
47
+ export declare function groundChoices(seed: SeedFile): Record<string, {
48
+ text: GroundFill;
49
+ tint: GroundFill;
50
+ }>;
51
+ /**
52
+ * One ground as it would be with other choices for its text and its lines, as a rule for
53
+ * `selector` that writes every token it reads: a preview, generated for real, that leaves the
54
+ * seed alone. Null when there's no such ground.
55
+ */
56
+ export declare function groundPreviewCss(seed: SeedFile, name: string, choice: {
57
+ text?: GroundFill;
58
+ tint?: GroundFill;
59
+ }, selector: string): string | null;
46
60
  /** The command that regenerates from a seed file, keeping how and where the output is written. */
47
61
  export declare function seedCommand({ seedPath, output, version }: {
48
62
  seedPath: string;
@@ -53,7 +53,10 @@ export function parseCommand(command) {
53
53
  const colours = [];
54
54
  const decorative = new Set();
55
55
  const surfaces = { light: {}, dark: {} };
56
- const backgrounds = {};
56
+ const grounds = {};
57
+ const pageSurfaces = {};
58
+ /** `--ground-tint name=…` and `--ground-text name=…`, applied once every ground is known */
59
+ const perGround = [];
57
60
  for (let i = 0; i < flags.length; i++) {
58
61
  const flag = flags[i];
59
62
  const eq = flag.indexOf("=");
@@ -87,10 +90,17 @@ export function parseCommand(command) {
87
90
  (surfaces.dark ??= {})[key] = dark;
88
91
  break;
89
92
  }
90
- case "--background": {
93
+ case "--ground": {
94
+ const m = /^([a-z][\w-]*)=([a-z][\w-]*)@(\d+)$/.exec(value());
95
+ if (m)
96
+ grounds[m[1]] = { ramp: m[2], step: Number(m[3]) };
97
+ break;
98
+ }
99
+ case "--page-surface": {
91
100
  const entry = value();
92
101
  const at = entry.indexOf("=");
93
- backgrounds[entry.slice(0, at)] = lightDark(entry.slice(at + 1));
102
+ const [light, dark] = entry.slice(at + 1).split("/");
103
+ pageSurfaces[entry.slice(0, at)] = { light, ...(dark ? { dark } : {}) };
94
104
  break;
95
105
  }
96
106
  case "--contrast":
@@ -108,9 +118,17 @@ export function parseCommand(command) {
108
118
  case "--no-wcag-floor":
109
119
  seed.wcagFloor = false;
110
120
  break;
111
- case "--grounds":
112
- seed.grounds = value();
121
+ case "--ground-tint":
122
+ case "--ground-text": {
123
+ const key = name === "--ground-tint" ? "tint" : "text";
124
+ const entry = value();
125
+ const at = entry.indexOf("=");
126
+ if (at === -1)
127
+ seed[key === "tint" ? "groundTint" : "groundText"] = entry;
128
+ else
129
+ perGround.push({ name: entry.slice(0, at), key, fill: entry.slice(at + 1) });
113
130
  break;
131
+ }
114
132
  case "--gamut":
115
133
  seed.gamut = value();
116
134
  break;
@@ -129,6 +147,9 @@ export function parseCommand(command) {
129
147
  case "--no-aliases":
130
148
  seed.aliases = false;
131
149
  break;
150
+ case "--aliases":
151
+ seed.aliases = true;
152
+ break;
132
153
  case "--tint-floor":
133
154
  seed.tintFloor = Number(value());
134
155
  break;
@@ -188,8 +209,13 @@ export function parseCommand(command) {
188
209
  seed.additionalColors = colours.map((c) => (decorative.has(c.name) ? { ...c, decorative: true } : c));
189
210
  if (Object.keys(surfaces.light ?? {}).length || Object.keys(surfaces.dark ?? {}).length)
190
211
  seed.surfaceColors = surfaces;
191
- if (Object.keys(backgrounds).length)
192
- seed.backgrounds = backgrounds;
212
+ for (const { name, key, fill } of perGround)
213
+ if (grounds[name])
214
+ grounds[name] = { ...grounds[name], [key]: fill };
215
+ if (Object.keys(grounds).length)
216
+ seed.grounds = grounds;
217
+ if (Object.keys(pageSurfaces).length)
218
+ seed.pageSurfaces = pageSurfaces;
193
219
  return { seed, files, output };
194
220
  }
195
221
  /** Fold a seed file under the flags, as the CLI does: a flag wins over the file. */
@@ -201,7 +227,8 @@ export function mergeSeed(file, flags) {
201
227
  surfaceColors: flags.surfaceColors
202
228
  ? { light: { ...file.surfaceColors?.light, ...flags.surfaceColors.light }, dark: { ...file.surfaceColors?.dark, ...flags.surfaceColors.dark } }
203
229
  : file.surfaceColors,
204
- backgrounds: flags.backgrounds ? { ...file.backgrounds, ...flags.backgrounds } : file.backgrounds,
230
+ grounds: flags.grounds ? { ...file.grounds, ...flags.grounds } : file.grounds,
231
+ pageSurfaces: flags.pageSurfaces ? { ...file.pageSurfaces, ...flags.pageSurfaces } : file.pageSurfaces,
205
232
  };
206
233
  }
207
234
  /** A seed as the generator's inputs, with the CLI's defaults. */
@@ -223,8 +250,10 @@ export function optionsFromSeed(seed) {
223
250
  ...(seed.foreground && seed.foreground !== "balanced" ? { foreground: seed.foreground } : {}),
224
251
  ...(seed.wcagFloor === false ? { wcagFloor: false } : {}),
225
252
  ...(seed.surfaceColors ? { surfaceColors: seed.surfaceColors } : {}),
226
- ...(seed.backgrounds ? { backgrounds: seed.backgrounds } : {}),
227
- ...(seed.grounds && seed.grounds !== "full" ? { grounds: seed.grounds } : {}),
253
+ ...(seed.pageSurfaces ? { pageSurfaces: seed.pageSurfaces } : {}),
254
+ ...(seed.grounds && Object.keys(seed.grounds).length ? { grounds: seed.grounds } : {}),
255
+ ...(seed.groundTint && seed.groundTint !== "own" ? { groundTint: seed.groundTint } : {}),
256
+ ...(seed.groundText && seed.groundText !== "page" ? { groundText: seed.groundText } : {}),
228
257
  seedShade: seed.seedShade,
229
258
  ...(Object.keys(seedShades).length ? { seedShades } : {}),
230
259
  ...(rangeOf(seed) ? { lightnessRange: rangeOf(seed) } : {}),
@@ -233,7 +262,7 @@ export function optionsFromSeed(seed) {
233
262
  ...(seed.hueDrift !== undefined && seed.hueDrift !== "none" ? { hueDrift: seed.hueDrift } : {}),
234
263
  ...(familiesOf(seed) ? { families: familiesOf(seed) } : {}),
235
264
  ...(seed.pins && Object.keys(seed.pins).length ? { pins: seed.pins } : {}),
236
- ...(seed.aliases === false ? { aliases: false } : {}),
265
+ ...(seed.aliases === true ? { aliases: true } : {}),
237
266
  ...(decorative.length ? { decorative: decorative.sort() } : {}),
238
267
  shades: seed.shades,
239
268
  shadeOverrides: seed.shadeOverrides ?? {},
@@ -254,7 +283,28 @@ export function generateFromSeed(seed) {
254
283
  */
255
284
  export function studioCss(seed, tokens = generateFromSeed(seed)) {
256
285
  const { options } = optionsFromSeed(seed);
257
- return exportTokens(tokens, "css", { colorFormat: seed.colorFormat ?? "oklch", shadeOverrides: options.shadeOverrides, seedShade: options.seedShade, seedShades: options.seedShades });
286
+ // a ground here writes every token, not only what differs from the page: the project's own scope
287
+ // for it is still on the page, and anything it writes that this one left out would show through
288
+ const grounds = tokens.grounds && Object.fromEntries(Object.entries(tokens.grounds).map(([name, g]) => [name, { ...g, own: Object.keys(g._refs) }]));
289
+ return exportTokens(grounds ? { ...tokens, grounds } : tokens, "css", { colorFormat: seed.colorFormat ?? "oklch", shadeOverrides: options.shadeOverrides, seedShade: options.seedShade, seedShades: options.seedShades });
290
+ }
291
+ /** Where a ground's text and its lines and panels come from, as it stands: its own choice, else every ground's, else the generator's */
292
+ export function groundChoices(seed) {
293
+ return Object.fromEntries(Object.entries(seed.grounds ?? {}).map(([name, g]) => [name, { text: g.text ?? seed.groundText ?? "page", tint: g.tint ?? seed.groundTint ?? "own" }]));
294
+ }
295
+ /**
296
+ * One ground as it would be with other choices for its text and its lines, as a rule for
297
+ * `selector` that writes every token it reads: a preview, generated for real, that leaves the
298
+ * seed alone. Null when there's no such ground.
299
+ */
300
+ export function groundPreviewCss(seed, name, choice, selector) {
301
+ const spec = seed.grounds?.[name];
302
+ if (!spec)
303
+ return null;
304
+ const key = "designtools-preview";
305
+ const css = studioCss({ ...seed, grounds: { [key]: { ...spec, ...choice } } });
306
+ const rule = new RegExp(`^\\[data-surface="${key}"\\] \\{([^}]*)\\}`, "m").exec(css);
307
+ return rule ? `${selector} {${rule[1]}}` : null;
258
308
  }
259
309
  /** The command that regenerates from a seed file, keeping how and where the output is written. */
260
310
  export function seedCommand({ seedPath, output, version }) {
@@ -6,7 +6,9 @@ import { useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from "r
6
6
  import { css, properties } from "./css.js";
7
7
  import { describeChanges, seedFromHash } from "./colour-controls.js";
8
8
  import { InPlaceEditor } from "./colour-editor.js";
9
- import { generateFromSeed, seedCommand, seedJson, studioCss } from "./seed.js";
9
+ import { generateFromSeed, groundChoices, groundPreviewCss, seedCommand, seedJson, studioCss } from "./seed.js";
10
+ /** Fired when the preview arrives, leaves, or the seed behind it changes */
11
+ const PREVIEW_EVENT = "designtools:colour";
10
12
  /** The seed the studio and the colour generator apply to the page, in every frame. */
11
13
  export const STUDIO_KEY = "studio:seed";
12
14
  const KEY = STUDIO_KEY;
@@ -118,6 +120,42 @@ function Colour({ project }) {
118
120
  if (generated)
119
121
  lastGood.current = generated;
120
122
  const system = generated ?? lastGood.current ?? base;
123
+ // the docs' ground explorer asks here what a ground would look like with other choices; it
124
+ // hears again whenever the seed changes
125
+ const current = useRef(seed);
126
+ current.current = seed;
127
+ useEffect(() => {
128
+ const api = {
129
+ grounds: () => groundChoices(current.current),
130
+ sources: () => {
131
+ const s = current.current;
132
+ const pins = (family) => Object.fromEntries(Object.entries(s.pins?.[family] ?? {}).filter((e) => typeof e[1] === "string"));
133
+ return {
134
+ primary: { color: s.primaryColor ?? "#3b82f6", step: s.seedShade ?? 500, pins: pins("primary") },
135
+ neutral: { color: s.neutralColor ?? "#737373", step: s.neutralSeedShade ?? 500, pins: pins("neutral") },
136
+ ...Object.fromEntries((s.additionalColors ?? []).map((c) => [c.name, { color: c.color, step: c.seedShade ?? 500, pins: pins(c.name) }])),
137
+ };
138
+ },
139
+ previewGround: (name, choice, selector) => {
140
+ try {
141
+ return groundPreviewCss(current.current, name, choice, selector);
142
+ }
143
+ catch {
144
+ return null;
145
+ }
146
+ },
147
+ };
148
+ window.__designtoolsColour = api;
149
+ window.dispatchEvent(new Event(PREVIEW_EVENT));
150
+ return () => {
151
+ delete window.__designtoolsColour;
152
+ window.dispatchEvent(new Event(PREVIEW_EVENT));
153
+ };
154
+ }, []);
155
+ // biome-ignore lint/correctness/useExhaustiveDependencies: a new seed is news for the explorer
156
+ useEffect(() => {
157
+ window.dispatchEvent(new Event(PREVIEW_EVENT));
158
+ }, [seed]);
121
159
  // a link carrying settings (#designtools-colours=…) opens with them, then tidies the address
122
160
  useEffect(() => {
123
161
  const shared = seedFromHash(location.hash);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@designtools/tokens",
3
- "version": "0.11.1",
3
+ "version": "0.12.0",
4
4
  "description": "Turn one brand colour into a complete design-token system: perceptual OKLCH ramps, contrast-checked semantic tokens (WCAG or APCA), light and dark, as CSS custom properties, Tailwind v4/v3 or DTCG. Deterministic CLI, safe for agents.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "peerDependencies": {
45
45
  "@base-ui/react": "^1.6.0",
46
- "@designtools/widgets": "^0.1.3",
46
+ "@designtools/widgets": "^0.1.6",
47
47
  "apca-w3": "^0.1.9",
48
48
  "lucide-react": "^1.23.0",
49
49
  "react": ">=19",
@@ -86,7 +86,7 @@
86
86
  "tsx": "^4.19.0",
87
87
  "typescript": "^5.9.3",
88
88
  "vitest": "^4.1.11",
89
- "@designtools/widgets": "0.1.5"
89
+ "@designtools/widgets": "0.1.6"
90
90
  },
91
91
  "keywords": [
92
92
  "design-tokens",