@designtools/tokens 0.11.1 → 0.13.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.
@@ -1,3 +1,3 @@
1
- export { generateFromSeed, mergeSeed, optionsFromSeed, parseCommand, seedCommand, seedJson, splitCommand, studioCss } from "./seed.js";
1
+ export { generateFromSeed, groundChoices, groundPreviewCss, mergeSeed, optionsFromSeed, parseCommand, seedCommand, seedJson, splitCommand, studioCss } from "./seed.js";
2
2
  export { ColourGenerator } from "./studio.js";
3
3
  export { TokenStudio } from "./studio.js";
@@ -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 GroundAction, 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,25 @@ 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
+ /**
47
+ * Where a ground's text, its lines and panels, and its main button come from, as it stands: its own
48
+ * choice, else every ground's, else the generator's (the button unset: it follows the tint)
49
+ */
50
+ export declare function groundChoices(seed: SeedFile): Record<string, {
51
+ text: GroundFill;
52
+ tint: GroundFill;
53
+ action?: GroundAction;
54
+ }>;
55
+ /**
56
+ * One ground as it would be with other choices for its text and its lines, as a rule for
57
+ * `selector` that writes every token it reads: a preview, generated for real, that leaves the
58
+ * seed alone. Null when there's no such ground.
59
+ */
60
+ export declare function groundPreviewCss(seed: SeedFile, name: string, choice: {
61
+ text?: GroundFill;
62
+ tint?: GroundFill;
63
+ action?: GroundAction;
64
+ }, selector: string): string | null;
46
65
  /** The command that regenerates from a seed file, keeping how and where the output is written. */
47
66
  export declare function seedCommand({ seedPath, output, version }: {
48
67
  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=…`, `--ground-text name=…` and `--ground-action 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,26 @@ 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) });
130
+ break;
131
+ }
132
+ case "--ground-action": {
133
+ const entry = value();
134
+ const at = entry.indexOf("=");
135
+ if (at === -1)
136
+ seed.groundAction = entry;
137
+ else
138
+ perGround.push({ name: entry.slice(0, at), key: "action", fill: entry.slice(at + 1) });
113
139
  break;
140
+ }
114
141
  case "--gamut":
115
142
  seed.gamut = value();
116
143
  break;
@@ -129,6 +156,9 @@ export function parseCommand(command) {
129
156
  case "--no-aliases":
130
157
  seed.aliases = false;
131
158
  break;
159
+ case "--aliases":
160
+ seed.aliases = true;
161
+ break;
132
162
  case "--tint-floor":
133
163
  seed.tintFloor = Number(value());
134
164
  break;
@@ -188,8 +218,13 @@ export function parseCommand(command) {
188
218
  seed.additionalColors = colours.map((c) => (decorative.has(c.name) ? { ...c, decorative: true } : c));
189
219
  if (Object.keys(surfaces.light ?? {}).length || Object.keys(surfaces.dark ?? {}).length)
190
220
  seed.surfaceColors = surfaces;
191
- if (Object.keys(backgrounds).length)
192
- seed.backgrounds = backgrounds;
221
+ for (const { name, key, fill } of perGround)
222
+ if (grounds[name])
223
+ grounds[name] = { ...grounds[name], [key]: fill };
224
+ if (Object.keys(grounds).length)
225
+ seed.grounds = grounds;
226
+ if (Object.keys(pageSurfaces).length)
227
+ seed.pageSurfaces = pageSurfaces;
193
228
  return { seed, files, output };
194
229
  }
195
230
  /** Fold a seed file under the flags, as the CLI does: a flag wins over the file. */
@@ -201,7 +236,8 @@ export function mergeSeed(file, flags) {
201
236
  surfaceColors: flags.surfaceColors
202
237
  ? { light: { ...file.surfaceColors?.light, ...flags.surfaceColors.light }, dark: { ...file.surfaceColors?.dark, ...flags.surfaceColors.dark } }
203
238
  : file.surfaceColors,
204
- backgrounds: flags.backgrounds ? { ...file.backgrounds, ...flags.backgrounds } : file.backgrounds,
239
+ grounds: flags.grounds ? { ...file.grounds, ...flags.grounds } : file.grounds,
240
+ pageSurfaces: flags.pageSurfaces ? { ...file.pageSurfaces, ...flags.pageSurfaces } : file.pageSurfaces,
205
241
  };
206
242
  }
207
243
  /** A seed as the generator's inputs, with the CLI's defaults. */
