@designtools/tokens 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/README.md +193 -77
- package/dist/{chunk-RQGC7LES.js → chunk-TRGCDLOM.js} +729 -336
- package/dist/cli.js +299 -86
- package/dist/index.d.ts +176 -78
- package/dist/index.js +13 -5
- package/dist/studio/changes.js +56 -0
- package/dist/studio/colour-editor.js +103 -31
- package/dist/studio/css.js +1 -1
- package/dist/studio/ground-controls.d.ts +49 -0
- package/dist/studio/ground-controls.js +251 -0
- package/dist/studio/index.d.ts +1 -1
- package/dist/studio/index.js +1 -1
- package/dist/studio/seed.d.ts +15 -1
- package/dist/studio/seed.js +62 -12
- package/dist/studio/studio.js +39 -1
- package/package.json +3 -3
|
@@ -64,9 +64,9 @@ function snapShade(shade2, active) {
|
|
|
64
64
|
}
|
|
65
65
|
return best;
|
|
66
66
|
}
|
|
67
|
-
function sampleCurve(curve,
|
|
67
|
+
function sampleCurve(curve, t2) {
|
|
68
68
|
if (curve.length === 1) return curve[0];
|
|
69
|
-
const pos =
|
|
69
|
+
const pos = t2 * (curve.length - 1);
|
|
70
70
|
const i = Math.min(Math.floor(pos), curve.length - 2);
|
|
71
71
|
return curve[i] + (curve[i + 1] - curve[i]) * (pos - i);
|
|
72
72
|
}
|
|
@@ -150,26 +150,26 @@ function oklchToHex(color) {
|
|
|
150
150
|
}
|
|
151
151
|
function computeScaleChroma(scaleValue, baseChroma) {
|
|
152
152
|
if (scaleValue < 0.03) {
|
|
153
|
-
const
|
|
154
|
-
return baseChroma * (0.08 + 0.15 * Math.pow(
|
|
153
|
+
const t3 = scaleValue / 0.03;
|
|
154
|
+
return baseChroma * (0.08 + 0.15 * Math.pow(t3, 1.5));
|
|
155
155
|
}
|
|
156
156
|
if (scaleValue < 0.12) {
|
|
157
|
-
const
|
|
158
|
-
return baseChroma * (0.23 + 0.32 * Math.pow(
|
|
157
|
+
const t3 = (scaleValue - 0.03) / 0.09;
|
|
158
|
+
return baseChroma * (0.23 + 0.32 * Math.pow(t3, 1.3));
|
|
159
159
|
}
|
|
160
160
|
if (scaleValue < 0.28) {
|
|
161
|
-
const
|
|
162
|
-
return baseChroma * (0.55 + 0.3 * Math.pow(
|
|
161
|
+
const t3 = (scaleValue - 0.12) / 0.16;
|
|
162
|
+
return baseChroma * (0.55 + 0.3 * Math.pow(t3, 1.1));
|
|
163
163
|
}
|
|
164
164
|
if (scaleValue < 0.45) {
|
|
165
|
-
const
|
|
166
|
-
return baseChroma * (0.85 + 0.15 *
|
|
165
|
+
const t3 = (scaleValue - 0.28) / 0.17;
|
|
166
|
+
return baseChroma * (0.85 + 0.15 * t3);
|
|
167
167
|
}
|
|
168
168
|
if (scaleValue < 0.85) {
|
|
169
169
|
return baseChroma;
|
|
170
170
|
}
|
|
171
|
-
const
|
|
172
|
-
return baseChroma * (1 - 0.06 *
|
|
171
|
+
const t2 = (scaleValue - 0.85) / 0.15;
|
|
172
|
+
return baseChroma * (1 - 0.06 * t2);
|
|
173
173
|
}
|
|
174
174
|
var LIGHTNESS_FACTORS = {
|
|
175
175
|
// Radix-style easing: smooth gradients at the light end, larger jumps at the
|
|
@@ -207,8 +207,8 @@ function refitThroughPins(lightness, anchor, pins) {
|
|
|
207
207
|
const da = held.get(a) - lightness[a];
|
|
208
208
|
const db = held.get(b) - lightness[b];
|
|
209
209
|
for (let i = a; i <= b; i++) {
|
|
210
|
-
const
|
|
211
|
-
out[i] = lightness[i] + da * (1 -
|
|
210
|
+
const t2 = (i - a) / (b - a);
|
|
211
|
+
out[i] = lightness[i] + da * (1 - t2) + db * t2;
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
for (let i = 1; i < n; i++) {
|
|
@@ -386,26 +386,26 @@ function classifyAPCALevel(lcValue) {
|
|
|
386
386
|
}
|
|
387
387
|
function computeScaleChromaPerceptual(scaleValue, baseChroma) {
|
|
388
388
|
if (scaleValue < 0.03) {
|
|
389
|
-
const
|
|
390
|
-
return baseChroma * (0.3 + 0.1 *
|
|
389
|
+
const t3 = scaleValue / 0.03;
|
|
390
|
+
return baseChroma * (0.3 + 0.1 * t3);
|
|
391
391
|
}
|
|
392
392
|
if (scaleValue < 0.12) {
|
|
393
|
-
const
|
|
394
|
-
return baseChroma * (0.4 + 0.25 *
|
|
393
|
+
const t3 = (scaleValue - 0.03) / 0.09;
|
|
394
|
+
return baseChroma * (0.4 + 0.25 * t3);
|
|
395
395
|
}
|
|
396
396
|
if (scaleValue < 0.3) {
|
|
397
|
-
const
|
|
398
|
-
return baseChroma * (0.65 + 0.25 *
|
|
397
|
+
const t3 = (scaleValue - 0.12) / 0.18;
|
|
398
|
+
return baseChroma * (0.65 + 0.25 * t3);
|
|
399
399
|
}
|
|
400
400
|
if (scaleValue < 0.45) {
|
|
401
|
-
const
|
|
402
|
-
return baseChroma * (0.9 + 0.1 *
|
|
401
|
+
const t3 = (scaleValue - 0.3) / 0.15;
|
|
402
|
+
return baseChroma * (0.9 + 0.1 * t3);
|
|
403
403
|
}
|
|
404
404
|
if (scaleValue < 0.85) {
|
|
405
405
|
return baseChroma;
|
|
406
406
|
}
|
|
407
|
-
const
|
|
408
|
-
return baseChroma * (1 - 0.08 *
|
|
407
|
+
const t2 = (scaleValue - 0.85) / 0.15;
|
|
408
|
+
return baseChroma * (1 - 0.08 * t2);
|
|
409
409
|
}
|
|
410
410
|
function formatColorShort(color, format) {
|
|
411
411
|
const full = formatColorAs(color, format);
|
|
@@ -553,7 +553,7 @@ function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed", opti
|
|
|
553
553
|
else if (overrodeAlias && !overrodeSemantic) palette[semantic] = palette[alias];
|
|
554
554
|
}
|
|
555
555
|
}
|
|
556
|
-
if (options?.aliases
|
|
556
|
+
if (options?.aliases !== true) for (const [, alias] of ALIAS_PAIRS) delete palette[alias];
|
|
557
557
|
return palette;
|
|
558
558
|
}
|
|
559
559
|
function getShade(palette, colorName, shade2) {
|
|
@@ -790,27 +790,14 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
|
|
|
790
790
|
});
|
|
791
791
|
const canvas = pageGrounds[0];
|
|
792
792
|
const whitePage = mode === "light" && !!canvas && parseToOklch(canvas).l > parseToOklch(neutral[snapShade(25, activeShades)]).l + 5e-3;
|
|
793
|
-
const
|
|
794
|
-
const
|
|
795
|
-
neutral,
|
|
796
|
-
mode === "light" ? 500 : 200,
|
|
797
|
-
mode,
|
|
798
|
-
[neutral[mutedBg]],
|
|
799
|
-
contrastTarget === "aaa" ? 7 : 4.5,
|
|
800
|
-
activeShades
|
|
801
|
-
);
|
|
802
|
-
const muted = {
|
|
803
|
-
DEFAULT: neutral[mutedBg],
|
|
804
|
-
foreground: neutral[mutedFg],
|
|
805
|
-
_shades: { DEFAULT: mutedBg, foreground: mutedFg }
|
|
806
|
-
};
|
|
793
|
+
const quietPanel = snapShade(mode === "light" ? whitePage ? 50 : 100 : 800, activeShades);
|
|
794
|
+
const neutralOverrides = { subdued: quietPanel, ...shadeOverrides.neutral?.[mode] };
|
|
807
795
|
const tokens = {
|
|
808
796
|
primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades, anchor("primary"), gamutsFor("primary"), options?.foreground, options?.wcagFloor, pageGrounds),
|
|
809
|
-
neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget,
|
|
797
|
+
neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, neutralOverrides, "neutral", activeShades, anchor("neutral"), gamutsFor("neutral"), options?.foreground, options?.wcagFloor, pageGrounds),
|
|
810
798
|
success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades, anchor("success"), gamutsFor("success"), options?.foreground, options?.wcagFloor, pageGrounds),
|
|
811
799
|
destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades, anchor("destructive"), gamutsFor("destructive"), options?.foreground, options?.wcagFloor, pageGrounds),
|
|
812
|
-
warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, anchor("warning"), gamutsFor("warning"), options?.foreground, options?.wcagFloor, pageGrounds)
|
|
813
|
-
muted
|
|
800
|
+
warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, anchor("warning"), gamutsFor("warning"), options?.foreground, options?.wcagFloor, pageGrounds)
|
|
814
801
|
};
|
|
815
802
|
const coreColors = [
|
|
816
803
|
"black",
|
|
@@ -847,6 +834,27 @@ function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }
|
|
|
847
834
|
}
|
|
848
835
|
return tokens;
|
|
849
836
|
}
|
|
837
|
+
function surfaceStep(name, raw, palette) {
|
|
838
|
+
const m = /^([a-z][a-z0-9-]*)@(\d+)$/.exec(raw.trim());
|
|
839
|
+
if (!m) throw new Error(`Page surface "${name}": expected <ramp>@<step> (got "${raw}")`);
|
|
840
|
+
const shadeNumber = Number(m[2]);
|
|
841
|
+
const scale = palette[m[1]];
|
|
842
|
+
if (!scale || typeof scale === "string") throw new Error(`Page surface "${name}" names no ramp: ${m[1]}`);
|
|
843
|
+
if (!(shadeNumber in scale)) throw new Error(`Page surface "${name}" names no step: ${m[2]}`);
|
|
844
|
+
return { colorName: m[1], shade: shadeNumber };
|
|
845
|
+
}
|
|
846
|
+
function mirrorOf(raw) {
|
|
847
|
+
const m = /^(.*)@(\d+)$/.exec(raw.trim());
|
|
848
|
+
if (!m) return raw;
|
|
849
|
+
const steps = [25, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
|
|
850
|
+
const i = steps.indexOf(Number(m[2]));
|
|
851
|
+
return i === -1 ? raw : `${m[1]}@${steps[steps.length - 1 - i]}`;
|
|
852
|
+
}
|
|
853
|
+
function secondaryOn(bg, neutral, floor, active) {
|
|
854
|
+
const dark = parseToOklch(bg).l < 0.6;
|
|
855
|
+
const step = accessibleStep(neutral, snapShade(dark ? 200 : 500, active), dark ? "dark" : "light", [bg], floor, active);
|
|
856
|
+
return { value: neutral[step], ref: { type: "shade", shade: step } };
|
|
857
|
+
}
|
|
850
858
|
function textOn(bg, neutral, white, black, active) {
|
|
851
859
|
const light = getContrastRatio(bg, black) > getContrastRatio(bg, white);
|
|
852
860
|
if (!light) return { value: white, ref: { type: "white" } };
|
|
@@ -854,7 +862,7 @@ function textOn(bg, neutral, white, black, active) {
|
|
|
854
862
|
if (getContrastRatio(bg, neutral[step]) >= 4.5) return { value: neutral[step], ref: { type: "shade", shade: step } };
|
|
855
863
|
return { value: black, ref: { type: "black" } };
|
|
856
864
|
}
|
|
857
|
-
function generateSurfaceTokens(palette, mode, overrides, shades, colors,
|
|
865
|
+
function generateSurfaceTokens(palette, mode, overrides, shades, colors, pageSurfaces, textFloor = 4.5) {
|
|
858
866
|
const activeShades = shadeKeys(shades);
|
|
859
867
|
const snap = (shade2) => snapShade(shade2, activeShades);
|
|
860
868
|
const neutral = palette.neutral;
|
|
@@ -864,7 +872,7 @@ function generateSurfaceTokens(palette, mode, overrides, shades, colors, backgro
|
|
|
864
872
|
const black = palette.black;
|
|
865
873
|
const white = palette.white;
|
|
866
874
|
const defaults = mode === "light" ? { canvas: snap(25), "canvas-foreground": snap(800), surface: "white", "surface-foreground": snap(800), overlay: "white", "overlay-foreground": snap(800) } : { canvas: snap(950), "canvas-foreground": "white", surface: "black", "surface-foreground": "white", overlay: "black", "overlay-foreground": "white" };
|
|
867
|
-
function
|
|
875
|
+
function resolve2(key) {
|
|
868
876
|
const raw = overrides?.[key] ?? defaults[key];
|
|
869
877
|
if (raw === "white") return { value: white, ref: { type: "white" } };
|
|
870
878
|
if (raw === "black") return { value: black, ref: { type: "black" } };
|
|
@@ -873,36 +881,54 @@ function generateSurfaceTokens(palette, mode, overrides, shades, colors, backgro
|
|
|
873
881
|
}
|
|
874
882
|
const ground = (key) => {
|
|
875
883
|
const given = colors?.[key];
|
|
876
|
-
if (!given) return { bg:
|
|
884
|
+
if (!given) return { bg: resolve2(key), fg: resolve2(`${key}-foreground`) };
|
|
877
885
|
const bg = { value: given, ref: { type: "value", value: given } };
|
|
878
|
-
return { bg, fg: overrides?.[`${key}-foreground`] ?
|
|
886
|
+
return { bg, fg: overrides?.[`${key}-foreground`] ? resolve2(`${key}-foreground`) : textOn(given, neutral, white, black, activeShades) };
|
|
879
887
|
};
|
|
880
888
|
const { bg: canvas, fg: canvasFg } = ground("canvas");
|
|
881
889
|
const { bg: surface, fg: surfaceFg } = ground("surface");
|
|
882
890
|
const { bg: overlay, fg: overlayFg } = ground("overlay");
|
|
883
|
-
const
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
891
|
+
const secondary = (key, bg) => {
|
|
892
|
+
const set = overrides?.[`${key}-foreground-secondary`];
|
|
893
|
+
if (set !== void 0) return { value: neutral[snap(set)], ref: { type: "shade", shade: snap(set) } };
|
|
894
|
+
return secondaryOn(bg, neutral, textFloor, activeShades);
|
|
895
|
+
};
|
|
896
|
+
const canvasQuiet = secondary("canvas", canvas.value);
|
|
897
|
+
const surfaceQuiet = secondary("surface", surface.value);
|
|
898
|
+
const overlayQuiet = secondary("overlay", overlay.value);
|
|
899
|
+
const extra = {};
|
|
900
|
+
for (const name of Object.keys(pageSurfaces ?? {}).sort()) {
|
|
901
|
+
const def = pageSurfaces[name];
|
|
902
|
+
const raw = mode === "dark" ? def.dark ?? mirrorOf(def.light) : def.light;
|
|
903
|
+
const at = raw.includes("@") ? surfaceStep(name, raw, palette) : void 0;
|
|
904
|
+
const value = at ? palette[at.colorName][at.shade] : raw;
|
|
905
|
+
const ref = at ? { type: "shade", shade: at.shade, colorName: at.colorName } : { type: "value", value: raw };
|
|
887
906
|
const fg = textOn(value, neutral, white, black, activeShades);
|
|
888
|
-
|
|
907
|
+
const quiet = secondaryOn(value, neutral, textFloor, activeShades);
|
|
908
|
+
extra[name] = { DEFAULT: value, foreground: fg.value, "foreground-secondary": quiet.value, _refs: { DEFAULT: ref, foreground: fg.ref, "foreground-secondary": quiet.ref } };
|
|
889
909
|
}
|
|
890
910
|
return {
|
|
891
911
|
canvas: canvas.value,
|
|
892
912
|
"canvas-foreground": canvasFg.value,
|
|
913
|
+
"canvas-foreground-secondary": canvasQuiet.value,
|
|
893
914
|
surface: surface.value,
|
|
894
915
|
"surface-foreground": surfaceFg.value,
|
|
916
|
+
"surface-foreground-secondary": surfaceQuiet.value,
|
|
895
917
|
overlay: overlay.value,
|
|
896
918
|
"overlay-foreground": overlayFg.value,
|
|
919
|
+
"overlay-foreground-secondary": overlayQuiet.value,
|
|
897
920
|
_refs: {
|
|
898
921
|
canvas: canvas.ref,
|
|
899
922
|
"canvas-foreground": canvasFg.ref,
|
|
923
|
+
"canvas-foreground-secondary": canvasQuiet.ref,
|
|
900
924
|
surface: surface.ref,
|
|
901
925
|
"surface-foreground": surfaceFg.ref,
|
|
926
|
+
"surface-foreground-secondary": surfaceQuiet.ref,
|
|
902
927
|
overlay: overlay.ref,
|
|
903
|
-
"overlay-foreground": overlayFg.ref
|
|
928
|
+
"overlay-foreground": overlayFg.ref,
|
|
929
|
+
"overlay-foreground-secondary": overlayQuiet.ref
|
|
904
930
|
},
|
|
905
|
-
...Object.keys(
|
|
931
|
+
...Object.keys(extra).length ? { pageSurfaces: extra } : {}
|
|
906
932
|
};
|
|
907
933
|
}
|
|
908
934
|
function generateUtilityTokens(palette, mode, overrides, shades, seedShade, grounds) {
|
|
@@ -935,9 +961,6 @@ function generateUtilityTokens(palette, mode, overrides, shades, seedShade, grou
|
|
|
935
961
|
function checkSemanticContrast(tokens, gamut = "srgb") {
|
|
936
962
|
const results = {};
|
|
937
963
|
for (const [name, color] of Object.entries(tokens)) {
|
|
938
|
-
if (name === "muted") {
|
|
939
|
-
continue;
|
|
940
|
-
}
|
|
941
964
|
const extendedColor = color;
|
|
942
965
|
const ratio = getContrastRatio(
|
|
943
966
|
extendedColor.DEFAULT,
|
|
@@ -960,24 +983,11 @@ function checkSemanticContrast(tokens, gamut = "srgb") {
|
|
|
960
983
|
passesAAA: subduedRatio >= 7
|
|
961
984
|
};
|
|
962
985
|
}
|
|
963
|
-
const mutedRatio = getContrastRatio(
|
|
964
|
-
tokens.muted.DEFAULT,
|
|
965
|
-
tokens.muted.foreground,
|
|
966
|
-
gamut
|
|
967
|
-
);
|
|
968
|
-
results["muted"] = {
|
|
969
|
-
ratio: Math.round(mutedRatio * 100) / 100,
|
|
970
|
-
passesAA: meetsWCAG_AA(mutedRatio),
|
|
971
|
-
passesAAA: mutedRatio >= 7
|
|
972
|
-
};
|
|
973
986
|
return results;
|
|
974
987
|
}
|
|
975
988
|
function checkSemanticContrastAPCA(tokens, gamut = "srgb") {
|
|
976
989
|
const results = {};
|
|
977
990
|
for (const [name, color] of Object.entries(tokens)) {
|
|
978
|
-
if (name === "muted") {
|
|
979
|
-
continue;
|
|
980
|
-
}
|
|
981
991
|
const ext = color;
|
|
982
992
|
const defaultLc = getAPCAContrast(ext.foreground, ext.DEFAULT, gamut);
|
|
983
993
|
results[`${name}/default`] = {
|
|
@@ -990,11 +1000,6 @@ function checkSemanticContrastAPCA(tokens, gamut = "srgb") {
|
|
|
990
1000
|
level: classifyAPCALevel(subduedLc)
|
|
991
1001
|
};
|
|
992
1002
|
}
|
|
993
|
-
const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT, gamut);
|
|
994
|
-
results["muted"] = {
|
|
995
|
-
lcValue: Math.round(mutedLc * 10) / 10,
|
|
996
|
-
level: classifyAPCALevel(mutedLc)
|
|
997
|
-
};
|
|
998
1003
|
return results;
|
|
999
1004
|
}
|
|
1000
1005
|
var NON_TEXT_CONTRAST_MIN = 3;
|
|
@@ -1014,6 +1019,9 @@ function checkSurfaceContrast(surface, utility, gamut = "srgb") {
|
|
|
1014
1019
|
"canvas/foreground": pair(surface.canvas, surface["canvas-foreground"]),
|
|
1015
1020
|
"surface/foreground": pair(surface.surface, surface["surface-foreground"]),
|
|
1016
1021
|
"overlay/foreground": pair(surface.overlay, surface["overlay-foreground"]),
|
|
1022
|
+
"canvas/foreground-secondary": pair(surface.canvas, surface["canvas-foreground-secondary"]),
|
|
1023
|
+
"surface/foreground-secondary": pair(surface.surface, surface["surface-foreground-secondary"]),
|
|
1024
|
+
"overlay/foreground-secondary": pair(surface.overlay, surface["overlay-foreground-secondary"]),
|
|
1017
1025
|
"ring/canvas": pair(surface.canvas, utility.ring, true),
|
|
1018
1026
|
"border/canvas": pair(surface.canvas, utility.border, true, true)
|
|
1019
1027
|
};
|
|
@@ -1042,7 +1050,7 @@ function checkRampChroma(palette) {
|
|
|
1042
1050
|
}
|
|
1043
1051
|
function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa", gamut = "srgb") {
|
|
1044
1052
|
const { floor } = getContrastThresholds(algorithm, contrastTarget);
|
|
1045
|
-
const skipKeys = /* @__PURE__ */ new Set(
|
|
1053
|
+
const skipKeys = /* @__PURE__ */ new Set();
|
|
1046
1054
|
let passed = 0;
|
|
1047
1055
|
let total = 0;
|
|
1048
1056
|
if (algorithm === "wcag") {
|
|
@@ -1071,46 +1079,44 @@ function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa", gam
|
|
|
1071
1079
|
}
|
|
1072
1080
|
|
|
1073
1081
|
// src/grounds.ts
|
|
1074
|
-
var
|
|
1075
|
-
|
|
1082
|
+
var GROUND_BASE_TOKENS = [
|
|
1083
|
+
"canvas",
|
|
1084
|
+
"canvas-foreground",
|
|
1085
|
+
"canvas-foreground-secondary",
|
|
1086
|
+
"border",
|
|
1087
|
+
"input",
|
|
1088
|
+
"ring",
|
|
1089
|
+
"surface",
|
|
1090
|
+
"surface-foreground",
|
|
1091
|
+
"surface-foreground-secondary"
|
|
1092
|
+
];
|
|
1076
1093
|
var GROUND_COLOR_PARTS = ["", "-foreground", "-subdued", "-subdued-foreground", "-hover", "-active"];
|
|
1077
1094
|
var FILL_FLOOR = 3;
|
|
1078
1095
|
var PANEL_FLOOR = 1.1;
|
|
1079
1096
|
var TEXT_FLOOR = { "aa-large": 3, aa: 4.5, aaa: 7 };
|
|
1097
|
+
var BODY_AIM = 7;
|
|
1080
1098
|
function groundTokenNames(colors) {
|
|
1081
1099
|
return [...GROUND_BASE_TOKENS, ...colors.flatMap((c) => GROUND_COLOR_PARTS.map((p) => `${c}${p}`))];
|
|
1082
1100
|
}
|
|
1083
1101
|
function semanticColors(semantic) {
|
|
1084
1102
|
const core = ["primary", "neutral", "success", "destructive", "warning"];
|
|
1085
|
-
return [...core, ...Object.keys(semantic).filter((n) => !core.includes(n)
|
|
1103
|
+
return [...core, ...Object.keys(semantic).filter((n) => !core.includes(n))];
|
|
1086
1104
|
}
|
|
1087
|
-
function
|
|
1105
|
+
function reservedSurfaceNames(colors) {
|
|
1088
1106
|
return [
|
|
1089
1107
|
"canvas",
|
|
1090
1108
|
"surface",
|
|
1091
1109
|
"overlay",
|
|
1092
1110
|
"foreground",
|
|
1093
|
-
"muted",
|
|
1094
1111
|
"border",
|
|
1095
1112
|
"input",
|
|
1096
1113
|
"ring",
|
|
1097
1114
|
"shadow",
|
|
1115
|
+
"white",
|
|
1116
|
+
"black",
|
|
1098
1117
|
...colors.flatMap((c) => [c, `${c}-subdued`])
|
|
1099
1118
|
];
|
|
1100
1119
|
}
|
|
1101
|
-
function assertDistinctGroundNames(grounds, colors) {
|
|
1102
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1103
|
-
for (const ground of grounds) {
|
|
1104
|
-
for (const token of groundTokenNames(colors)) {
|
|
1105
|
-
const name = `--on-${ground}-${token}`;
|
|
1106
|
-
const other = seen.get(name);
|
|
1107
|
-
if (other && other !== ground) {
|
|
1108
|
-
throw new Error(`Grounds "${other}" and "${ground}" would both emit ${name}; rename one of the backgrounds`);
|
|
1109
|
-
}
|
|
1110
|
-
seen.set(name, ground);
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
1120
|
var valueOf = (ref, palette) => {
|
|
1115
1121
|
switch (ref.type) {
|
|
1116
1122
|
case "white":
|
|
@@ -1132,24 +1138,25 @@ function fromSurfaceRef(ref) {
|
|
|
1132
1138
|
case "black":
|
|
1133
1139
|
return { type: "black" };
|
|
1134
1140
|
case "shade":
|
|
1135
|
-
return shade("neutral", ref.shade);
|
|
1141
|
+
return shade(ref.colorName ?? "neutral", ref.shade);
|
|
1136
1142
|
case "value":
|
|
1137
1143
|
return { type: "value", value: ref.value };
|
|
1138
1144
|
}
|
|
1139
1145
|
}
|
|
1140
|
-
function baseRefs(base, colors,
|
|
1146
|
+
function baseRefs(base, colors, active) {
|
|
1141
1147
|
const { semantic, surface, utility } = base;
|
|
1142
1148
|
const refs = {};
|
|
1143
1149
|
const sr = surface._refs;
|
|
1144
|
-
refs.canvas = fromSurfaceRef(
|
|
1145
|
-
refs["canvas-foreground"] = fromSurfaceRef(
|
|
1146
|
-
|
|
1147
|
-
refs.muted = shade("neutral", snapShade(muted.DEFAULT, active));
|
|
1148
|
-
refs["muted-foreground"] = shade("neutral", snapShade(muted.foreground, active));
|
|
1150
|
+
refs.canvas = fromSurfaceRef(sr.canvas);
|
|
1151
|
+
refs["canvas-foreground"] = fromSurfaceRef(sr["canvas-foreground"]);
|
|
1152
|
+
refs["canvas-foreground-secondary"] = fromSurfaceRef(sr["canvas-foreground-secondary"]);
|
|
1149
1153
|
const steps = utility._steps;
|
|
1150
1154
|
refs.border = shade("neutral", steps.border);
|
|
1151
1155
|
refs.input = shade("neutral", steps.input);
|
|
1152
1156
|
refs.ring = shade("primary", steps.ring);
|
|
1157
|
+
refs.surface = fromSurfaceRef(sr.surface);
|
|
1158
|
+
refs["surface-foreground"] = fromSurfaceRef(sr["surface-foreground"]);
|
|
1159
|
+
refs["surface-foreground-secondary"] = fromSurfaceRef(sr["surface-foreground-secondary"]);
|
|
1153
1160
|
for (const name of colors) {
|
|
1154
1161
|
const c = semantic[name];
|
|
1155
1162
|
refs[name] = shade(name, c._shades.DEFAULT);
|
|
@@ -1161,30 +1168,65 @@ function baseRefs(base, colors, ground, active) {
|
|
|
1161
1168
|
}
|
|
1162
1169
|
return refs;
|
|
1163
1170
|
}
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
const
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
const
|
|
1174
|
-
if ((ref.c ?? 0) < 0.03) continue;
|
|
1175
|
-
const d = Math.abs((((ref.h ?? 0) - (o.h ?? 0)) % 360 + 540) % 360 - 180);
|
|
1171
|
+
function suggestStep(color, options) {
|
|
1172
|
+
const o = parseToOklch(color);
|
|
1173
|
+
const reference = generateColorScale(oklchToHex({ mode: "oklch", l: 0.6, c: Math.max(o.c ?? 0, 0.01), h: o.h ?? 0 }), {
|
|
1174
|
+
lightEnd: options?.lightnessRange?.light,
|
|
1175
|
+
darkEnd: options?.lightnessRange?.dark
|
|
1176
|
+
});
|
|
1177
|
+
let best = 500;
|
|
1178
|
+
let distance = Infinity;
|
|
1179
|
+
for (const step of VALID_SHADES) {
|
|
1180
|
+
const d = Math.abs(parseToOklch(reference[step]).l - o.l);
|
|
1176
1181
|
if (d < distance) {
|
|
1177
1182
|
distance = d;
|
|
1178
|
-
best =
|
|
1183
|
+
best = step;
|
|
1179
1184
|
}
|
|
1180
1185
|
}
|
|
1181
1186
|
return best;
|
|
1182
1187
|
}
|
|
1188
|
+
function rampRefs(ramp, step, light, palette, active) {
|
|
1189
|
+
const fill = shade(ramp, snapShade(step, active));
|
|
1190
|
+
const fillValue = valueOf(fill, palette);
|
|
1191
|
+
const label = getContrastRatio(fillValue, palette.white) >= getContrastRatio(fillValue, palette.black) ? { type: "white" } : { type: "black" };
|
|
1192
|
+
return {
|
|
1193
|
+
[ramp]: fill,
|
|
1194
|
+
[`${ramp}-foreground`]: label,
|
|
1195
|
+
[`${ramp}-hover`]: { type: "value", value: fillValue },
|
|
1196
|
+
[`${ramp}-active`]: { type: "value", value: fillValue },
|
|
1197
|
+
[`${ramp}-subdued`]: shade(ramp, snapShade(light ? 100 : 900, active)),
|
|
1198
|
+
[`${ramp}-subdued-foreground`]: shade(ramp, snapShade(light ? 700 : 300, active))
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
function fillRef(fill, palette) {
|
|
1202
|
+
if (fill === "white") return { type: "white" };
|
|
1203
|
+
if (fill === "black") return { type: "black" };
|
|
1204
|
+
const m = /^([a-z][a-z0-9-]*)-(\d+)$/.exec(fill);
|
|
1205
|
+
if (!m) throw new Error(`"${fill}" is not page, own, white, black or a step (<ramp>-<step>)`);
|
|
1206
|
+
const step = Number(m[2]);
|
|
1207
|
+
const scale = palette[m[1]];
|
|
1208
|
+
if (!scale || typeof scale === "string") throw new Error(`"${fill}" names no ramp: ${m[1]}`);
|
|
1209
|
+
if (!VALID_SHADES.includes(step)) throw new Error(`"${fill}" names no step: ${m[2]}`);
|
|
1210
|
+
return shade(m[1], step);
|
|
1211
|
+
}
|
|
1212
|
+
function statesFor(fill, label, palette, active) {
|
|
1213
|
+
if (fill.type === "white") return [shade("neutral", snapShade(50, active)), shade("neutral", snapShade(100, active))];
|
|
1214
|
+
if (fill.type === "black") return [shade("neutral", snapShade(900, active)), shade("neutral", snapShade(800, active))];
|
|
1215
|
+
if (fill.type !== "shade") return [fill, fill];
|
|
1216
|
+
const scale = palette[fill.colorName];
|
|
1217
|
+
const steps = active.filter((s) => scale[s] !== void 0);
|
|
1218
|
+
const at = steps.indexOf(fill.shade);
|
|
1219
|
+
const towards = parseToOklch(valueOf(label, palette)).l > parseToOklch(scale[fill.shade]).l ? -1 : 1;
|
|
1220
|
+
const pick = (n) => {
|
|
1221
|
+
const i = at + towards * n;
|
|
1222
|
+
return shade(fill.colorName, steps[i >= 0 && i < steps.length ? i : Math.max(0, Math.min(steps.length - 1, at - towards * n))]);
|
|
1223
|
+
};
|
|
1224
|
+
return [pick(1), pick(2)];
|
|
1225
|
+
}
|
|
1183
1226
|
function searchGround(spec, base, baseCanvas, colors, views, mode, options, active) {
|
|
1184
1227
|
const target = options?.contrastTarget ?? "aa";
|
|
1185
1228
|
const algorithm = options?.contrastAlgorithm ?? "wcag";
|
|
1186
1229
|
const textFloor = TEXT_FLOOR[target];
|
|
1187
|
-
const mutedFloor = target === "aaa" ? 7 : 4.5;
|
|
1188
1230
|
const srgb = views[0].palette;
|
|
1189
1231
|
const notes = [];
|
|
1190
1232
|
const cr = (a, b) => Math.min(...views.map((v) => getContrastRatio(valueOf(a, v.palette), valueOf(b, v.palette), v.gamut)));
|
|
@@ -1199,8 +1241,7 @@ function searchGround(spec, base, baseCanvas, colors, views, mode, options, acti
|
|
|
1199
1241
|
const tinted = spec.family !== "neutral";
|
|
1200
1242
|
const own = steps(spec.family).filter(farSide);
|
|
1201
1243
|
const pool = own.length >= 3 || tinted && own.length ? own : [...own, ...steps("neutral").filter(farSide)];
|
|
1202
|
-
const
|
|
1203
|
-
const closest = (list, to, aim2) => [...list].sort((a, b) => Math.abs(cr(a, to) - aim2) - Math.abs(cr(b, to) - aim2))[0];
|
|
1244
|
+
const closest = (list, to, aim) => [...list].sort((a, b) => Math.abs(cr(a, to) - aim) - Math.abs(cr(b, to) - aim))[0];
|
|
1204
1245
|
const panelCandidates = (list, vis) => {
|
|
1205
1246
|
const drawn = list.filter((r) => cr(r, ground) >= PANEL_FLOOR);
|
|
1206
1247
|
const byVis = (a, b) => Math.abs(cr(a, ground) - vis) - Math.abs(cr(b, ground) - vis);
|
|
@@ -1218,39 +1259,19 @@ function searchGround(spec, base, baseCanvas, colors, views, mode, options, acti
|
|
|
1218
1259
|
const refs = {};
|
|
1219
1260
|
refs.canvas = ground;
|
|
1220
1261
|
refs["canvas-foreground"] = spec.foreground;
|
|
1221
|
-
const fgContrast = cr(spec.foreground, ground);
|
|
1222
|
-
const mutedVis = Math.max(cr(base.muted, baseCanvas), PANEL_FLOOR);
|
|
1223
|
-
const mutedPanels = [
|
|
1224
|
-
...keepsPlace(base.muted) ? [base.muted] : [],
|
|
1225
|
-
...panelCandidates(spec.family === "neutral" ? steps("neutral") : steps(spec.family), mutedVis)
|
|
1226
|
-
];
|
|
1227
|
-
const aim = Math.max(mutedFloor, Math.min(7, fgContrast * 0.65));
|
|
1228
|
-
const quietOn = (panel) => {
|
|
1229
|
-
const reads = (r) => cr(r, ground) >= mutedFloor && (!panel || cr(r, panel) >= mutedFloor) && cr(r, ground) < fgContrast;
|
|
1230
|
-
if (inHue(base["muted-foreground"]) && reads(base["muted-foreground"]) && keepsPlace(base.muted)) return base["muted-foreground"];
|
|
1231
|
-
return closest(textPool.filter((r) => reads(r) && cr(r, ground) < fgContrast * 0.92), ground, aim);
|
|
1232
|
-
};
|
|
1233
|
-
const settled = mutedPanels.map((panel) => ({ panel, text: quietOn(panel) })).find((m) => m.text);
|
|
1234
|
-
refs.muted = settled?.panel ?? mutedPanels[0] ?? base.muted;
|
|
1235
|
-
const quiet = settled?.text ?? quietOn(void 0);
|
|
1236
|
-
if (quiet) {
|
|
1237
|
-
refs["muted-foreground"] = quiet;
|
|
1238
|
-
if (!settled) notes.push(`muted-foreground: reads on the ground but not on a muted panel at ${mutedFloor}:1`);
|
|
1239
|
-
} else {
|
|
1240
|
-
refs["muted-foreground"] = spec.foreground;
|
|
1241
|
-
notes.push(`muted-foreground: no step quieter than the body text reads at ${mutedFloor}:1, so it is the body colour`);
|
|
1242
|
-
}
|
|
1243
1262
|
const borderVis = cr(base.border, baseCanvas);
|
|
1244
1263
|
const borderReads = (r) => cr(r, ground) >= Math.min(1.15, borderVis);
|
|
1245
|
-
|
|
1264
|
+
const ownRamp = tinted;
|
|
1265
|
+
const farBorder = ownRamp ? closest(steps(spec.family).filter((r) => farSide(r) && borderReads(r) && cr(r, ground) <= borderVis * 2), ground, borderVis) : void 0;
|
|
1266
|
+
refs.border = keepsPlace(base.border) && cr(base.border, ground) >= borderVis * 0.9 && cr(base.border, ground) <= borderVis * 2 ? base.border : farBorder ?? closest([...pool, ...steps(spec.family)].filter(borderReads), ground, borderVis) ?? base.border;
|
|
1246
1267
|
refs.input = inHue(base.input) && cr(base.input, ground) >= FILL_FLOOR ? base.input : [...pool].filter((r) => cr(r, ground) >= FILL_FLOOR).sort((a, b) => cr(a, ground) - cr(b, ground))[0] ?? bestEnd(ground);
|
|
1247
1268
|
const ringFamily = tinted ? spec.family : "primary";
|
|
1248
1269
|
const ringFrom = tinted ? base[spec.family].shade : base.ring.shade;
|
|
1249
1270
|
refs.ring = inHue(base.ring) && cr(base.ring, ground) >= FILL_FLOOR ? base.ring : nearest(ringFamily, ringFrom, ground, FILL_FLOOR) ?? bestEnd(ground);
|
|
1250
1271
|
for (const name of colors) {
|
|
1251
1272
|
const src = tinted && (name === "primary" || name === "neutral") ? spec.family : name;
|
|
1252
|
-
const
|
|
1253
|
-
const fillShade =
|
|
1273
|
+
const fillRef2 = base[src];
|
|
1274
|
+
const fillShade = fillRef2.shade;
|
|
1254
1275
|
const scaleIn = (v) => v.palette[src];
|
|
1255
1276
|
const neutralIn = (v) => v.palette.neutral;
|
|
1256
1277
|
const labelOn = (bg, role2, pageGrounds = []) => {
|
|
@@ -1276,19 +1297,19 @@ function searchGround(spec, base, baseCanvas, colors, views, mode, options, acti
|
|
|
1276
1297
|
refs[`${name}-active`] = { type: "value", value: stateOf(f, l, ACTIVE_SHIFT, views[0].gamut) };
|
|
1277
1298
|
};
|
|
1278
1299
|
if (tinted && name === "neutral") {
|
|
1279
|
-
const tonal = panelCandidates(steps(src), 1.6).map((fill2) => ({ fill: fill2, label: labelOn(fill2, "tint") })).find((
|
|
1300
|
+
const tonal = panelCandidates(steps(src), 1.6).map((fill2) => ({ fill: fill2, label: labelOn(fill2, "tint") })).find((t2) => cr(t2.label, t2.fill) >= textFloor);
|
|
1280
1301
|
const fill = tonal?.fill ?? steps(src).filter(farSide)[0] ?? bestEnd(ground);
|
|
1281
1302
|
const label = tonal?.label ?? labelOn(fill, "fill");
|
|
1282
1303
|
refs[name] = fill;
|
|
1283
1304
|
refs[`${name}-foreground`] = label;
|
|
1284
1305
|
setStates(fill, label);
|
|
1285
|
-
} else if (inHue(
|
|
1286
|
-
refs[name] =
|
|
1306
|
+
} else if (inHue(fillRef2) && cr(fillRef2, ground) >= FILL_FLOOR) {
|
|
1307
|
+
refs[name] = fillRef2;
|
|
1287
1308
|
refs[`${name}-foreground`] = base[`${src}-foreground`];
|
|
1288
1309
|
refs[`${name}-hover`] = base[`${src}-hover`];
|
|
1289
1310
|
refs[`${name}-active`] = base[`${src}-active`];
|
|
1290
1311
|
} else {
|
|
1291
|
-
const turnsOver = spec.
|
|
1312
|
+
const turnsOver = spec.family === src && src !== "neutral";
|
|
1292
1313
|
const ramp = steps(src);
|
|
1293
1314
|
const fill = turnsOver ? light ? ramp[ramp.length - 1] : ramp[0] : nearest(src, fillShade, ground, FILL_FLOOR) ?? bestEnd(ground);
|
|
1294
1315
|
const label = turnsOver ? labelOn(fill, "tint") : labelOn(fill, "fill");
|
|
@@ -1322,64 +1343,139 @@ function searchGround(spec, base, baseCanvas, colors, views, mode, options, acti
|
|
|
1322
1343
|
refs[`${name}-subdued`] = pair.tint;
|
|
1323
1344
|
refs[`${name}-subdued-foreground`] = pair.label;
|
|
1324
1345
|
}
|
|
1325
|
-
|
|
1326
|
-
return { kind: spec.kind, tokens, _refs: refs, changed: [], notes };
|
|
1346
|
+
return { refs, notes };
|
|
1327
1347
|
}
|
|
1328
|
-
function
|
|
1329
|
-
const base = { semantic: tiers.semantic[mode], surface: tiers.surface[mode], utility: tiers.utility[mode] };
|
|
1348
|
+
function buildGround(name, given, tiers, views, options) {
|
|
1330
1349
|
const active = resolveShades(options?.shades);
|
|
1331
1350
|
const srgb = views[0].palette;
|
|
1332
|
-
const
|
|
1333
|
-
const
|
|
1334
|
-
const
|
|
1335
|
-
const
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
const
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1351
|
+
const target = options?.contrastTarget ?? "aa";
|
|
1352
|
+
const textFloor = TEXT_FLOOR[target];
|
|
1353
|
+
const secondaryFloor = target === "aaa" ? 7 : 4.5;
|
|
1354
|
+
const scale = srgb[given.ramp];
|
|
1355
|
+
if (!scale || typeof scale === "string") throw new Error(`Ground "${name}" names no ramp: ${given.ramp} (add it with --color)`);
|
|
1356
|
+
if (!VALID_SHADES.includes(given.step)) throw new Error(`Ground "${name}" names no step: ${given.step}`);
|
|
1357
|
+
const step = snapShade(given.step, active);
|
|
1358
|
+
const ground = shade(given.ramp, step);
|
|
1359
|
+
const cr = (a, b) => Math.min(...views.map((v) => getContrastRatio(valueOf(a, v.palette), valueOf(b, v.palette), v.gamut)));
|
|
1360
|
+
const light = cr(ground, { type: "black" }) > cr(ground, { type: "white" });
|
|
1361
|
+
const polarity = light ? "light" : "dark";
|
|
1362
|
+
const tint = given.tint ?? options?.groundTint ?? "own";
|
|
1363
|
+
const text = given.text ?? options?.groundText ?? "page";
|
|
1364
|
+
const notes = [];
|
|
1365
|
+
const bestEnd = (on) => cr({ type: "white" }, on) >= cr({ type: "black" }, on) ? { type: "white" } : { type: "black" };
|
|
1366
|
+
const tier = { semantic: tiers.semantic[polarity], surface: tiers.surface[polarity], utility: tiers.utility[polarity] };
|
|
1367
|
+
const colors = semanticColors(tier.semantic);
|
|
1368
|
+
const page = baseRefs(tier, colors, active);
|
|
1369
|
+
const base = colors.includes(given.ramp) ? page : { ...page, ...rampRefs(given.ramp, step, light, srgb, active) };
|
|
1370
|
+
const rampSteps = active.filter((s) => scale[s] !== void 0).map((s) => shade(given.ramp, s));
|
|
1371
|
+
let body;
|
|
1372
|
+
if (text === "page") {
|
|
1373
|
+
body = cr(page["canvas-foreground"], ground) >= textFloor ? page["canvas-foreground"] : bestEnd(ground);
|
|
1374
|
+
} else if (text === "own") {
|
|
1375
|
+
const reads = rampSteps.filter((r) => cr(r, ground) >= BODY_AIM);
|
|
1376
|
+
body = reads.sort((a, b) => cr(a, ground) - cr(b, ground))[0] ?? rampSteps.filter((r) => cr(r, ground) >= textFloor).sort((a, b) => cr(b, ground) - cr(a, ground))[0] ?? bestEnd(ground);
|
|
1377
|
+
} else {
|
|
1378
|
+
body = fillRef(text, srgb);
|
|
1379
|
+
}
|
|
1380
|
+
if (cr(body, ground) < textFloor) notes.push(`canvas-foreground: ${cr(body, ground).toFixed(2)}:1, under ${textFloor}:1`);
|
|
1381
|
+
const searched = searchGround(
|
|
1382
|
+
{ name, canvas: ground, foreground: body, family: tint === "page" ? "neutral" : given.ramp, textFamily: text === "own" ? given.ramp : "neutral" },
|
|
1383
|
+
base,
|
|
1384
|
+
page.canvas,
|
|
1385
|
+
colors,
|
|
1386
|
+
views,
|
|
1387
|
+
polarity,
|
|
1388
|
+
options,
|
|
1389
|
+
active
|
|
1390
|
+
);
|
|
1391
|
+
const refs = searched.refs;
|
|
1392
|
+
notes.push(...searched.notes);
|
|
1393
|
+
if (tint === "page") {
|
|
1394
|
+
for (const token of ["neutral-subdued", "neutral-subdued-foreground", "border", "input", "ring"]) refs[token] = page[token];
|
|
1395
|
+
} else if (tint !== "own") {
|
|
1396
|
+
const one = fillRef(tint, srgb);
|
|
1397
|
+
refs.border = one;
|
|
1398
|
+
refs.input = one;
|
|
1399
|
+
refs.ring = one;
|
|
1400
|
+
}
|
|
1401
|
+
if (tint !== "own") {
|
|
1402
|
+
for (const token of ["input", "ring"]) {
|
|
1403
|
+
if (cr(refs[token], ground) < FILL_FLOOR) notes.push(`${token}: ${cr(refs[token], ground).toFixed(2)}:1, under ${FILL_FLOOR}:1`);
|
|
1404
|
+
}
|
|
1354
1405
|
}
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1406
|
+
const pageCardVis = Math.max(cr(page.surface, page.canvas), PANEL_FLOOR);
|
|
1407
|
+
if (tint !== "page") {
|
|
1408
|
+
const groundL = parseToOklch(valueOf(ground, srgb)).l;
|
|
1409
|
+
const lighter = (r) => parseToOklch(valueOf(r, srgb)).l > groundL;
|
|
1410
|
+
const cards = rampSteps.filter((r) => cr(r, ground) >= PANEL_FLOOR);
|
|
1411
|
+
const byVis = (a, b) => Math.abs(cr(a, ground) - pageCardVis) - Math.abs(cr(b, ground) - pageCardVis);
|
|
1412
|
+
refs.surface = [...cards.filter(lighter).sort(byVis), ...cards.filter((r) => !lighter(r)).sort(byVis)][0] ?? page.surface;
|
|
1413
|
+
} else {
|
|
1414
|
+
refs.surface = page.surface;
|
|
1363
1415
|
}
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1416
|
+
refs.canvas = ground;
|
|
1417
|
+
refs["canvas-foreground"] = body;
|
|
1418
|
+
refs["surface-foreground"] = cr(body, refs.surface) >= textFloor ? body : bestEnd(refs.surface);
|
|
1419
|
+
const textPool = text === "own" ? rampSteps : active.map((s) => shade("neutral", s));
|
|
1420
|
+
const secondary = (key, on) => {
|
|
1421
|
+
const token = `${key}-foreground-secondary`;
|
|
1422
|
+
const bodyOn = refs[`${key}-foreground`];
|
|
1423
|
+
if (text !== "page" && text !== "own") {
|
|
1424
|
+
refs[token] = bodyOn;
|
|
1425
|
+
notes.push(`${token}: one colour (${text}) for all text, so secondary text is no quieter than body text`);
|
|
1426
|
+
return;
|
|
1427
|
+
}
|
|
1428
|
+
const bodyContrast = cr(bodyOn, on);
|
|
1429
|
+
const reads = (r) => cr(r, on) >= secondaryFloor && cr(r, on) < bodyContrast;
|
|
1430
|
+
const aim = Math.max(secondaryFloor, Math.min(7, bodyContrast * 0.65));
|
|
1431
|
+
const kept = text === "page" && reads(page[token]) ? page[token] : void 0;
|
|
1432
|
+
const found = kept ?? textPool.filter(reads).sort((a, b) => Math.abs(cr(a, on) - aim) - Math.abs(cr(b, on) - aim))[0];
|
|
1433
|
+
if (found) refs[token] = found;
|
|
1434
|
+
else {
|
|
1435
|
+
refs[token] = bodyOn;
|
|
1436
|
+
notes.push(`${token}: nothing quieter than the body text reads at ${secondaryFloor}:1, so it is the body colour`);
|
|
1437
|
+
}
|
|
1438
|
+
};
|
|
1439
|
+
secondary("canvas", ground);
|
|
1440
|
+
secondary("surface", refs.surface);
|
|
1441
|
+
for (const c of colors) {
|
|
1442
|
+
if (sameRef(refs[c], page[c]) && sameRef(refs[`${c}-foreground`], page[`${c}-foreground`])) {
|
|
1443
|
+
refs[`${c}-hover`] = page[`${c}-hover`];
|
|
1444
|
+
refs[`${c}-active`] = page[`${c}-active`];
|
|
1445
|
+
continue;
|
|
1370
1446
|
}
|
|
1447
|
+
[refs[`${c}-hover`], refs[`${c}-active`]] = statesFor(refs[c], refs[`${c}-foreground`], srgb, active);
|
|
1371
1448
|
}
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1449
|
+
const order = groundTokenNames(colors);
|
|
1450
|
+
const _refs = Object.fromEntries(order.map((k) => [k, refs[k] ?? page[k]]));
|
|
1451
|
+
return {
|
|
1452
|
+
ramp: given.ramp,
|
|
1453
|
+
step,
|
|
1454
|
+
polarity,
|
|
1455
|
+
tint,
|
|
1456
|
+
text,
|
|
1457
|
+
tokens: Object.fromEntries(order.map((k) => [k, valueOf(_refs[k], srgb)])),
|
|
1458
|
+
_refs,
|
|
1459
|
+
// the ground itself always; the rest where they differ from the page of the ground's polarity
|
|
1460
|
+
own: order.filter((k) => k === "canvas" || !sameRef(_refs[k], page[k])),
|
|
1461
|
+
notes
|
|
1462
|
+
};
|
|
1463
|
+
}
|
|
1464
|
+
function assertGroundsShape(options) {
|
|
1465
|
+
if (typeof options?.grounds === "string") {
|
|
1466
|
+
throw new Error(`grounds: "${options.grounds}" is 0.11's setting. From 0.12 grounds are declared one by one, { pine: { ramp: "pine", step: 800 } }; "text" is groundTint: "page"`);
|
|
1467
|
+
}
|
|
1468
|
+
if (options && "backgrounds" in options) {
|
|
1469
|
+
throw new Error("backgrounds is gone from 0.12: a section that changes with the mode is a page surface (pageSurfaces); one painted the same in both is a ground on a ramp (grounds)");
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
function generateGroundTokens(tiers, views, options) {
|
|
1473
|
+
const out = {};
|
|
1474
|
+
assertGroundsShape(options);
|
|
1475
|
+
for (const name of Object.keys(options?.grounds ?? {}).sort()) {
|
|
1476
|
+
if (name === "canvas") throw new Error(`A ground can't be called "canvas": data-surface="canvas" resets to the page`);
|
|
1477
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name)) throw new Error(`Ground name must be lower-case kebab-case (got "${name}")`);
|
|
1478
|
+
out[name] = buildGround(name, options.grounds[name], tiers, views, options);
|
|
1383
1479
|
}
|
|
1384
1480
|
return out;
|
|
1385
1481
|
}
|
|
@@ -1471,16 +1567,16 @@ function generateTokens(brandColors, options) {
|
|
|
1471
1567
|
hueDrift: options?.hueDrift,
|
|
1472
1568
|
families: options?.families,
|
|
1473
1569
|
familyPins: options?.pins,
|
|
1474
|
-
|
|
1570
|
+
// green, red and yellow beside success, destructive and warning only when asked (from 0.12)
|
|
1571
|
+
aliases: options?.aliases ?? false
|
|
1475
1572
|
};
|
|
1476
1573
|
const primitives = generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "srgb" });
|
|
1477
1574
|
const wide = options?.gamut === "p3" ? generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "p3" }) : void 0;
|
|
1478
1575
|
const grounds = (palette, mode) => {
|
|
1479
|
-
const surface = generateSurfaceTokens(palette, mode, options?.surfaceShadeOverrides?.[mode], options?.shades, options?.surfaceColors?.[mode], options?.
|
|
1576
|
+
const surface = generateSurfaceTokens(palette, mode, options?.surfaceShadeOverrides?.[mode], options?.shades, options?.surfaceColors?.[mode], options?.pageSurfaces, textFloorOf(options));
|
|
1480
1577
|
const light = (c) => getContrastRatio(c, "#000000") > getContrastRatio(c, "#ffffff");
|
|
1481
|
-
const
|
|
1482
|
-
|
|
1483
|
-
return [surface.canvas, surface.surface, surface.overlay, ...named];
|
|
1578
|
+
const same = Object.values(surface.pageSurfaces ?? {}).map((b) => b.DEFAULT).filter((c) => light(c) === light(surface.canvas));
|
|
1579
|
+
return [surface.canvas, surface.surface, surface.overlay, ...same];
|
|
1484
1580
|
};
|
|
1485
1581
|
const colourTiers = (palette, gamuts) => ({
|
|
1486
1582
|
primitives: palette,
|
|
@@ -1489,8 +1585,8 @@ function generateTokens(brandColors, options) {
|
|
|
1489
1585
|
dark: generateSemanticTokens(palette, "dark", options, gamuts, grounds(palette, "dark"))
|
|
1490
1586
|
},
|
|
1491
1587
|
surface: {
|
|
1492
|
-
light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades, options?.surfaceColors?.light, options?.
|
|
1493
|
-
dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades, options?.surfaceColors?.dark, options?.
|
|
1588
|
+
light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades, options?.surfaceColors?.light, options?.pageSurfaces, textFloorOf(options)),
|
|
1589
|
+
dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades, options?.surfaceColors?.dark, options?.pageSurfaces, textFloorOf(options))
|
|
1494
1590
|
},
|
|
1495
1591
|
utility: {
|
|
1496
1592
|
light: generateUtilityTokens(palette, "light", options?.utilityShadeOverrides?.light, options?.shades, options?.seedShade, grounds(palette, "light")),
|
|
@@ -1501,21 +1597,20 @@ function generateTokens(brandColors, options) {
|
|
|
1501
1597
|
own: "srgb",
|
|
1502
1598
|
...wide ? { other: { gamut: "p3", palette: wide } } : {}
|
|
1503
1599
|
});
|
|
1504
|
-
const withGrounds = options?.grounds !== "none";
|
|
1505
1600
|
const colors = semanticColors(tiers.semantic.light);
|
|
1506
|
-
const reserved =
|
|
1507
|
-
for (const name of
|
|
1601
|
+
const reserved = reservedSurfaceNames(colors);
|
|
1602
|
+
for (const name of Object.keys(options?.pageSurfaces ?? {})) {
|
|
1508
1603
|
if (reserved.includes(name)) {
|
|
1509
|
-
throw new Error(`
|
|
1604
|
+
throw new Error(`Page surface name "${name}" is taken: it is already a token. Reserved: ${reserved.join(", ")}`);
|
|
1510
1605
|
}
|
|
1511
1606
|
}
|
|
1512
1607
|
const views = [{ palette: primitives, gamut: "srgb" }, ...wide ? [{ palette: wide, gamut: "p3" }] : []];
|
|
1513
|
-
|
|
1514
|
-
|
|
1608
|
+
assertGroundsShape(options);
|
|
1609
|
+
const groundTokens = Object.keys(options?.grounds ?? {}).length ? generateGroundTokens(tiers, views, options) : void 0;
|
|
1515
1610
|
return {
|
|
1516
1611
|
...tiers,
|
|
1517
1612
|
...groundTokens ? { grounds: groundTokens } : {},
|
|
1518
|
-
pageContrast: { light: pageContrast(tiers, "light"), dark: pageContrast(tiers, "dark") },
|
|
1613
|
+
pageContrast: { light: pageContrast(tiers, "light", groundTokens), dark: pageContrast(tiers, "dark", groundTokens) },
|
|
1519
1614
|
spacing: {},
|
|
1520
1615
|
typography: defaultTypography,
|
|
1521
1616
|
radii: defaultRadii,
|
|
@@ -1523,14 +1618,17 @@ function generateTokens(brandColors, options) {
|
|
|
1523
1618
|
...wide ? { p3: colourTiers(wide, { own: "p3", other: { gamut: "srgb", palette: primitives } }) } : {}
|
|
1524
1619
|
};
|
|
1525
1620
|
}
|
|
1526
|
-
function
|
|
1621
|
+
function textFloorOf(options) {
|
|
1622
|
+
return options?.contrastTarget === "aaa" ? 7 : 4.5;
|
|
1623
|
+
}
|
|
1624
|
+
function pageContrast(tiers, mode, groundTokens) {
|
|
1527
1625
|
const surface = tiers.surface[mode];
|
|
1528
1626
|
const grounds = { canvas: surface.canvas, surface: surface.surface, overlay: surface.overlay };
|
|
1529
|
-
for (const [name, bg] of Object.entries(surface.
|
|
1627
|
+
for (const [name, bg] of Object.entries(surface.pageSurfaces ?? {})) grounds[name] = bg.DEFAULT;
|
|
1628
|
+
for (const [name, g] of Object.entries(groundTokens ?? {})) grounds[`ground-${name}`] = g.tokens.canvas;
|
|
1530
1629
|
const colours = {};
|
|
1531
|
-
for (const [name, colour] of Object.entries(tiers.semantic[mode]))
|
|
1532
|
-
|
|
1533
|
-
}
|
|
1630
|
+
for (const [name, colour] of Object.entries(tiers.semantic[mode])) colours[name] = colour.DEFAULT;
|
|
1631
|
+
for (const key of ["canvas", "surface", "overlay"]) colours[`${key}-foreground-secondary`] = surface[`${key}-foreground-secondary`];
|
|
1534
1632
|
const out = {};
|
|
1535
1633
|
for (const name of Object.keys(colours).sort()) {
|
|
1536
1634
|
out[name] = {};
|
|
@@ -1549,13 +1647,11 @@ function usageReport(tokens, target = "aa") {
|
|
|
1549
1647
|
const mode = (m) => {
|
|
1550
1648
|
const surface = tokens.surface[m];
|
|
1551
1649
|
const grounds = { canvas: surface.canvas, surface: surface.surface, overlay: surface.overlay };
|
|
1552
|
-
for (const [name,
|
|
1650
|
+
for (const [name, page] of Object.entries(surface.pageSurfaces ?? {})) grounds[name] = page.DEFAULT;
|
|
1651
|
+
for (const [name, g] of Object.entries(tokens.grounds ?? {})) grounds[`ground-${name}`] = g.tokens.canvas;
|
|
1553
1652
|
const colours = {};
|
|
1653
|
+
for (const key of ["canvas", "surface", "overlay"]) colours[`${key}-foreground-secondary`] = surface[`${key}-foreground-secondary`];
|
|
1554
1654
|
for (const [name, colour] of Object.entries(tokens.semantic[m])) {
|
|
1555
|
-
if (name === "muted") {
|
|
1556
|
-
colours["muted-foreground"] = colour.foreground;
|
|
1557
|
-
continue;
|
|
1558
|
-
}
|
|
1559
1655
|
colours[name] = colour.DEFAULT;
|
|
1560
1656
|
colours[`${name}-subdued-foreground`] = colour["subdued-foreground"];
|
|
1561
1657
|
}
|
|
@@ -1622,10 +1718,6 @@ function getExtendedMappings(mode, colorName, overrides, active, anchors) {
|
|
|
1622
1718
|
const seedShade = familySeedShade(colorName, anchors.seedShade, anchors.seedShades);
|
|
1623
1719
|
return extendedBridgeShades(mode, seedShade, active, overrides?.[colorName]?.[mode]);
|
|
1624
1720
|
}
|
|
1625
|
-
var MUTED_SHADE_MAPPINGS = {
|
|
1626
|
-
light: { DEFAULT: 100, foreground: 500 },
|
|
1627
|
-
dark: { DEFAULT: 800, foreground: 200 }
|
|
1628
|
-
};
|
|
1629
1721
|
var UTILITY_SHADE_MAPPINGS = {
|
|
1630
1722
|
light: {
|
|
1631
1723
|
border: "neutral-200",
|
|
@@ -1658,7 +1750,7 @@ function surfaceRefToVar(ref, colorFormat) {
|
|
|
1658
1750
|
case "black":
|
|
1659
1751
|
return "var(--color-black)";
|
|
1660
1752
|
case "shade":
|
|
1661
|
-
return `var(--color
|
|
1753
|
+
return `var(--color-${ref.colorName ?? "neutral"}-${ref.shade})`;
|
|
1662
1754
|
// a background passed in as a colour is not on any ramp, so it is written as it is
|
|
1663
1755
|
case "value":
|
|
1664
1756
|
return formatColor(ref.value, colorFormat);
|
|
@@ -1666,7 +1758,17 @@ function surfaceRefToVar(ref, colorFormat) {
|
|
|
1666
1758
|
}
|
|
1667
1759
|
function emitSurfaceVars(surface, colorFormat, indent) {
|
|
1668
1760
|
const lines = [];
|
|
1669
|
-
const keys = [
|
|
1761
|
+
const keys = [
|
|
1762
|
+
"canvas",
|
|
1763
|
+
"canvas-foreground",
|
|
1764
|
+
"canvas-foreground-secondary",
|
|
1765
|
+
"surface",
|
|
1766
|
+
"surface-foreground",
|
|
1767
|
+
"surface-foreground-secondary",
|
|
1768
|
+
"overlay",
|
|
1769
|
+
"overlay-foreground",
|
|
1770
|
+
"overlay-foreground-secondary"
|
|
1771
|
+
];
|
|
1670
1772
|
for (const key of keys) {
|
|
1671
1773
|
if (surface._refs) {
|
|
1672
1774
|
lines.push(`${indent}--${key}: ${surfaceRefToVar(surface._refs[key], colorFormat)};`);
|
|
@@ -1674,9 +1776,10 @@ function emitSurfaceVars(surface, colorFormat, indent) {
|
|
|
1674
1776
|
lines.push(`${indent}--${key}: ${formatColor(surface[key], colorFormat)};`);
|
|
1675
1777
|
}
|
|
1676
1778
|
}
|
|
1677
|
-
for (const [name,
|
|
1678
|
-
lines.push(`${indent}
|
|
1679
|
-
lines.push(`${indent}
|
|
1779
|
+
for (const [name, page] of Object.entries(surface.pageSurfaces ?? {})) {
|
|
1780
|
+
lines.push(`${indent}--${name}: ${surfaceRefToVar(page._refs.DEFAULT, colorFormat)};`);
|
|
1781
|
+
lines.push(`${indent}--${name}-foreground: ${surfaceRefToVar(page._refs.foreground, colorFormat)};`);
|
|
1782
|
+
lines.push(`${indent}--${name}-foreground-secondary: ${surfaceRefToVar(page._refs["foreground-secondary"], colorFormat)};`);
|
|
1680
1783
|
}
|
|
1681
1784
|
return lines;
|
|
1682
1785
|
}
|
|
@@ -1695,7 +1798,7 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
|
|
|
1695
1798
|
lines.push(`${indent}--${name}-active: ${formatColor(color.active, colorFormat)};`);
|
|
1696
1799
|
}
|
|
1697
1800
|
for (const [name, color] of Object.entries(tokens.semantic[mode])) {
|
|
1698
|
-
if (!extendedSemanticNames.includes(name) &&
|
|
1801
|
+
if (!extendedSemanticNames.includes(name) && inFamily(name)) {
|
|
1699
1802
|
const ext = color;
|
|
1700
1803
|
const mappings = ext._shades ?? getExtendedMappings(mode, name, shadeOverrides, active, anchors);
|
|
1701
1804
|
lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
|
|
@@ -1706,11 +1809,6 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
|
|
|
1706
1809
|
lines.push(`${indent}--${name}-active: ${formatColor(ext.active, colorFormat)};`);
|
|
1707
1810
|
}
|
|
1708
1811
|
}
|
|
1709
|
-
if (!scoped || inFamily("neutral")) {
|
|
1710
|
-
const muted = tokens.semantic[mode].muted._shades ?? MUTED_SHADE_MAPPINGS[mode];
|
|
1711
|
-
lines.push(`${indent}--muted: var(--color-neutral-${snapShade(muted.DEFAULT, active)});`);
|
|
1712
|
-
lines.push(`${indent}--muted-foreground: var(--color-neutral-${snapShade(muted.foreground, active)});`);
|
|
1713
|
-
}
|
|
1714
1812
|
return lines;
|
|
1715
1813
|
}
|
|
1716
1814
|
function emitUtilityVars(mode, indent, active, seedShade, steps) {
|
|
@@ -1737,36 +1835,30 @@ function emitUtilityVars(mode, indent, active, seedShade, steps) {
|
|
|
1737
1835
|
function groundRefToVar(ref, colorFormat) {
|
|
1738
1836
|
return ref.type === "value" ? formatColor(ref.value, colorFormat) : refToVar(ref);
|
|
1739
1837
|
}
|
|
1740
|
-
function
|
|
1741
|
-
const grounds = tokens.grounds;
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
const
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
lines.push(`${indent}--on-${ground}-${token}: ${groundRefToVar(set._refs[token], colorFormat)};`);
|
|
1752
|
-
}
|
|
1753
|
-
}
|
|
1754
|
-
return lines;
|
|
1838
|
+
function modeSelectors(tokens) {
|
|
1839
|
+
const grounds = Object.entries(tokens.grounds ?? {});
|
|
1840
|
+
if (!grounds.length) return { ...MODE_SELECTORS };
|
|
1841
|
+
const of = (polarity) => grounds.filter(([, g]) => g.polarity === polarity).map(([name]) => GROUND_SELECTOR(name));
|
|
1842
|
+
const reset = GROUND_SELECTOR("canvas");
|
|
1843
|
+
const own = `${reset}:not([data-theme="light"]):not(.light)`;
|
|
1844
|
+
return {
|
|
1845
|
+
light: [MODE_SELECTORS.light, reset, ...of("light")].join(", "),
|
|
1846
|
+
dark: [MODE_SELECTORS.dark, ...MODE_SELECTORS.dark.split(", ").map((s) => `${s} ${own}`), ...of("dark")].join(", "),
|
|
1847
|
+
systemDark: [MODE_SELECTORS.systemDark, `${MODE_SELECTORS.systemDark} ${own}`].join(", ")
|
|
1848
|
+
};
|
|
1755
1849
|
}
|
|
1756
|
-
function emitGroundScopes(tokens, rebind) {
|
|
1757
|
-
const grounds = tokens.grounds
|
|
1758
|
-
if (!grounds
|
|
1850
|
+
function emitGroundScopes(tokens, rebind, colorFormat) {
|
|
1851
|
+
const grounds = Object.entries(tokens.grounds ?? {});
|
|
1852
|
+
if (!grounds.length) return [];
|
|
1759
1853
|
const lines = [
|
|
1760
1854
|
"",
|
|
1761
|
-
'/* Grounds: mark an element data-surface="<ground>" and everything inside it reads',
|
|
1762
|
-
|
|
1855
|
+
'/* Grounds: mark an element data-surface="<ground>" and everything inside it reads tokens',
|
|
1856
|
+
' that work on it. data-surface="canvas" resets to the page, for a card inside a band. */'
|
|
1763
1857
|
];
|
|
1764
|
-
for (const [ground, set] of
|
|
1765
|
-
const names = Object.keys(set._refs);
|
|
1766
|
-
const own = new Set(ownGroundTokens(tokens, ground));
|
|
1858
|
+
for (const [ground, set] of grounds) {
|
|
1767
1859
|
lines.push(`${GROUND_SELECTOR(ground)} {`);
|
|
1768
|
-
for (const token of
|
|
1769
|
-
if (rebind) for (const token of
|
|
1860
|
+
for (const token of set.own) lines.push(` --${token}: ${groundRefToVar(set._refs[token], colorFormat)};`);
|
|
1861
|
+
if (rebind) for (const token of set.own) lines.push(` --color-${token}: var(--${token});`);
|
|
1770
1862
|
lines.push(" background-color: var(--canvas);");
|
|
1771
1863
|
lines.push(" color: var(--canvas-foreground);");
|
|
1772
1864
|
lines.push("}");
|
|
@@ -1801,10 +1893,6 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
1801
1893
|
body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
1802
1894
|
if (withComments) body.push("");
|
|
1803
1895
|
}
|
|
1804
|
-
if (wantGrounds) {
|
|
1805
|
-
if (withComments) body.push(`${indent}/* Grounds: read through data-surface, below */`);
|
|
1806
|
-
body.push(...emitGroundVars(tokens, mode, colorFormat, indent));
|
|
1807
|
-
}
|
|
1808
1896
|
while (body.length > 0 && body[body.length - 1] === "") body.pop();
|
|
1809
1897
|
return body;
|
|
1810
1898
|
};
|
|
@@ -1816,25 +1904,26 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
1816
1904
|
lines.push(...emitWideGamutBlock(tokens, inFamily));
|
|
1817
1905
|
}
|
|
1818
1906
|
if (wantModes) {
|
|
1907
|
+
const selectors = wantGrounds ? modeSelectors(tokens) : MODE_SELECTORS;
|
|
1819
1908
|
if (wantPrimitives) lines.push("");
|
|
1820
1909
|
lines.push("/* Light Mode */");
|
|
1821
|
-
lines.push(`${
|
|
1910
|
+
lines.push(`${selectors.light} {`);
|
|
1822
1911
|
lines.push(...modeBody("light", " ", true));
|
|
1823
1912
|
lines.push("}");
|
|
1824
1913
|
lines.push("");
|
|
1825
1914
|
lines.push("/* Dark Mode */");
|
|
1826
|
-
lines.push(`${
|
|
1915
|
+
lines.push(`${selectors.dark} {`);
|
|
1827
1916
|
lines.push(...modeBody("dark", " ", true));
|
|
1828
1917
|
lines.push("}");
|
|
1829
1918
|
lines.push("");
|
|
1830
1919
|
lines.push("/* System Preference: Dark Mode */");
|
|
1831
1920
|
lines.push("@media (prefers-color-scheme: dark) {");
|
|
1832
|
-
lines.push(` ${
|
|
1921
|
+
lines.push(` ${selectors.systemDark} {`);
|
|
1833
1922
|
lines.push(...modeBody("dark", " ", false));
|
|
1834
1923
|
lines.push(" }");
|
|
1835
1924
|
lines.push("}");
|
|
1836
1925
|
}
|
|
1837
|
-
if (wantGrounds) lines.push(...emitGroundScopes(tokens, false));
|
|
1926
|
+
if (wantGrounds) lines.push(...emitGroundScopes(tokens, false, colorFormat));
|
|
1838
1927
|
return lines.join("\n");
|
|
1839
1928
|
}
|
|
1840
1929
|
var MODE_SELECTORS = {
|
|
@@ -1905,21 +1994,23 @@ function semanticAliasNames(tokens, sections, families) {
|
|
|
1905
1994
|
if (want.has("semantic")) {
|
|
1906
1995
|
for (const name of extended) if (inFamily(name)) pushExtended(name);
|
|
1907
1996
|
for (const name of Object.keys(tokens.semantic.light)) {
|
|
1908
|
-
if (!extended.includes(name) &&
|
|
1997
|
+
if (!extended.includes(name) && inFamily(name)) pushExtended(name);
|
|
1909
1998
|
}
|
|
1910
|
-
if (!scoped || inFamily("neutral")) names.push("muted", "muted-foreground");
|
|
1911
1999
|
}
|
|
1912
2000
|
if (want.has("surface") && !scoped) {
|
|
1913
2001
|
names.push(
|
|
1914
2002
|
"canvas",
|
|
1915
2003
|
"canvas-foreground",
|
|
2004
|
+
"canvas-foreground-secondary",
|
|
1916
2005
|
"surface",
|
|
1917
2006
|
"surface-foreground",
|
|
2007
|
+
"surface-foreground-secondary",
|
|
1918
2008
|
"overlay",
|
|
1919
|
-
"overlay-foreground"
|
|
2009
|
+
"overlay-foreground",
|
|
2010
|
+
"overlay-foreground-secondary"
|
|
1920
2011
|
);
|
|
1921
|
-
for (const name of Object.keys(tokens.surface.light.
|
|
1922
|
-
names.push(
|
|
2012
|
+
for (const name of Object.keys(tokens.surface.light.pageSurfaces ?? {})) {
|
|
2013
|
+
names.push(name, `${name}-foreground`, `${name}-foreground-secondary`);
|
|
1923
2014
|
}
|
|
1924
2015
|
}
|
|
1925
2016
|
if (want.has("utility") && !scoped) {
|
|
@@ -1936,7 +2027,7 @@ function emitThemeRebinding(aliases, indent) {
|
|
|
1936
2027
|
}
|
|
1937
2028
|
return lines;
|
|
1938
2029
|
}
|
|
1939
|
-
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped, anchors
|
|
2030
|
+
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped, anchors) {
|
|
1940
2031
|
const lines = [];
|
|
1941
2032
|
const seedShade = anchors.seedShade;
|
|
1942
2033
|
if (want.has("surface") && !scoped) {
|
|
@@ -1954,10 +2045,6 @@ function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases,
|
|
|
1954
2045
|
lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
1955
2046
|
lines.push("");
|
|
1956
2047
|
}
|
|
1957
|
-
if (wantGrounds) {
|
|
1958
|
-
lines.push(`${indent}/* Grounds: read through data-surface, below */`);
|
|
1959
|
-
lines.push(...emitGroundVars(tokens, mode, colorFormat, indent));
|
|
1960
|
-
}
|
|
1961
2048
|
while (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
|
|
1962
2049
|
if (aliases.length > 0) lines.push(...emitThemeRebinding(aliases, indent));
|
|
1963
2050
|
return lines;
|
|
@@ -1995,25 +2082,26 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
|
|
|
1995
2082
|
lines.push("}");
|
|
1996
2083
|
if (wantPrimitives) lines.push(...emitWideGamutBlock(tokens, inFamily));
|
|
1997
2084
|
if (wantModes) {
|
|
2085
|
+
const selectors = wantGrounds ? modeSelectors(tokens) : MODE_SELECTORS;
|
|
1998
2086
|
lines.push("");
|
|
1999
2087
|
lines.push("/* Light Mode */");
|
|
2000
|
-
lines.push(`${
|
|
2001
|
-
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors
|
|
2088
|
+
lines.push(`${selectors.light} {`);
|
|
2089
|
+
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
2002
2090
|
lines.push("}");
|
|
2003
2091
|
lines.push("");
|
|
2004
2092
|
lines.push("/* Dark Mode */");
|
|
2005
|
-
lines.push(`${
|
|
2006
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors
|
|
2093
|
+
lines.push(`${selectors.dark} {`);
|
|
2094
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
2007
2095
|
lines.push("}");
|
|
2008
2096
|
lines.push("");
|
|
2009
2097
|
lines.push("/* System Preference: Dark Mode */");
|
|
2010
2098
|
lines.push("@media (prefers-color-scheme: dark) {");
|
|
2011
|
-
lines.push(` ${
|
|
2012
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors
|
|
2099
|
+
lines.push(` ${selectors.systemDark} {`);
|
|
2100
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
2013
2101
|
lines.push(" }");
|
|
2014
2102
|
lines.push("}");
|
|
2015
2103
|
}
|
|
2016
|
-
if (wantGrounds) lines.push(...emitGroundScopes(tokens, true));
|
|
2104
|
+
if (wantGrounds) lines.push(...emitGroundScopes(tokens, true, colorFormat));
|
|
2017
2105
|
return lines.join("\n");
|
|
2018
2106
|
}
|
|
2019
2107
|
function exportToTailwindV3(tokens, colorFormat = "hex") {
|
|
@@ -2053,7 +2141,7 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
|
|
|
2053
2141
|
config.theme.extend.colors[name] = colorObj;
|
|
2054
2142
|
}
|
|
2055
2143
|
for (const name of Object.keys(tokens.semantic.light)) {
|
|
2056
|
-
if (!extendedSemanticColors.includes(name)
|
|
2144
|
+
if (!extendedSemanticColors.includes(name)) {
|
|
2057
2145
|
const primitiveScale = tokens.primitives[name];
|
|
2058
2146
|
const colorObj = {
|
|
2059
2147
|
DEFAULT: `var(--${name})`,
|
|
@@ -2071,22 +2159,13 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
|
|
|
2071
2159
|
config.theme.extend.colors[name] = colorObj;
|
|
2072
2160
|
}
|
|
2073
2161
|
}
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
};
|
|
2082
|
-
config.theme.extend.colors["surface"] = {
|
|
2083
|
-
DEFAULT: "var(--surface)",
|
|
2084
|
-
foreground: "var(--surface-foreground)"
|
|
2085
|
-
};
|
|
2086
|
-
config.theme.extend.colors["overlay"] = {
|
|
2087
|
-
DEFAULT: "var(--overlay)",
|
|
2088
|
-
foreground: "var(--overlay-foreground)"
|
|
2089
|
-
};
|
|
2162
|
+
for (const key of ["canvas", "surface", "overlay"]) {
|
|
2163
|
+
config.theme.extend.colors[key] = {
|
|
2164
|
+
DEFAULT: `var(--${key})`,
|
|
2165
|
+
foreground: `var(--${key}-foreground)`,
|
|
2166
|
+
"foreground-secondary": `var(--${key}-foreground-secondary)`
|
|
2167
|
+
};
|
|
2168
|
+
}
|
|
2090
2169
|
config.theme.extend.colors["border"] = "var(--border)";
|
|
2091
2170
|
config.theme.extend.colors["input"] = "var(--input)";
|
|
2092
2171
|
config.theme.extend.colors["ring"] = "var(--ring)";
|
|
@@ -2130,7 +2209,7 @@ function exportToDtcg(tokens) {
|
|
|
2130
2209
|
};
|
|
2131
2210
|
}
|
|
2132
2211
|
for (const [colorName, color] of Object.entries(tokens.semantic[mode])) {
|
|
2133
|
-
if (!extendedNames.includes(colorName)
|
|
2212
|
+
if (!extendedNames.includes(colorName)) {
|
|
2134
2213
|
const ext = color;
|
|
2135
2214
|
semanticGroup[colorName] = {
|
|
2136
2215
|
$type: "color",
|
|
@@ -2145,21 +2224,18 @@ function exportToDtcg(tokens) {
|
|
|
2145
2224
|
};
|
|
2146
2225
|
}
|
|
2147
2226
|
}
|
|
2148
|
-
const muted = tokens.semantic[mode].muted;
|
|
2149
|
-
semanticGroup.muted = {
|
|
2150
|
-
$type: "color",
|
|
2151
|
-
DEFAULT: { $value: formatColorAs(muted.DEFAULT, "hex") },
|
|
2152
|
-
foreground: { $value: formatColorAs(muted.foreground, "hex") }
|
|
2153
|
-
};
|
|
2154
2227
|
const surf = tokens.surface[mode];
|
|
2155
2228
|
semanticGroup.surface = {
|
|
2156
2229
|
$type: "color",
|
|
2157
2230
|
canvas: { $value: formatColorAs(surf.canvas, "hex") },
|
|
2158
2231
|
"canvas-foreground": { $value: formatColorAs(surf["canvas-foreground"], "hex") },
|
|
2232
|
+
"canvas-foreground-secondary": { $value: formatColorAs(surf["canvas-foreground-secondary"], "hex") },
|
|
2159
2233
|
surface: { $value: formatColorAs(surf.surface, "hex") },
|
|
2160
2234
|
"surface-foreground": { $value: formatColorAs(surf["surface-foreground"], "hex") },
|
|
2235
|
+
"surface-foreground-secondary": { $value: formatColorAs(surf["surface-foreground-secondary"], "hex") },
|
|
2161
2236
|
overlay: { $value: formatColorAs(surf.overlay, "hex") },
|
|
2162
|
-
"overlay-foreground": { $value: formatColorAs(surf["overlay-foreground"], "hex") }
|
|
2237
|
+
"overlay-foreground": { $value: formatColorAs(surf["overlay-foreground"], "hex") },
|
|
2238
|
+
"overlay-foreground-secondary": { $value: formatColorAs(surf["overlay-foreground-secondary"], "hex") }
|
|
2163
2239
|
};
|
|
2164
2240
|
const util = tokens.utility[mode];
|
|
2165
2241
|
semanticGroup.utility = {
|
|
@@ -2169,27 +2245,27 @@ function exportToDtcg(tokens) {
|
|
|
2169
2245
|
ring: { $value: formatColorAs(util.ring, "hex") },
|
|
2170
2246
|
shadow: { $value: formatColorAs(util.shadow, "hex") }
|
|
2171
2247
|
};
|
|
2172
|
-
const grounds = tokens.grounds?.[mode];
|
|
2173
|
-
if (grounds) {
|
|
2174
|
-
const groundGroup = {};
|
|
2175
|
-
for (const [name, set] of Object.entries(grounds)) {
|
|
2176
|
-
const group = { $type: "color" };
|
|
2177
|
-
for (const [token, value] of Object.entries(set.tokens)) group[token] = { $value: formatColorAs(value, "hex") };
|
|
2178
|
-
groundGroup[name] = group;
|
|
2179
|
-
}
|
|
2180
|
-
semanticGroup.ground = groundGroup;
|
|
2181
|
-
}
|
|
2182
2248
|
dtcg[mode] = semanticGroup;
|
|
2183
2249
|
}
|
|
2250
|
+
if (tokens.grounds && Object.keys(tokens.grounds).length) {
|
|
2251
|
+
const groundGroup = {};
|
|
2252
|
+
for (const [name, set] of Object.entries(tokens.grounds)) {
|
|
2253
|
+
const group = { $type: "color" };
|
|
2254
|
+
for (const [token, value] of Object.entries(set.tokens)) group[token] = { $value: formatColorAs(value, "hex") };
|
|
2255
|
+
groundGroup[name] = group;
|
|
2256
|
+
}
|
|
2257
|
+
dtcg.ground = groundGroup;
|
|
2258
|
+
}
|
|
2184
2259
|
return JSON.stringify(dtcg, null, 2);
|
|
2185
2260
|
}
|
|
2186
2261
|
var SECTIONED_FORMATS = ["css", "tailwind-v4"];
|
|
2187
2262
|
function tokenFamilies(tokens) {
|
|
2188
2263
|
return Object.entries(tokens.primitives).filter(([, scale]) => typeof scale !== "string").map(([name]) => name);
|
|
2189
2264
|
}
|
|
2190
|
-
function externalReferences(css) {
|
|
2265
|
+
function externalReferences(css, against) {
|
|
2191
2266
|
const defined = /* @__PURE__ */ new Set();
|
|
2192
|
-
for (const line of css
|
|
2267
|
+
for (const line of `${css}
|
|
2268
|
+
${against ?? ""}`.split("\n")) {
|
|
2193
2269
|
const m = /^\s*(--[a-z0-9-]+)\s*:/i.exec(line);
|
|
2194
2270
|
if (m) defined.add(m[1]);
|
|
2195
2271
|
}
|
|
@@ -2270,8 +2346,8 @@ function exportTokens(tokens, format, options) {
|
|
|
2270
2346
|
}
|
|
2271
2347
|
|
|
2272
2348
|
// src/merge.ts
|
|
2273
|
-
function keyOf(
|
|
2274
|
-
return `${
|
|
2349
|
+
function keyOf(block2, property) {
|
|
2350
|
+
return `${block2} ${property}`;
|
|
2275
2351
|
}
|
|
2276
2352
|
function normaliseSelector(raw) {
|
|
2277
2353
|
return raw.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\s+/g, " ").trim();
|
|
@@ -2318,7 +2394,7 @@ function readDeclarations(css) {
|
|
|
2318
2394
|
const out = [];
|
|
2319
2395
|
walk(
|
|
2320
2396
|
css,
|
|
2321
|
-
(_line, _index,
|
|
2397
|
+
(_line, _index, block2, property, value) => out.push({ block: block2, property, value })
|
|
2322
2398
|
);
|
|
2323
2399
|
return out;
|
|
2324
2400
|
}
|
|
@@ -2331,8 +2407,8 @@ function mergeSlice(target, slice) {
|
|
|
2331
2407
|
const updated = [];
|
|
2332
2408
|
const unchanged = [];
|
|
2333
2409
|
const seen = /* @__PURE__ */ new Set();
|
|
2334
|
-
walk(target, (line, index,
|
|
2335
|
-
const key = keyOf(
|
|
2410
|
+
walk(target, (line, index, block2, property, value) => {
|
|
2411
|
+
const key = keyOf(block2, property);
|
|
2336
2412
|
const wanted = incoming.get(key);
|
|
2337
2413
|
if (!wanted) return;
|
|
2338
2414
|
seen.add(key);
|
|
@@ -2349,10 +2425,10 @@ function mergeSlice(target, slice) {
|
|
|
2349
2425
|
if (additions.length > 0) {
|
|
2350
2426
|
const lastLineOfBlock = /* @__PURE__ */ new Map();
|
|
2351
2427
|
const indentOfBlock = /* @__PURE__ */ new Map();
|
|
2352
|
-
walk(lines.join("\n"), (line, index,
|
|
2353
|
-
lastLineOfBlock.set(
|
|
2354
|
-
if (!indentOfBlock.has(
|
|
2355
|
-
indentOfBlock.set(
|
|
2428
|
+
walk(lines.join("\n"), (line, index, block2) => {
|
|
2429
|
+
lastLineOfBlock.set(block2, index);
|
|
2430
|
+
if (!indentOfBlock.has(block2)) {
|
|
2431
|
+
indentOfBlock.set(block2, /^\s*/.exec(line)?.[0] ?? " ");
|
|
2356
2432
|
}
|
|
2357
2433
|
});
|
|
2358
2434
|
const byBlock = /* @__PURE__ */ new Map();
|
|
@@ -2368,9 +2444,9 @@ function mergeSlice(target, slice) {
|
|
|
2368
2444
|
const ordered = [...byBlock.entries()].sort(
|
|
2369
2445
|
(a, b) => (lastLineOfBlock.get(b[0]) ?? 0) - (lastLineOfBlock.get(a[0]) ?? 0)
|
|
2370
2446
|
);
|
|
2371
|
-
for (const [
|
|
2372
|
-
const at = lastLineOfBlock.get(
|
|
2373
|
-
const indent = indentOfBlock.get(
|
|
2447
|
+
for (const [block2, declarations] of ordered) {
|
|
2448
|
+
const at = lastLineOfBlock.get(block2);
|
|
2449
|
+
const indent = indentOfBlock.get(block2) ?? " ";
|
|
2374
2450
|
lines.splice(
|
|
2375
2451
|
at + 1,
|
|
2376
2452
|
0,
|
|
@@ -2409,6 +2485,319 @@ function diffSlice(target, slice) {
|
|
|
2409
2485
|
return { matches: differing.length === 0 && missing.length === 0, differing, missing };
|
|
2410
2486
|
}
|
|
2411
2487
|
|
|
2488
|
+
// src/scale.ts
|
|
2489
|
+
var t = (name, value, description) => ({ name, value, description });
|
|
2490
|
+
var textStep = (step, size, px, leading, tracking, use) => [
|
|
2491
|
+
t(`--text-${step}`, size, `${px}: ${use}`),
|
|
2492
|
+
t(`--text-${step}--line-height`, `var(--leading-${leading})`),
|
|
2493
|
+
t(`--text-${step}--letter-spacing`, `var(--tracking-${tracking})`)
|
|
2494
|
+
];
|
|
2495
|
+
var shadow = (layer, strength) => `${layer} color-mix(in oklch, var(--shadow) ${strength}, transparent)`;
|
|
2496
|
+
function defaultScale() {
|
|
2497
|
+
return [
|
|
2498
|
+
{
|
|
2499
|
+
id: "spacing",
|
|
2500
|
+
title: "Space: the gap between things",
|
|
2501
|
+
themed: true,
|
|
2502
|
+
tokens: [t("--spacing", "0.25rem", "4px: every space is a whole number of these, so 4 is 16px and 24 is 96px")]
|
|
2503
|
+
},
|
|
2504
|
+
{
|
|
2505
|
+
id: "size",
|
|
2506
|
+
title: "Size: the extent of a thing",
|
|
2507
|
+
themed: false,
|
|
2508
|
+
tokens: [
|
|
2509
|
+
t("--size-xs", "1rem", "16px: small icon"),
|
|
2510
|
+
t("--size-sm", "1.25rem", "20px: default icon"),
|
|
2511
|
+
t("--size-md", "1.5rem", "24px: large icon, small avatar"),
|
|
2512
|
+
t("--size-lg", "2rem", "32px: compact control, avatar"),
|
|
2513
|
+
t("--size-xl", "2.5rem", "40px: default control height"),
|
|
2514
|
+
t("--size-2xl", "3rem", "48px: touch control, large avatar"),
|
|
2515
|
+
t("--size-3xl", "4rem", "64px: feature avatar, empty-state icon")
|
|
2516
|
+
]
|
|
2517
|
+
},
|
|
2518
|
+
{
|
|
2519
|
+
id: "text",
|
|
2520
|
+
title: "Type sizes, each with its line height and tracking",
|
|
2521
|
+
themed: true,
|
|
2522
|
+
tokens: [
|
|
2523
|
+
...textStep("xs", "0.75rem", "12px", "normal", "normal", "captions, meta"),
|
|
2524
|
+
...textStep("sm", "0.875rem", "14px", "normal", "normal", "labels, secondary text"),
|
|
2525
|
+
...textStep("base", "1rem", "16px", "normal", "normal", "body"),
|
|
2526
|
+
...textStep("lg", "1.125rem", "18px", "snug", "normal", "lead copy"),
|
|
2527
|
+
...textStep("xl", "1.25rem", "20px", "snug", "normal", "subheads"),
|
|
2528
|
+
...textStep("2xl", "1.5rem", "24px", "tight", "tight", "headings"),
|
|
2529
|
+
...textStep("3xl", "1.875rem", "30px", "tight", "tight", "headings"),
|
|
2530
|
+
...textStep("4xl", "2.25rem", "36px", "none", "tight", "display")
|
|
2531
|
+
]
|
|
2532
|
+
},
|
|
2533
|
+
{
|
|
2534
|
+
id: "leading",
|
|
2535
|
+
title: "Line height",
|
|
2536
|
+
themed: true,
|
|
2537
|
+
tokens: [
|
|
2538
|
+
t("--leading-none", "1.1", "display"),
|
|
2539
|
+
t("--leading-tight", "1.25", "headings"),
|
|
2540
|
+
t("--leading-snug", "1.4", "subheads, lead copy"),
|
|
2541
|
+
t("--leading-normal", "1.5", "body and interface"),
|
|
2542
|
+
t("--leading-relaxed", "1.65", "long reading")
|
|
2543
|
+
]
|
|
2544
|
+
},
|
|
2545
|
+
{
|
|
2546
|
+
id: "tracking",
|
|
2547
|
+
title: "Tracking",
|
|
2548
|
+
themed: true,
|
|
2549
|
+
tokens: [t("--tracking-tight", "-0.02em", "headings and display"), t("--tracking-normal", "0em", "body"), t("--tracking-wide", "0.04em", "all-caps labels")]
|
|
2550
|
+
},
|
|
2551
|
+
{
|
|
2552
|
+
id: "font-weight",
|
|
2553
|
+
title: "Weights",
|
|
2554
|
+
themed: true,
|
|
2555
|
+
tokens: [t("--font-weight-normal", "400"), t("--font-weight-medium", "500"), t("--font-weight-semibold", "600"), t("--font-weight-bold", "700")]
|
|
2556
|
+
},
|
|
2557
|
+
{
|
|
2558
|
+
id: "radius",
|
|
2559
|
+
title: "Corners, scaled to the element: bigger box, bigger step",
|
|
2560
|
+
themed: true,
|
|
2561
|
+
tokens: [
|
|
2562
|
+
t("--radius-xs", "0.125rem", "2px: checkbox, tag"),
|
|
2563
|
+
t("--radius-sm", "0.25rem", "4px: small control, badge"),
|
|
2564
|
+
t("--radius-md", "0.375rem", "6px: button, input, the default control radius"),
|
|
2565
|
+
t("--radius-lg", "0.5rem", "8px: card"),
|
|
2566
|
+
t("--radius-xl", "0.75rem", "12px: modal, sheet, large panel"),
|
|
2567
|
+
t("--radius-2xl", "1rem", "16px: feature surface"),
|
|
2568
|
+
t("--radius-full", "9999px", "pill, avatar")
|
|
2569
|
+
]
|
|
2570
|
+
},
|
|
2571
|
+
{
|
|
2572
|
+
id: "shadow",
|
|
2573
|
+
title: "Elevation: a contact shadow and an ambient one, over the --shadow colour",
|
|
2574
|
+
themed: true,
|
|
2575
|
+
tokens: [
|
|
2576
|
+
t("--shadow-xs", `${shadow("0 1px 1px -0.5px", "10%")}, ${shadow("0 2px 4px -2px", "8%")}`, "a resting input or button"),
|
|
2577
|
+
t("--shadow-sm", `${shadow("0 1px 1px -0.5px", "12%")}, ${shadow("0 3px 8px -3px", "10%")}`, "a resting card"),
|
|
2578
|
+
t("--shadow-md", `${shadow("0 1px 1px -0.5px", "14%")}, ${shadow("0 6px 16px -4px", "14%")}`, "a dropdown, popover or menu"),
|
|
2579
|
+
t("--shadow-lg", `${shadow("0 1px 2px -1px", "16%")}, ${shadow("0 12px 28px -6px", "16%")}`, "a dialog or sheet"),
|
|
2580
|
+
t("--shadow-xl", `${shadow("0 2px 4px -2px", "18%")}, ${shadow("0 24px 48px -12px", "20%")}`, "a full-screen overlay")
|
|
2581
|
+
]
|
|
2582
|
+
},
|
|
2583
|
+
{
|
|
2584
|
+
id: "duration",
|
|
2585
|
+
title: "How long a change takes",
|
|
2586
|
+
themed: false,
|
|
2587
|
+
tokens: [t("--duration-fast", "120ms", "hover, press, colour and opacity"), t("--duration-base", "200ms", "small surfaces entering or leaving"), t("--duration-slow", "320ms", "dialogs, sheets, layout shifts")]
|
|
2588
|
+
},
|
|
2589
|
+
{
|
|
2590
|
+
id: "ease",
|
|
2591
|
+
title: "How a change moves",
|
|
2592
|
+
themed: true,
|
|
2593
|
+
tokens: [
|
|
2594
|
+
t("--ease-out", "cubic-bezier(0.2, 0, 0, 1)", "entering, responding to input"),
|
|
2595
|
+
t("--ease-in", "cubic-bezier(0.4, 0, 1, 1)", "leaving"),
|
|
2596
|
+
t("--ease-in-out", "cubic-bezier(0.4, 0, 0.2, 1)", "starts and ends on screen")
|
|
2597
|
+
]
|
|
2598
|
+
},
|
|
2599
|
+
{
|
|
2600
|
+
id: "breakpoint",
|
|
2601
|
+
title: "Screen sizes: where a page can change shape, from each size up",
|
|
2602
|
+
themed: true,
|
|
2603
|
+
tokens: [
|
|
2604
|
+
t("--breakpoint-sm", "40rem", "640px: a large phone, on its side"),
|
|
2605
|
+
t("--breakpoint-md", "48rem", "768px: a tablet, upright"),
|
|
2606
|
+
t("--breakpoint-lg", "64rem", "1024px: a tablet on its side, a small laptop"),
|
|
2607
|
+
t("--breakpoint-xl", "80rem", "1280px: a laptop"),
|
|
2608
|
+
t("--breakpoint-2xl", "96rem", "1536px: a desktop")
|
|
2609
|
+
]
|
|
2610
|
+
},
|
|
2611
|
+
{
|
|
2612
|
+
id: "container",
|
|
2613
|
+
title: "Widths content is set to",
|
|
2614
|
+
themed: true,
|
|
2615
|
+
tokens: [
|
|
2616
|
+
t("--container-3xs", "16rem"),
|
|
2617
|
+
t("--container-2xs", "18rem"),
|
|
2618
|
+
t("--container-xs", "20rem", "320px: a narrow panel"),
|
|
2619
|
+
t("--container-sm", "24rem", "384px: a dialog"),
|
|
2620
|
+
t("--container-md", "28rem", "448px: a form"),
|
|
2621
|
+
t("--container-lg", "32rem", "512px: a wide dialog"),
|
|
2622
|
+
t("--container-xl", "36rem"),
|
|
2623
|
+
t("--container-2xl", "42rem", "672px: reading width"),
|
|
2624
|
+
t("--container-3xl", "48rem"),
|
|
2625
|
+
t("--container-4xl", "56rem"),
|
|
2626
|
+
t("--container-5xl", "64rem", "1024px: a page of an app"),
|
|
2627
|
+
t("--container-6xl", "72rem"),
|
|
2628
|
+
t("--container-7xl", "80rem", "1280px: the widest a page frame gets")
|
|
2629
|
+
]
|
|
2630
|
+
}
|
|
2631
|
+
];
|
|
2632
|
+
}
|
|
2633
|
+
var TIER_OF = [
|
|
2634
|
+
[/^--spacing(-|$)/, "spacing"],
|
|
2635
|
+
[/^--size-/, "size"],
|
|
2636
|
+
[/^--text-/, "text"],
|
|
2637
|
+
[/^--leading-/, "leading"],
|
|
2638
|
+
[/^--tracking-/, "tracking"],
|
|
2639
|
+
[/^--font-weight-/, "font-weight"],
|
|
2640
|
+
[/^--radius-/, "radius"],
|
|
2641
|
+
[/^--shadow-/, "shadow"],
|
|
2642
|
+
[/^--duration-/, "duration"],
|
|
2643
|
+
[/^--ease-/, "ease"],
|
|
2644
|
+
[/^--breakpoint-/, "breakpoint"],
|
|
2645
|
+
[/^--container-/, "container"]
|
|
2646
|
+
];
|
|
2647
|
+
function applyScaleOverrides(tiers, overrides) {
|
|
2648
|
+
const out = tiers.map((tier) => ({ ...tier, tokens: [...tier.tokens] }));
|
|
2649
|
+
for (const [name, o] of Object.entries(overrides)) {
|
|
2650
|
+
const id = TIER_OF.find(([re]) => re.test(name))?.[1];
|
|
2651
|
+
if (!id) throw new Error(`override "${name}" names no scale tier (--spacing, --size-*, --text-*, --leading-*, --tracking-*, --font-weight-*, --radius-*, --shadow-*, --duration-*, --ease-*, --breakpoint-*, --container-*)`);
|
|
2652
|
+
const tier = out.find((x) => x.id === id);
|
|
2653
|
+
const at = tier.tokens.findIndex((x) => x.name === name);
|
|
2654
|
+
if (o === null) {
|
|
2655
|
+
if (at >= 0) tier.tokens.splice(at, 1);
|
|
2656
|
+
tier.tokens = tier.tokens.filter((x) => !x.name.startsWith(`${name}--`));
|
|
2657
|
+
continue;
|
|
2658
|
+
}
|
|
2659
|
+
const value = typeof o === "string" ? o : o.value;
|
|
2660
|
+
const description = typeof o === "string" ? tier.tokens[at]?.description : o.description ?? tier.tokens[at]?.description;
|
|
2661
|
+
const token = { name, value, description };
|
|
2662
|
+
if (at >= 0) tier.tokens[at] = token;
|
|
2663
|
+
else {
|
|
2664
|
+
const owner = /^(--text-[\w-]+?)--/.exec(name)?.[1];
|
|
2665
|
+
const after = owner ? tier.tokens.map((x) => x.name.startsWith(owner)).lastIndexOf(true) : -1;
|
|
2666
|
+
tier.tokens.splice(after >= 0 ? after + 1 : tier.tokens.length, 0, token);
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
for (const tier of out) if (ORDERED.has(tier.id)) tier.tokens.sort((a, b) => Number.parseFloat(a.value) - Number.parseFloat(b.value));
|
|
2670
|
+
return out;
|
|
2671
|
+
}
|
|
2672
|
+
var ORDERED = /* @__PURE__ */ new Set(["leading", "tracking", "font-weight"]);
|
|
2673
|
+
var decl = (tok, indent) => {
|
|
2674
|
+
const value = tok.value.includes("),") && tok.value.length > 70 ? `
|
|
2675
|
+
${indent} ${tok.value.replace(/\),\s*/g, "),\n" + indent + " ")}` : tok.value;
|
|
2676
|
+
return `${indent}${tok.name}:${value.startsWith("\n") ? "" : " "}${value};${tok.description ? ` /* ${tok.description} */` : ""}`;
|
|
2677
|
+
};
|
|
2678
|
+
function block(tiers, indent) {
|
|
2679
|
+
const out = [];
|
|
2680
|
+
for (const tier of tiers) {
|
|
2681
|
+
if (!tier.tokens.length) continue;
|
|
2682
|
+
out.push("", `${indent}/* ${tier.title} */`);
|
|
2683
|
+
for (const tok of tier.tokens) out.push(decl(tok, indent));
|
|
2684
|
+
}
|
|
2685
|
+
return out.slice(1);
|
|
2686
|
+
}
|
|
2687
|
+
function toCss(tiers) {
|
|
2688
|
+
const screens = tiers.find((x) => x.id === "breakpoint")?.tokens ?? [];
|
|
2689
|
+
return [
|
|
2690
|
+
":root {",
|
|
2691
|
+
...block(tiers, " "),
|
|
2692
|
+
"}",
|
|
2693
|
+
...screens.length ? [
|
|
2694
|
+
"",
|
|
2695
|
+
"/* Screen sizes for media queries: @media (--breakpoint-md) { \u2026 }. A custom property can't go in @media, so these",
|
|
2696
|
+
" are custom media queries, which Lightning CSS and postcss-preset-env compile. */",
|
|
2697
|
+
...screens.map((s) => `@custom-media ${s.name} (width >= ${s.value});`)
|
|
2698
|
+
] : []
|
|
2699
|
+
].join("\n");
|
|
2700
|
+
}
|
|
2701
|
+
function toTailwindV4(tiers) {
|
|
2702
|
+
const themed = tiers.filter((x) => x.themed);
|
|
2703
|
+
const plain = tiers.filter((x) => !x.themed && x.tokens.length);
|
|
2704
|
+
return ["@theme static {", ...block(themed, " "), "}", ...plain.length ? ["", "/* Tailwind has no namespace for these: use them as h-(--size-xl), duration-(--duration-base) */", "@layer base {", " :root {", ...block(plain, " "), " }", "}"] : []].join("\n");
|
|
2705
|
+
}
|
|
2706
|
+
function resolve(value, all, depth = 0) {
|
|
2707
|
+
return depth > 5 ? value : value.replace(/var\((--[\w-]+)\)/g, (m, name) => all.has(name) && !/--shadow$/.test(name) ? resolve(all.get(name), all, depth + 1) : m);
|
|
2708
|
+
}
|
|
2709
|
+
function toTailwindV3(tiers) {
|
|
2710
|
+
const all = new Map(tiers.flatMap((x) => x.tokens.map((tok) => [tok.name, tok.value])));
|
|
2711
|
+
const of = (id, prefix) => Object.fromEntries((tiers.find((x) => x.id === id)?.tokens ?? []).filter((tok) => !tok.name.includes("--", 2)).map((tok) => [tok.name.slice(prefix.length), resolve(tok.value, all)]));
|
|
2712
|
+
const base = Number.parseFloat(all.get("--spacing") ?? "0.25");
|
|
2713
|
+
const steps = [0.5, 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32];
|
|
2714
|
+
const text = Object.fromEntries(
|
|
2715
|
+
(tiers.find((x) => x.id === "text")?.tokens ?? []).filter((tok) => !tok.name.includes("--", 2)).map((tok) => {
|
|
2716
|
+
const lh = all.get(`${tok.name}--line-height`);
|
|
2717
|
+
const ls = all.get(`${tok.name}--letter-spacing`);
|
|
2718
|
+
const extra = { ...lh ? { lineHeight: resolve(lh, all) } : {}, ...ls ? { letterSpacing: resolve(ls, all) } : {} };
|
|
2719
|
+
return [tok.name.slice("--text-".length), Object.keys(extra).length ? [tok.value, extra] : tok.value];
|
|
2720
|
+
})
|
|
2721
|
+
);
|
|
2722
|
+
const theme = {
|
|
2723
|
+
screens: of("breakpoint", "--breakpoint-"),
|
|
2724
|
+
extend: {
|
|
2725
|
+
spacing: Object.fromEntries(steps.map((n) => [String(n), `${Math.round(n * base * 1e4) / 1e4}rem`])),
|
|
2726
|
+
size: of("size", "--size-"),
|
|
2727
|
+
fontSize: text,
|
|
2728
|
+
lineHeight: of("leading", "--leading-"),
|
|
2729
|
+
letterSpacing: of("tracking", "--tracking-"),
|
|
2730
|
+
fontWeight: of("font-weight", "--font-weight-"),
|
|
2731
|
+
borderRadius: of("radius", "--radius-"),
|
|
2732
|
+
boxShadow: of("shadow", "--shadow-"),
|
|
2733
|
+
transitionDuration: of("duration", "--duration-"),
|
|
2734
|
+
transitionTimingFunction: of("ease", "--ease-"),
|
|
2735
|
+
maxWidth: of("container", "--container-")
|
|
2736
|
+
}
|
|
2737
|
+
};
|
|
2738
|
+
return `/** @type {import('tailwindcss').Config['theme']} */
|
|
2739
|
+
module.exports = ${JSON.stringify(theme, null, 2)};`;
|
|
2740
|
+
}
|
|
2741
|
+
var DTCG_TYPE = {
|
|
2742
|
+
spacing: "dimension",
|
|
2743
|
+
size: "dimension",
|
|
2744
|
+
text: "dimension",
|
|
2745
|
+
leading: "number",
|
|
2746
|
+
tracking: "dimension",
|
|
2747
|
+
"font-weight": "fontWeight",
|
|
2748
|
+
radius: "dimension",
|
|
2749
|
+
shadow: void 0,
|
|
2750
|
+
duration: "duration",
|
|
2751
|
+
ease: "cubicBezier",
|
|
2752
|
+
breakpoint: "dimension",
|
|
2753
|
+
container: "dimension"
|
|
2754
|
+
};
|
|
2755
|
+
function dtcgValue(value, type) {
|
|
2756
|
+
const ref = /^var\(--([\w-]+?)-([\w]+)\)$/.exec(value);
|
|
2757
|
+
if (ref) return `{${ref[1]}.${ref[2]}}`;
|
|
2758
|
+
if (type === "cubicBezier") return (/cubic-bezier\(([^)]+)\)/.exec(value)?.[1] ?? "").split(",").map((n) => Number(n.trim()));
|
|
2759
|
+
if (type === "fontWeight" || type === "number") return Number(value);
|
|
2760
|
+
return value;
|
|
2761
|
+
}
|
|
2762
|
+
function toDtcg(tiers) {
|
|
2763
|
+
const doc = {};
|
|
2764
|
+
for (const tier of tiers) {
|
|
2765
|
+
const type = DTCG_TYPE[tier.id];
|
|
2766
|
+
if (tier.id === "spacing") {
|
|
2767
|
+
const tok = tier.tokens.find((x) => x.name === "--spacing");
|
|
2768
|
+
if (tok) doc.spacing = { $type: type, $value: tok.value, ...tok.description ? { $description: tok.description } : {} };
|
|
2769
|
+
continue;
|
|
2770
|
+
}
|
|
2771
|
+
const prefix = tier.tokens[0]?.name.match(/^--[a-z]+(?:-weight)?-/)?.[0] ?? "";
|
|
2772
|
+
const group = { $description: tier.title };
|
|
2773
|
+
if (type) group.$type = type;
|
|
2774
|
+
for (const tok of tier.tokens) {
|
|
2775
|
+
const companion = /^--text-([\w-]+?)--(line-height|letter-spacing)$/.exec(tok.name);
|
|
2776
|
+
if (companion) {
|
|
2777
|
+
const owner = group[companion[1]];
|
|
2778
|
+
if (owner) {
|
|
2779
|
+
owner.$extensions = { designtools: { ...owner.$extensions?.designtools ?? {}, [companion[2]]: dtcgValue(tok.value, void 0) } };
|
|
2780
|
+
}
|
|
2781
|
+
continue;
|
|
2782
|
+
}
|
|
2783
|
+
group[tok.name.slice(prefix.length)] = { $value: dtcgValue(tok.value, type), ...tok.description ? { $description: tok.description } : {} };
|
|
2784
|
+
}
|
|
2785
|
+
doc[tier.id] = group;
|
|
2786
|
+
}
|
|
2787
|
+
return JSON.stringify(doc, null, 2);
|
|
2788
|
+
}
|
|
2789
|
+
function toJson(tiers) {
|
|
2790
|
+
return JSON.stringify({ scale: tiers.flatMap((tier) => tier.tokens.map((tok) => ({ ...tok, tier: tier.id }))) }, null, 2);
|
|
2791
|
+
}
|
|
2792
|
+
function exportScale(tiers, format, header) {
|
|
2793
|
+
const body = { css: toCss, "tailwind-v4": toTailwindV4, "tailwind-v3": toTailwindV3, dtcg: toDtcg, json: toJson }[format](tiers);
|
|
2794
|
+
if (!header || format === "dtcg" || format === "json") return `${body}
|
|
2795
|
+
`;
|
|
2796
|
+
return `${header}
|
|
2797
|
+
${body}
|
|
2798
|
+
`;
|
|
2799
|
+
}
|
|
2800
|
+
|
|
2412
2801
|
// src/contract.ts
|
|
2413
2802
|
var ALIASES = new Set(ALIAS_PAIRS.map(([, alias]) => alias));
|
|
2414
2803
|
var TEXT = 4.5;
|
|
@@ -2538,16 +2927,17 @@ export {
|
|
|
2538
2927
|
checkSurfaceContrast,
|
|
2539
2928
|
checkRampChroma,
|
|
2540
2929
|
computeAccessibilityScore,
|
|
2541
|
-
STATUS_GROUNDS,
|
|
2542
2930
|
GROUND_BASE_TOKENS,
|
|
2543
2931
|
GROUND_COLOR_PARTS,
|
|
2544
2932
|
groundTokenNames,
|
|
2545
|
-
|
|
2933
|
+
reservedSurfaceNames,
|
|
2934
|
+
suggestStep,
|
|
2546
2935
|
generateGroundTokens,
|
|
2547
2936
|
generateTokens,
|
|
2548
2937
|
usageReport,
|
|
2549
2938
|
serialiseUsage,
|
|
2550
2939
|
WIDE_GAMUT_QUERY,
|
|
2940
|
+
modeSelectors,
|
|
2551
2941
|
GROUND_SELECTOR,
|
|
2552
2942
|
exportToCss,
|
|
2553
2943
|
overrideContract,
|
|
@@ -2563,6 +2953,9 @@ export {
|
|
|
2563
2953
|
readDeclarations,
|
|
2564
2954
|
mergeSlice,
|
|
2565
2955
|
diffSlice,
|
|
2956
|
+
defaultScale,
|
|
2957
|
+
applyScaleOverrides,
|
|
2958
|
+
exportScale,
|
|
2566
2959
|
stepContract,
|
|
2567
2960
|
GENERATOR_NAME,
|
|
2568
2961
|
stableStringify,
|