@designtools/tokens 0.9.0 → 0.11.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/dist/cli.js CHANGED
@@ -16,7 +16,9 @@ import {
16
16
  exportTokens,
17
17
  extendedBridgeShades,
18
18
  externalReferences,
19
+ familiesOf,
19
20
  generateTokens,
21
+ getContrastRatio,
20
22
  isAPCAAvailable,
21
23
  isInGamut,
22
24
  mergeSlice,
@@ -30,10 +32,11 @@ import {
30
32
  selectTokenSections,
31
33
  serialiseUsage,
32
34
  snapShade,
35
+ stepContract,
33
36
  tokenFamilies,
34
37
  usageReport,
35
38
  withJsonProvenance
36
- } from "./chunk-P562LKFL.js";
39
+ } from "./chunk-RQGC7LES.js";
37
40
 
38
41
  // src/cli.ts
39
42
  import { parseArgs } from "util";
@@ -50,12 +53,15 @@ Seeds:
50
53
  unless supplied by --seed.
51
54
  --neutral <colour> Neutral seed. Default: ${DEFAULT_NEUTRAL_COLOR}
52
55
  --color <name>=<colour>[@<shade>]
53
- Additional brand colour scale. Repeatable. Append
54
- @<shade> to anchor its seed on that step, as
55
- --seed-shade does for the primary: accent=#fde68a@200.
56
- --decorative <name> Give an added colour its ramp only, with no semantic
57
- slots (no --<name>, no bg-<name>), for an accent that
58
- must never be text or an action. Repeatable.
56
+ Additional brand colour: a ramp plus semantic tokens
57
+ (--<name>, -foreground, -subdued, -hover, -active) for
58
+ buttons, badges and text. Repeatable. Append @<shade> to
59
+ anchor its seed on that step, as --seed-shade does for
60
+ the primary: accent=#fde68a@200.
61
+ --decorative <name> Give a --color its ramp only, with no semantic slots (no
62
+ --<name>, no bg-<name>), for an accent that must never be
63
+ text or an action: --color sand=#e8d5b0 --decorative sand.
64
+ Repeatable.
59
65
  --canvas <colour>[/<dark>]
60
66
  --surface <colour>[/<dark>]
61
67
  --overlay <colour>[/<dark>]
@@ -63,9 +69,19 @@ Seeds:
63
69
  (light, then dark after a slash), in place of the
64
70
  neutral steps they default to. Text on each is measured.
65
71
  --background <name>=<colour>[/<dark>]
66
- A brand background a section is painted with, as
67
- --surface-<name> with measured text. Repeatable. Every
68
- semantic colour is measured against it (--report).
72
+ A colour a whole section is painted with (a band on a
73
+ marketing page), not a ramp: --surface-<name> with
74
+ measured text, and a ground. Inside data-surface="<name>"
75
+ every token (buttons, muted text, borders, inputs, the
76
+ ring, status text) is searched again against it, so
77
+ components need no change. Repeatable. A name a token or
78
+ ground already uses (canvas, primary, warning-subdued\u2026)
79
+ is refused.
80
+ --grounds <full|text|none>
81
+ What data-surface gives each ground. full (default):
82
+ every token, tinted to the ground and contrast-checked
83
+ on it. text: the background and its text only; buttons,
84
+ borders and the rest keep the page's. none: no grounds.
69
85
  --usage <file.json> Also write where each colour can go: every fill, status
70
86
  text tone and muted text on every background, per mode,
71
87
  as text, shape or neither. @designtools/manifest reads it
@@ -94,6 +110,27 @@ Generation options:
94
110
  every family.) Anchor the others with
95
111
  --neutral-seed-shade and --color name=colour@shade.
96
112
  --neutral-seed-shade <25..950> Which step the neutral seed lands on. Default: 500.
