@designtools/tokens 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +106 -7
- package/dist/{chunk-YOUMF42P.js → chunk-RQGC7LES.js} +464 -51
- package/dist/cli.js +63 -15
- package/dist/index.d.ts +99 -3
- package/dist/index.js +15 -1
- package/dist/studio/seed.js +4 -0
- package/package.json +2 -2
|
@@ -47,12 +47,12 @@ function resolveShades(requested) {
|
|
|
47
47
|
const seen = new Set(requested);
|
|
48
48
|
return VALID_SHADES.filter((s) => seen.has(s));
|
|
49
49
|
}
|
|
50
|
-
function snapShade(
|
|
51
|
-
if (active.includes(
|
|
50
|
+
function snapShade(shade2, active) {
|
|
51
|
+
if (active.includes(shade2)) return shade2;
|
|
52
52
|
if (active.length === 0) {
|
|
53
53
|
throw new Error("cannot snap a shade against an empty shade set");
|
|
54
54
|
}
|
|
55
|
-
const target = VALID_SHADES.indexOf(
|
|
55
|
+
const target = VALID_SHADES.indexOf(shade2);
|
|
56
56
|
let best = active[0];
|
|
57
57
|
let bestDistance = Infinity;
|
|
58
58
|
for (const candidate of active) {
|
|
@@ -295,18 +295,18 @@ function buildScale(baseColor, strategy, options) {
|
|
|
295
295
|
};
|
|
296
296
|
const scale = {};
|
|
297
297
|
for (let i = 0; i < n; i++) {
|
|
298
|
-
const
|
|
299
|
-
if (!emitted.includes(
|
|
298
|
+
const shade2 = VALID_SHADES[i];
|
|
299
|
+
if (!emitted.includes(shade2)) continue;
|
|
300
300
|
if (i === anchor) {
|
|
301
|
-
scale[
|
|
301
|
+
scale[shade2] = formatOklchInGamut(baseLightness, baseChroma, baseHue, gamut);
|
|
302
302
|
continue;
|
|
303
303
|
}
|
|
304
304
|
const pin = pins.get(i);
|
|
305
305
|
if (pin) {
|
|
306
|
-
scale[
|
|
306
|
+
scale[shade2] = formatOklchInGamut(pin.l, pin.c ?? 0, pin.h ?? 0, gamut);
|
|
307
307
|
continue;
|
|
308
308
|
}
|
|
309
|
-
scale[
|
|
309
|
+
scale[shade2] = formatOklchInGamut(lightness[i], chromaAt(i), hueAt(i), gamut);
|
|
310
310
|
}
|
|
311
311
|
return scale;
|
|
312
312
|
}
|
|
@@ -556,12 +556,12 @@ function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed", opti
|
|
|
556
556
|
if (options?.aliases === false) for (const [, alias] of ALIAS_PAIRS) delete palette[alias];
|
|
557
557
|
return palette;
|
|
558
558
|
}
|
|
559
|
-
function getShade(palette, colorName,
|
|
559
|
+
function getShade(palette, colorName, shade2) {
|
|
560
560
|
const scale = palette[colorName];
|
|
561
561
|
if (!scale) {
|
|
562
562
|
throw new Error(`Color "${colorName}" not found in palette`);
|
|
563
563
|
}
|
|
564
|
-
return scale[
|
|
564
|
+
return scale[shade2];
|
|
565
565
|
}
|
|
566
566
|
|
|
567
567
|
// src/bridge.ts
|
|
@@ -584,7 +584,7 @@ function bridgeShade(offset, seedShade, active) {
|
|
|
584
584
|
function extendedBridgeShades(mode, seedShade, active, overrides) {
|
|
585
585
|
const offsets = BRIDGE_OFFSETS[mode].extended;
|
|
586
586
|
const at = (offset) => bridgeShade(offset, seedShade, active);
|
|
587
|
-
const snap = (
|
|
587
|
+
const snap = (shade2) => snapShade(shade2, active);
|
|
588
588
|
const offsetSubdued = at(offsets.subdued);
|
|
589
589
|
const subdued = mode === "light" ? snap(Math.min(offsetSubdued, 100)) : snap(Math.max(offsetSubdued, 800));
|
|
590
590
|
return {
|
|
@@ -639,20 +639,20 @@ var TINT_HUE_SHARE = 0.8;
|
|
|
639
639
|
var APCA_WHITE_MAX_Y = { "aa-large": 0.4564, aa: 0.314, aaa: 0.1946 };
|
|
640
640
|
var APCA_BLACK_MIN_Y = { "aa-large": 0.2831, aa: 0.4233, aaa: 0.5906 };
|
|
641
641
|
var WCAG_FLOOR = { "aa-large": 3, aa: 4.5, aaa: 7 };
|
|
642
|
-
function settleFill(scale,
|
|
642
|
+
function settleFill(scale, shade2, target, active, views) {
|
|
643
643
|
const lum = (s) => views.map((v) => getContrastRatio(v.scale[s], "#000000", v.gamut) * 0.05 - 0.05);
|
|
644
644
|
const whiteMax = Math.min(1.05 / WCAG_FLOOR[target] - 0.05, APCA_WHITE_MAX_Y[target]);
|
|
645
645
|
const blackMin = Math.max(WCAG_FLOOR[target] * 0.05 - 0.05, APCA_BLACK_MIN_Y[target]);
|
|
646
646
|
const ok = (s) => lum(s).every((y2) => y2 <= whiteMax) || lum(s).every((y2) => y2 >= blackMin);
|
|
647
|
-
if (ok(
|
|
648
|
-
const y = Math.max(...lum(
|
|
647
|
+
if (ok(shade2)) return shade2;
|
|
648
|
+
const y = Math.max(...lum(shade2));
|
|
649
649
|
const darkerFirst = y - whiteMax <= 2 * (blackMin - y);
|
|
650
|
-
const at = active.indexOf(
|
|
650
|
+
const at = active.indexOf(shade2);
|
|
651
651
|
const darker = [at + 1, at + 2], lighter = [at - 1, at - 2];
|
|
652
652
|
for (const i of darkerFirst ? [...darker, ...lighter] : [...lighter, ...darker]) {
|
|
653
653
|
if (i >= 0 && i < active.length && scale[active[i]] && ok(active[i])) return active[i];
|
|
654
654
|
}
|
|
655
|
-
return
|
|
655
|
+
return shade2;
|
|
656
656
|
}
|
|
657
657
|
var HOVER_SHIFT = 0.09;
|
|
658
658
|
var ACTIVE_SHIFT = 0.15;
|
|
@@ -687,13 +687,13 @@ function findBestForeground(views, white, black, algorithm, contrastTarget, colo
|
|
|
687
687
|
].sort((a, b) => Number(b.wcag >= wcagMin) - Number(a.wcag >= wcagMin) || b.contrast - a.contrast);
|
|
688
688
|
const extreme = { value: ends[0].value, ref: ends[0].ref };
|
|
689
689
|
const best = ends[0].contrast;
|
|
690
|
-
const steps = (which, refName) => activeShades.map((
|
|
691
|
-
value: views[0][which][
|
|
692
|
-
ref: { type: "shade", colorName: refName, shade },
|
|
693
|
-
contrast: getContrast((view) => view[which][
|
|
694
|
-
wcag: wcagMin ? getWcag((view) => view[which][
|
|
690
|
+
const steps = (which, refName) => activeShades.map((shade2) => ({
|
|
691
|
+
value: views[0][which][shade2],
|
|
692
|
+
ref: { type: "shade", colorName: refName, shade: shade2 },
|
|
693
|
+
contrast: getContrast((view) => view[which][shade2]),
|
|
694
|
+
wcag: wcagMin ? getWcag((view) => view[which][shade2]) : Infinity,
|
|
695
695
|
// the least chroma any view shows, so the sRGB and P3 tiers judge the hue alike and pick the same step
|
|
696
|
-
chroma: Math.min(...views.map((view) => chromaOf(view[which][
|
|
696
|
+
chroma: Math.min(...views.map((view) => chromaOf(view[which][shade2]))),
|
|
697
697
|
own: which === "scale"
|
|
698
698
|
}));
|
|
699
699
|
const own = steps("scale", colorName);
|
|
@@ -736,7 +736,7 @@ function createExtendedSemanticColor(scale, neutral, white, black, mode, contras
|
|
|
736
736
|
const warm = mode === "light" && seedColour.c >= 0.05 && (seedColour.h ?? 0) >= 40 && (seedColour.h ?? 0) <= 115;
|
|
737
737
|
const subduedShade = warm && !overrides?.subdued ? snapShade(200, activeShades) : offsetSubdued;
|
|
738
738
|
const seedStep = bridgeShade(0, seedShade, activeShades);
|
|
739
|
-
const blackLabel = (
|
|
739
|
+
const blackLabel = (shade2) => getContrastRatio(scale[shade2], black) > getContrastRatio(scale[shade2], white);
|
|
740
740
|
const bridgeDefault = mode === "dark" && !overrides?.DEFAULT && offsetDefault !== seedStep && blackLabel(seedStep) ? seedStep : offsetDefault;
|
|
741
741
|
const fillViews = [{ scale, gamut: gamuts.own }, ...gamuts.other ? [{ scale: gamuts.other.scale, gamut: gamuts.other.gamut }] : []];
|
|
742
742
|
const defaultShade = colorName === "primary" || overrides?.DEFAULT ? bridgeDefault : settleFill(scale, bridgeDefault, contrastTarget, activeShades, fillViews);
|
|
@@ -856,7 +856,7 @@ function textOn(bg, neutral, white, black, active) {
|
|
|
856
856
|
}
|
|
857
857
|
function generateSurfaceTokens(palette, mode, overrides, shades, colors, backgrounds) {
|
|
858
858
|
const activeShades = shadeKeys(shades);
|
|
859
|
-
const snap = (
|
|
859
|
+
const snap = (shade2) => snapShade(shade2, activeShades);
|
|
860
860
|
const neutral = palette.neutral;
|
|
861
861
|
if (!neutral) {
|
|
862
862
|
throw new Error("Neutral palette not found");
|
|
@@ -907,7 +907,7 @@ function generateSurfaceTokens(palette, mode, overrides, shades, colors, backgro
|
|
|
907
907
|
}
|
|
908
908
|
function generateUtilityTokens(palette, mode, overrides, shades, seedShade, grounds) {
|
|
909
909
|
const activeShades = shadeKeys(shades);
|
|
910
|
-
const snap = (
|
|
910
|
+
const snap = (shade2) => snapShade(shade2, activeShades);
|
|
911
911
|
const neutral = palette.neutral;
|
|
912
912
|
const primary = palette.primary;
|
|
913
913
|
if (!neutral || !primary) {
|
|
@@ -1025,11 +1025,11 @@ function checkRampChroma(palette) {
|
|
|
1025
1025
|
if (typeof scale === "string" || !scale) continue;
|
|
1026
1026
|
let peak = 0;
|
|
1027
1027
|
let atShade = 500;
|
|
1028
|
-
for (const [
|
|
1028
|
+
for (const [shade2, color] of Object.entries(scale)) {
|
|
1029
1029
|
const { c } = parseToOklch(color);
|
|
1030
1030
|
if (c > peak) {
|
|
1031
1031
|
peak = c;
|
|
1032
|
-
atShade = Number(
|
|
1032
|
+
atShade = Number(shade2);
|
|
1033
1033
|
}
|
|
1034
1034
|
}
|
|
1035
1035
|
results[name] = {
|
|
@@ -1070,6 +1070,320 @@ function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa", gam
|
|
|
1070
1070
|
return { score, passed, total };
|
|
1071
1071
|
}
|
|
1072
1072
|
|
|
1073
|
+
// src/grounds.ts
|
|
1074
|
+
var STATUS_GROUNDS = ["success", "warning", "destructive"];
|
|
1075
|
+
var GROUND_BASE_TOKENS = ["canvas", "canvas-foreground", "muted", "muted-foreground", "border", "input", "ring"];
|
|
1076
|
+
var GROUND_COLOR_PARTS = ["", "-foreground", "-subdued", "-subdued-foreground", "-hover", "-active"];
|
|
1077
|
+
var FILL_FLOOR = 3;
|
|
1078
|
+
var PANEL_FLOOR = 1.1;
|
|
1079
|
+
var TEXT_FLOOR = { "aa-large": 3, aa: 4.5, aaa: 7 };
|
|
1080
|
+
function groundTokenNames(colors) {
|
|
1081
|
+
return [...GROUND_BASE_TOKENS, ...colors.flatMap((c) => GROUND_COLOR_PARTS.map((p) => `${c}${p}`))];
|
|
1082
|
+
}
|
|
1083
|
+
function semanticColors(semantic) {
|
|
1084
|
+
const core = ["primary", "neutral", "success", "destructive", "warning"];
|
|
1085
|
+
return [...core, ...Object.keys(semantic).filter((n) => !core.includes(n) && n !== "muted")];
|
|
1086
|
+
}
|
|
1087
|
+
function reservedGroundNames(colors) {
|
|
1088
|
+
return [
|
|
1089
|
+
"canvas",
|
|
1090
|
+
"surface",
|
|
1091
|
+
"overlay",
|
|
1092
|
+
"foreground",
|
|
1093
|
+
"muted",
|
|
1094
|
+
"border",
|
|
1095
|
+
"input",
|
|
1096
|
+
"ring",
|
|
1097
|
+
"shadow",
|
|
1098
|
+
...colors.flatMap((c) => [c, `${c}-subdued`])
|
|
1099
|
+
];
|
|
1100
|
+
}
|
|
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
|
+
var valueOf = (ref, palette) => {
|
|
1115
|
+
switch (ref.type) {
|
|
1116
|
+
case "white":
|
|
1117
|
+
return palette.white;
|
|
1118
|
+
case "black":
|
|
1119
|
+
return palette.black;
|
|
1120
|
+
case "value":
|
|
1121
|
+
return ref.value;
|
|
1122
|
+
case "shade":
|
|
1123
|
+
return palette[ref.colorName][ref.shade];
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
var shade = (colorName, step) => ({ type: "shade", colorName, shade: step });
|
|
1127
|
+
var sameRef = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
1128
|
+
function fromSurfaceRef(ref) {
|
|
1129
|
+
switch (ref.type) {
|
|
1130
|
+
case "white":
|
|
1131
|
+
return { type: "white" };
|
|
1132
|
+
case "black":
|
|
1133
|
+
return { type: "black" };
|
|
1134
|
+
case "shade":
|
|
1135
|
+
return shade("neutral", ref.shade);
|
|
1136
|
+
case "value":
|
|
1137
|
+
return { type: "value", value: ref.value };
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
function baseRefs(base, colors, ground, active) {
|
|
1141
|
+
const { semantic, surface, utility } = base;
|
|
1142
|
+
const refs = {};
|
|
1143
|
+
const sr = surface._refs;
|
|
1144
|
+
refs.canvas = fromSurfaceRef(ground === "canvas" ? sr.canvas : sr.surface);
|
|
1145
|
+
refs["canvas-foreground"] = fromSurfaceRef(ground === "canvas" ? sr["canvas-foreground"] : sr["surface-foreground"]);
|
|
1146
|
+
const muted = semantic.muted._shades;
|
|
1147
|
+
refs.muted = shade("neutral", snapShade(muted.DEFAULT, active));
|
|
1148
|
+
refs["muted-foreground"] = shade("neutral", snapShade(muted.foreground, active));
|
|
1149
|
+
const steps = utility._steps;
|
|
1150
|
+
refs.border = shade("neutral", steps.border);
|
|
1151
|
+
refs.input = shade("neutral", steps.input);
|
|
1152
|
+
refs.ring = shade("primary", steps.ring);
|
|
1153
|
+
for (const name of colors) {
|
|
1154
|
+
const c = semantic[name];
|
|
1155
|
+
refs[name] = shade(name, c._shades.DEFAULT);
|
|
1156
|
+
refs[`${name}-foreground`] = c._refs.foreground;
|
|
1157
|
+
refs[`${name}-subdued`] = shade(name, c._shades.subdued);
|
|
1158
|
+
refs[`${name}-subdued-foreground`] = c._refs["subdued-foreground"];
|
|
1159
|
+
refs[`${name}-hover`] = { type: "value", value: c.hover };
|
|
1160
|
+
refs[`${name}-active`] = { type: "value", value: c.active };
|
|
1161
|
+
}
|
|
1162
|
+
return refs;
|
|
1163
|
+
}
|
|
1164
|
+
var FAMILY_CHROMA = 0.05;
|
|
1165
|
+
function familyOf(bg, palette, colors, active) {
|
|
1166
|
+
const o = parseToOklch(bg);
|
|
1167
|
+
if ((o.c ?? 0) < FAMILY_CHROMA) return "neutral";
|
|
1168
|
+
let best = "neutral";
|
|
1169
|
+
let distance = 30;
|
|
1170
|
+
for (const name of colors) {
|
|
1171
|
+
if (name === "neutral") continue;
|
|
1172
|
+
const scale = palette[name];
|
|
1173
|
+
const ref = parseToOklch(scale[snapShade(500, active)]);
|
|
1174
|
+
if ((ref.c ?? 0) < 0.03) continue;
|
|
1175
|
+
const d = Math.abs((((ref.h ?? 0) - (o.h ?? 0)) % 360 + 540) % 360 - 180);
|
|
1176
|
+
if (d < distance) {
|
|
1177
|
+
distance = d;
|
|
1178
|
+
best = name;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
return best;
|
|
1182
|
+
}
|
|
1183
|
+
function searchGround(spec, base, baseCanvas, colors, views, mode, options, active) {
|
|
1184
|
+
const target = options?.contrastTarget ?? "aa";
|
|
1185
|
+
const algorithm = options?.contrastAlgorithm ?? "wcag";
|
|
1186
|
+
const textFloor = TEXT_FLOOR[target];
|
|
1187
|
+
const mutedFloor = target === "aaa" ? 7 : 4.5;
|
|
1188
|
+
const srgb = views[0].palette;
|
|
1189
|
+
const notes = [];
|
|
1190
|
+
const cr = (a, b) => Math.min(...views.map((v) => getContrastRatio(valueOf(a, v.palette), valueOf(b, v.palette), v.gamut)));
|
|
1191
|
+
const ground = spec.canvas;
|
|
1192
|
+
const groundL = parseToOklch(valueOf(ground, srgb)).l;
|
|
1193
|
+
const light = cr(ground, { type: "black" }) > cr(ground, { type: "white" });
|
|
1194
|
+
const farSide = (ref) => {
|
|
1195
|
+
const l = parseToOklch(valueOf(ref, srgb)).l;
|
|
1196
|
+
return light ? l < groundL : l > groundL;
|
|
1197
|
+
};
|
|
1198
|
+
const steps = (colorName) => active.filter((s) => srgb[colorName]?.[s] !== void 0).map((s) => shade(colorName, s));
|
|
1199
|
+
const tinted = spec.family !== "neutral";
|
|
1200
|
+
const own = steps(spec.family).filter(farSide);
|
|
1201
|
+
const pool = own.length >= 3 || tinted && own.length ? own : [...own, ...steps("neutral").filter(farSide)];
|
|
1202
|
+
const textPool = steps(spec.family);
|
|
1203
|
+
const closest = (list, to, aim2) => [...list].sort((a, b) => Math.abs(cr(a, to) - aim2) - Math.abs(cr(b, to) - aim2))[0];
|
|
1204
|
+
const panelCandidates = (list, vis) => {
|
|
1205
|
+
const drawn = list.filter((r) => cr(r, ground) >= PANEL_FLOOR);
|
|
1206
|
+
const byVis = (a, b) => Math.abs(cr(a, ground) - vis) - Math.abs(cr(b, ground) - vis);
|
|
1207
|
+
return [...drawn.filter(farSide).sort(byVis), ...drawn.filter((r) => !farSide(r)).sort(byVis)];
|
|
1208
|
+
};
|
|
1209
|
+
const inHue = (r) => !tinted || r.type === "shade" && r.colorName === spec.family;
|
|
1210
|
+
const keepsPlace = (r) => inHue(r) && farSide(r) && cr(r, ground) >= PANEL_FLOOR;
|
|
1211
|
+
const nearest = (colorName, from, on, floor) => {
|
|
1212
|
+
const list = steps(colorName).filter((r) => cr(r, on) >= floor);
|
|
1213
|
+
const at = active.indexOf(from);
|
|
1214
|
+
const position = (r) => active.indexOf(r.shade);
|
|
1215
|
+
return list.sort((a, b) => Math.abs(position(a) - at) - Math.abs(position(b) - at))[0];
|
|
1216
|
+
};
|
|
1217
|
+
const bestEnd = (on) => cr({ type: "white" }, on) >= cr({ type: "black" }, on) ? { type: "white" } : { type: "black" };
|
|
1218
|
+
const refs = {};
|
|
1219
|
+
refs.canvas = ground;
|
|
1220
|
+
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
|
+
const borderVis = cr(base.border, baseCanvas);
|
|
1244
|
+
const borderReads = (r) => cr(r, ground) >= Math.min(1.15, borderVis);
|
|
1245
|
+
refs.border = keepsPlace(base.border) && cr(base.border, ground) >= borderVis * 0.9 && cr(base.border, ground) <= borderVis * 2 ? base.border : closest([...pool, ...steps(spec.family)].filter(borderReads), ground, borderVis) ?? base.border;
|
|
1246
|
+
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
|
+
const ringFamily = tinted ? spec.family : "primary";
|
|
1248
|
+
const ringFrom = tinted ? base[spec.family].shade : base.ring.shade;
|
|
1249
|
+
refs.ring = inHue(base.ring) && cr(base.ring, ground) >= FILL_FLOOR ? base.ring : nearest(ringFamily, ringFrom, ground, FILL_FLOOR) ?? bestEnd(ground);
|
|
1250
|
+
for (const name of colors) {
|
|
1251
|
+
const src = tinted && (name === "primary" || name === "neutral") ? spec.family : name;
|
|
1252
|
+
const fillRef = base[src];
|
|
1253
|
+
const fillShade = fillRef.shade;
|
|
1254
|
+
const scaleIn = (v) => v.palette[src];
|
|
1255
|
+
const neutralIn = (v) => v.palette.neutral;
|
|
1256
|
+
const labelOn = (bg, role2, pageGrounds = []) => {
|
|
1257
|
+
const found = findBestForeground(
|
|
1258
|
+
views.map((v) => ({ background: valueOf(bg, v.palette), scale: scaleIn(v), neutral: neutralIn(v), gamut: v.gamut })),
|
|
1259
|
+
srgb.white,
|
|
1260
|
+
srgb.black,
|
|
1261
|
+
algorithm,
|
|
1262
|
+
target,
|
|
1263
|
+
src,
|
|
1264
|
+
active,
|
|
1265
|
+
role2,
|
|
1266
|
+
options?.foreground ?? "balanced",
|
|
1267
|
+
mode,
|
|
1268
|
+
options?.wcagFloor ?? true,
|
|
1269
|
+
pageGrounds
|
|
1270
|
+
);
|
|
1271
|
+
return found.ref;
|
|
1272
|
+
};
|
|
1273
|
+
const setStates = (fill, label) => {
|
|
1274
|
+
const f = valueOf(fill, srgb), l = valueOf(label, srgb);
|
|
1275
|
+
refs[`${name}-hover`] = { type: "value", value: stateOf(f, l, HOVER_SHIFT, views[0].gamut) };
|
|
1276
|
+
refs[`${name}-active`] = { type: "value", value: stateOf(f, l, ACTIVE_SHIFT, views[0].gamut) };
|
|
1277
|
+
};
|
|
1278
|
+
if (tinted && name === "neutral") {
|
|
1279
|
+
const tonal = panelCandidates(steps(src), 1.6).map((fill2) => ({ fill: fill2, label: labelOn(fill2, "tint") })).find((t) => cr(t.label, t.fill) >= textFloor);
|
|
1280
|
+
const fill = tonal?.fill ?? steps(src).filter(farSide)[0] ?? bestEnd(ground);
|
|
1281
|
+
const label = tonal?.label ?? labelOn(fill, "fill");
|
|
1282
|
+
refs[name] = fill;
|
|
1283
|
+
refs[`${name}-foreground`] = label;
|
|
1284
|
+
setStates(fill, label);
|
|
1285
|
+
} else if (inHue(fillRef) && cr(fillRef, ground) >= FILL_FLOOR) {
|
|
1286
|
+
refs[name] = fillRef;
|
|
1287
|
+
refs[`${name}-foreground`] = base[`${src}-foreground`];
|
|
1288
|
+
refs[`${name}-hover`] = base[`${src}-hover`];
|
|
1289
|
+
refs[`${name}-active`] = base[`${src}-active`];
|
|
1290
|
+
} else {
|
|
1291
|
+
const turnsOver = spec.kind === "background" && spec.family === src && src !== "neutral";
|
|
1292
|
+
const ramp = steps(src);
|
|
1293
|
+
const fill = turnsOver ? light ? ramp[ramp.length - 1] : ramp[0] : nearest(src, fillShade, ground, FILL_FLOOR) ?? bestEnd(ground);
|
|
1294
|
+
const label = turnsOver ? labelOn(fill, "tint") : labelOn(fill, "fill");
|
|
1295
|
+
refs[name] = fill;
|
|
1296
|
+
refs[`${name}-foreground`] = label;
|
|
1297
|
+
setStates(fill, label);
|
|
1298
|
+
if (cr(fill, ground) < FILL_FLOOR) notes.push(`${name}: no step stands out from the ground at ${FILL_FLOOR}:1`);
|
|
1299
|
+
}
|
|
1300
|
+
const tint = base[`${src}-subdued`];
|
|
1301
|
+
const tintLabel = base[`${src}-subdued-foreground`];
|
|
1302
|
+
const readsOnBoth = (label, on) => cr(label, on) >= textFloor && cr(label, ground) >= textFloor;
|
|
1303
|
+
const role = tintLabel.type === "shade" && tintLabel.colorName === src ? "tint" : "fill";
|
|
1304
|
+
const groundValue = [valueOf(ground, srgb)];
|
|
1305
|
+
const tintVis = Math.max(cr(tint, baseCanvas), PANEL_FLOOR);
|
|
1306
|
+
let pair = keepsPlace(tint) && readsOnBoth(tintLabel, tint) ? { tint, label: tintLabel } : void 0;
|
|
1307
|
+
const tints = panelCandidates(steps(src), tintVis);
|
|
1308
|
+
for (const candidate of tints) {
|
|
1309
|
+
if (pair) break;
|
|
1310
|
+
if (readsOnBoth(tintLabel, candidate)) {
|
|
1311
|
+
pair = { tint: candidate, label: tintLabel };
|
|
1312
|
+
break;
|
|
1313
|
+
}
|
|
1314
|
+
const label = labelOn(candidate, role, groundValue);
|
|
1315
|
+
if (readsOnBoth(label, candidate)) pair = { tint: candidate, label };
|
|
1316
|
+
}
|
|
1317
|
+
if (!pair) {
|
|
1318
|
+
const fallback = tints[0] ?? tint;
|
|
1319
|
+
pair = { tint: fallback, label: labelOn(fallback, role, groundValue) };
|
|
1320
|
+
notes.push(`${name}-subdued-foreground: no label reads on both a tint and the ground at ${textFloor}:1`);
|
|
1321
|
+
}
|
|
1322
|
+
refs[`${name}-subdued`] = pair.tint;
|
|
1323
|
+
refs[`${name}-subdued-foreground`] = pair.label;
|
|
1324
|
+
}
|
|
1325
|
+
const tokens = Object.fromEntries(Object.entries(refs).map(([k, r]) => [k, valueOf(r, srgb)]));
|
|
1326
|
+
return { kind: spec.kind, tokens, _refs: refs, changed: [], notes };
|
|
1327
|
+
}
|
|
1328
|
+
function generateGroundTokens(tiers, mode, views, options) {
|
|
1329
|
+
const base = { semantic: tiers.semantic[mode], surface: tiers.surface[mode], utility: tiers.utility[mode] };
|
|
1330
|
+
const active = resolveShades(options?.shades);
|
|
1331
|
+
const srgb = views[0].palette;
|
|
1332
|
+
const colors = semanticColors(base.semantic);
|
|
1333
|
+
const out = {};
|
|
1334
|
+
const canvasRefs = baseRefs(base, colors, "canvas", active);
|
|
1335
|
+
const surfaceRefs = baseRefs(base, colors, "surface", active);
|
|
1336
|
+
const baseOf = (refs) => ({
|
|
1337
|
+
kind: "base",
|
|
1338
|
+
tokens: Object.fromEntries(Object.entries(refs).map(([k, r]) => [k, valueOf(r, srgb)])),
|
|
1339
|
+
_refs: refs,
|
|
1340
|
+
changed: [],
|
|
1341
|
+
notes: []
|
|
1342
|
+
});
|
|
1343
|
+
out.canvas = baseOf(canvasRefs);
|
|
1344
|
+
out.surface = baseOf(surfaceRefs);
|
|
1345
|
+
const specs = [];
|
|
1346
|
+
for (const name of STATUS_GROUNDS) {
|
|
1347
|
+
specs.push({
|
|
1348
|
+
name: `${name}-subdued`,
|
|
1349
|
+
kind: "status",
|
|
1350
|
+
canvas: canvasRefs[`${name}-subdued`],
|
|
1351
|
+
foreground: canvasRefs[`${name}-subdued-foreground`],
|
|
1352
|
+
family: name
|
|
1353
|
+
});
|
|
1354
|
+
}
|
|
1355
|
+
for (const [name, bg] of Object.entries(base.surface.backgrounds ?? {})) {
|
|
1356
|
+
specs.push({
|
|
1357
|
+
name,
|
|
1358
|
+
kind: "background",
|
|
1359
|
+
canvas: { type: "value", value: bg.DEFAULT },
|
|
1360
|
+
foreground: fromSurfaceRef(bg._refs.foreground),
|
|
1361
|
+
family: familyOf(bg.DEFAULT, srgb, colors, active)
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
const order = options?.grounds === "text" ? ["canvas", "canvas-foreground"] : groundTokenNames(colors);
|
|
1365
|
+
if (options?.grounds === "text") {
|
|
1366
|
+
for (const name of ["canvas", "surface"]) {
|
|
1367
|
+
const g = out[name];
|
|
1368
|
+
g.tokens = Object.fromEntries(order.map((k) => [k, g.tokens[k]]));
|
|
1369
|
+
g._refs = Object.fromEntries(order.map((k) => [k, g._refs[k]]));
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
for (const spec of specs) {
|
|
1373
|
+
const searched = searchGround(spec, canvasRefs, canvasRefs.canvas, colors, views, mode, options, active);
|
|
1374
|
+
const g = {
|
|
1375
|
+
...searched,
|
|
1376
|
+
// notes describe tokens a text-only ground does not carry
|
|
1377
|
+
notes: options?.grounds === "text" ? [] : searched.notes,
|
|
1378
|
+
tokens: Object.fromEntries(order.map((k) => [k, searched.tokens[k]])),
|
|
1379
|
+
_refs: Object.fromEntries(order.map((k) => [k, searched._refs[k]]))
|
|
1380
|
+
};
|
|
1381
|
+
g.changed = Object.keys(g._refs).filter((k) => !GROUND_BASE_TOKENS.slice(0, 2).includes(k) && !sameRef(g._refs[k], canvasRefs[k]));
|
|
1382
|
+
out[spec.name] = g;
|
|
1383
|
+
}
|
|
1384
|
+
return out;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1073
1387
|
// src/generate.ts
|
|
1074
1388
|
var defaultTypography = {
|
|
1075
1389
|
fontSize: {
|
|
@@ -1187,8 +1501,20 @@ function generateTokens(brandColors, options) {
|
|
|
1187
1501
|
own: "srgb",
|
|
1188
1502
|
...wide ? { other: { gamut: "p3", palette: wide } } : {}
|
|
1189
1503
|
});
|
|
1504
|
+
const withGrounds = options?.grounds !== "none";
|
|
1505
|
+
const colors = semanticColors(tiers.semantic.light);
|
|
1506
|
+
const reserved = reservedGroundNames(colors);
|
|
1507
|
+
for (const name of withGrounds ? Object.keys(options?.backgrounds ?? {}) : []) {
|
|
1508
|
+
if (reserved.includes(name)) {
|
|
1509
|
+
throw new Error(`Background name "${name}" is taken: it is already a token or a ground. Reserved: ${reserved.join(", ")}`);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
const views = [{ palette: primitives, gamut: "srgb" }, ...wide ? [{ palette: wide, gamut: "p3" }] : []];
|
|
1513
|
+
const groundTokens = withGrounds ? { light: generateGroundTokens(tiers, "light", views, options), dark: generateGroundTokens(tiers, "dark", views, options) } : void 0;
|
|
1514
|
+
if (groundTokens) assertDistinctGroundNames(Object.keys(groundTokens.light), colors);
|
|
1190
1515
|
return {
|
|
1191
1516
|
...tiers,
|
|
1517
|
+
...groundTokens ? { grounds: groundTokens } : {},
|
|
1192
1518
|
pageContrast: { light: pageContrast(tiers, "light"), dark: pageContrast(tiers, "dark") },
|
|
1193
1519
|
spacing: {},
|
|
1194
1520
|
typography: defaultTypography,
|
|
@@ -1216,9 +1542,9 @@ function pageContrast(tiers, mode) {
|
|
|
1216
1542
|
}
|
|
1217
1543
|
|
|
1218
1544
|
// src/usage.ts
|
|
1219
|
-
var
|
|
1545
|
+
var TEXT_FLOOR2 = { "aa-large": 3, aa: 4.5, aaa: 7 };
|
|
1220
1546
|
function usageReport(tokens, target = "aa") {
|
|
1221
|
-
const floors = { text:
|
|
1547
|
+
const floors = { text: TEXT_FLOOR2[target], shape: 3 };
|
|
1222
1548
|
const verdict = (r) => r >= floors.text ? "text" : r >= floors.shape ? "shape" : "none";
|
|
1223
1549
|
const mode = (m) => {
|
|
1224
1550
|
const surface = tokens.surface[m];
|
|
@@ -1255,7 +1581,7 @@ function formatColor(color, format) {
|
|
|
1255
1581
|
}
|
|
1256
1582
|
function colorScaleToCssVars(name, scale, colorFormat = "hex", indent = " ") {
|
|
1257
1583
|
return Object.entries(scale).map(
|
|
1258
|
-
([
|
|
1584
|
+
([shade2, color]) => `${indent}--color-${name}-${shade2}: ${formatColor(color, colorFormat)};`
|
|
1259
1585
|
);
|
|
1260
1586
|
}
|
|
1261
1587
|
function emitPrimitiveVars(tokens, colorFormat, want, indent, inFamily, scoped) {
|
|
@@ -1402,12 +1728,52 @@ function emitUtilityVars(mode, indent, active, seedShade, steps) {
|
|
|
1402
1728
|
];
|
|
1403
1729
|
const lines = [];
|
|
1404
1730
|
for (const [token, ref] of refs) {
|
|
1405
|
-
const [scale,
|
|
1406
|
-
const snapped = `${scale}-${snapShade(Number(
|
|
1731
|
+
const [scale, shade2] = ref.split(/-(?=\d+$)/);
|
|
1732
|
+
const snapped = `${scale}-${snapShade(Number(shade2), active)}`;
|
|
1407
1733
|
lines.push(`${indent}--${token}: ${primitiveToVar(snapped)};`);
|
|
1408
1734
|
}
|
|
1409
1735
|
return lines;
|
|
1410
1736
|
}
|
|
1737
|
+
function groundRefToVar(ref, colorFormat) {
|
|
1738
|
+
return ref.type === "value" ? formatColor(ref.value, colorFormat) : refToVar(ref);
|
|
1739
|
+
}
|
|
1740
|
+
function ownGroundTokens(tokens, ground) {
|
|
1741
|
+
const grounds = tokens.grounds;
|
|
1742
|
+
const names = Object.keys(grounds.light[ground]._refs);
|
|
1743
|
+
if (ground === "canvas") return names;
|
|
1744
|
+
const same = (mode, token) => JSON.stringify(grounds[mode][ground]._refs[token]) === JSON.stringify(grounds[mode].canvas._refs[token]);
|
|
1745
|
+
return names.filter((token) => !same("light", token) || !same("dark", token));
|
|
1746
|
+
}
|
|
1747
|
+
function emitGroundVars(tokens, mode, colorFormat, indent) {
|
|
1748
|
+
const lines = [];
|
|
1749
|
+
for (const [ground, set] of Object.entries(tokens.grounds?.[mode] ?? {})) {
|
|
1750
|
+
for (const token of ownGroundTokens(tokens, ground)) {
|
|
1751
|
+
lines.push(`${indent}--on-${ground}-${token}: ${groundRefToVar(set._refs[token], colorFormat)};`);
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
return lines;
|
|
1755
|
+
}
|
|
1756
|
+
function emitGroundScopes(tokens, rebind) {
|
|
1757
|
+
const grounds = tokens.grounds?.light;
|
|
1758
|
+
if (!grounds || Object.keys(grounds).length === 0) return [];
|
|
1759
|
+
const lines = [
|
|
1760
|
+
"",
|
|
1761
|
+
'/* Grounds: mark an element data-surface="<ground>" and everything inside it reads',
|
|
1762
|
+
" that ground's tokens. canvas and surface reset to the page's own, for a card inside a band. */"
|
|
1763
|
+
];
|
|
1764
|
+
for (const [ground, set] of Object.entries(grounds)) {
|
|
1765
|
+
const names = Object.keys(set._refs);
|
|
1766
|
+
const own = new Set(ownGroundTokens(tokens, ground));
|
|
1767
|
+
lines.push(`${GROUND_SELECTOR(ground)} {`);
|
|
1768
|
+
for (const token of names) lines.push(` --${token}: var(--on-${own.has(token) ? ground : "canvas"}-${token});`);
|
|
1769
|
+
if (rebind) for (const token of names) lines.push(` --color-${token}: var(--${token});`);
|
|
1770
|
+
lines.push(" background-color: var(--canvas);");
|
|
1771
|
+
lines.push(" color: var(--canvas-foreground);");
|
|
1772
|
+
lines.push("}");
|
|
1773
|
+
}
|
|
1774
|
+
return lines;
|
|
1775
|
+
}
|
|
1776
|
+
var GROUND_SELECTOR = (ground) => `[data-surface="${ground}"]`;
|
|
1411
1777
|
function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shades, families, seedShade, seedShades) {
|
|
1412
1778
|
const lines = [];
|
|
1413
1779
|
const anchors = { seedShade, seedShades };
|
|
@@ -1416,7 +1782,8 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
1416
1782
|
const scoped = !!families && families.length > 0;
|
|
1417
1783
|
const inFamily = familyFilter(families);
|
|
1418
1784
|
const wantPrimitives = want.has("primitives") || want.has("ramp");
|
|
1419
|
-
const
|
|
1785
|
+
const wantGrounds = want.has("grounds") && !scoped && !!tokens.grounds;
|
|
1786
|
+
const wantModes = want.has("semantic") || !scoped && (want.has("surface") || want.has("utility")) || wantGrounds;
|
|
1420
1787
|
const modeBody = (mode, indent, withComments) => {
|
|
1421
1788
|
const body = [];
|
|
1422
1789
|
if (want.has("surface") && !scoped) {
|
|
@@ -1432,6 +1799,11 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
1432
1799
|
if (want.has("semantic")) {
|
|
1433
1800
|
if (withComments) body.push(`${indent}/* Semantic Colors */`);
|
|
1434
1801
|
body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
1802
|
+
if (withComments) body.push("");
|
|
1803
|
+
}
|
|
1804
|
+
if (wantGrounds) {
|
|
1805
|
+
if (withComments) body.push(`${indent}/* Grounds: read through data-surface, below */`);
|
|
1806
|
+
body.push(...emitGroundVars(tokens, mode, colorFormat, indent));
|
|
1435
1807
|
}
|
|
1436
1808
|
while (body.length > 0 && body[body.length - 1] === "") body.pop();
|
|
1437
1809
|
return body;
|
|
@@ -1462,6 +1834,7 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
1462
1834
|
lines.push(" }");
|
|
1463
1835
|
lines.push("}");
|
|
1464
1836
|
}
|
|
1837
|
+
if (wantGrounds) lines.push(...emitGroundScopes(tokens, false));
|
|
1465
1838
|
return lines.join("\n");
|
|
1466
1839
|
}
|
|
1467
1840
|
var MODE_SELECTORS = {
|
|
@@ -1496,7 +1869,8 @@ var TOKEN_SECTIONS = [
|
|
|
1496
1869
|
"ramp",
|
|
1497
1870
|
"semantic",
|
|
1498
1871
|
"surface",
|
|
1499
|
-
"utility"
|
|
1872
|
+
"utility",
|
|
1873
|
+
"grounds"
|
|
1500
1874
|
];
|
|
1501
1875
|
var ALL_SECTIONS = [...TOKEN_SECTIONS];
|
|
1502
1876
|
function familyFilter(families) {
|
|
@@ -1562,7 +1936,7 @@ function emitThemeRebinding(aliases, indent) {
|
|
|
1562
1936
|
}
|
|
1563
1937
|
return lines;
|
|
1564
1938
|
}
|
|
1565
|
-
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped, anchors) {
|
|
1939
|
+
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped, anchors, wantGrounds) {
|
|
1566
1940
|
const lines = [];
|
|
1567
1941
|
const seedShade = anchors.seedShade;
|
|
1568
1942
|
if (want.has("surface") && !scoped) {
|
|
@@ -1578,9 +1952,14 @@ function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases,
|
|
|
1578
1952
|
if (want.has("semantic")) {
|
|
1579
1953
|
lines.push(`${indent}/* Semantic Colors */`);
|
|
1580
1954
|
lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
1955
|
+
lines.push("");
|
|
1956
|
+
}
|
|
1957
|
+
if (wantGrounds) {
|
|
1958
|
+
lines.push(`${indent}/* Grounds: read through data-surface, below */`);
|
|
1959
|
+
lines.push(...emitGroundVars(tokens, mode, colorFormat, indent));
|
|
1581
1960
|
}
|
|
1582
1961
|
while (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
|
|
1583
|
-
lines.push(...emitThemeRebinding(aliases, indent));
|
|
1962
|
+
if (aliases.length > 0) lines.push(...emitThemeRebinding(aliases, indent));
|
|
1584
1963
|
return lines;
|
|
1585
1964
|
}
|
|
1586
1965
|
function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replacePalette = true, sections, shades, families, seedShade, seedShades) {
|
|
@@ -1592,7 +1971,8 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
|
|
|
1592
1971
|
const inFamily = familyFilter(families);
|
|
1593
1972
|
const aliases = semanticAliasNames(tokens, sections, families);
|
|
1594
1973
|
const wantPrimitives = want.has("primitives") || want.has("ramp");
|
|
1595
|
-
const
|
|
1974
|
+
const wantGrounds = want.has("grounds") && !scoped && !!tokens.grounds;
|
|
1975
|
+
const wantModes = aliases.length > 0 || wantGrounds;
|
|
1596
1976
|
lines.push("@theme static {");
|
|
1597
1977
|
if (replacePalette && !scoped) {
|
|
1598
1978
|
lines.push(" /* Clear Tailwind's stock palette, so a colour class this file does not");
|
|
@@ -1618,21 +1998,22 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
|
|
|
1618
1998
|
lines.push("");
|
|
1619
1999
|
lines.push("/* Light Mode */");
|
|
1620
2000
|
lines.push(`${MODE_SELECTORS.light} {`);
|
|
1621
|
-
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
2001
|
+
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors, wantGrounds));
|
|
1622
2002
|
lines.push("}");
|
|
1623
2003
|
lines.push("");
|
|
1624
2004
|
lines.push("/* Dark Mode */");
|
|
1625
2005
|
lines.push(`${MODE_SELECTORS.dark} {`);
|
|
1626
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
2006
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors, wantGrounds));
|
|
1627
2007
|
lines.push("}");
|
|
1628
2008
|
lines.push("");
|
|
1629
2009
|
lines.push("/* System Preference: Dark Mode */");
|
|
1630
2010
|
lines.push("@media (prefers-color-scheme: dark) {");
|
|
1631
2011
|
lines.push(` ${MODE_SELECTORS.systemDark} {`);
|
|
1632
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
2012
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors, wantGrounds));
|
|
1633
2013
|
lines.push(" }");
|
|
1634
2014
|
lines.push("}");
|
|
1635
2015
|
}
|
|
2016
|
+
if (wantGrounds) lines.push(...emitGroundScopes(tokens, true));
|
|
1636
2017
|
return lines.join("\n");
|
|
1637
2018
|
}
|
|
1638
2019
|
function exportToTailwindV3(tokens, colorFormat = "hex") {
|
|
@@ -1648,8 +2029,8 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
|
|
|
1648
2029
|
config.theme.extend.colors[name] = formatColor(scale, colorFormat);
|
|
1649
2030
|
} else {
|
|
1650
2031
|
config.theme.extend.colors[name] = {};
|
|
1651
|
-
for (const [
|
|
1652
|
-
config.theme.extend.colors[name][
|
|
2032
|
+
for (const [shade2, color] of Object.entries(scale)) {
|
|
2033
|
+
config.theme.extend.colors[name][shade2] = formatColor(color, colorFormat);
|
|
1653
2034
|
}
|
|
1654
2035
|
}
|
|
1655
2036
|
}
|
|
@@ -1665,8 +2046,8 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
|
|
|
1665
2046
|
active: `var(--${name}-active)`
|
|
1666
2047
|
};
|
|
1667
2048
|
if (primitiveScale && typeof primitiveScale !== "string") {
|
|
1668
|
-
for (const [
|
|
1669
|
-
colorObj[
|
|
2049
|
+
for (const [shade2, color] of Object.entries(primitiveScale)) {
|
|
2050
|
+
colorObj[shade2] = formatColor(color, colorFormat);
|
|
1670
2051
|
}
|
|
1671
2052
|
}
|
|
1672
2053
|
config.theme.extend.colors[name] = colorObj;
|
|
@@ -1683,8 +2064,8 @@ function exportToTailwindV3(tokens, colorFormat = "hex") {
|
|
|
1683
2064
|
active: `var(--${name}-active)`
|
|
1684
2065
|
};
|
|
1685
2066
|
if (primitiveScale && typeof primitiveScale !== "string") {
|
|
1686
|
-
for (const [
|
|
1687
|
-
colorObj[
|
|
2067
|
+
for (const [shade2, color] of Object.entries(primitiveScale)) {
|
|
2068
|
+
colorObj[shade2] = formatColor(color, colorFormat);
|
|
1688
2069
|
}
|
|
1689
2070
|
}
|
|
1690
2071
|
config.theme.extend.colors[name] = colorObj;
|
|
@@ -1725,8 +2106,8 @@ function exportToDtcg(tokens) {
|
|
|
1725
2106
|
dtcg[name] = dtcgColorValue(scale);
|
|
1726
2107
|
} else {
|
|
1727
2108
|
const group = { $type: "color" };
|
|
1728
|
-
for (const [
|
|
1729
|
-
group[
|
|
2109
|
+
for (const [shade2, color] of Object.entries(scale)) {
|
|
2110
|
+
group[shade2] = { $value: formatColorAs(color, "hex") };
|
|
1730
2111
|
}
|
|
1731
2112
|
dtcg[name] = group;
|
|
1732
2113
|
}
|
|
@@ -1788,6 +2169,16 @@ function exportToDtcg(tokens) {
|
|
|
1788
2169
|
ring: { $value: formatColorAs(util.ring, "hex") },
|
|
1789
2170
|
shadow: { $value: formatColorAs(util.shadow, "hex") }
|
|
1790
2171
|
};
|
|
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
|
+
}
|
|
1791
2182
|
dtcg[mode] = semanticGroup;
|
|
1792
2183
|
}
|
|
1793
2184
|
return JSON.stringify(dtcg, null, 2);
|
|
@@ -1836,6 +2227,7 @@ function selectTokenSections(tokens, sections, families) {
|
|
|
1836
2227
|
}
|
|
1837
2228
|
if (want.has("surface") && !scoped) out.surface = tokens.surface;
|
|
1838
2229
|
if (want.has("utility") && !scoped) out.utility = tokens.utility;
|
|
2230
|
+
if (want.has("grounds") && !scoped && tokens.grounds) out.grounds = tokens.grounds;
|
|
1839
2231
|
if (tokens.p3) {
|
|
1840
2232
|
const wide = selectTokenSections({ ...tokens, ...tokens.p3, p3: void 0 }, sections, families);
|
|
1841
2233
|
out.p3 = Object.fromEntries(
|
|
@@ -1888,13 +2280,27 @@ function walk(css, onDeclaration) {
|
|
|
1888
2280
|
const lines = css.split("\n");
|
|
1889
2281
|
const stack = [];
|
|
1890
2282
|
let pending = "";
|
|
2283
|
+
let inComment = false;
|
|
1891
2284
|
lines.forEach((line, index) => {
|
|
1892
2285
|
const trimmed = line.trim();
|
|
1893
2286
|
const declaration = /^(--[a-z0-9-]+)\s*:\s*(.+?);\s*$/i.exec(trimmed);
|
|
1894
|
-
if (declaration && stack.length > 0) {
|
|
2287
|
+
if (declaration && stack.length > 0 && !inComment) {
|
|
1895
2288
|
onDeclaration(line, index, stack.join(" > "), declaration[1], declaration[2]);
|
|
1896
2289
|
}
|
|
1897
|
-
for (
|
|
2290
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
2291
|
+
const char = trimmed[i];
|
|
2292
|
+
if (inComment) {
|
|
2293
|
+
if (char === "*" && trimmed[i + 1] === "/") {
|
|
2294
|
+
inComment = false;
|
|
2295
|
+
i++;
|
|
2296
|
+
}
|
|
2297
|
+
continue;
|
|
2298
|
+
}
|
|
2299
|
+
if (char === "/" && trimmed[i + 1] === "*") {
|
|
2300
|
+
inComment = true;
|
|
2301
|
+
i++;
|
|
2302
|
+
continue;
|
|
2303
|
+
}
|
|
1898
2304
|
if (char === "{") {
|
|
1899
2305
|
stack.push(normaliseSelector(pending));
|
|
1900
2306
|
pending = "";
|
|
@@ -1905,7 +2311,7 @@ function walk(css, onDeclaration) {
|
|
|
1905
2311
|
pending += char;
|
|
1906
2312
|
}
|
|
1907
2313
|
}
|
|
1908
|
-
if (trimmed.endsWith("{") || trimmed.endsWith("}")) pending = "";
|
|
2314
|
+
if (!inComment && (trimmed.endsWith("{") || trimmed.endsWith("}"))) pending = "";
|
|
1909
2315
|
});
|
|
1910
2316
|
}
|
|
1911
2317
|
function readDeclarations(css) {
|
|
@@ -2132,10 +2538,17 @@ export {
|
|
|
2132
2538
|
checkSurfaceContrast,
|
|
2133
2539
|
checkRampChroma,
|
|
2134
2540
|
computeAccessibilityScore,
|
|
2541
|
+
STATUS_GROUNDS,
|
|
2542
|
+
GROUND_BASE_TOKENS,
|
|
2543
|
+
GROUND_COLOR_PARTS,
|
|
2544
|
+
groundTokenNames,
|
|
2545
|
+
reservedGroundNames,
|
|
2546
|
+
generateGroundTokens,
|
|
2135
2547
|
generateTokens,
|
|
2136
2548
|
usageReport,
|
|
2137
2549
|
serialiseUsage,
|
|
2138
2550
|
WIDE_GAMUT_QUERY,
|
|
2551
|
+
GROUND_SELECTOR,
|
|
2139
2552
|
exportToCss,
|
|
2140
2553
|
overrideContract,
|
|
2141
2554
|
TOKEN_SECTIONS,
|