@designtools/tokens 0.3.0 → 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.
@@ -303,6 +303,18 @@ function formatColorAs(color, format) {
303
303
 
304
304
  // src/primitives.ts
305
305
  var DEFAULT_NEUTRAL_COLOR = "#747474";
306
+ var ALIAS_PAIRS = [
307
+ ["success", "green"],
308
+ ["destructive", "red"],
309
+ ["warning", "yellow"]
310
+ ];
311
+ function aliasOf(name) {
312
+ for (const [semantic, alias] of ALIAS_PAIRS) {
313
+ if (name === semantic) return alias;
314
+ if (name === alias) return semantic;
315
+ }
316
+ return void 0;
317
+ }
306
318
  function clamp(v, min, max) {
307
319
  return Math.min(max, Math.max(min, v));
308
320
  }
@@ -360,6 +372,12 @@ function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed", scal
360
372
  for (const [name, color] of Object.entries(brandColors.additionalColors)) {
361
373
  palette[name] = genScale(color);
362
374
  }
375
+ for (const [semantic, alias] of ALIAS_PAIRS) {
376
+ const overrodeSemantic = semantic in brandColors.additionalColors;
377
+ const overrodeAlias = alias in brandColors.additionalColors;
378
+ if (overrodeSemantic && !overrodeAlias) palette[alias] = palette[semantic];
379
+ else if (overrodeAlias && !overrodeSemantic) palette[semantic] = palette[alias];
380
+ }
363
381
  }
364
382
  return palette;
365
383
  }
@@ -371,6 +389,36 @@ function getShade(palette, colorName, shade) {
371
389
  return scale[shade];
372
390
  }
373
391
 
392
+ // src/bridge.ts
393
+ var BRIDGE_OFFSETS = {
394
+ light: {
395
+ extended: { DEFAULT: 0, subdued: -5, highlight: 2 },
396
+ ring: -1
397
+ },
398
+ dark: {
399
+ extended: { DEFAULT: 1, subdued: -3, highlight: -1 },
400
+ ring: 2
401
+ }
402
+ };
403
+ function bridgeShade(offset, seedShade, active) {
404
+ const anchor = VALID_SHADES.indexOf(seedShade ?? DEFAULT_SEED_SHADE);
405
+ const index = Math.min(VALID_SHADES.length - 1, Math.max(0, anchor + offset));
406
+ return snapShade(VALID_SHADES[index], active);
407
+ }
408
+ function extendedBridgeShades(mode, seedShade, active, overrides) {
409
+ const offsets = BRIDGE_OFFSETS[mode].extended;
410
+ const at = (offset) => bridgeShade(offset, seedShade, active);
411
+ const snap = (shade) => snapShade(shade, active);
412
+ return {
413
+ DEFAULT: overrides?.DEFAULT ? snap(overrides.DEFAULT) : at(offsets.DEFAULT),
414
+ subdued: overrides?.subdued ? snap(overrides.subdued) : at(offsets.subdued),
415
+ highlight: overrides?.highlight ? snap(overrides.highlight) : at(offsets.highlight)
416
+ };
417
+ }
418
+ function ringBridgeShade(mode, seedShade, active) {
419
+ return bridgeShade(BRIDGE_OFFSETS[mode].ring, seedShade, active);
420
+ }
421
+
374
422
  // src/semantic.ts
375
423
  function shadeKeys(shades) {
376
424
  return resolveShades(shades);
@@ -447,14 +495,10 @@ function findBestForeground(background, scale, neutralScale, white, black, algor
447
495
  if (whiteC >= blackC) return { value: white, ref: { type: "white" } };
448
496
  return { value: black, ref: { type: "black" } };
449
497
  }
450
- function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades) {
498
+ function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades, seedShade) {
451
499
  const activeShades = shadeKeys(shades);
452
500
  const findFg = (bg) => findBestForeground(bg, scale, neutral, white, black, contrastAlgorithm, contrastTarget, colorName, activeShades);
453
- const snap = (shade) => snapShade(shade, activeShades);
454
- const defaults = mode === "light" ? { DEFAULT: snap(500), subdued: snap(50), highlight: snap(700) } : { DEFAULT: snap(600), subdued: snap(200), highlight: snap(400) };
455
- const defaultShade = overrides?.DEFAULT ? snap(overrides.DEFAULT) : defaults.DEFAULT;
456
- const subduedShade = overrides?.subdued ? snap(overrides.subdued) : defaults.subdued;
457
- const highlightShade = overrides?.highlight ? snap(overrides.highlight) : defaults.highlight;
501
+ const { DEFAULT: defaultShade, subdued: subduedShade, highlight: highlightShade } = extendedBridgeShades(mode, seedShade, activeShades, overrides);
458
502
  const DEFAULT = scale[defaultShade];
459
503
  const subdued = scale[subduedShade];
460
504
  const highlight = scale[highlightShade];
@@ -480,6 +524,7 @@ function generateSemanticTokens(palette, mode, options) {
480
524
  const contrastTarget = options?.contrastTarget ?? "aa";
481
525
  const shadeOverrides = options?.shadeOverrides ?? {};
482
526
  const activeShades = shadeKeys(options?.shades);
527
+ const seedShade = options?.seedShade;
483
528
  const neutral = palette.neutral;
484
529
  if (!neutral) {
485
530
  throw new Error("Neutral palette not found");
@@ -501,11 +546,11 @@ function generateSemanticTokens(palette, mode, options) {
501
546
  foreground: neutral[snapShade(200, activeShades)]
502
547
  };
503
548
  const tokens = {
504
- primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades),
505
- neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades),
506
- success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades),
507
- destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades),
508
- warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades),
549
+ primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades, seedShade),
550
+ neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades, seedShade),
551
+ success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades, seedShade),
552
+ destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades, seedShade),
553
+ warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, seedShade),
509
554
  muted
