Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/DESIGN_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ A project hook refuses the first edit of a web source or the tokens in an agent
Use a general token (`--size-control`, `--size-icon`, the spacing scale) before adding a feature-specific `--size-*`; a new token needs a reason the existing ones cannot carry.
3. Open every layer (menu, dialog, popover, sheet, select) through its part.
The parts register with the shell's one Escape owner and hand keyboard focus back when they close (`components/ui/layer.ts`); a hand-made layer leaves Escape to the terminal under it or loses focus.
A surface no part can draw, such as the Start panel's floating palette, registers itself with `useEscapeLayer` and hands focus back as the parts do; that is the exception, and it stays in the baseline.

`check-ui-parts.mjs` counts raw `<button>`, `<input>`, `<select>`, `<textarea>` and `role="dialog"`/`role="menu"` outside `components/ui` against `scripts/ui-parts-baseline.json`.
A file may only go down: a new raw control fails, and a file with fewer than its baseline asks you to lock the lower number in with `node scripts/check-ui-parts.mjs --update`.
Expand Down
17 changes: 14 additions & 3 deletions scripts/check-ui-parts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ const KINDS = {
input: [/<input\b/g, 'use `Input`, `Checkbox`, `Switch`, `RadioGroup` or `Slider` from components/ui'],
select: [/<select\b/g, 'use `Select` (components/ui/select)'],
textarea: [/<textarea\b/g, 'add a multi-line part to components/ui and use it'],
// A JSX attribute, not a `[role="menu"]` selector string.
layer: [/(?<=\s)role="(?:dialog|alertdialog|menu)"/g, 'open it through `Dialog`, `AlertDialog`, `Popover`, `Sheet` or `DropdownMenu`'],
layer: [null, 'open it through `Dialog`, `AlertDialog`, `Popover`, `Sheet` or `DropdownMenu`'],
};

// A layer role on a raw element (`<div role="dialog">`). The same role on a
// part (`<PopoverContent role="dialog">`) is the part's layer, and a
// `[role="menu"]` selector string is no element at all.
function rawLayers(source) {
let n = 0;
for (const match of source.matchAll(/(?<=\s)role="(?:dialog|alertdialog|menu)"/g)) {
const open = source.lastIndexOf('<', match.index);
if (/^<[a-z]/.test(source.slice(open, open + 2))) n += 1;
}
return n;
}

const args = process.argv.slice(2);
const update = args.includes('--update');
const root = args.find(arg => !arg.startsWith('--')) ?? process.cwd();
Expand All @@ -42,7 +53,7 @@ function count() {
const source = fs.readFileSync(file, 'utf8');
const rel = path.relative(root, file).split(path.sep).join('/');
for (const [kind, [pattern]] of Object.entries(KINDS)) {
const n = source.match(pattern)?.length ?? 0;
const n = pattern ? (source.match(pattern)?.length ?? 0) : rawLayers(source);
if (n) (counts[rel] ??= {})[kind] = n;
}
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/tests/ui-parts.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ function fixture(t, files, baseline) {

const twoButtons = '<button>a</button><button>b</button>';

test('a file at its baseline passes and the parts, tests and selector strings are not counted', t => {
test('a file at its baseline passes and the parts, a part given a layer role, tests and selector strings are not counted', t => {
const {run} = fixture(t, {
'web/src/A.tsx': `${twoButtons} const s = '[role="menu"]';`,
'web/src/A.tsx': `${twoButtons} const s = '[role="menu"]'; <PopoverContent align="start" role="dialog" />`,
'web/src/components/ui/button.tsx': '<button />',
'web/src/A.test.tsx': '<button />',
}, {'web/src/A.tsx': {button: 2}});
Expand Down
3 changes: 0 additions & 3 deletions scripts/ui-parts-baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@
"web/src/Palette.tsx": {
"button": 2
},
"web/src/PaneConnection.tsx": {
"layer": 1
},
"web/src/PaneHeaderBand.tsx": {
"button": 7
},
Expand Down
Loading