@behavioral-agentic-design-system/core 0.37.0 → 0.39.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 -4
- package/scripts/a11y-lint.cjs +8 -2
- package/scripts/a11y-ratchet.mjs +172 -0
- package/scripts/accent-as-text.mjs +274 -0
- package/scripts/bads-doctor.mjs +2 -1
- package/scripts/check-stories.mjs +265 -52
- package/scripts/contrast.mjs +44 -0
- package/scripts/health-report.mjs +211 -122
- package/scripts/lint-slop.mjs +44 -2
- package/scripts/oklch-to-hex.cjs +7 -1
- package/scripts/ratchet.mjs +326 -0
- package/scripts/slop-rules.mjs +156 -0
- package/scripts/token-nearest.mjs +333 -0
- package/scripts/wcag-audit.cjs +59 -8
- package/src/app/preview/components/page.tsx +363 -0
- package/src/components/Foundations/Typography.stories.tsx +82 -21
- package/src/components/ui/accordion.tsx +67 -5
- package/src/components/ui/alert.tsx +20 -7
- package/src/components/ui/avatar.tsx +18 -4
- package/src/components/ui/breadcrumb.tsx +47 -0
- package/src/components/ui/checkbox-card.tsx +48 -8
- package/src/components/ui/checkbox.tsx +17 -8
- package/src/components/ui/format-date.tsx +107 -0
- package/src/components/ui/progress.tsx +6 -5
- package/src/components/ui/sparkline.tsx +198 -0
- package/src/components/ui/tag.tsx +17 -1
- package/src/components/ui/tags-input.tsx +84 -11
- package/src/components/ui/toggle-group.tsx +13 -0
- package/src/components/ui/week-picker.tsx +463 -0
- package/src/data/docs-index.ts +12 -0
- package/src/lib/theme-editor.ts +7 -1
- package/src/stories/Accordion.stories.tsx +59 -1
- package/src/stories/ActionBar.stories.tsx +1 -1
- package/src/stories/Affix.stories.tsx +1 -1
- package/src/stories/AiBadge.stories.tsx +1 -1
- package/src/stories/Alert.stories.tsx +36 -1
- package/src/stories/AlertDialog.stories.tsx +1 -1
- package/src/stories/Anchor.stories.tsx +1 -1
- package/src/stories/AngleSlider.stories.tsx +1 -1
- package/src/stories/AspectRatio.stories.tsx +1 -1
- package/src/stories/Autocomplete.stories.tsx +1 -1
- package/src/stories/Avatar.stories.tsx +41 -1
- package/src/stories/Badge.stories.tsx +1 -1
- package/src/stories/Blockquote.stories.tsx +1 -1
- package/src/stories/BottomNavigation.stories.tsx +1 -1
- package/src/stories/Breadcrumb.stories.tsx +38 -5
- package/src/stories/Burger.stories.tsx +1 -1
- package/src/stories/Button.stories.tsx +9 -1
- package/src/stories/ButtonGroup.stories.tsx +1 -1
- package/src/stories/Calendar.stories.tsx +1 -1
- package/src/stories/Card.stories.tsx +1 -1
- package/src/stories/Carousel.stories.tsx +1 -1
- package/src/stories/Cascader.stories.tsx +1 -1
- package/src/stories/Chart.stories.tsx +1 -1
- package/src/stories/ChatBubble.stories.tsx +1 -1
- package/src/stories/ChatMarker.stories.tsx +1 -1
- package/src/stories/Checkbox.stories.tsx +36 -5
- package/src/stories/CheckboxCard.stories.tsx +37 -2
- package/src/stories/CheckboxGroup.stories.tsx +1 -1
- package/src/stories/Chip.stories.tsx +1 -1
- package/src/stories/CircularProgress.stories.tsx +17 -1
- package/src/stories/Clipboard.stories.tsx +1 -1
- package/src/stories/CloseButton.stories.tsx +1 -1
- package/src/stories/Code.stories.tsx +1 -1
- package/src/stories/Collapsible.stories.tsx +1 -1
- package/src/stories/ColorPicker.stories.tsx +1 -1
- package/src/stories/ColorSwatch.stories.tsx +1 -1
- package/src/stories/Combobox.stories.tsx +1 -1
- package/src/stories/Command.stories.tsx +1 -1
- package/src/stories/Container.stories.tsx +1 -1
- package/src/stories/ContextMenu.stories.tsx +1 -1
- package/src/stories/CopyButton.stories.tsx +9 -1
- package/src/stories/DataList.stories.tsx +1 -1
- package/src/stories/DataTable.stories.tsx +1 -1
- package/src/stories/DateField.stories.tsx +1 -1
- package/src/stories/DatePicker.stories.tsx +1 -1
- package/src/stories/DateRangePicker.stories.tsx +1 -1
- package/src/stories/Dialog.stories.tsx +1 -1
- package/src/stories/Drawer.stories.tsx +1 -1
- package/src/stories/DropdownMenu.stories.tsx +1 -1
- package/src/stories/Editable.stories.tsx +1 -1
- package/src/stories/EmptyState.stories.tsx +1 -1
- package/src/stories/Field.stories.tsx +1 -1
- package/src/stories/Fieldset.stories.tsx +1 -1
- package/src/stories/FileButton.stories.tsx +1 -1
- package/src/stories/FileUpload.stories.tsx +1 -1
- package/src/stories/FloatLabel.stories.tsx +1 -1
- package/src/stories/FloatingActionButton.stories.tsx +1 -1
- package/src/stories/FloatingIndicator.stories.tsx +1 -1
- package/src/stories/FloatingPanel.stories.tsx +9 -1
- package/src/stories/FocusTrap.stories.tsx +1 -1
- package/src/stories/FormatByte.stories.tsx +1 -1
- package/src/stories/FormatDate.stories.tsx +174 -0
- package/src/stories/FormatNumber.stories.tsx +1 -1
- package/src/stories/Highlight.stories.tsx +1 -1
- package/src/stories/HoverCard.stories.tsx +1 -1
- package/src/stories/Image.stories.tsx +1 -1
- package/src/stories/Indicator.stories.tsx +17 -1
- package/src/stories/Input.stories.tsx +1 -1
- package/src/stories/InputClearButton.stories.tsx +1 -1
- package/src/stories/InputGroup.stories.tsx +1 -1
- package/src/stories/Kbd.stories.tsx +1 -1
- package/src/stories/KeyboardShortcut.stories.tsx +1 -1
- package/src/stories/Label.stories.tsx +1 -1
- package/src/stories/Link.stories.tsx +1 -1
- package/src/stories/List.stories.tsx +1 -1
- package/src/stories/Listbox.stories.tsx +1 -1
- package/src/stories/LoadingDots.stories.tsx +1 -1
- package/src/stories/LoadingOverlay.stories.tsx +1 -1
- package/src/stories/Mark.stories.tsx +1 -1
- package/src/stories/Marquee.stories.tsx +1 -1
- package/src/stories/Masonry.stories.tsx +1 -1
- package/src/stories/Menubar.stories.tsx +1 -1
- package/src/stories/Meter.stories.tsx +1 -1
- package/src/stories/MonthPicker.stories.tsx +1 -1
- package/src/stories/MotionToggle.stories.tsx +1 -1
- package/src/stories/MultiSelect.stories.tsx +1 -1
- package/src/stories/NativeSelect.stories.tsx +1 -1
- package/src/stories/NavLink.stories.tsx +1 -1
- package/src/stories/NavigationMenu.stories.tsx +1 -1
- package/src/stories/NavigationRail.stories.tsx +9 -1
- package/src/stories/Notification.stories.tsx +1 -1
- package/src/stories/NumberInput.stories.tsx +1 -1
- package/src/stories/Overlay.stories.tsx +1 -1
- package/src/stories/Pagination.stories.tsx +1 -1
- package/src/stories/PasswordInput.stories.tsx +1 -1
- package/src/stories/PinInput.stories.tsx +1 -1
- package/src/stories/Popover.stories.tsx +1 -1
- package/src/stories/Progress.stories.tsx +11 -1
- package/src/stories/Prose.stories.tsx +1 -1
- package/src/stories/QrCode.stories.tsx +1 -1
- package/src/stories/Questionnaire.stories.tsx +1 -1
- package/src/stories/RadioGroup.stories.tsx +1 -1
- package/src/stories/RatingGroup.stories.tsx +1 -1
- package/src/stories/Resizable.stories.tsx +1 -1
- package/src/stories/Result.stories.tsx +1 -1
- package/src/stories/Reveal.stories.tsx +1 -1
- package/src/stories/RollingNumber.stories.tsx +1 -1
- package/src/stories/ScrollArea.stories.tsx +1 -1
- package/src/stories/SearchInput.stories.tsx +1 -1
- package/src/stories/SegmentedControl.stories.tsx +1 -1
- package/src/stories/Select.stories.tsx +1 -1
- package/src/stories/Separator.stories.tsx +1 -1
- package/src/stories/Sheet.stories.tsx +1 -1
- package/src/stories/Sidebar.stories.tsx +1 -1
- package/src/stories/SignaturePad.stories.tsx +1 -1
- package/src/stories/SimpleGrid.stories.tsx +1 -1
- package/src/stories/Skeleton.stories.tsx +1 -1
- package/src/stories/SkeletonTable.stories.tsx +1 -1
- package/src/stories/SkipLink.stories.tsx +1 -1
- package/src/stories/Slider.stories.tsx +1 -1
- package/src/stories/Sonner.stories.tsx +1 -1
- package/src/stories/Sparkline.stories.tsx +208 -0
- package/src/stories/Spinner.stories.tsx +9 -1
- package/src/stories/Spoiler.stories.tsx +1 -1
- package/src/stories/Squircle.stories.tsx +1 -1
- package/src/stories/Stack.stories.tsx +1 -1
- package/src/stories/StatusBadge.stories.tsx +1 -1
- package/src/stories/Stepper.stories.tsx +1 -1
- package/src/stories/Switch.stories.tsx +1 -1
- package/src/stories/Table.stories.tsx +1 -1
- package/src/stories/Tabs.stories.tsx +1 -1
- package/src/stories/Tag.stories.tsx +23 -1
- package/src/stories/TagsInput.stories.tsx +46 -1
- package/src/stories/Text.stories.tsx +1 -1
- package/src/stories/Textarea.stories.tsx +1 -1
- package/src/stories/ThemeIcon.stories.tsx +1 -1
- package/src/stories/TimePicker.stories.tsx +1 -1
- package/src/stories/Timeline.stories.tsx +9 -1
- package/src/stories/Timer.stories.tsx +1 -1
- package/src/stories/Toggle.stories.tsx +1 -1
- package/src/stories/ToggleGroup.stories.tsx +21 -1
- package/src/stories/ToggleTip.stories.tsx +1 -1
- package/src/stories/Toolbar.stories.tsx +1 -1
- package/src/stories/Tooltip.stories.tsx +1 -1
- package/src/stories/Tour.stories.tsx +1 -1
- package/src/stories/TransferList.stories.tsx +1 -1
- package/src/stories/TreeSelect.stories.tsx +1 -1
- package/src/stories/TreeView.stories.tsx +1 -1
- package/src/stories/TypingIndicator.stories.tsx +1 -1
- package/src/stories/VisuallyHidden.stories.tsx +1 -1
- package/src/stories/Watermark.stories.tsx +1 -1
- package/src/stories/WeekPicker.stories.tsx +90 -0
- package/src/stories/WordRotate.stories.tsx +1 -1
- package/src/stories/YearPicker.stories.tsx +1 -1
- package/src/stories/story-groups.json +199 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* token-nearest — resolve a raw value to the nearest token in the live system.
|
|
4
|
+
*
|
|
5
|
+
* Why this exists: BADS's token lints answer "is this off-token?" and stop
|
|
6
|
+
* there. `lint-tokens` reports a hardcoded `#3b82f6` as a violation without
|
|
7
|
+
* saying what to use instead, which puts the burden of the fix on the reader
|
|
8
|
+
* every single time. The reader then guesses, and a guess is how a second
|
|
9
|
+
* near-duplicate blue enters a palette.
|
|
10
|
+
*
|
|
11
|
+
* The pattern is lifted from ClosedLoop's design-inventory pipeline
|
|
12
|
+
* (`extract-visual-spec.ts`, Apache 2.0, used with permission — audited
|
|
13
|
+
* 2026-09-03, see docs/roadmap-closedloop-import.md item 2). Their framing is
|
|
14
|
+
* the useful part: drift is inventory signal, not a rule violation. A value
|
|
15
|
+
* that resolves to a token IS that token. A value that resolves to nothing is
|
|
16
|
+
* a question — "did you mean this one?" — with the nearest legal answer
|
|
17
|
+
* attached.
|
|
18
|
+
*
|
|
19
|
+
* One deviation from the source. They measure colour distance in RGB. BADS
|
|
20
|
+
* authors its palette in oklch, and oklch-to-hex.cjs already carries the
|
|
21
|
+
* transform, so distance here is Euclidean in OKLab. Perceptual distance is
|
|
22
|
+
* what "nearest" should mean, and RGB distance ranks a dark navy and a dark
|
|
23
|
+
* maroon as neighbours when the eye does not.
|
|
24
|
+
*
|
|
25
|
+
* Scope: colours, and the three dimension scales (fontSize, spacing,
|
|
26
|
+
* borderRadius). A dimension answer doubles as the AAA-floor helper — the
|
|
27
|
+
* nearest LEGAL step is a better error message than "11px is below the floor".
|
|
28
|
+
*
|
|
29
|
+
* Run:
|
|
30
|
+
* node packages/core/scripts/token-nearest.mjs "#3b82f6"
|
|
31
|
+
* node packages/core/scripts/token-nearest.mjs "11px" --kind fontSize
|
|
32
|
+
* node packages/core/scripts/token-nearest.mjs "oklch(0.55 0.15 150)" --mode dark
|
|
33
|
+
* npx bads-token-nearest "#3b82f6" --json
|
|
34
|
+
*
|
|
35
|
+
* Exit codes: 0 a match was reported, 1 usage or input error.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import fs from "node:fs";
|
|
39
|
+
import path from "node:path";
|
|
40
|
+
import { createRequire } from "node:module";
|
|
41
|
+
import { fileURLToPath } from "node:url";
|
|
42
|
+
|
|
43
|
+
const require = createRequire(import.meta.url);
|
|
44
|
+
const { colorToHex } = require("./oklch-to-hex.cjs");
|
|
45
|
+
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// Token loading
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
/** Candidate tokens.json locations, consumer repo first, then this package. */
|
|
51
|
+
export function tokenPaths(root = process.cwd()) {
|
|
52
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
53
|
+
return [
|
|
54
|
+
path.join(root, "packages/core/src/tokens.json"),
|
|
55
|
+
path.join(root, "tokens.json"),
|
|
56
|
+
path.join(root, "src/tokens.json"),
|
|
57
|
+
path.join(here, "../src/tokens.json"),
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function loadTokens(root = process.cwd()) {
|
|
62
|
+
for (const p of tokenPaths(root)) {
|
|
63
|
+
if (fs.existsSync(p)) return { tokens: JSON.parse(fs.readFileSync(p, "utf8")), source: p };
|
|
64
|
+
}
|
|
65
|
+
return { tokens: null, source: null };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Flatten a DTCG token tree to `{ "dotted.path": "$value" }`.
|
|
70
|
+
* Groups are plain objects; a leaf is any object carrying `$value`.
|
|
71
|
+
*/
|
|
72
|
+
export function flattenTokens(node, prefix = "") {
|
|
73
|
+
const out = {};
|
|
74
|
+
if (!node || typeof node !== "object") return out;
|
|
75
|
+
for (const [key, value] of Object.entries(node)) {
|
|
76
|
+
if (key.startsWith("$")) continue;
|
|
77
|
+
if (!value || typeof value !== "object") continue;
|
|
78
|
+
if ("$value" in value) out[prefix + key] = value.$value;
|
|
79
|
+
else Object.assign(out, flattenTokens(value, `${prefix}${key}.`));
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
// Colour
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
/** sRGB hex to OKLab. The forward direction of oklch-to-hex's transform. */
|
|
89
|
+
export function hexToOklab(hex) {
|
|
90
|
+
const body = hex.replace("#", "");
|
|
91
|
+
const full = body.length === 3 ? body.split("").map((c) => c + c).join("") : body.slice(0, 6);
|
|
92
|
+
const toLinear = (v) => {
|
|
93
|
+
const s = parseInt(v, 16) / 255;
|
|
94
|
+
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
95
|
+
};
|
|
96
|
+
const r = toLinear(full.slice(0, 2));
|
|
97
|
+
const g = toLinear(full.slice(2, 4));
|
|
98
|
+
const b = toLinear(full.slice(4, 6));
|
|
99
|
+
|
|
100
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
101
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
102
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
103
|
+
|
|
104
|
+
return [
|
|
105
|
+
0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
|
|
106
|
+
1.9779984951 * l - 0.2428592205 * m + 0.4505937099 * s,
|
|
107
|
+
0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Perceptual distance between two hex colours. 0 is identical. */
|
|
112
|
+
export function oklabDistance(hexA, hexB) {
|
|
113
|
+
const a = hexToOklab(hexA);
|
|
114
|
+
const b = hexToOklab(hexB);
|
|
115
|
+
return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Resolve one token value to hex, chasing `var(--x)` references through the
|
|
120
|
+
* same table. Returns null for anything not resolvable to a flat colour —
|
|
121
|
+
* `color-mix()` and unresolvable references included. A null is honest: a
|
|
122
|
+
* half-resolved colour would poison every distance computed against it.
|
|
123
|
+
*/
|
|
124
|
+
export function resolveColor(value, table, depth = 0) {
|
|
125
|
+
if (typeof value !== "string" || depth > 8) return null;
|
|
126
|
+
const v = value.trim();
|
|
127
|
+
|
|
128
|
+
const ref = v.match(/^var\(\s*--([\w-]+)\s*\)$/);
|
|
129
|
+
if (ref) {
|
|
130
|
+
const target = table[ref[1]];
|
|
131
|
+
return target === undefined ? null : resolveColor(target, table, depth + 1);
|
|
132
|
+
}
|
|
133
|
+
if (/^color-mix\(/i.test(v)) return null;
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
return colorToHex(v);
|
|
137
|
+
} catch {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Build `{ tokenName: hex }` for one theme mode, plus the raw palette.
|
|
144
|
+
* Theme tokens win a name collision: they are what a consumer actually types.
|
|
145
|
+
*/
|
|
146
|
+
export function buildColorTable(tokens, { mode = "light", includePrimitives = true } = {}) {
|
|
147
|
+
const theme = tokens?.theme?.[mode] ?? {};
|
|
148
|
+
const raw = flattenTokens(theme);
|
|
149
|
+
|
|
150
|
+
const table = {};
|
|
151
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
152
|
+
const hex = resolveColor(value, raw);
|
|
153
|
+
if (hex) table[name] = hex;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (includePrimitives) {
|
|
157
|
+
for (const [name, value] of Object.entries(flattenTokens(tokens?.color ?? {}))) {
|
|
158
|
+
if (name in table) continue;
|
|
159
|
+
const hex = resolveColor(value, raw);
|
|
160
|
+
if (hex) table[name] = hex;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return table;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Nearest colour tokens to a raw value, closest first.
|
|
168
|
+
*
|
|
169
|
+
* `exact` means the value IS this token, so the correct report is "you wrote
|
|
170
|
+
* the literal for a token you already have", which is a different and much
|
|
171
|
+
* more actionable finding than "this is near primary".
|
|
172
|
+
*/
|
|
173
|
+
export function nearestColorToken(value, table, { limit = 3 } = {}) {
|
|
174
|
+
const hex = resolveColor(value, {});
|
|
175
|
+
if (!hex) return null;
|
|
176
|
+
const ranked = Object.entries(table)
|
|
177
|
+
.map(([token, tokenHex]) => ({
|
|
178
|
+
token,
|
|
179
|
+
hex: tokenHex,
|
|
180
|
+
distance: Number(oklabDistance(hex, tokenHex).toFixed(4)),
|
|
181
|
+
}))
|
|
182
|
+
.sort((a, b) => a.distance - b.distance);
|
|
183
|
+
if (!ranked.length) return null;
|
|
184
|
+
return {
|
|
185
|
+
input: hex,
|
|
186
|
+
exact: ranked[0].distance === 0,
|
|
187
|
+
matches: ranked.slice(0, limit),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// Dimension scales
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
|
|
195
|
+
export const SCALE_KINDS = ["fontSize", "spacing", "borderRadius"];
|
|
196
|
+
|
|
197
|
+
const toPx = (value) => {
|
|
198
|
+
if (typeof value !== "string") return null;
|
|
199
|
+
const m = value.trim().match(/^(-?[\d.]+)(px|rem)?$/);
|
|
200
|
+
if (!m) return null;
|
|
201
|
+
const n = Number(m[1]);
|
|
202
|
+
if (!Number.isFinite(n)) return null;
|
|
203
|
+
return m[2] === "rem" ? n * 16 : n;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/** `[{ name, px }]` for one scale, ascending. */
|
|
207
|
+
export function buildScale(tokens, kind) {
|
|
208
|
+
const flat = flattenTokens(tokens?.[kind] ?? {});
|
|
209
|
+
return Object.entries(flat)
|
|
210
|
+
.map(([name, value]) => ({ name, px: toPx(value) }))
|
|
211
|
+
.filter((s) => s.px !== null)
|
|
212
|
+
.sort((a, b) => a.px - b.px);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Nearest step on a scale.
|
|
217
|
+
*
|
|
218
|
+
* `floor` is the smallest step at or above the value, and it is the field the
|
|
219
|
+
* AAA type rule wants: when 11px is below the floor, "the nearest legal step
|
|
220
|
+
* is xs (14px)" is a fix, while "11px is below the floor" is only a verdict.
|
|
221
|
+
*/
|
|
222
|
+
export function nearestScaleStep(value, scale) {
|
|
223
|
+
const px = toPx(value);
|
|
224
|
+
if (px === null || !scale.length) return null;
|
|
225
|
+
const ranked = [...scale].sort((a, b) => Math.abs(a.px - px) - Math.abs(b.px - px));
|
|
226
|
+
const floor = scale.find((s) => s.px >= px) ?? null;
|
|
227
|
+
return {
|
|
228
|
+
input: px,
|
|
229
|
+
exact: ranked[0].px === px,
|
|
230
|
+
nearest: ranked[0],
|
|
231
|
+
floor,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ---------------------------------------------------------------------------
|
|
236
|
+
// One-call helper for lint integrations
|
|
237
|
+
// ---------------------------------------------------------------------------
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A short, human-readable "did you mean" suffix for a lint message, or "" when
|
|
241
|
+
* nothing useful can be said. Returning "" rather than a hedge keeps a lint
|
|
242
|
+
* line from growing a tail that carries no information.
|
|
243
|
+
*/
|
|
244
|
+
export function suggestion(value, { tokens, mode = "light", kind = "color" } = {}) {
|
|
245
|
+
if (!tokens) return "";
|
|
246
|
+
if (kind === "color") {
|
|
247
|
+
const table = buildColorTable(tokens, { mode });
|
|
248
|
+
const hit = nearestColorToken(value, table, { limit: 1 });
|
|
249
|
+
if (!hit) return "";
|
|
250
|
+
const best = hit.matches[0];
|
|
251
|
+
if (hit.exact) return ` This is exactly --${best.token}; use the token.`;
|
|
252
|
+
if (best.distance > 0.12) return "";
|
|
253
|
+
return ` Nearest token: --${best.token} (${best.hex}, ΔOKLab ${best.distance}).`;
|
|
254
|
+
}
|
|
255
|
+
const hit = nearestScaleStep(value, buildScale(tokens, kind));
|
|
256
|
+
if (!hit) return "";
|
|
257
|
+
if (hit.exact) return ` This is exactly ${kind}.${hit.nearest.name}; use the token.`;
|
|
258
|
+
const floor = hit.floor ? `, nearest step at or above it is ${hit.floor.name} (${hit.floor.px}px)` : "";
|
|
259
|
+
return ` Nearest ${kind} step: ${hit.nearest.name} (${hit.nearest.px}px)${floor}.`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ---------------------------------------------------------------------------
|
|
263
|
+
// CLI
|
|
264
|
+
// ---------------------------------------------------------------------------
|
|
265
|
+
|
|
266
|
+
function main(argv) {
|
|
267
|
+
const flags = argv.filter((a) => a.startsWith("--"));
|
|
268
|
+
const positional = argv.filter((a) => !a.startsWith("--"));
|
|
269
|
+
const flagValue = (name, fallback) => {
|
|
270
|
+
const i = argv.indexOf(`--${name}`);
|
|
271
|
+
return i >= 0 && argv[i + 1] && !argv[i + 1].startsWith("--") ? argv[i + 1] : fallback;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const value = positional.find((a) => a !== flagValue("kind") && a !== flagValue("mode"));
|
|
275
|
+
if (!value) {
|
|
276
|
+
console.error('usage: bads-token-nearest "<value>" [--kind color|fontSize|spacing|borderRadius] [--mode light|dark] [--json]');
|
|
277
|
+
return 1;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const kind = flagValue("kind", "color");
|
|
281
|
+
const mode = flagValue("mode", "light");
|
|
282
|
+
const asJson = flags.includes("--json");
|
|
283
|
+
|
|
284
|
+
const { tokens, source } = loadTokens();
|
|
285
|
+
if (!tokens) {
|
|
286
|
+
console.error("token-nearest: no tokens.json found. Run from a BADS repo root or a fork of one.");
|
|
287
|
+
return 1;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
let result;
|
|
291
|
+
if (kind === "color") {
|
|
292
|
+
const table = buildColorTable(tokens, { mode });
|
|
293
|
+
result = nearestColorToken(value, table);
|
|
294
|
+
if (!result) {
|
|
295
|
+
console.error(`token-nearest: could not resolve ${JSON.stringify(value)} to a colour.`);
|
|
296
|
+
return 1;
|
|
297
|
+
}
|
|
298
|
+
} else if (SCALE_KINDS.includes(kind)) {
|
|
299
|
+
result = nearestScaleStep(value, buildScale(tokens, kind));
|
|
300
|
+
if (!result) {
|
|
301
|
+
console.error(`token-nearest: could not read ${JSON.stringify(value)} as a ${kind} value.`);
|
|
302
|
+
return 1;
|
|
303
|
+
}
|
|
304
|
+
} else {
|
|
305
|
+
console.error(`token-nearest: unknown --kind ${kind}. One of: color, ${SCALE_KINDS.join(", ")}.`);
|
|
306
|
+
return 1;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (asJson) {
|
|
310
|
+
console.log(JSON.stringify({ value, kind, mode, source, ...result }, null, 2));
|
|
311
|
+
return 0;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
console.log(`token-nearest: ${value} (${kind}${kind === "color" ? `, ${mode} mode` : ""})`);
|
|
315
|
+
if (kind === "color") {
|
|
316
|
+
console.log(` resolved to ${result.input}`);
|
|
317
|
+
for (const m of result.matches) {
|
|
318
|
+
const tag = m.distance === 0 ? "EXACT" : `Δ ${m.distance}`;
|
|
319
|
+
console.log(` ${tag.padEnd(10)} --${m.token} ${m.hex}`);
|
|
320
|
+
}
|
|
321
|
+
if (!result.exact) console.log("\n Off-token. Use the nearest token above, or add a new one deliberately.");
|
|
322
|
+
} else {
|
|
323
|
+
console.log(` ${result.input}px`);
|
|
324
|
+
console.log(` nearest ${result.nearest.name} (${result.nearest.px}px)`);
|
|
325
|
+
if (result.floor) console.log(` floor ${result.floor.name} (${result.floor.px}px) — smallest step at or above the value`);
|
|
326
|
+
if (result.exact) console.log("\n Exact match. Use the token rather than the literal.");
|
|
327
|
+
}
|
|
328
|
+
return 0;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === path.resolve(process.argv[1])) {
|
|
332
|
+
process.exit(main(process.argv.slice(2)));
|
|
333
|
+
}
|
package/scripts/wcag-audit.cjs
CHANGED
|
@@ -43,6 +43,44 @@
|
|
|
43
43
|
const fs = require('fs');
|
|
44
44
|
const path = require('path');
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Documented exceptions (tokens.json -> wcagExceptions.pairs) whose fgKey/bgKey
|
|
48
|
+
* no longer matches an "exception"-status row in `results`.
|
|
49
|
+
*
|
|
50
|
+
* An exception outlives the problem it documented when someone fixes the pair
|
|
51
|
+
* it was written for: the entry stays in tokens.json, silently matching
|
|
52
|
+
* nothing, and would quietly hide a real regression if that same pair failed
|
|
53
|
+
* again later. Detecting that is the same failure class this audit already
|
|
54
|
+
* exists to prevent, arriving from the other direction.
|
|
55
|
+
*
|
|
56
|
+
* Pure so the stale-exceptions logic can be unit tested against a fixture,
|
|
57
|
+
* without running the rest of this script (which reads the real tokens.json,
|
|
58
|
+
* prints to stdout, and can call process.exit) just to reach it.
|
|
59
|
+
*/
|
|
60
|
+
function findStaleExceptions(pairs, results) {
|
|
61
|
+
const list = pairs || [];
|
|
62
|
+
const rows = results || [];
|
|
63
|
+
return list.filter(
|
|
64
|
+
(entry) => !rows.some(
|
|
65
|
+
(r) => r.status === 'exception' && r.fgKey === entry.fgKey && r.bgKey === entry.bgKey,
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = { findStaleExceptions };
|
|
71
|
+
|
|
72
|
+
// Everything below this line is the script's CLI behaviour: it reads the real
|
|
73
|
+
// tokens.json, prints a report, and can call process.exit. Guard it behind
|
|
74
|
+
// the main-module check so `require("./wcag-audit.cjs")` from a test can pull
|
|
75
|
+
// in findStaleExceptions above without any of that running. This is a plain
|
|
76
|
+
// top-level return, legal in a CommonJS module because Node wraps the whole
|
|
77
|
+
// file in a function. require.main resolves correctly through a symlink by
|
|
78
|
+
// default (unlike the import.meta.url / process.argv[1] mismatch fixed for
|
|
79
|
+
// the .mjs scripts), so this holds for the shipped bads-wcag-audit bin too.
|
|
80
|
+
if (require.main !== module) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
46
84
|
// Shipped as the `bads-wcag-audit` bin. The token file is consumer-owned, but
|
|
47
85
|
// "consumer" means three different repo layouts and only one of them has a
|
|
48
86
|
// packages/ directory:
|
|
@@ -428,21 +466,28 @@ for (const brand of brandColorsRaw.colors ?? []) {
|
|
|
428
466
|
brandResults.push(...simulateBrand(brand, 'dark', tokens.theme.dark));
|
|
429
467
|
}
|
|
430
468
|
|
|
469
|
+
// Documented exceptions live in tokens.json next to the tokens they concern, each with a
|
|
470
|
+
// required reason. A pair listed there is reported as an exception rather than failing the
|
|
471
|
+
// build: the point is that it stays visible, not that it disappears. Computed ahead of the
|
|
472
|
+
// json/human-readable split so both report the same promoted statuses and the same stale list.
|
|
473
|
+
const exceptions = (tokens.wcagExceptions && tokens.wcagExceptions.pairs) || [];
|
|
474
|
+
const exceptionFor = (r) => exceptions.find((e) => e.fgKey === r.fgKey && e.bgKey === r.bgKey && e.reason);
|
|
475
|
+
for (const r of all) {
|
|
476
|
+
if (r.status === 'fail' && exceptionFor(r)) r.status = 'exception';
|
|
477
|
+
}
|
|
478
|
+
// An entry whose fgKey/bgKey matches no 'exception'-status row is stale: either its pair now
|
|
479
|
+
// passes (the common case -- someone fixed the token and the promotion above never fired), or
|
|
480
|
+
// it never matched a real row at all. Reported, never build-failing: see findStaleExceptions.
|
|
481
|
+
const staleExceptions = findStaleExceptions(exceptions, all);
|
|
482
|
+
|
|
431
483
|
if (json) {
|
|
432
484
|
console.log(JSON.stringify({
|
|
433
485
|
floor: { normal: NORMAL_FLOOR, large: LARGE_FLOOR },
|
|
434
486
|
results: all,
|
|
435
487
|
runtimeMutated: brandResults,
|
|
488
|
+
staleExceptions,
|
|
436
489
|
}, null, 2));
|
|
437
490
|
} else {
|
|
438
|
-
// Documented exceptions live in tokens.json next to the tokens they concern, each with a
|
|
439
|
-
// required reason. A pair listed there is reported as an exception rather than failing the
|
|
440
|
-
// build — the point is that it stays visible, not that it disappears.
|
|
441
|
-
const exceptions = (tokens.wcagExceptions && tokens.wcagExceptions.pairs) || [];
|
|
442
|
-
const exceptionFor = (r) => exceptions.find((e) => e.fgKey === r.fgKey && e.bgKey === r.bgKey && e.reason);
|
|
443
|
-
for (const r of all) {
|
|
444
|
-
if (r.status === 'fail' && exceptionFor(r)) r.status = 'exception';
|
|
445
|
-
}
|
|
446
491
|
const excepted = all.filter((r) => r.status === 'exception');
|
|
447
492
|
const failed = all.filter((r) => r.status === 'fail');
|
|
448
493
|
const warned = all.filter((r) => r.status === 'warn');
|
|
@@ -490,6 +535,12 @@ if (json) {
|
|
|
490
535
|
console.log(` ! ${r.label} — ${exceptionFor(r).reason}`);
|
|
491
536
|
}
|
|
492
537
|
}
|
|
538
|
+
if (staleExceptions.length) {
|
|
539
|
+
console.log('\nStale exceptions (these pairs now pass; remove them from tokens.json):');
|
|
540
|
+
for (const e of staleExceptions) {
|
|
541
|
+
console.log(` ! ${e.fgKey} on ${e.bgKey}`);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
493
544
|
if (warned.length || brandWarnings) {
|
|
494
545
|
console.log(
|
|
495
546
|
`\nWarnings are brand-derived pairs below AAA. The brand picker surfaces` +
|