@@ -223,8 +259,11 @@ export function optionsFromSeed(seed) {
223
259
  ...(seed.foreground && seed.foreground !== "balanced" ? { foreground: seed.foreground } : {}),
224
260
  ...(seed.wcagFloor === false ? { wcagFloor: false } : {}),
225
261
  ...(seed.surfaceColors ? { surfaceColors: seed.surfaceColors } : {}),
226
- ...(seed.backgrounds ? { backgrounds: seed.backgrounds } : {}),
227
- ...(seed.grounds && seed.grounds !== "full" ? { grounds: seed.grounds } : {}),
262
+ ...(seed.pageSurfaces ? { pageSurfaces: seed.pageSurfaces } : {}),
263
+ ...(seed.grounds && Object.keys(seed.grounds).length ? { grounds: seed.grounds } : {}),
264
+ ...(seed.groundTint && seed.groundTint !== "own" ? { groundTint: seed.groundTint } : {}),
265
+ ...(seed.groundText && seed.groundText !== "page" ? { groundText: seed.groundText } : {}),
266
+ ...(seed.groundAction ? { groundAction: seed.groundAction } : {}),
228
267
  seedShade: seed.seedShade,
229
268
  ...(Object.keys(seedShades).length ? { seedShades } : {}),
230
269
  ...(rangeOf(seed) ? { lightnessRange: rangeOf(seed) } : {}),
@@ -233,7 +272,7 @@ export function optionsFromSeed(seed) {
233
272
  ...(seed.hueDrift !== undefined && seed.hueDrift !== "none" ? { hueDrift: seed.hueDrift } : {}),
234
273
  ...(familiesOf(seed) ? { families: familiesOf(seed) } : {}),
235
274
  ...(seed.pins && Object.keys(seed.pins).length ? { pins: seed.pins } : {}),
236
- ...(seed.aliases === false ? { aliases: false } : {}),
275
+ ...(seed.aliases === true ? { aliases: true } : {}),
237
276
  ...(decorative.length ? { decorative: decorative.sort() } : {}),
238
277
  shades: seed.shades,
239
278
  shadeOverrides: seed.shadeOverrides ?? {},
@@ -254,7 +293,34 @@ export function generateFromSeed(seed) {
254
293
  */
255
294
  export function studioCss(seed, tokens = generateFromSeed(seed)) {
256
295
  const { options } = optionsFromSeed(seed);
257
- return exportTokens(tokens, "css", { colorFormat: seed.colorFormat ?? "oklch", shadeOverrides: options.shadeOverrides, seedShade: options.seedShade, seedShades: options.seedShades });
296
+ // a ground here writes every token, not only what differs from the page: the project's own scope
297
+ // for it is still on the page, and anything it writes that this one left out would show through
298
+ const grounds = tokens.grounds && Object.fromEntries(Object.entries(tokens.grounds).map(([name, g]) => [name, { ...g, own: Object.keys(g._refs) }]));
299
+ return exportTokens(grounds ? { ...tokens, grounds } : tokens, "css", { colorFormat: seed.colorFormat ?? "oklch", shadeOverrides: options.shadeOverrides, seedShade: options.seedShade, seedShades: options.seedShades });
300
+ }
301
+ /**
302
+ * Where a ground's text, its lines and panels, and its main button come from, as it stands: its own
303
+ * choice, else every ground's, else the generator's (the button unset: it follows the tint)
304
+ */
305
+ export function groundChoices(seed) {
306
+ return Object.fromEntries(Object.entries(seed.grounds ?? {}).map(([name, g]) => {
307
+ const action = g.action ?? seed.groundAction;
308
+ return [name, { text: g.text ?? seed.groundText ?? "page", tint: g.tint ?? seed.groundTint ?? "own", ...(action ? { action } : {}) }];
309
+ }));
310
+ }
311
+ /**
312
+ * One ground as it would be with other choices for its text and its lines, as a rule for
313
+ * `selector` that writes every token it reads: a preview, generated for real, that leaves the
314
+ * seed alone. Null when there's no such ground.
315
+ */
316
+ export function groundPreviewCss(seed, name, choice, selector) {
317
+ const spec = seed.grounds?.[name];
318
+ if (!spec)
319
+ return null;
320
+ const key = "designtools-preview";
321
+ const css = studioCss({ ...seed, grounds: { [key]: { ...spec, ...choice } } });
322
+ const rule = new RegExp(`^\\[data-surface="${key}"\\] \\{([^}]*)\\}`, "m").exec(css);
323
+ return rule ? `${selector} {${rule[1]}}` : null;
258
324
  }
259
325
  /** The command that regenerates from a seed file, keeping how and where the output is written. */
260
326
  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.13.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",