113
+ --light-end <50..100> OKLCH lightness (percent) of the colour ramps' palest
114
+ step. Default: 98. The neutral keeps its own range.
115
+ --dark-end <0..60> OKLCH lightness (percent) the colour ramps' deepest step
116
+ reaches. Default: 15. Raise it for a brand that only uses
117
+ soft deep tones; adjacent dark steps keep a 2-point gap.
118
+ --tint-floor <0..1> How much of the seed's colourfulness the pale steps keep.
119
+ Default: none (they fade towards grey).
120
+ --chroma <profile> How colourful each step is: follow (default; eases from
121
+ pale tints to the seed's chroma), constant (the seed's
122
+ chroma throughout, as the screen allows), relative (the
123
+ seed's share of what each step can hold) or peak.
124
+ --chroma-strength <0.25..3> More or less colour than the seed's, eased in beside it;
125
+ the seed's own step never moves. Default: 1.
126
+ --chroma-peak <25..950> Where --chroma peak peaks. Default: the seed's step.
127
+ --hue-drift <none|natural|deg> How the hue turns in the darks: natural deepens yellows
128
+ to amber and blues to indigo; a number is degrees at 950
129
+ (a negative one as --hue-drift=-20).
130
+ --pin <family@step=colour> An exact colour on a step; the ramp is refitted through
131
+ it. Refused if it puts the ramp out of order or off the
132
+ screen. Repeatable. Per-family shape (a colour leaning
133
+ away from the system) is set in a --seed file: "families".
97
134
  --shades <list|preset> Which steps to emit. Default: all twelve (25-950).
98
135
  Presets: tailwind (50-950, matching Tailwind's
99
136
  stock ramps) and full. Or a list: 50,100,500,900.
@@ -119,8 +156,9 @@ Selection:
119
156
  the contrast search runs inside it.
120
157
  Use with --merge to regenerate part of a token file
121
158
  without rewriting regions a human has edited.
122
- Surfaces and utilities belong to no single colour, so a
123
- family-scoped slice leaves them alone.
159
+ Surfaces, utilities and grounds belong to no single
160
+ colour, so a family-scoped slice leaves them alone:
161
+ refresh grounds with --only grounds.
124
162
 
125
163
  --only <sections> Emit only these slices, comma-separated or repeated:
126
164
  ramp the twelve-step colour ramps alone
@@ -128,6 +166,7 @@ Selection:
128
166
  semantic intent tokens (primary, destructive, muted, \u2026)
129
167
  surface canvas / surface / overlay
130
168
  utility border / input / ring / shadow
169
+ grounds --on-<ground>-* and the data-surface scopes
131
170
  Keeps generated colour separable from hand-tuned files,
132
171
  so a re-theme does not clobber them. Supported by the
133
172
  css, tailwind-v4 and json formats.
@@ -171,6 +210,13 @@ Verification:
171
210
  With --ramps or --merge, only the declarations this run
172
211
  generates are compared, so hand-edited regions elsewhere
173
212
  in the file are not reported as drift.
213
+ --no-aliases Emit each status ramp once, as success, destructive and
214
+ warning, without green, red and yellow beside them as
215
+ the same ramps.
216
+ --contract Print the step contract: each step that carries text on
217
+ white (or black) in some colours and not others, so
218
+ "the 700" means the same everywhere. Measured, not
219
+ enforced.
174
220
  --report Print the measured contrast of every semantic foreground
175
221
  pairing to stderr, with an overall score against the
176
222
  --contrast algorithm and --target level. Writes no files
@@ -220,9 +266,19 @@ try {
220
266
  lightness: { type: "string" },
221
267
  foreground: { type: "string" },
222
268
  "no-wcag-floor": { type: "boolean" },
269
+ grounds: { type: "string" },
223
270
  gamut: { type: "string" },
224
271
  "seed-shade": { type: "string" },
225
272
  "neutral-seed-shade": { type: "string" },
273
+ "light-end": { type: "string" },
274
+ "dark-end": { type: "string" },
275
+ "tint-floor": { type: "string" },
276
+ chroma: { type: "string" },
277
+ "chroma-strength": { type: "string" },
278
+ "chroma-peak": { type: "string" },
279
+ "hue-drift": { type: "string" },
280
+ pin: { type: "string", multiple: true },
281
+ "no-aliases": { type: "boolean" },
226
282
  shades: { type: "string" },
227
283
  overrides: { type: "string" },
228
284
  only: { type: "string", multiple: true },
@@ -237,6 +293,7 @@ try {
237
293
  seed: { type: "string" },
238
294
  check: { type: "boolean" },
239
295
  report: { type: "boolean" },
296
+ contract: { type: "boolean" },
240
297
  stdin: { type: "boolean" },
241
298
  "no-provenance": { type: "boolean" },
242
299
  help: { type: "boolean" }
@@ -325,6 +382,7 @@ var contrastTarget = validateChoice("target", values.target ?? seed.contrastTarg
325
382
  var lightnessStrategy = validateChoice("lightness", values.lightness ?? seed.lightnessStrategy, ["fixed", "perceptual"], "fixed");
326
383
  var foreground = validateChoice("foreground", values.foreground ?? seed.foreground, ["balanced", "max"], "balanced");
327
384
  var wcagFloor = values["no-wcag-floor"] === true ? false : seed.wcagFloor ?? true;
385
+ var grounds = validateChoice("grounds", values.grounds ?? seed.grounds, ["full", "text", "none"], "full");
328
386
  function lightDark(flag, raw) {
329
387
  const [light, dark] = raw.split("/").map((s) => s.trim());
330
388
  return { light: validateColor(flag, light), ...dark ? { dark: validateColor(flag, dark) } : {} };
@@ -372,6 +430,55 @@ if (shadesInput !== void 0) {
372
430
  }
373
431
  var seedShade = values["seed-shade"] !== void 0 ? parseShade("seed-shade", values["seed-shade"]) : seed.seedShade;
374
432
  var neutralSeedShade = values["neutral-seed-shade"] !== void 0 ? parseShade("neutral-seed-shade", values["neutral-seed-shade"]) : seed.neutralSeedShade;
433
+ function parseNumberIn(flag, raw, min, max) {
434
+ if (raw === void 0) return void 0;
435
+ const n = Number(String(raw).trim());
436
+ if (!Number.isFinite(n) || n < min || n > max) fail(`--${flag} must be a number from ${min} to ${max} (got "${raw}")`);
437
+ return n;
438
+ }
439
+ var lightEnd = parseNumberIn("light-end", values["light-end"] ?? seed.lightnessRange?.light, 50, 100);
440
+ var darkEnd = parseNumberIn("dark-end", values["dark-end"] ?? seed.lightnessRange?.dark, 0, 60);
441
+ if (lightEnd !== void 0 && darkEnd !== void 0 && lightEnd - darkEnd < 30) {
442
+ fail(`--light-end and --dark-end need at least 30 points between them, so the steps can be told apart (got ${lightEnd} and ${darkEnd})`);
443
+ }
444
+ var lightnessRange = lightEnd !== void 0 || darkEnd !== void 0 ? { ...lightEnd !== void 0 ? { light: lightEnd } : {}, ...darkEnd !== void 0 ? { dark: darkEnd } : {} } : void 0;
445
+ var tintFloor = parseNumberIn("tint-floor", values["tint-floor"] ?? seed.tintFloor, 0, 1);
446
+ var chromaProfile = values.chroma ?? seed.chroma?.profile;
447
+ if (chromaProfile !== void 0 && !["follow", "constant", "relative", "peak"].includes(chromaProfile)) {
448
+ fail(`--chroma must be one of: follow, constant, relative, peak (got "${chromaProfile}")`);
449
+ }
450
+ var chromaStrength = parseNumberIn("chroma-strength", values["chroma-strength"] ?? seed.chroma?.strength, 0.25, 3);
451
+ var chromaPeakRaw = values["chroma-peak"] ?? seed.chroma?.peakAt;
452
+ var chromaPeak = chromaPeakRaw !== void 0 ? parseShade("chroma-peak", chromaPeakRaw) : void 0;
453
+ var chroma = chromaProfile !== void 0 && chromaProfile !== "follow" || chromaStrength !== void 0 || chromaPeak !== void 0 ? {
454
+ ...chromaProfile !== void 0 && chromaProfile !== "follow" ? { profile: chromaProfile } : {},
455
+ ...chromaStrength !== void 0 ? { strength: chromaStrength } : {},
456
+ ...chromaPeak !== void 0 ? { peakAt: chromaPeak } : {}
457
+ } : void 0;
458
+ var hueDriftRaw = values["hue-drift"] ?? (seed.hueDrift !== void 0 ? String(seed.hueDrift) : void 0);
459
+ var hueDrift;
460
+ if (hueDriftRaw !== void 0 && hueDriftRaw !== "none") {
461
+ hueDrift = hueDriftRaw === "natural" ? "natural" : parseNumberIn("hue-drift", hueDriftRaw, -90, 90);
462
+ }
463
+ var familyShapes = seed.families && Object.keys(seed.families).length ? seed.families : void 0;
464
+ var pins = {};
465
+ for (const [name, steps] of Object.entries(seed.pins ?? {})) {
466
+ for (const [step, colour] of Object.entries(steps ?? {})) if (colour) pins[name] = { ...pins[name], [parseShade(`pin ${name}`, step)]: colour };
467
+ }
468
+ for (const raw of values.pin ?? []) {
469
+ const m = /^([a-z][\w-]*)@(\d+)=(.+)$/.exec(raw.trim());
470
+ if (!m) fail(`--pin takes family@step=colour, as in camel@300=#B7B7A3 (got "${raw}")`);
471
+ else {
472
+ try {
473
+ parseToOklch(m[3]);
474
+ } catch {
475
+ fail(`--pin ${raw}: "${m[3]}" is not a colour`);
476
+ }
477
+ pins[m[1]] = { ...pins[m[1]], [parseShade("pin", m[2])]: m[3] };
478
+ }
479
+ }
480
+ var hasPins = Object.keys(pins).length > 0;
481
+ var aliases = values["no-aliases"] === true ? false : seed.aliases ?? true;
375
482
  var seedShades = {};
376
483
  if (neutralSeedShade !== void 0) seedShades.neutral = neutralSeedShade;
377
484
  for (const [name, { flag, raw }] of Object.entries(additionalAnchors)) {
@@ -437,8 +544,16 @@ var options = {
437
544
  ...!wcagFloor ? { wcagFloor } : {},
438
545
  ...hasSurfaceColors ? { surfaceColors } : {},
439
546
  ...Object.keys(backgrounds).length ? { backgrounds } : {},
547
+ ...grounds !== "full" ? { grounds } : {},
440
548
  seedShade,
441
549
  ...Object.keys(seedShades).length > 0 ? { seedShades } : {},
550
+ ...lightnessRange ? { lightnessRange: { ...lightnessRange.light !== void 0 ? { light: lightnessRange.light / 100 } : {}, ...lightnessRange.dark !== void 0 ? { dark: lightnessRange.dark / 100 } : {} } } : {},
551
+ ...tintFloor !== void 0 ? { tintFloor } : {},
552
+ ...chroma ? { chroma } : {},
553
+ ...hueDrift !== void 0 ? { hueDrift } : {},
554
+ ...familyShapes ? { families: familiesOf({ families: familyShapes }) } : {},
555
+ ...hasPins ? { pins } : {},
556
+ ...aliases ? {} : { aliases: false },
442
557
  ...decorative.size > 0 ? { decorative: [...decorative].sort() } : {},
443
558
  shades,
444
559
  shadeOverrides: overrides.shadeOverrides ?? {},
@@ -482,7 +597,13 @@ for (const [flag, why] of [
482
597
  `);
483
598
  }
484
599
  }
485
- var tokens = generateTokens(brandColors, options);
600
+ var tokens;
601
+ try {
602
+ tokens = generateTokens(brandColors, options);
603
+ } catch (error) {
604
+ if (error instanceof APCAUnavailableError) throw error;
605
+ fail(error.message);
606
+ }
486
607
  if (families.length > 0) {
487
608
  const known = tokenFamilies(tokens);
488
609
  const unknown = families.filter((f) => !known.includes(f));
@@ -534,8 +655,19 @@ var resolvedSeed = {
534
655
  ...!wcagFloor ? { wcagFloor } : {},
535
656
  ...hasSurfaceColors ? { surfaceColors } : {},
536
657
  ...Object.keys(backgrounds).length ? { backgrounds } : {},
658
+ // left out at the default, so a seed from before grounds keeps its fingerprint
659
+ ...grounds !== "full" ? { grounds } : {},
537
660
  seedShade,
538
661
  neutralSeedShade,
662
+ // left out when unset, so a seed without them keeps the fingerprint it always had
663
+ ...lightnessRange ? { lightnessRange } : {},
664
+ ...tintFloor !== void 0 ? { tintFloor } : {},
665
+ ...chroma ? { chroma } : {},
666
+ ...hueDrift !== void 0 ? { hueDrift } : {},
667
+ ...familyShapes ? { families: familyShapes } : {},
668
+ ...hasPins ? { pins } : {},
669
+ // left out when on, the default, so existing seeds keep their fingerprint
670
+ ...aliases ? {} : { aliases: false },
539
671
  shades,
540
672
  shadeOverrides: options.shadeOverrides ?? {},
541
673
  surfaceShadeOverrides: options.surfaceShadeOverrides,
@@ -661,13 +793,13 @@ searched to their contrast floor (ring and input 3:1 on canvas and surface; mute
661
793
  }
662
794
  }
663
795
  function reportChroma() {
664
- const chroma = checkRampChroma(tokens.primitives);
796
+ const chroma2 = checkRampChroma(tokens.primitives);
665
797
  const unintended = ([name, r]) => r.low && name !== "neutral";
666
- const low = Object.entries(chroma).filter(unintended);
798
+ const low = Object.entries(chroma2).filter(unintended);
667
799
  process.stderr.write(`
668
800
  ramp chroma (peak C per family)
669
801
  `);
670
- for (const [name, r] of Object.entries(chroma)) {
802
+ for (const [name, r] of Object.entries(chroma2)) {
671
803
  const mark = unintended([name, r]) ? "grey" : " ";
672
804
  process.stderr.write(` ${mark} ${name.padEnd(14)} ${r.peak.toFixed(4)} at ${r.atShade}
673
805
  `);
@@ -710,14 +842,14 @@ function reportPage() {
710
842
  for (const mode of ["light", "dark"]) {
711
843
  const grid = tokens.pageContrast?.[mode];
712
844
  if (!grid) continue;
713
- const grounds = Object.keys(Object.values(grid)[0] ?? {});
845
+ const grounds2 = Object.keys(Object.values(grid)[0] ?? {});
714
846
  process.stderr.write(`
715
847
  ${mode} mode: each colour on each background (T text ${floor}:1, S shape 3:1, - neither)
716
848
  `);
717
- process.stderr.write(` ${"".padEnd(18)}${grounds.map((g) => g.padStart(16)).join("")}
849
+ process.stderr.write(` ${"".padEnd(18)}${grounds2.map((g) => g.padStart(16)).join("")}
718
850
  `);
719
851
  for (const [colour, row] of Object.entries(grid)) {
720
- const cells = grounds.map((g) => {
852
+ const cells = grounds2.map((g) => {
721
853
  const r = row[g];
722
854
  const mark = r >= floor ? "T" : r >= 3 ? "S" : "-";
723
855
  return `${r.toFixed(2)} ${mark}`.padStart(16);
@@ -763,8 +895,46 @@ ${mode} mode${label} (${contrastAlgorithm}, target ${contrastTarget})
763
895
  }
764
896
  }
765
897
  reportSurfaces();
898
+ reportGrounds();
899
+ }
900
+ function reportGrounds() {
901
+ const grounds2 = tokens.grounds;
902
+ if (!grounds2) return;
903
+ const step = (ref) => ref.type === "shade" ? `${ref.colorName}-${ref.shade}` : ref.type === "value" ? "computed" : ref.type;
904
+ process.stderr.write(`
905
+ grounds: what data-surface="<ground>" changes from the page (ratio against the ground; a label's against its fill)
906
+ `);
907
+ const under = (token) => token.endsWith("-foreground") && token !== "canvas-foreground" && token !== "muted-foreground" ? token.slice(0, -"-foreground".length) : "canvas";
908
+ for (const mode of ["light", "dark"]) {
909
+ for (const [name, g] of Object.entries(grounds2[mode])) {
910
+ if (g.kind === "base") continue;
911
+ const shown = g.changed.filter((t) => !t.endsWith("-hover") && !t.endsWith("-active"));
912
+ const head = ` ${mode.padEnd(6)}${name.padEnd(22)}`;
913
+ if (!shown.length) {
914
+ process.stderr.write(`${head}the page's tokens already work here
915
+ `);
916
+ } else {
917
+ const items = shown.map((t) => `${t} ${step(g._refs[t])} ${getContrastRatio(g.tokens[t], g.tokens[under(t)]).toFixed(1)}`);
918
+ process.stderr.write(`${head}${items.join(", ")}
919
+ `);
920
+ }
921
+ for (const note of g.notes) process.stderr.write(` ${"".padEnd(28)}! ${note}
922
+ `);
923
+ }
924
+ }
766
925
  }
767
926
  if (values.report) report();
927
+ if (values.contract) {
928
+ const breaks = stepContract(tokens.primitives);
929
+ process.stderr.write(`
930
+ step contract (does a step mean the same in every colour?)
931
+ `);
932
+ if (!breaks.length) process.stderr.write(" every step carries text on white, and on black, in every colour or in none\n");
933
+ for (const b of breaks) {
934
+ process.stderr.write(` ${String(b.step).padStart(3)} ${b.role}: ${b.holds.join(", ")} yes; ${b.fails.join(", ")} no
935
+ `);
936
+ }
937
+ }
768
938
  var formats = format === "all" ? Object.keys(FILE_NAMES) : [format];
769
939
  var isSlice = families.length > 0 || sections.length > 0;
770
940
  function reportExternalReferences(css) {