510
555
  };
511
556
  const coreColors = [
@@ -532,7 +577,8 @@ function generateSemanticTokens(palette, mode, options) {
532
577
  contrastTarget,
533
578
  shadeOverrides[colorName]?.[mode],
534
579
  colorName,
535
- activeShades
580
+ activeShades,
581
+ seedShade
536
582
  );
537
583
  }
538
584
  }
@@ -578,7 +624,7 @@ function generateSurfaceTokens(palette, mode, overrides, shades) {
578
624
  }
579
625
  };
580
626
  }
581
- function generateUtilityTokens(palette, mode, overrides, shades) {
627
+ function generateUtilityTokens(palette, mode, overrides, shades, seedShade) {
582
628
  const activeShades = shadeKeys(shades);
583
629
  const snap = (shade) => snapShade(shade, activeShades);
584
630
  const neutral = palette.neutral;
@@ -586,7 +632,8 @@ function generateUtilityTokens(palette, mode, overrides, shades) {
586
632
  if (!neutral || !primary) {
587
633
  throw new Error("Required palettes not found");
588
634
  }
589
- const defaults = mode === "light" ? { border: snap(200), input: snap(200), ring: snap(400), shadow: snap(950) } : { border: snap(800), input: snap(800), ring: snap(700), shadow: snap(950) };
635
+ const ring = ringBridgeShade(mode, seedShade, activeShades);
636
+ const defaults = mode === "light" ? { border: snap(200), input: snap(200), ring, shadow: snap(950) } : { border: snap(800), input: snap(800), ring, shadow: snap(950) };
590
637
  return {
591
638
  border: neutral[overrides?.border ? snap(overrides.border) : defaults.border],
592
639
  input: neutral[overrides?.input ? snap(overrides.input) : defaults.input],
@@ -670,6 +717,49 @@ function checkSemanticContrastAPCA(tokens) {
670
717
  };
671
718
  return results;
672
719
  }
720
+ var NON_TEXT_CONTRAST_MIN = 3;
721
+ function checkSurfaceContrast(surface, utility) {
722
+ const pair = (bg, fg, nonText = false, advisory = false) => {
723
+ const ratio = getContrastRatio(bg, fg);
724
+ return {
725
+ ratio: Math.round(ratio * 100) / 100,
726
+ passesAA: meetsWCAG_AA(ratio),
727
+ passesAAA: ratio >= 7,
728
+ nonText,
729
+ passes: nonText ? ratio >= NON_TEXT_CONTRAST_MIN : meetsWCAG_AA(ratio),
730
+ advisory
731
+ };
732
+ };
733
+ return {
734
+ "canvas/foreground": pair(surface.canvas, surface["canvas-foreground"]),
735
+ "surface/foreground": pair(surface.surface, surface["surface-foreground"]),
736
+ "overlay/foreground": pair(surface.overlay, surface["overlay-foreground"]),
737
+ "ring/canvas": pair(surface.canvas, utility.ring, true),
738
+ "border/canvas": pair(surface.canvas, utility.border, true, true)
739
+ };
740
+ }
741
+ var LOW_CHROMA_THRESHOLD = 0.04;
742
+ function checkRampChroma(palette) {
743
+ const results = {};
744
+ for (const [name, scale] of Object.entries(palette)) {
745
+ if (typeof scale === "string" || !scale) continue;
746
+ let peak = 0;
747
+ let atShade = 500;
748
+ for (const [shade, color] of Object.entries(scale)) {
749
+ const { c } = parseToOklch(color);
750
+ if (c > peak) {
751
+ peak = c;
752
+ atShade = Number(shade);
753
+ }
754
+ }
755
+ results[name] = {
756
+ peak: Math.round(peak * 1e4) / 1e4,
757
+ atShade,
758
+ low: peak < LOW_CHROMA_THRESHOLD
759
+ };
760
+ }
761
+ return results;
762
+ }
673
763
  function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa") {
674
764
  const { floor } = getContrastThresholds(algorithm, contrastTarget);
675
765
  const skipKeys = /* @__PURE__ */ new Set(["muted"]);
@@ -784,8 +874,8 @@ function generateTokens(brandColors, options) {
784
874
  const darkSemantic = generateSemanticTokens(primitives, "dark", options);
785
875
  const lightSurface = generateSurfaceTokens(primitives, "light", options?.surfaceShadeOverrides?.light, options?.shades);
786
876
  const darkSurface = generateSurfaceTokens(primitives, "dark", options?.surfaceShadeOverrides?.dark, options?.shades);
787
- const lightUtility = generateUtilityTokens(primitives, "light", options?.utilityShadeOverrides?.light, options?.shades);
788
- const darkUtility = generateUtilityTokens(primitives, "dark", options?.utilityShadeOverrides?.dark, options?.shades);
877
+ const lightUtility = generateUtilityTokens(primitives, "light", options?.utilityShadeOverrides?.light, options?.shades, options?.seedShade);
878
+ const darkUtility = generateUtilityTokens(primitives, "dark", options?.utilityShadeOverrides?.dark, options?.shades, options?.seedShade);
789
879
  return {
790
880
  primitives,
791
881
  semantic: {
@@ -816,64 +906,36 @@ function colorScaleToCssVars(name, scale, colorFormat = "hex", indent = " ") {
816
906
  ([shade, color]) => `${indent}--color-${name}-${shade}: ${formatColor(color, colorFormat)};`
817
907
  );
818
908
  }
819
- function emitPrimitiveVars(tokens, colorFormat, want, indent) {
909
+ function emitPrimitiveVars(tokens, colorFormat, want, indent, inFamily, scoped) {
820
910
  const lines = [];
821
911
  const rampsOnly = !want.has("primitives");
822
912
  for (const [name, scale] of Object.entries(tokens.primitives)) {
823
913
  if (typeof scale === "string") {
824
- if (rampsOnly) continue;
914
+ if (rampsOnly || scoped) continue;
825
915
  lines.push(`${indent}--color-${name}: ${formatColor(scale, colorFormat)};`);
826
916
  } else {
917
+ if (!inFamily(name)) continue;
827
918
  lines.push(...colorScaleToCssVars(name, scale, colorFormat, indent));
828
919
  }
829
920
  }
830
921
  return lines;
831
922
  }
832
- function getExtendedMappings(mode, colorName, overrides, active) {
833
- const defaults = DEFAULT_SHADE_MAPPINGS[mode].extended;
834
- const ov = overrides?.[colorName]?.[mode];
835
- const snap = (shade) => snapShade(shade, active);
836
- return {
837
- DEFAULT: snap(ov?.DEFAULT ?? defaults.DEFAULT),
838
- subdued: snap(ov?.subdued ?? defaults.subdued),
839
- highlight: snap(ov?.highlight ?? defaults.highlight)
840
- };
923
+ function getExtendedMappings(mode, colorName, overrides, active, seedShade) {
924
+ return extendedBridgeShades(mode, seedShade, active, overrides?.[colorName]?.[mode]);
841
925
  }
842
- var DEFAULT_SHADE_MAPPINGS = {
843
- light: {
844
- extended: {
845
- DEFAULT: 500,
846
- subdued: 50,
847
- highlight: 700
848
- },
849
- muted: {
850
- DEFAULT: 25,
851
- foreground: 500
852
- }
853
- },
854
- dark: {
855
- extended: {
856
- DEFAULT: 600,
857
- subdued: 200,
858
- highlight: 400
859
- },
860
- muted: {
861
- DEFAULT: 800,
862
- foreground: 200
863
- }
864
- }
926
+ var MUTED_SHADE_MAPPINGS = {
927
+ light: { DEFAULT: 25, foreground: 500 },
928
+ dark: { DEFAULT: 800, foreground: 200 }
865
929
  };
866
930
  var UTILITY_SHADE_MAPPINGS = {
867
931
  light: {
868
932
  border: "neutral-200",
869
933
  input: "neutral-200",
870
- ring: "primary-400",
871
934
  shadow: "neutral-950"
872
935
  },
873
936
  dark: {
874
937
  border: "neutral-800",
875
938
  input: "neutral-800",
876
- ring: "primary-700",
877
939
  shadow: "neutral-950"
878
940
  }
879
941
  };
@@ -919,12 +981,13 @@ function emitSurfaceVars(surface, colorFormat, indent) {
919
981
  }
920
982
  return lines;
921
983
  }
922
- function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active) {
984
+ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, seedShade) {
923
985
  const lines = [];
924
986
  const extendedSemanticNames = ["primary", "neutral", "success", "destructive", "warning"];
925
987
  for (const name of extendedSemanticNames) {
988
+ if (!inFamily(name)) continue;
926
989
  const color = tokens.semantic[mode][name];
927
- const mappings = getExtendedMappings(mode, name, shadeOverrides, active);
990
+ const mappings = getExtendedMappings(mode, name, shadeOverrides, active, seedShade);
928
991
  lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
929
992
  lines.push(`${indent}--${name}-foreground: ${color._refs ? refToVar(color._refs.foreground) : formatColor(color.foreground, colorFormat)};`);
930
993
  lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
@@ -933,9 +996,9 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
933
996
  lines.push(`${indent}--${name}-highlight-foreground: ${color._refs ? refToVar(color._refs["highlight-foreground"]) : formatColor(color["highlight-foreground"], colorFormat)};`);
934
997
  }
935
998
  for (const [name, color] of Object.entries(tokens.semantic[mode])) {
936
- if (!extendedSemanticNames.includes(name) && name !== "muted") {
999
+ if (!extendedSemanticNames.includes(name) && name !== "muted" && inFamily(name)) {
937
1000
  const ext = color;
938
- const mappings = getExtendedMappings(mode, name, shadeOverrides, active);
1001
+ const mappings = getExtendedMappings(mode, name, shadeOverrides, active, seedShade);
939
1002
  lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
940
1003
  lines.push(`${indent}--${name}-foreground: ${ext._refs ? refToVar(ext._refs.foreground) : formatColor(ext.foreground, colorFormat)};`);
941
1004
  lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
@@ -944,41 +1007,52 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
944
1007
  lines.push(`${indent}--${name}-highlight-foreground: ${ext._refs ? refToVar(ext._refs["highlight-foreground"]) : formatColor(ext["highlight-foreground"], colorFormat)};`);
945
1008
  }
946
1009
  }
947
- const muted = DEFAULT_SHADE_MAPPINGS[mode].muted;
948
- lines.push(`${indent}--muted: var(--color-neutral-${snapShade(muted.DEFAULT, active)});`);
949
- lines.push(`${indent}--muted-foreground: var(--color-neutral-${snapShade(muted.foreground, active)});`);
1010
+ if (!scoped || inFamily("neutral")) {
1011
+ const muted = MUTED_SHADE_MAPPINGS[mode];
1012
+ lines.push(`${indent}--muted: var(--color-neutral-${snapShade(muted.DEFAULT, active)});`);
1013
+ lines.push(`${indent}--muted-foreground: var(--color-neutral-${snapShade(muted.foreground, active)});`);
1014
+ }
950
1015
  return lines;
951
1016
  }
952
- function emitUtilityVars(mode, indent, active) {
1017
+ function emitUtilityVars(mode, indent, active, seedShade) {
1018
+ const fixed = UTILITY_SHADE_MAPPINGS[mode];
1019
+ const refs = [
1020
+ ["border", fixed.border],
1021
+ ["input", fixed.input],
1022
+ ["ring", `primary-${ringBridgeShade(mode, seedShade, active)}`],
1023
+ ["shadow", fixed.shadow]
1024
+ ];
953
1025
  const lines = [];
954
- for (const [token, ref] of Object.entries(UTILITY_SHADE_MAPPINGS[mode])) {
1026
+ for (const [token, ref] of refs) {
955
1027
  const [scale, shade] = ref.split(/-(?=\d+$)/);
956
1028
  const snapped = `${scale}-${snapShade(Number(shade), active)}`;
957
1029
  lines.push(`${indent}--${token}: ${primitiveToVar(snapped)};`);
958
1030
  }
959
1031
  return lines;
960
1032
  }
961
- function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shades) {
1033
+ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shades, families, seedShade) {
962
1034
  const lines = [];
963
1035
  const active = resolveShades(shades);
964
1036
  const want = sectionSet(sections);
1037
+ const scoped = !!families && families.length > 0;
1038
+ const inFamily = familyFilter(families);
965
1039
  const wantPrimitives = want.has("primitives") || want.has("ramp");
966
- const wantModes = want.has("semantic") || want.has("surface") || want.has("utility");
1040
+ const wantModes = want.has("semantic") || !scoped && (want.has("surface") || want.has("utility"));
967
1041
  const modeBody = (mode, indent, withComments) => {
968
1042
  const body = [];
969
- if (want.has("surface")) {
1043
+ if (want.has("surface") && !scoped) {
970
1044
  if (withComments) body.push(`${indent}/* Surface */`);
971
1045
  body.push(...emitSurfaceVars(tokens.surface[mode], colorFormat, indent));
972
1046
  if (withComments) body.push("");
973
1047
  }
974
- if (want.has("utility")) {
1048
+ if (want.has("utility") && !scoped) {
975
1049
  if (withComments) body.push(`${indent}/* Utility */`);
976
- body.push(...emitUtilityVars(mode, indent, active));
1050
+ body.push(...emitUtilityVars(mode, indent, active, seedShade));
977
1051
  if (withComments) body.push("");
978
1052
  }
979
1053
  if (want.has("semantic")) {
980
1054
  if (withComments) body.push(`${indent}/* Semantic Colors */`);
981
- body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active));
1055
+ body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, seedShade));
982
1056
  }
983
1057
  while (body.length > 0 && body[body.length - 1] === "") body.pop();
984
1058
  return body;
@@ -986,30 +1060,56 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
986
1060
  if (wantPrimitives) {
987
1061
  lines.push(":root {");
988
1062
  lines.push(" /* Primitive Colors */");
989
- lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " "));
1063
+ lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " ", inFamily, scoped));
990
1064
  lines.push("}");
991
1065
  }
992
1066
  if (wantModes) {
993
1067
  if (wantPrimitives) lines.push("");
994
1068
  lines.push("/* Light Mode */");
995
- lines.push(':root, [data-theme="light"] {');
1069
+ lines.push(`${MODE_SELECTORS.light} {`);
996
1070
  lines.push(...modeBody("light", " ", true));
997
1071
  lines.push("}");
998
1072
  lines.push("");
999
1073
  lines.push("/* Dark Mode */");
1000
- lines.push('[data-theme="dark"] {');
1074
+ lines.push(`${MODE_SELECTORS.dark} {`);
1001
1075
  lines.push(...modeBody("dark", " ", true));
1002
1076
  lines.push("}");
1003
1077
  lines.push("");
1004
1078
  lines.push("/* System Preference: Dark Mode */");
1005
1079
  lines.push("@media (prefers-color-scheme: dark) {");
1006
- lines.push(' :root:not([data-theme="light"]) {');
1080
+ lines.push(` ${MODE_SELECTORS.systemDark} {`);
1007
1081
  lines.push(...modeBody("dark", " ", false));
1008
1082
  lines.push(" }");
1009
1083
  lines.push("}");
1010
1084
  }
1011
1085
  return lines.join("\n");
1012
1086
  }
1087
+ var MODE_SELECTORS = {
1088
+ light: ':root, [data-theme="light"]',
1089
+ dark: '[data-theme="dark"], .dark',
1090
+ systemDark: ':root:not([data-theme="light"]):not(.light)'
1091
+ };
1092
+ function overrideContract() {
1093
+ return [
1094
+ "/*",
1095
+ " * Overriding these tokens: match the selector, not just the layer.",
1096
+ " *",
1097
+ " * These blocks are UNLAYERED. An override inside @layer loses to them",
1098
+ " * regardless of import order. An unlayered override still loses if it is",
1099
+ " * less specific, so match the selector you are overriding:",
1100
+ " *",
1101
+ ` * ${MODE_SELECTORS.light.split(", ")[0]},`,
1102
+ ` * ${MODE_SELECTORS.systemDark} { --canvas: ...; }`,
1103
+ " *",
1104
+ " * Then check BOTH colour schemes. A headless browser defaults to light and",
1105
+ " * will report a broken dark mode as passing.",
1106
+ " *",
1107
+ " * A --color-<name>-<step> ramp step cannot be overridden this way at all:",
1108
+ " * Tailwind inlines its literal value into every utility it emits, so there",
1109
+ " * is no var() left to intercept. Change the seed and regenerate instead.",
1110
+ " */"
1111
+ ].join("\n");
1112
+ }
1013
1113
  var TOKEN_SECTIONS = [
1014
1114
  "primitives",
1015
1115
  "ramp",
@@ -1018,11 +1118,23 @@ var TOKEN_SECTIONS = [
1018
1118
  "utility"
1019
1119
  ];
1020
1120
  var ALL_SECTIONS = [...TOKEN_SECTIONS];
1121
+ function familyFilter(families) {
1122
+ if (!families || families.length === 0) return () => true;
1123
+ const want = /* @__PURE__ */ new Set();
1124
+ for (const name of families) {
1125
+ want.add(name);
1126
+ const other = aliasOf(name);
1127
+ if (other) want.add(other);
1128
+ }
1129
+ return (name) => want.has(name);
1130
+ }
1021
1131
  function sectionSet(sections) {
1022
1132
  return new Set(sections && sections.length > 0 ? sections : ALL_SECTIONS);
1023
1133
  }
1024
- function semanticAliasNames(tokens, sections) {
1134
+ function semanticAliasNames(tokens, sections, families) {
1025
1135
  const want = sectionSet(sections);
1136
+ const scoped = !!families && families.length > 0;
1137
+ const inFamily = familyFilter(families);
1026
1138
  const extended = ["primary", "neutral", "success", "destructive", "warning"];
1027
1139
  const names = [];
1028
1140
  const pushExtended = (n) => {
@@ -1036,13 +1148,13 @@ function semanticAliasNames(tokens, sections) {
1036
1148
  );
1037
1149
  };
1038
1150
  if (want.has("semantic")) {
1039
- for (const name of extended) pushExtended(name);
1151
+ for (const name of extended) if (inFamily(name)) pushExtended(name);
1040
1152
  for (const name of Object.keys(tokens.semantic.light)) {
1041
- if (!extended.includes(name) && name !== "muted") pushExtended(name);
1153
+ if (!extended.includes(name) && name !== "muted" && inFamily(name)) pushExtended(name);
1042
1154
  }
1043
- names.push("muted", "muted-foreground");
1155
+ if (!scoped || inFamily("neutral")) names.push("muted", "muted-foreground");
1044
1156
  }
1045
- if (want.has("surface")) {
1157
+ if (want.has("surface") && !scoped) {
1046
1158
  names.push(
1047
1159
  "canvas",
1048
1160
  "canvas-foreground",
@@ -1052,7 +1164,7 @@ function semanticAliasNames(tokens, sections) {
1052
1164
  "overlay-foreground"
1053
1165
  );
1054
1166
  }
1055
- if (want.has("utility")) {
1167
+ if (want.has("utility") && !scoped) {
1056
1168
  names.push("border", "input", "ring", "shadow");
1057
1169
  }
1058
1170
  return names;
@@ -1066,35 +1178,37 @@ function emitThemeRebinding(aliases, indent) {
1066
1178
  }
1067
1179
  return lines;
1068
1180
  }
1069
- function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active) {
1181
+ function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped, seedShade) {
1070
1182
  const lines = [];
1071
- if (want.has("surface")) {
1183
+ if (want.has("surface") && !scoped) {
1072
1184
  lines.push(`${indent}/* Surface */`);
1073
1185
  lines.push(...emitSurfaceVars(tokens.surface[mode], colorFormat, indent));
1074
1186
  lines.push("");
1075
1187
  }
1076
- if (want.has("utility")) {
1188
+ if (want.has("utility") && !scoped) {
1077
1189
  lines.push(`${indent}/* Utility */`);
1078
- lines.push(...emitUtilityVars(mode, indent, active));
1190
+ lines.push(...emitUtilityVars(mode, indent, active, seedShade));
1079
1191
  lines.push("");
1080
1192
  }
1081
1193
  if (want.has("semantic")) {
1082
1194
  lines.push(`${indent}/* Semantic Colors */`);
1083
- lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active));
1195
+ lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, seedShade));
1084
1196
  }
1085
1197
  while (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
1086
1198
  lines.push(...emitThemeRebinding(aliases, indent));
1087
1199
  return lines;
1088
1200
  }
1089
- function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replacePalette = true, sections, shades) {
1201
+ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replacePalette = true, sections, shades, families, seedShade) {
1090
1202
  const lines = [];
1091
1203
  const active = resolveShades(shades);
1092
1204
  const want = sectionSet(sections);
1093
- const aliases = semanticAliasNames(tokens, sections);
1205
+ const scoped = !!families && families.length > 0;
1206
+ const inFamily = familyFilter(families);
1207
+ const aliases = semanticAliasNames(tokens, sections, families);
1094
1208
  const wantPrimitives = want.has("primitives") || want.has("ramp");
1095
1209
  const wantModes = aliases.length > 0;
1096
1210
  lines.push("@theme static {");
1097
- if (replacePalette) {
1211
+ if (replacePalette && !scoped) {
1098
1212
  lines.push(" /* Clear Tailwind's stock palette, so a colour class this file does not");
1099
1213
  lines.push(" define fails to compile instead of silently rendering off-brand. */");
1100
1214
  lines.push(" --color-*: initial;");
@@ -1103,7 +1217,7 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
1103
1217
  if (wantPrimitives) {
1104
1218
  lines.push(" /* Primitives \u2014 real values, mode-invariant. Deliberately override");
1105
1219
  lines.push(" Tailwind's stock palette: bg-red-500 becomes the project's red. */");
1106
- lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " "));
1220
+ lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " ", inFamily, scoped));
1107
1221
  }
1108
1222
  if (wantModes) {
1109
1223
  if (wantPrimitives) lines.push("");
@@ -1116,19 +1230,19 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
1116
1230
  if (wantModes) {
1117
1231
  lines.push("");
1118
1232
  lines.push("/* Light Mode */");
1119
- lines.push(':root, [data-theme="light"] {');
1120
- lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active));
1233
+ lines.push(`${MODE_SELECTORS.light} {`);
1234
+ lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, seedShade));
1121
1235
  lines.push("}");
1122
1236
  lines.push("");
1123
1237
  lines.push("/* Dark Mode */");
1124
- lines.push('[data-theme="dark"], .dark {');
1125
- lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active));
1238
+ lines.push(`${MODE_SELECTORS.dark} {`);
1239
+ lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, seedShade));
1126
1240
  lines.push("}");
