@behavioral-agentic-design-system/core 0.15.0 → 0.17.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/package.json +8 -2
- package/scripts/a11y-lint.cjs +229 -0
- package/scripts/build-tokens.cjs +3 -3
- package/scripts/check-stories.mjs +109 -0
- package/scripts/fix-stories.mjs +785 -0
- package/scripts/generate-stories.cjs +101 -0
- package/scripts/oklch-to-hex.cjs +156 -0
- package/scripts/wcag-audit.cjs +444 -0
- package/src/app/preview/components/page.tsx +538 -51
- package/src/components/dev-identity-badge.tsx +1 -1
- package/src/components/ui/button-group.tsx +57 -0
- package/src/components/ui/calendar.tsx +4 -0
- package/src/components/ui/circular-progress.tsx +120 -0
- package/src/components/ui/code.tsx +55 -0
- package/src/components/ui/copy-button.tsx +114 -0
- package/src/components/ui/empty-state.tsx +115 -0
- package/src/components/ui/index.ts +0 -1
- package/src/components/ui/indicator.tsx +146 -0
- package/src/components/ui/list.tsx +70 -0
- package/src/components/ui/multi-select.tsx +196 -0
- package/src/components/ui/native-select.tsx +68 -0
- package/src/components/ui/navigation-menu.tsx +23 -0
- package/src/components/ui/sheet.tsx +23 -6
- package/src/components/ui/spinner.tsx +14 -3
- package/src/components/ui/tags-input.tsx +210 -69
- package/src/hooks/use-global-primary-color.ts +1 -1
- package/src/styles.css +2 -2
- package/src/components/ui/bottom-sheet.tsx +0 -133
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* wcag-audit.js — audit token color pairs against WCAG AAA (7:1 normal, 4.5:1 large).
|
|
4
|
+
*
|
|
5
|
+
* Reads packages/core/src/tokens.json, resolves every oklch/hex value used in
|
|
6
|
+
* theme.light and theme.dark, then computes contrast ratios for every
|
|
7
|
+
* meaningful text-on-surface pair (foreground/background, primary/primary-fg,
|
|
8
|
+
* destructive/destructive-fg, sidebar pairs, popover, etc.).
|
|
9
|
+
*
|
|
10
|
+
* Exit code 0 if every text pair passes AAA, 1 otherwise.
|
|
11
|
+
*
|
|
12
|
+
* Run:
|
|
13
|
+
* node scripts/wcag-audit.js # AAA (7:1 / 4.5:1)
|
|
14
|
+
* node scripts/wcag-audit.js --aa # AA (4.5:1 / 3:1)
|
|
15
|
+
* node scripts/wcag-audit.js --json # machine-readable
|
|
16
|
+
*
|
|
17
|
+
* Scope: this script checks CONTRAST only (the color-math part of the AAA
|
|
18
|
+
* floor). It is intentionally a static, browser-free check. Geometry-based
|
|
19
|
+
* success criteria are covered elsewhere:
|
|
20
|
+
*
|
|
21
|
+
* SC 2.4.13 Focus Appearance — the focus indicator's perimeter-area
|
|
22
|
+
* requirement (≥2 CSS px thick outline around the entire component, or
|
|
23
|
+
* a change area ≥ the area of a 2px-thick perimeter). BADS's current
|
|
24
|
+
* implementation (outline: 2px solid + outline-offset: 2px on every
|
|
25
|
+
* :focus-visible, set in the build-tokens base layer) complies by
|
|
26
|
+
* construction. This script does NOT measure rendered outline geometry,
|
|
27
|
+
* so a component that resizes its focus ring (e.g. a compact variant) or
|
|
28
|
+
* uses an inset ring could regress without failing here. A true geometry
|
|
29
|
+
* check needs a browser context (Playwright) measuring outline width ×
|
|
30
|
+
* perimeter — add when component count makes visual spot-checking
|
|
31
|
+
* unreliable.
|
|
32
|
+
*
|
|
33
|
+
* SC 2.4.11 Focus Not Obscured + SC 2.5.8 Target Size — now evaluated at
|
|
34
|
+
* the RENDERED level by the Storybook axe-core run (.storybook/preview.ts
|
|
35
|
+
* enables the wcag22a/wcag22aa tags + the target-size rule, exercised in
|
|
36
|
+
* CI by .github/workflows/a11y-ci.yml). That run catches the geometry
|
|
37
|
+
* regressions this static script cannot. The two are complementary:
|
|
38
|
+
* wcag-audit = token contrast (no browser), Storybook axe = rendered DOM.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
'use strict';
|
|
42
|
+
|
|
43
|
+
const fs = require('fs');
|
|
44
|
+
const path = require('path');
|
|
45
|
+
|
|
46
|
+
// Shipped as the `bads-wcag-audit` bin. Both inputs are consumer-owned (the
|
|
47
|
+
// fork keeps its own brand tokens + brand-colors), so resolve them against
|
|
48
|
+
// process.cwd() — works identically in upstream BADS and in a client fork.
|
|
49
|
+
const TOKENS = path.join(process.cwd(), 'packages/core/src/tokens.json');
|
|
50
|
+
const BRAND_COLORS = path.join(process.cwd(), 'brand-colors.json');
|
|
51
|
+
|
|
52
|
+
const aa = process.argv.includes('--aa');
|
|
53
|
+
const json = process.argv.includes('--json');
|
|
54
|
+
const NORMAL_FLOOR = aa ? 4.5 : 7.0;
|
|
55
|
+
const LARGE_FLOOR = aa ? 3.0 : 4.5;
|
|
56
|
+
|
|
57
|
+
// ---- color math (oklch → linear sRGB → relative luminance) -------------
|
|
58
|
+
|
|
59
|
+
function clamp(n, min, max) { return Math.min(max, Math.max(min, n)); }
|
|
60
|
+
|
|
61
|
+
function linearToSrgb(c) {
|
|
62
|
+
const a = Math.abs(c);
|
|
63
|
+
const sign = Math.sign(c);
|
|
64
|
+
return sign * (a > 0.0031308 ? 1.055 * Math.pow(a, 1 / 2.4) - 0.055 : 12.92 * a);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function srgbToLinear(c) {
|
|
68
|
+
return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function oklabToLinearSrgb(L, a, b) {
|
|
72
|
+
const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
|
|
73
|
+
const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
|
|
74
|
+
const s_ = L - 0.0894841775 * a - 1.2914855480 * b;
|
|
75
|
+
const l = l_ ** 3, m = m_ ** 3, s = s_ ** 3;
|
|
76
|
+
return [
|
|
77
|
+
+4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
|
|
78
|
+
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
|
|
79
|
+
-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function parseColor(input) {
|
|
84
|
+
const v = String(input).trim();
|
|
85
|
+
if (/^#[0-9a-f]{3}$/i.test(v)) {
|
|
86
|
+
const [r, g, b] = v.slice(1).split('').map((c) => parseInt(c + c, 16));
|
|
87
|
+
return [r / 255, g / 255, b / 255];
|
|
88
|
+
}
|
|
89
|
+
if (/^#[0-9a-f]{6}$/i.test(v)) {
|
|
90
|
+
return [
|
|
91
|
+
parseInt(v.slice(1, 3), 16) / 255,
|
|
92
|
+
parseInt(v.slice(3, 5), 16) / 255,
|
|
93
|
+
parseInt(v.slice(5, 7), 16) / 255,
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
const m = v.match(/^oklch\(([^)]+)\)$/i);
|
|
97
|
+
if (m) {
|
|
98
|
+
const parts = m[1].replace(/[\/,]/g, ' ').split(/\s+/).filter(Boolean);
|
|
99
|
+
const L = parts[0].endsWith('%') ? parseFloat(parts[0]) / 100 : parseFloat(parts[0]);
|
|
100
|
+
const C = parseFloat(parts[1]);
|
|
101
|
+
const h = parseFloat(parts[2]);
|
|
102
|
+
const a = C * Math.cos((h * Math.PI) / 180);
|
|
103
|
+
const b = C * Math.sin((h * Math.PI) / 180);
|
|
104
|
+
return oklabToLinearSrgb(L, a, b).map((c) => clamp(linearToSrgb(c), 0, 1));
|
|
105
|
+
}
|
|
106
|
+
// rgb()/rgba() — channels as 0-255 ints or percentages; optional alpha (0-1
|
|
107
|
+
// or %). A translucent color returns a 4th element so the caller can
|
|
108
|
+
// composite it over its backdrop before measuring contrast (a 0.55-alpha
|
|
109
|
+
// foreground read as solid would report a falsely-high ratio). Without this,
|
|
110
|
+
// any rgba() token resolves to null and its row is silently skipped.
|
|
111
|
+
const rgbm = v.match(/^rgba?\(([^)]+)\)$/i);
|
|
112
|
+
if (rgbm) {
|
|
113
|
+
const parts = rgbm[1].replace(/\//g, ' ').split(/[\s,]+/).filter(Boolean);
|
|
114
|
+
if (parts.length < 3) return null;
|
|
115
|
+
const chan = (s) => (s.endsWith('%') ? parseFloat(s) / 100 : parseFloat(s) / 255);
|
|
116
|
+
const rgb = parts.slice(0, 3).map(chan);
|
|
117
|
+
if (rgb.some((n) => Number.isNaN(n))) return null;
|
|
118
|
+
const aStr = parts[3];
|
|
119
|
+
const alpha = aStr == null ? 1 : aStr.endsWith('%') ? parseFloat(aStr) / 100 : parseFloat(aStr);
|
|
120
|
+
return alpha >= 1 || Number.isNaN(alpha) ? rgb : [...rgb, alpha];
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Composite a possibly-translucent color ([r,g,b] or [r,g,b,a]) over an opaque
|
|
126
|
+
// backdrop, returning an opaque [r,g,b]. Opaque inputs pass through unchanged,
|
|
127
|
+
// so solid-hex tokens are unaffected.
|
|
128
|
+
function flattenOver(c, backdrop) {
|
|
129
|
+
if (!c) return c;
|
|
130
|
+
if (c.length < 4) return c.slice(0, 3);
|
|
131
|
+
const a = c[3];
|
|
132
|
+
return [0, 1, 2].map((i) => c[i] * a + backdrop[i] * (1 - a));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function relativeLuminance([r, g, b]) {
|
|
136
|
+
const [R, G, B] = [r, g, b].map(srgbToLinear);
|
|
137
|
+
return 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function contrast(rgbA, rgbB) {
|
|
141
|
+
const lA = relativeLuminance(rgbA);
|
|
142
|
+
const lB = relativeLuminance(rgbB);
|
|
143
|
+
const lighter = Math.max(lA, lB);
|
|
144
|
+
const darker = Math.min(lA, lB);
|
|
145
|
+
return (lighter + 0.05) / (darker + 0.05);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function toHex([r, g, b]) {
|
|
149
|
+
const h = (c) => clamp(Math.round(c * 255), 0, 255).toString(16).padStart(2, '0');
|
|
150
|
+
return `#${h(r)}${h(g)}${h(b)}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ---- token resolution ---------------------------------------------------
|
|
154
|
+
|
|
155
|
+
const tokens = JSON.parse(fs.readFileSync(TOKENS, 'utf8'));
|
|
156
|
+
|
|
157
|
+
function resolveValue(value, modeTokens) {
|
|
158
|
+
if (typeof value !== 'string') return null;
|
|
159
|
+
// var(--name) — look up in same-mode tokens
|
|
160
|
+
const varMatch = value.match(/^var\(--([^)]+)\)$/);
|
|
161
|
+
if (varMatch) {
|
|
162
|
+
const ref = modeTokens[varMatch[1]];
|
|
163
|
+
return ref ? resolveValue(ref.$value, modeTokens) : null;
|
|
164
|
+
}
|
|
165
|
+
// oklch(from var(--x) calc(l + 0.1) c h) etc — too complex to evaluate, skip.
|
|
166
|
+
if (value.includes('oklch(from')) return null;
|
|
167
|
+
return parseColor(value);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ---- pairs to audit -----------------------------------------------------
|
|
171
|
+
//
|
|
172
|
+
// Format: [foreground, background, "label", { large?: true }]
|
|
173
|
+
// `large` flag = treat as large text (≥18pt or 14pt bold) — uses 4.5:1 AAA floor.
|
|
174
|
+
|
|
175
|
+
// `brand: true` marks pairs whose values are overwritten at runtime by the
|
|
176
|
+
// brand color picker (see hooks/use-global-primary-color → applyColor). When
|
|
177
|
+
// these fail AAA, the audit emits a warning instead of an error: the picker
|
|
178
|
+
// surfaces the same warning in-product and clients can ship brand colors that
|
|
179
|
+
// don't meet AAA as long as they accept the documented trade-off (see
|
|
180
|
+
// CLAUDE.md → Accessibility Floor).
|
|
181
|
+
const PAIRS = [
|
|
182
|
+
['foreground', 'background', 'body text on page'],
|
|
183
|
+
['card-foreground', 'card', 'text on card'],
|
|
184
|
+
['popover-foreground', 'popover', 'text on popover'],
|
|
185
|
+
['primary-foreground', 'primary', 'text on primary action', { brand: true }],
|
|
186
|
+
['secondary-foreground', 'secondary', 'text on secondary surface'],
|
|
187
|
+
['muted-foreground', 'muted', 'muted text on muted bg'],
|
|
188
|
+
['muted-foreground', 'background', 'muted text on page'],
|
|
189
|
+
['muted-foreground', 'card', 'muted text on card'],
|
|
190
|
+
['accent-foreground', 'accent', 'text on accent'],
|
|
191
|
+
['destructive-foreground', 'destructive', 'text on destructive'],
|
|
192
|
+
['sidebar-foreground', 'sidebar', 'sidebar body text'],
|
|
193
|
+
['sidebar-primary-foreground', 'sidebar-primary', 'sidebar primary text', { brand: true }],
|
|
194
|
+
['sidebar-accent-foreground', 'sidebar-accent', 'sidebar accent text'],
|
|
195
|
+
];
|
|
196
|
+
|
|
197
|
+
// Non-text contrast (WCAG 1.4.11) — UI components / borders need 3:1.
|
|
198
|
+
// Chart series are visually-essential information that need 3:1 vs the
|
|
199
|
+
// surface they're rendered on (typically the card or popover).
|
|
200
|
+
const NON_TEXT_FLOOR = 3.0;
|
|
201
|
+
const NON_TEXT_PAIRS = [
|
|
202
|
+
['border', 'background', 'border on page'],
|
|
203
|
+
['border', 'card', 'border on card'],
|
|
204
|
+
['input', 'card', 'input border on card'],
|
|
205
|
+
['ring', 'background', 'focus ring on page'],
|
|
206
|
+
['ring', 'card', 'focus ring on card'],
|
|
207
|
+
['primary', 'background', 'primary button on page', { brand: true }],
|
|
208
|
+
['primary', 'card', 'primary button on card', { brand: true }],
|
|
209
|
+
['destructive', 'background', 'destructive accent on page'],
|
|
210
|
+
// Status accents (success/warning/info) — used as toast icons + accent
|
|
211
|
+
// borders on the popover surface. Non-text, so 3:1 against popover.
|
|
212
|
+
['success', 'popover', 'success accent on popover'],
|
|
213
|
+
['warning', 'popover', 'warning accent on popover'],
|
|
214
|
+
['info', 'popover', 'info accent on popover'],
|
|
215
|
+
['success', 'background', 'success accent on page'],
|
|
216
|
+
['warning', 'background', 'warning accent on page'],
|
|
217
|
+
['info', 'background', 'info accent on page'],
|
|
218
|
+
['sidebar-border', 'sidebar', 'sidebar border'],
|
|
219
|
+
['sidebar-ring', 'sidebar', 'sidebar focus ring'],
|
|
220
|
+
// Chart series derive from --primary at runtime via the brand picker; their
|
|
221
|
+
// contrast against card/page tracks the brand color. Marked brand so an
|
|
222
|
+
// intentionally low-contrast brand pick degrades to a warning, not a fail.
|
|
223
|
+
['chart-1', 'card', 'chart-1 on card', { brand: true }],
|
|
224
|
+
['chart-2', 'card', 'chart-2 on card', { brand: true }],
|
|
225
|
+
['chart-3', 'card', 'chart-3 on card', { brand: true }],
|
|
226
|
+
['chart-4', 'card', 'chart-4 on card', { brand: true }],
|
|
227
|
+
['chart-5', 'card', 'chart-5 on card', { brand: true }],
|
|
228
|
+
['chart-1', 'background', 'chart-1 on page', { brand: true }],
|
|
229
|
+
['chart-2', 'background', 'chart-2 on page', { brand: true }],
|
|
230
|
+
['chart-3', 'background', 'chart-3 on page', { brand: true }],
|
|
231
|
+
['chart-4', 'background', 'chart-4 on page', { brand: true }],
|
|
232
|
+
['chart-5', 'background', 'chart-5 on page', { brand: true }],
|
|
233
|
+
];
|
|
234
|
+
|
|
235
|
+
// ---- run ----------------------------------------------------------------
|
|
236
|
+
|
|
237
|
+
function audit(modeName, modeTokens) {
|
|
238
|
+
const results = [];
|
|
239
|
+
for (const [fgKey, bgKey, label, opts] of PAIRS) {
|
|
240
|
+
const fgTok = modeTokens[fgKey];
|
|
241
|
+
const bgTok = modeTokens[bgKey];
|
|
242
|
+
if (!fgTok || !bgTok) continue;
|
|
243
|
+
const fgRaw = resolveValue(fgTok.$value, modeTokens);
|
|
244
|
+
const bgRaw = resolveValue(bgTok.$value, modeTokens);
|
|
245
|
+
if (!fgRaw || !bgRaw) {
|
|
246
|
+
results.push({ mode: modeName, fgKey, bgKey, label, status: 'skipped', reason: 'unresolvable' });
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
// Composite translucent text over its surface (and a translucent surface
|
|
250
|
+
// over white) so the ratio reflects what's actually rendered. Solid-hex
|
|
251
|
+
// tokens pass through flattenOver unchanged.
|
|
252
|
+
const bg = flattenOver(bgRaw, [1, 1, 1]);
|
|
253
|
+
const fg = flattenOver(fgRaw, bg);
|
|
254
|
+
const ratio = contrast(fg, bg);
|
|
255
|
+
const floor = opts?.large ? LARGE_FLOOR : NORMAL_FLOOR;
|
|
256
|
+
const pass = ratio >= floor;
|
|
257
|
+
const brand = Boolean(opts?.brand);
|
|
258
|
+
results.push({
|
|
259
|
+
mode: modeName,
|
|
260
|
+
fgKey, bgKey, label,
|
|
261
|
+
ratio: Math.round(ratio * 100) / 100,
|
|
262
|
+
floor,
|
|
263
|
+
pass,
|
|
264
|
+
brand,
|
|
265
|
+
fgHex: toHex(fg),
|
|
266
|
+
bgHex: toHex(bg),
|
|
267
|
+
status: pass ? 'pass' : brand ? 'warn' : 'fail',
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
for (const [fgKey, bgKey, label, opts] of NON_TEXT_PAIRS) {
|
|
271
|
+
const fgTok = modeTokens[fgKey];
|
|
272
|
+
const bgTok = modeTokens[bgKey];
|
|
273
|
+
if (!fgTok || !bgTok) continue;
|
|
274
|
+
const fgRaw = resolveValue(fgTok.$value, modeTokens);
|
|
275
|
+
const bgRaw = resolveValue(bgTok.$value, modeTokens);
|
|
276
|
+
if (!fgRaw || !bgRaw) continue;
|
|
277
|
+
const bg = flattenOver(bgRaw, [1, 1, 1]);
|
|
278
|
+
const fg = flattenOver(fgRaw, bg);
|
|
279
|
+
const ratio = contrast(fg, bg);
|
|
280
|
+
const pass = ratio >= NON_TEXT_FLOOR;
|
|
281
|
+
const brand = Boolean(opts?.brand);
|
|
282
|
+
results.push({
|
|
283
|
+
mode: modeName,
|
|
284
|
+
fgKey, bgKey, label, kind: 'non-text',
|
|
285
|
+
ratio: Math.round(ratio * 100) / 100,
|
|
286
|
+
floor: NON_TEXT_FLOOR,
|
|
287
|
+
pass,
|
|
288
|
+
brand,
|
|
289
|
+
fgHex: toHex(fg),
|
|
290
|
+
bgHex: toHex(bg),
|
|
291
|
+
status: pass ? 'pass' : brand ? 'warn' : 'fail',
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
return results;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const lightResults = audit('light', tokens.theme.light);
|
|
298
|
+
const darkResults = audit('dark', tokens.theme.dark);
|
|
299
|
+
const all = [...lightResults, ...darkResults];
|
|
300
|
+
|
|
301
|
+
// ---- runtime-mutated tokens audit --------------------------------------
|
|
302
|
+
//
|
|
303
|
+
// applyColor() in use-global-primary-color.ts writes the active brand color
|
|
304
|
+
// to --primary, --ring, --sidebar-primary, etc. — variables the static audit
|
|
305
|
+
// above sees only at their fallback (#1F1F1F neutral) value. A brand color
|
|
306
|
+
// that fails AAA against the accent or card surface stays hidden until a
|
|
307
|
+
// user picks it at runtime.
|
|
308
|
+
//
|
|
309
|
+
// This section simulates each entry in brand-colors.json as if it had been
|
|
310
|
+
// applied via the picker, then re-audits the brand-affected pairs in both
|
|
311
|
+
// modes. Failures here aren't strictly blocking (matches the brand-warn
|
|
312
|
+
// policy in PAIRS / NON_TEXT_PAIRS) but they flag combinations that will
|
|
313
|
+
// surface as in-product picker warnings.
|
|
314
|
+
|
|
315
|
+
const brandColorsRaw = (() => {
|
|
316
|
+
try {
|
|
317
|
+
return JSON.parse(fs.readFileSync(BRAND_COLORS, 'utf8'));
|
|
318
|
+
} catch {
|
|
319
|
+
return { colors: [] };
|
|
320
|
+
}
|
|
321
|
+
})();
|
|
322
|
+
|
|
323
|
+
function deriveForegroundHex(primaryHex) {
|
|
324
|
+
const rgb = parseColor(primaryHex);
|
|
325
|
+
if (!rgb) return '#FAFAFA';
|
|
326
|
+
const white = parseColor('#FAFAFA');
|
|
327
|
+
const black = parseColor('#0A0A0A');
|
|
328
|
+
return contrast(rgb, black) >= contrast(rgb, white) ? '#0A0A0A' : '#FAFAFA';
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function simulateBrand(brand, modeName, modeTokens) {
|
|
332
|
+
const primaryRgb = parseColor(brand.primary);
|
|
333
|
+
if (!primaryRgb) return [];
|
|
334
|
+
const foregroundHex = brand.primaryForeground ?? deriveForegroundHex(brand.primary);
|
|
335
|
+
const foregroundRgb = parseColor(foregroundHex);
|
|
336
|
+
if (!foregroundRgb) return [];
|
|
337
|
+
|
|
338
|
+
// Surfaces from the live token map (background, card, accent, sidebar, etc).
|
|
339
|
+
const surface = (key) => {
|
|
340
|
+
const tok = modeTokens[key];
|
|
341
|
+
return tok ? resolveValue(tok.$value, modeTokens) : null;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const pairs = [
|
|
345
|
+
['primary-foreground', 'primary', 'text on primary action', foregroundRgb, primaryRgb, NORMAL_FLOOR, false],
|
|
346
|
+
['primary', 'background', 'primary button on page', primaryRgb, surface('background'), NON_TEXT_FLOOR, true],
|
|
347
|
+
['primary', 'card', 'primary button on card', primaryRgb, surface('card'), NON_TEXT_FLOOR, true],
|
|
348
|
+
['ring', 'background', 'focus ring on page', primaryRgb, surface('background'), NON_TEXT_FLOOR, true],
|
|
349
|
+
['ring', 'card', 'focus ring on card', primaryRgb, surface('card'), NON_TEXT_FLOOR, true],
|
|
350
|
+
// accent-foreground now picks the higher-contrast of primary / primaryForeground
|
|
351
|
+
// against --accent (see applyColor in use-global-primary-color.ts). Mirror
|
|
352
|
+
// that logic here so the audit reports what the runtime will actually do.
|
|
353
|
+
...(() => {
|
|
354
|
+
const accent = surface('accent');
|
|
355
|
+
if (!accent) return [];
|
|
356
|
+
const pPrimary = contrast(primaryRgb, accent);
|
|
357
|
+
const pFg = contrast(foregroundRgb, accent);
|
|
358
|
+
const picked = pPrimary >= 4.5 ? { rgb: primaryRgb, name: 'primary' } :
|
|
359
|
+
pFg >= 4.5 ? { rgb: foregroundRgb, name: 'primary-foreground' } : null;
|
|
360
|
+
if (!picked) return [['accent-foreground', 'accent', 'accent-foreground on accent (no AAA candidate — falls back to stylesheet)', null, accent, NORMAL_FLOOR, false]];
|
|
361
|
+
return [['accent-foreground', 'accent', `accent-foreground on accent (runtime picks ${picked.name})`, picked.rgb, accent, NORMAL_FLOOR, false]];
|
|
362
|
+
})(),
|
|
363
|
+
['sidebar-primary-foreground', 'sidebar-primary', 'sidebar primary text', foregroundRgb, primaryRgb, NORMAL_FLOOR, false],
|
|
364
|
+
];
|
|
365
|
+
|
|
366
|
+
return pairs.map(([fgKey, bgKey, label, fg, bg, floor, isNonText]) => {
|
|
367
|
+
if (!fg || !bg) return { mode: modeName, brand: brand.label, fgKey, bgKey, label, status: 'skipped' };
|
|
368
|
+
const ratio = contrast(fg, bg);
|
|
369
|
+
const pass = ratio >= floor;
|
|
370
|
+
return {
|
|
371
|
+
mode: modeName,
|
|
372
|
+
brand: brand.label,
|
|
373
|
+
fgKey, bgKey, label,
|
|
374
|
+
kind: isNonText ? 'non-text' : 'text',
|
|
375
|
+
ratio: Math.round(ratio * 100) / 100,
|
|
376
|
+
floor,
|
|
377
|
+
pass,
|
|
378
|
+
fgHex: toHex(fg),
|
|
379
|
+
bgHex: toHex(bg),
|
|
380
|
+
status: pass ? 'pass' : 'warn', // brand-derived runtime overrides → warn, not fail
|
|
381
|
+
};
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const brandResults = [];
|
|
386
|
+
for (const brand of brandColorsRaw.colors ?? []) {
|
|
387
|
+
brandResults.push(...simulateBrand(brand, 'light', tokens.theme.light));
|
|
388
|
+
brandResults.push(...simulateBrand(brand, 'dark', tokens.theme.dark));
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (json) {
|
|
392
|
+
console.log(JSON.stringify({
|
|
393
|
+
floor: { normal: NORMAL_FLOOR, large: LARGE_FLOOR },
|
|
394
|
+
results: all,
|
|
395
|
+
runtimeMutated: brandResults,
|
|
396
|
+
}, null, 2));
|
|
397
|
+
} else {
|
|
398
|
+
const failed = all.filter((r) => r.status === 'fail');
|
|
399
|
+
const warned = all.filter((r) => r.status === 'warn');
|
|
400
|
+
const passed = all.filter((r) => r.status === 'pass');
|
|
401
|
+
const skipped = all.filter((r) => r.status === 'skipped');
|
|
402
|
+
console.log(`WCAG ${aa ? 'AA' : 'AAA'} contrast audit — ${NORMAL_FLOOR}:1 normal text, ${LARGE_FLOOR}:1 large text, ${NON_TEXT_FLOOR}:1 non-text\n`);
|
|
403
|
+
for (const mode of ['light', 'dark']) {
|
|
404
|
+
console.log(`▸ ${mode}`);
|
|
405
|
+
for (const r of all.filter((x) => x.mode === mode)) {
|
|
406
|
+
const mark =
|
|
407
|
+
r.status === 'pass' ? '✓' :
|
|
408
|
+
r.status === 'fail' ? '✗' :
|
|
409
|
+
r.status === 'warn' ? '!' : '·';
|
|
410
|
+
const kind = r.kind === 'non-text' ? ' [non-text]' : '';
|
|
411
|
+
const brandNote = r.brand && r.status !== 'pass' ? ' (brand)' : '';
|
|
412
|
+
console.log(
|
|
413
|
+
` ${mark} ${r.label.padEnd(34)} ${String(r.ratio).padStart(5)}:1 ` +
|
|
414
|
+
`${r.fgKey} ${r.fgHex} on ${r.bgKey} ${r.bgHex}${kind}${brandNote}`
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
console.log('');
|
|
418
|
+
}
|
|
419
|
+
if (brandResults.length) {
|
|
420
|
+
console.log('▸ runtime-mutated tokens (per brand color, both modes)');
|
|
421
|
+
const brands = [...new Set(brandResults.map((r) => r.brand))];
|
|
422
|
+
for (const b of brands) {
|
|
423
|
+
console.log(` ${b}`);
|
|
424
|
+
for (const r of brandResults.filter((x) => x.brand === b)) {
|
|
425
|
+
const mark = r.status === 'pass' ? '✓' : r.status === 'warn' ? '!' : '·';
|
|
426
|
+
const kind = r.kind === 'non-text' ? ' [non-text]' : '';
|
|
427
|
+
console.log(
|
|
428
|
+
` ${mark} ${r.mode.padEnd(5)} ${r.label.padEnd(54)} ${String(r.ratio).padStart(5)}:1 ` +
|
|
429
|
+
`${r.fgKey} ${r.fgHex ?? '—'} on ${r.bgKey} ${r.bgHex ?? '—'}${kind}`
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
console.log('');
|
|
434
|
+
}
|
|
435
|
+
const brandWarnings = brandResults.filter((r) => r.status === 'warn').length;
|
|
436
|
+
console.log(`${passed.length} pass · ${failed.length} fail · ${warned.length + brandWarnings} warn · ${skipped.length} skipped`);
|
|
437
|
+
if (warned.length || brandWarnings) {
|
|
438
|
+
console.log(
|
|
439
|
+
`\nWarnings are brand-derived pairs below AAA. The brand picker surfaces` +
|
|
440
|
+
` the same warning in-product (see CLAUDE.md → Accessibility Floor). Not blocking.`,
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
if (failed.length) process.exit(1);
|
|
444
|
+
}
|