@designtools/tokens 0.2.1 → 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.
- package/README.md +336 -116
- package/dist/{chunk-757P7AQA.js → chunk-MCLZSLHE.js} +389 -102
- package/dist/cli.js +201 -13
- package/dist/index.d.ts +110 -10
- package/dist/index.js +15 -1
- package/package.json +37 -3
|
@@ -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
|
|
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
|
|
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
|
-
|
|
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
|
|
843
|
-
light: {
|
|
844
|
-
|
|
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
|
-
|
|
948
|
-
|
|
949
|
-
|
|
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
|
|
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(
|
|
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(
|
|
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(
|
|
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,43 +1178,46 @@ 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 =
|
|
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
|
|
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) {
|
|
1098
|
-
lines.push(" /* Clear Tailwind's stock palette
|
|
1211
|
+
if (replacePalette && !scoped) {
|
|
1212
|
+
lines.push(" /* Clear Tailwind's stock palette, so a colour class this file does not");
|
|
1213
|
+
lines.push(" define fails to compile instead of silently rendering off-brand. */");
|
|
1099
1214
|
lines.push(" --color-*: initial;");
|
|
1100
1215
|
lines.push("");
|
|
1101
1216
|
}
|
|
1102
1217
|
if (wantPrimitives) {
|
|
1103
1218
|
lines.push(" /* Primitives \u2014 real values, mode-invariant. Deliberately override");
|
|
1104
1219
|
lines.push(" Tailwind's stock palette: bg-red-500 becomes the project's red. */");
|
|
1105
|
-
lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " "));
|
|
1220
|
+
lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " ", inFamily, scoped));
|
|
1106
1221
|
}
|
|
1107
1222
|
if (wantModes) {
|
|
1108
1223
|
if (wantPrimitives) lines.push("");
|
|
@@ -1115,19 +1230,19 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
|
|
|
1115
1230
|
if (wantModes) {
|
|
1116
1231
|
lines.push("");
|
|
1117
1232
|
lines.push("/* Light Mode */");
|
|
1118
|
-
lines.push(
|
|
1119
|
-
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));
|
|
1120
1235
|
lines.push("}");
|
|
1121
1236
|
lines.push("");
|
|
1122
1237
|
lines.push("/* Dark Mode */");
|
|
1123
|
-
lines.push(
|
|
1124
|
-
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));
|
|
1125
1240
|
lines.push("}");
|
|
1126
1241
|
lines.push("");
|
|
1127
1242
|
lines.push("/* System Preference: Dark Mode */");
|
|
1128
1243
|
lines.push("@media (prefers-color-scheme: dark) {");
|
|
1129
|
-
lines.push(
|
|
1130
|
-
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));
|
|
1131
1246
|
lines.push(" }");
|
|
1132
1247
|
lines.push("}");
|
|
1133
1248
|
}
|
|
@@ -1295,30 +1410,64 @@ function exportToDtcg(tokens) {
|
|
|
1295
1410
|
return JSON.stringify(dtcg, null, 2);
|
|
1296
1411
|
}
|
|
1297
1412
|
var SECTIONED_FORMATS = ["css", "tailwind-v4"];
|
|
1298
|
-
function
|
|
1299
|
-
|
|
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;
|
|
1300
1431
|
const want = sectionSet(sections);
|
|
1432
|
+
const inFamily = familyFilter(families);
|
|
1301
1433
|
const out = {};
|
|
1302
1434
|
if (want.has("primitives") || want.has("ramp")) {
|
|
1303
1435
|
const rampsOnly = !want.has("primitives");
|
|
1304
1436
|
const primitives = {};
|
|
1305
1437
|
for (const [name, scale] of Object.entries(tokens.primitives)) {
|
|
1306
|
-
if (typeof scale === "string" && rampsOnly) continue;
|
|
1438
|
+
if (typeof scale === "string" && (rampsOnly || scoped)) continue;
|
|
1439
|
+
if (typeof scale !== "string" && !inFamily(name)) continue;
|
|
1307
1440
|
primitives[name] = scale;
|
|
1308
1441
|
}
|
|
1309
1442
|
out.primitives = primitives;
|
|
1310
1443
|
}
|
|
1311
|
-
if (want.has("semantic"))
|
|
1312
|
-
|
|
1313
|
-
|
|
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;
|
|
1314
1456
|
return out;
|
|
1315
1457
|
}
|
|
1316
1458
|
function exportTokens(tokens, format, options) {
|
|
1317
1459
|
const colorFormat = options?.colorFormat ?? "hex";
|
|
1318
1460
|
const shadeOverrides = options?.shadeOverrides;
|
|
1319
|
-
const replacePalette = options?.replacePalette
|
|
1461
|
+
const replacePalette = options?.replacePalette !== false;
|
|
1320
1462
|
const sections = options?.sections;
|
|
1321
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
|
+
}
|
|
1322
1471
|
if (sections && sections.length > 0 && !SECTIONED_FORMATS.includes(format)) {
|
|
1323
1472
|
throw new Error(
|
|
1324
1473
|
`Format "${format}" cannot be filtered by section; supported: ${SECTIONED_FORMATS.join(", ")}`
|
|
@@ -1326,11 +1475,11 @@ function exportTokens(tokens, format, options) {
|
|
|
1326
1475
|
}
|
|
1327
1476
|
switch (format) {
|
|
1328
1477
|
case "css":
|
|
1329
|
-
return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades);
|
|
1478
|
+
return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades, families, seedShade);
|
|
1330
1479
|
case "tailwind-v3":
|
|
1331
1480
|
return exportToTailwindV3(tokens, colorFormat);
|
|
1332
1481
|
case "tailwind-v4":
|
|
1333
|
-
return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades);
|
|
1482
|
+
return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades, families, seedShade);
|
|
1334
1483
|
case "dtcg":
|
|
1335
1484
|
return exportToDtcg(tokens);
|
|
1336
1485
|
default:
|
|
@@ -1338,6 +1487,132 @@ function exportTokens(tokens, format, options) {
|
|
|
1338
1487
|
}
|
|
1339
1488
|
}
|
|
1340
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
|
+
|
|
1341
1616
|
// src/seed.ts
|
|
1342
1617
|
var GENERATOR_NAME = "@designtools/tokens";
|
|
1343
1618
|
function stableStringify(value) {
|
|
@@ -1406,23 +1681,35 @@ export {
|
|
|
1406
1681
|
getOklchString,
|
|
1407
1682
|
formatColorAs,
|
|
1408
1683
|
DEFAULT_NEUTRAL_COLOR,
|
|
1684
|
+
ALIAS_PAIRS,
|
|
1685
|
+
aliasOf,
|
|
1409
1686
|
generatePrimitivePalette,
|
|
1410
1687
|
getShade,
|
|
1688
|
+
extendedBridgeShades,
|
|
1689
|
+
ringBridgeShade,
|
|
1411
1690
|
generateSemanticTokens,
|
|
1412
1691
|
generateSurfaceTokens,
|
|
1413
1692
|
generateUtilityTokens,
|
|
1414
1693
|
checkSemanticContrast,
|
|
1415
1694
|
checkSemanticContrastAPCA,
|
|
1695
|
+
checkSurfaceContrast,
|
|
1696
|
+
checkRampChroma,
|
|
1416
1697
|
computeAccessibilityScore,
|
|
1417
1698
|
generateTokens,
|
|
1418
1699
|
exportToCss,
|
|
1700
|
+
overrideContract,
|
|
1419
1701
|
TOKEN_SECTIONS,
|
|
1420
1702
|
exportToTailwindV4,
|
|
1421
1703
|
exportToTailwindV3,
|
|
1422
1704
|
exportToDtcg,
|
|
1423
1705
|
SECTIONED_FORMATS,
|
|
1706
|
+
tokenFamilies,
|
|
1707
|
+
externalReferences,
|
|
1424
1708
|
selectTokenSections,
|
|
1425
1709
|
exportTokens,
|
|
1710
|
+
readDeclarations,
|
|
1711
|
+
mergeSlice,
|
|
1712
|
+
diffSlice,
|
|
1426
1713
|
GENERATOR_NAME,
|
|
1427
1714
|
stableStringify,
|
|
1428
1715
|
fingerprint,
|