1127
1241
  lines.push("");
1128
1242
  lines.push("/* System Preference: Dark Mode */");
1129
1243
  lines.push("@media (prefers-color-scheme: dark) {");
1130
- lines.push(' :root:not([data-theme="light"]):not(.light) {');
1131
- lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active));
1244
+ lines.push(` ${MODE_SELECTORS.systemDark} {`);
1245
+ lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, seedShade));
1132
1246
  lines.push(" }");
1133
1247
  lines.push("}");
1134
1248
  }
@@ -1296,22 +1410,49 @@ function exportToDtcg(tokens) {
1296
1410
  return JSON.stringify(dtcg, null, 2);
1297
1411
  }
1298
1412
  var SECTIONED_FORMATS = ["css", "tailwind-v4"];
1299
- function selectTokenSections(tokens, sections) {
1300
- if (!sections || sections.length === 0) return tokens;
1413
+ function tokenFamilies(tokens) {
1414
+ return Object.entries(tokens.primitives).filter(([, scale]) => typeof scale !== "string").map(([name]) => name);
1415
+ }
1416
+ function externalReferences(css) {
1417
+ const defined = /* @__PURE__ */ new Set();
1418
+ for (const line of css.split("\n")) {
1419
+ const m = /^\s*(--[a-z0-9-]+)\s*:/i.exec(line);
1420
+ if (m) defined.add(m[1]);
1421
+ }
1422
+ const external = /* @__PURE__ */ new Set();
1423
+ for (const m of css.matchAll(/var\(\s*(--[a-z0-9-]+)/gi)) {
1424
+ if (!defined.has(m[1])) external.add(m[1]);
1425
+ }
1426
+ return [...external].sort();
1427
+ }
1428
+ function selectTokenSections(tokens, sections, families) {
1429
+ const scoped = !!families && families.length > 0;
1430
+ if ((!sections || sections.length === 0) && !scoped) return tokens;
1301
1431
  const want = sectionSet(sections);
1432
+ const inFamily = familyFilter(families);
1302
1433
  const out = {};
1303
1434
  if (want.has("primitives") || want.has("ramp")) {
1304
1435
  const rampsOnly = !want.has("primitives");
1305
1436
  const primitives = {};
1306
1437
  for (const [name, scale] of Object.entries(tokens.primitives)) {
1307
- if (typeof scale === "string" && rampsOnly) continue;
1438
+ if (typeof scale === "string" && (rampsOnly || scoped)) continue;
1439
+ if (typeof scale !== "string" && !inFamily(name)) continue;
1308
1440
  primitives[name] = scale;
1309
1441
  }
1310
1442
  out.primitives = primitives;
1311
1443
  }
1312
- if (want.has("semantic")) out.semantic = tokens.semantic;
1313
- if (want.has("surface")) out.surface = tokens.surface;
1314
- if (want.has("utility")) out.utility = tokens.utility;
1444
+ if (want.has("semantic")) {
1445
+ out.semantic = scoped ? {
1446
+ light: Object.fromEntries(
1447
+ Object.entries(tokens.semantic.light).filter(([n]) => inFamily(n))
1448
+ ),
1449
+ dark: Object.fromEntries(
1450
+ Object.entries(tokens.semantic.dark).filter(([n]) => inFamily(n))
1451
+ )
1452
+ } : tokens.semantic;
1453
+ }
1454
+ if (want.has("surface") && !scoped) out.surface = tokens.surface;
1455
+ if (want.has("utility") && !scoped) out.utility = tokens.utility;
1315
1456
  return out;
1316
1457
  }
1317
1458
  function exportTokens(tokens, format, options) {
@@ -1320,6 +1461,13 @@ function exportTokens(tokens, format, options) {
1320
1461
  const replacePalette = options?.replacePalette !== false;
1321
1462
  const sections = options?.sections;
1322
1463
  const shades = options?.shades;
1464
+ const families = options?.families;
1465
+ const seedShade = options?.seedShade;
1466
+ if (families && families.length > 0 && !SECTIONED_FORMATS.includes(format)) {
1467
+ throw new Error(
1468
+ `Format "${format}" cannot be scoped to colour families; supported: ${SECTIONED_FORMATS.join(", ")}`
1469
+ );
1470
+ }
1323
1471
  if (sections && sections.length > 0 && !SECTIONED_FORMATS.includes(format)) {
1324
1472
  throw new Error(
1325
1473
  `Format "${format}" cannot be filtered by section; supported: ${SECTIONED_FORMATS.join(", ")}`
@@ -1327,11 +1475,11 @@ function exportTokens(tokens, format, options) {
1327
1475
  }
1328
1476
  switch (format) {
1329
1477
  case "css":
1330
- return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades);
1478
+ return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades, families, seedShade);
1331
1479
  case "tailwind-v3":
1332
1480
  return exportToTailwindV3(tokens, colorFormat);
1333
1481
  case "tailwind-v4":
1334
- return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades);
1482
+ return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades, families, seedShade);
1335
1483
  case "dtcg":
1336
1484
  return exportToDtcg(tokens);
1337
1485
  default:
@@ -1339,6 +1487,132 @@ function exportTokens(tokens, format, options) {
1339
1487
  }
1340
1488
  }
1341
1489
 
1490
+ // src/merge.ts
1491
+ function keyOf(block, property) {
1492
+ return `${block} ${property}`;
1493
+ }
1494
+ function normaliseSelector(raw) {
1495
+ return raw.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\s+/g, " ").trim();
1496
+ }
1497
+ function walk(css, onDeclaration) {
1498
+ const lines = css.split("\n");
1499
+ const stack = [];
1500
+ let pending = "";
1501
+ lines.forEach((line, index) => {
1502
+ const trimmed = line.trim();
1503
+ const declaration = /^(--[a-z0-9-]+)\s*:\s*(.+?);\s*$/i.exec(trimmed);
1504
+ if (declaration && stack.length > 0) {
1505
+ onDeclaration(line, index, stack[stack.length - 1], declaration[1], declaration[2]);
1506
+ }
1507
+ for (const char of trimmed) {
1508
+ if (char === "{") {
1509
+ stack.push(normaliseSelector(pending));
1510
+ pending = "";
1511
+ } else if (char === "}") {
1512
+ stack.pop();
1513
+ pending = "";
1514
+ } else {
1515
+ pending += char;
1516
+ }
1517
+ }
1518
+ if (trimmed.endsWith("{") || trimmed.endsWith("}")) pending = "";
1519
+ });
1520
+ }
1521
+ function readDeclarations(css) {
1522
+ const out = [];
1523
+ walk(
1524
+ css,
1525
+ (_line, _index, block, property, value) => out.push({ block, property, value })
1526
+ );
1527
+ return out;
1528
+ }
1529
+ function mergeSlice(target, slice) {
1530
+ const incoming = /* @__PURE__ */ new Map();
1531
+ for (const declaration of readDeclarations(slice)) {
1532
+ incoming.set(keyOf(declaration.block, declaration.property), declaration);
1533
+ }
1534
+ const lines = target.split("\n");
1535
+ const updated = [];
1536
+ const unchanged = [];
1537
+ const seen = /* @__PURE__ */ new Set();
1538
+ walk(target, (line, index, block, property, value) => {
1539
+ const key = keyOf(block, property);
1540
+ const wanted = incoming.get(key);
1541
+ if (!wanted) return;
1542
+ seen.add(key);
1543
+ if (wanted.value === value) {
1544
+ unchanged.push(wanted);
1545
+ return;
1546
+ }
1547
+ lines[index] = line.replace(`${property}: ${value};`, `${property}: ${wanted.value};`);
1548
+ updated.push(wanted);
1549
+ });
1550
+ const additions = [...incoming.entries()].filter(([key]) => !seen.has(key));
1551
+ const added = [];
1552
+ const missingBlocks = /* @__PURE__ */ new Set();
1553
+ if (additions.length > 0) {
1554
+ const lastLineOfBlock = /* @__PURE__ */ new Map();
1555
+ const indentOfBlock = /* @__PURE__ */ new Map();
1556
+ walk(lines.join("\n"), (line, index, block) => {
1557
+ lastLineOfBlock.set(block, index);
1558
+ if (!indentOfBlock.has(block)) {
1559
+ indentOfBlock.set(block, /^\s*/.exec(line)?.[0] ?? " ");
1560
+ }
1561
+ });
1562
+ const byBlock = /* @__PURE__ */ new Map();
1563
+ for (const [, declaration] of additions) {
1564
+ if (!lastLineOfBlock.has(declaration.block)) {
1565
+ missingBlocks.add(declaration.block);
1566
+ continue;
1567
+ }
1568
+ const list = byBlock.get(declaration.block) ?? [];
1569
+ list.push(declaration);
1570
+ byBlock.set(declaration.block, list);
1571
+ }
1572
+ const ordered = [...byBlock.entries()].sort(
1573
+ (a, b) => (lastLineOfBlock.get(b[0]) ?? 0) - (lastLineOfBlock.get(a[0]) ?? 0)
1574
+ );
1575
+ for (const [block, declarations] of ordered) {
1576
+ const at = lastLineOfBlock.get(block);
1577
+ const indent = indentOfBlock.get(block) ?? " ";
1578
+ lines.splice(
1579
+ at + 1,
1580
+ 0,
1581
+ ...declarations.map((d) => `${indent}${d.property}: ${d.value};`)
1582
+ );
1583
+ added.push(...declarations);
1584
+ }
1585
+ }
1586
+ return {
1587
+ css: lines.join("\n"),
1588
+ updated,
1589
+ added,
1590
+ unchanged,
1591
+ missingBlocks: [...missingBlocks].sort()
1592
+ };
1593
+ }
1594
+ function diffSlice(target, slice) {
1595
+ const actual = /* @__PURE__ */ new Map();
1596
+ for (const declaration of readDeclarations(target)) {
1597
+ actual.set(keyOf(declaration.block, declaration.property), declaration.value);
1598
+ }
1599
+ const differing = [];
1600
+ const missing = [];
1601
+ for (const declaration of readDeclarations(slice)) {
1602
+ const found = actual.get(keyOf(declaration.block, declaration.property));
1603
+ if (found === void 0) missing.push(declaration);
1604
+ else if (found !== declaration.value) {
1605
+ differing.push({
1606
+ block: declaration.block,
1607
+ property: declaration.property,
1608
+ expected: declaration.value,
1609
+ actual: found
1610
+ });
1611
+ }
1612
+ }
1613
+ return { matches: differing.length === 0 && missing.length === 0, differing, missing };
1614
+ }
1615
+
1342
1616
  // src/seed.ts
1343
1617
  var GENERATOR_NAME = "@designtools/tokens";
1344
1618
  function stableStringify(value) {
@@ -1407,23 +1681,35 @@ export {
1407
1681
  getOklchString,
1408
1682
  formatColorAs,
1409
1683
  DEFAULT_NEUTRAL_COLOR,
1684
+ ALIAS_PAIRS,
1685
+ aliasOf,
1410
1686
  generatePrimitivePalette,
1411
1687
  getShade,
1688
+ extendedBridgeShades,
1689
+ ringBridgeShade,
1412
1690
  generateSemanticTokens,
1413
1691
  generateSurfaceTokens,
1414
1692
  generateUtilityTokens,
1415
1693
  checkSemanticContrast,
1416
1694
  checkSemanticContrastAPCA,
1695
+ checkSurfaceContrast,
1696
+ checkRampChroma,
1417
1697
  computeAccessibilityScore,
1418
1698
  generateTokens,
1419
1699
  exportToCss,
1700
+ overrideContract,
1420
1701
  TOKEN_SECTIONS,
1421
1702
  exportToTailwindV4,
1422
1703
  exportToTailwindV3,
1423
1704
  exportToDtcg,
1424
1705
  SECTIONED_FORMATS,
1706
+ tokenFamilies,
1707
+ externalReferences,
1425
1708
  selectTokenSections,
1426
1709
  exportTokens,
1710
+ readDeclarations,
1711
+ mergeSlice,
1712
+ diffSlice,
1427
1713
  GENERATOR_NAME,
1428
1714
  stableStringify,
1429
1715
  fingerprint,