@nurvus/ui 0.1.0-alpha.12 → 0.1.0-alpha.14
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/README.md +5 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +6 -4
- package/dist/respawn.js +1 -1
- package/dist/types/components/Menu/Menu.d.ts +35 -27
- package/dist/types/components/Select/Select.d.ts +55 -0
- package/dist/types/components/Tree/Tree.d.ts +39 -0
- package/dist/types/index.d.ts +5 -1
- package/dist/types/menu.d.ts +24 -0
- package/dist/types/respawn/Menu.d.ts +36 -26
- package/dist/types/respawn/Select.d.ts +54 -0
- package/dist/types/respawn/Tree.d.ts +36 -0
- package/dist/types/respawn/index.d.ts +5 -1
- package/dist/types/select.d.ts +70 -0
- package/dist/types/tree.d.ts +9 -0
- package/package.json +2 -2
- package/src/components/Button/Button.scss +8 -2
- package/src/components/Link/Link.md +11 -2
- package/src/components/Menu/Menu.md +32 -126
- package/src/components/Menu/Menu.scss +133 -51
- package/src/components/Menu/Menu.tsx +118 -67
- package/src/components/Select/Select.md +72 -0
- package/src/components/Select/Select.scss +170 -0
- package/src/components/Select/Select.tsx +196 -0
- package/src/components/Table/Table.md +3 -2
- package/src/components/Tag/Tag.scss +3 -1
- package/src/components/Text/Text.md +1 -1
- package/src/components/Tree/Tree.md +62 -0
- package/src/components/Tree/Tree.scss +73 -0
- package/src/components/Tree/Tree.tsx +125 -0
- package/src/css.ts +2 -0
- package/src/index.ts +5 -1
- package/src/menu.ts +66 -0
- package/src/respawn/Menu.ts +113 -62
- package/src/respawn/Select.ts +212 -0
- package/src/respawn/Tree.ts +116 -0
- package/src/respawn/index.ts +5 -1
- package/src/select.ts +140 -0
- package/src/styles/theme.scss +1 -1
- package/src/styles/type.json +1 -1
- package/src/tree.ts +46 -0
package/src/select.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Select rules shared by the React and the Respawn Select (no React here):
|
|
2
|
+
// the order options are listed in, typing to find one, and what each key does
|
|
3
|
+
// (the WAI-ARIA "select-only combobox" pattern).
|
|
4
|
+
|
|
5
|
+
export interface OptionLike {
|
|
6
|
+
value: string;
|
|
7
|
+
label?: unknown;
|
|
8
|
+
textValue?: string;
|
|
9
|
+
group?: string;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A run of options under one heading ('' = no heading). */
|
|
14
|
+
export interface OptionGroup<O> { name: string; options: { option: O; index: number }[] }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The options as the list shows them: grouped, groups in the order they first
|
|
18
|
+
* appear. `index` is the place in that order, which the keys move through.
|
|
19
|
+
*/
|
|
20
|
+
export function groupOptions<O extends OptionLike>(options: O[]): { groups: OptionGroup<O>[]; ordered: O[] } {
|
|
21
|
+
const groups: OptionGroup<O>[] = [];
|
|
22
|
+
options.forEach((option) => {
|
|
23
|
+
const name = option.group || '';
|
|
24
|
+
let group = groups.find((g) => g.name === name);
|
|
25
|
+
if (!group) { group = { name, options: [] }; groups.push(group); }
|
|
26
|
+
group.options.push({ option, index: -1 });
|
|
27
|
+
});
|
|
28
|
+
const ordered: O[] = [];
|
|
29
|
+
groups.forEach((g) => g.options.forEach((entry) => { entry.index = ordered.length; ordered.push(entry.option); }));
|
|
30
|
+
return { groups, ordered };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The text typing matches: textValue, else a plain-text label, else the value. */
|
|
34
|
+
export const optionText = (option: OptionLike): string => (
|
|
35
|
+
option.textValue !== undefined ? option.textValue
|
|
36
|
+
: typeof option.label === 'string' || typeof option.label === 'number' ? String(option.label) : option.value
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/** The next option that can be picked, `delta` steps away (stops at the ends). */
|
|
40
|
+
export function stepOption(options: OptionLike[], from: number, delta: number): number {
|
|
41
|
+
const dir = delta < 0 ? -1 : 1;
|
|
42
|
+
let at = from;
|
|
43
|
+
let found = from;
|
|
44
|
+
for (let moved = 0; moved < Math.abs(delta);) {
|
|
45
|
+
at += dir;
|
|
46
|
+
if (at < 0 || at >= options.length) break;
|
|
47
|
+
if (!options[at].disabled) { found = at; moved += 1; }
|
|
48
|
+
}
|
|
49
|
+
return found;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The first (or last) option that can be picked, or -1. */
|
|
53
|
+
export function edgeOption(options: OptionLike[], last = false): number {
|
|
54
|
+
const list = options.map((o, i) => (o.disabled ? -1 : i)).filter((i) => i >= 0);
|
|
55
|
+
if (!list.length) return -1;
|
|
56
|
+
return last ? list[list.length - 1] : list[0];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Typing: the next option (after `from`, going round) whose text starts with
|
|
61
|
+
* what was typed. Typing one letter again and again steps through the options
|
|
62
|
+
* that start with it, as a native select does.
|
|
63
|
+
*/
|
|
64
|
+
export function matchOption(options: OptionLike[], typed: string, from: number): number {
|
|
65
|
+
const query = typed.toLowerCase();
|
|
66
|
+
const same = query.split('').every((c) => c === query[0]);
|
|
67
|
+
const find = (text: string, start: number) => {
|
|
68
|
+
for (let k = 0; k < options.length; k += 1) {
|
|
69
|
+
const i = (start + k) % options.length;
|
|
70
|
+
if (!options[i].disabled && optionText(options[i]).toLowerCase().startsWith(text)) return i;
|
|
71
|
+
}
|
|
72
|
+
return -1;
|
|
73
|
+
};
|
|
74
|
+
// A new search starts at the current option (it may already match); a repeated letter moves on.
|
|
75
|
+
const exact = find(query, query.length > 1 && !same ? Math.max(from, 0) : from + 1);
|
|
76
|
+
if (exact >= 0) return exact;
|
|
77
|
+
return same ? find(query[0], from + 1) : -1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type SelectAction =
|
|
81
|
+
| { type: 'open'; active: number }
|
|
82
|
+
| { type: 'move'; active: number }
|
|
83
|
+
| { type: 'pick'; active: number }
|
|
84
|
+
| { type: 'close' };
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* What a key does. Closed: ↓ ↑ Enter Space (and Alt+↓) open the list at the
|
|
88
|
+
* picked option; Home / End open it at the first / last. Open: ↓ ↑ Home End
|
|
89
|
+
* Page Up / Page Down move; Enter, Space, Alt+↑ and Tab pick and close; Esc
|
|
90
|
+
* closes without picking. null: not a key the select uses.
|
|
91
|
+
*/
|
|
92
|
+
export function selectKey(
|
|
93
|
+
key: string,
|
|
94
|
+
altKey: boolean,
|
|
95
|
+
state: { open: boolean; active: number; selected: number },
|
|
96
|
+
options: OptionLike[],
|
|
97
|
+
): SelectAction | null {
|
|
98
|
+
const { open, active, selected } = state;
|
|
99
|
+
const start = selected >= 0 && !options[selected].disabled ? selected : edgeOption(options);
|
|
100
|
+
if (!open) {
|
|
101
|
+
if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(key)) return { type: 'open', active: start };
|
|
102
|
+
if (key === 'Home') return { type: 'open', active: edgeOption(options) };
|
|
103
|
+
if (key === 'End') return { type: 'open', active: edgeOption(options, true) };
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
switch (key) {
|
|
107
|
+
case 'ArrowDown': return { type: 'move', active: active < 0 ? start : stepOption(options, active, 1) };
|
|
108
|
+
case 'ArrowUp':
|
|
109
|
+
if (altKey) return { type: 'pick', active };
|
|
110
|
+
return { type: 'move', active: active < 0 ? start : stepOption(options, active, -1) };
|
|
111
|
+
case 'Home': return { type: 'move', active: edgeOption(options) };
|
|
112
|
+
case 'End': return { type: 'move', active: edgeOption(options, true) };
|
|
113
|
+
case 'PageDown': return { type: 'move', active: stepOption(options, Math.max(active, 0), 10) };
|
|
114
|
+
case 'PageUp': return { type: 'move', active: stepOption(options, Math.max(active, 0), -10) };
|
|
115
|
+
case 'Enter':
|
|
116
|
+
case ' ':
|
|
117
|
+
case 'Tab': return { type: 'pick', active };
|
|
118
|
+
case 'Escape': return { type: 'close' };
|
|
119
|
+
default: return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Remembers what was typed for half a second (typeahead). While text is pending, Space is part of the search, not a pick. */
|
|
124
|
+
export function typeahead(): { add: (key: string) => string; pending: () => boolean } {
|
|
125
|
+
let text = '';
|
|
126
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
127
|
+
return {
|
|
128
|
+
add: (key: string) => {
|
|
129
|
+
text += key;
|
|
130
|
+
if (timer) clearTimeout(timer);
|
|
131
|
+
timer = setTimeout(() => { text = ''; }, 500);
|
|
132
|
+
return text;
|
|
133
|
+
},
|
|
134
|
+
pending: () => text !== '',
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** A key that types a character (no Ctrl, Cmd or Alt). */
|
|
139
|
+
export const isPrintable = (event: { key: string; ctrlKey: boolean; metaKey: boolean; altKey: boolean }): boolean =>
|
|
140
|
+
event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey;
|
package/src/styles/theme.scss
CHANGED
|
@@ -247,7 +247,7 @@
|
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
:root {
|
|
250
|
-
--nv-font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
250
|
+
--nv-font-sans: 'Inter Variable', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
251
251
|
--nv-font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
252
252
|
--nv-type-display-size: clamp(30px, 4vw, 44px);
|
|
253
253
|
--nv-type-display-line: 1.08;
|
package/src/styles/type.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"font": {
|
|
3
|
-
"sans": "'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif",
|
|
3
|
+
"sans": "'Inter Variable', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif",
|
|
4
4
|
"mono": "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace"
|
|
5
5
|
},
|
|
6
6
|
"roles": {
|
package/src/tree.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Tree rules shared by the React and the Respawn Tree (no React here): the
|
|
2
|
+
// keys of the WAI-ARIA tree pattern, for rows that sit flat in the DOM with
|
|
3
|
+
// their depth in aria-level.
|
|
4
|
+
|
|
5
|
+
const level = (row: HTMLElement) => Number(row.getAttribute('aria-level')) || 1;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Moves focus like the WAI-ARIA tree pattern, with one row in the tab order
|
|
9
|
+
* at a time (roving tabindex):
|
|
10
|
+
* - Up / Down: the previous / next row; Home / End: the first / last;
|
|
11
|
+
* - Right: the first child row; Left: the parent row;
|
|
12
|
+
* - Enter / Space: selects the row (the same as a click).
|
|
13
|
+
* Returns true when it used the key.
|
|
14
|
+
*/
|
|
15
|
+
export function treeKeydown(event: KeyboardEvent, tree: HTMLElement): boolean {
|
|
16
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return false;
|
|
17
|
+
const rows = Array.from(tree.querySelectorAll<HTMLElement>('[role="treeitem"]'));
|
|
18
|
+
const row = (event.target as HTMLElement).closest<HTMLElement>('[role="treeitem"]');
|
|
19
|
+
// Keys typed into something inside a row (a button in a custom row) are its own.
|
|
20
|
+
if (!row || row !== event.target) return false;
|
|
21
|
+
const at = rows.indexOf(row);
|
|
22
|
+
let next: HTMLElement | undefined;
|
|
23
|
+
|
|
24
|
+
switch (event.key) {
|
|
25
|
+
case 'ArrowDown': next = rows[at + 1]; break;
|
|
26
|
+
case 'ArrowUp': next = rows[at - 1]; break;
|
|
27
|
+
case 'Home': next = rows[0]; break;
|
|
28
|
+
case 'End': next = rows[rows.length - 1]; break;
|
|
29
|
+
case 'ArrowRight': next = rows[at + 1] && level(rows[at + 1]) > level(row) ? rows[at + 1] : undefined; break;
|
|
30
|
+
case 'ArrowLeft':
|
|
31
|
+
for (let i = at - 1; i >= 0; i -= 1) if (level(rows[i]) < level(row)) { next = rows[i]; break; }
|
|
32
|
+
break;
|
|
33
|
+
case 'Enter':
|
|
34
|
+
case ' ':
|
|
35
|
+
row.click();
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
return true;
|
|
38
|
+
default: return false;
|
|
39
|
+
}
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
if (next) {
|
|
42
|
+
rows.forEach((r) => r.setAttribute('tabindex', r === next ? '0' : '-1'));
|
|
43
|
+
next.focus();
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|