@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
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import { groupOptions, selectKey, matchOption, typeahead, isPrintable, SelectAction } from '../../select';
|
|
5
|
+
import './Select.scss';
|
|
6
|
+
|
|
7
|
+
export interface SelectOption {
|
|
8
|
+
/** The value onChange reports. */
|
|
9
|
+
value: string;
|
|
10
|
+
/** What the option says. */
|
|
11
|
+
label: React.ReactNode;
|
|
12
|
+
/** A second, quieter line under the label. */
|
|
13
|
+
description?: React.ReactNode;
|
|
14
|
+
/** Options with the same group are listed together, under the group as a heading. */
|
|
15
|
+
group?: string;
|
|
16
|
+
/** Shown, but can't be picked. */
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
/** The text typing matches, when the label isn't plain text. Defaults to the label. */
|
|
19
|
+
textValue?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Where an option stands while the list is open. */
|
|
23
|
+
export interface SelectOptionState {
|
|
24
|
+
/** It is the picked value. */
|
|
25
|
+
selected: boolean;
|
|
26
|
+
/** The keys or the pointer are on it. */
|
|
27
|
+
active: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SelectProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue' | 'placeholder'> {
|
|
31
|
+
/** The options, in order (grouped ones are listed together under their group). */
|
|
32
|
+
options: SelectOption[];
|
|
33
|
+
/** The picked value. Pass it with onChange to control the select yourself. */
|
|
34
|
+
value?: string;
|
|
35
|
+
/** The value it starts with, when you don't pass value. */
|
|
36
|
+
defaultValue?: string;
|
|
37
|
+
/** Called when an option is picked (not when the same one is picked again). */
|
|
38
|
+
onChange?: (value: string, option: SelectOption) => void;
|
|
39
|
+
/** Accessible name (screen readers say it), e.g. "Theme". */
|
|
40
|
+
label: string;
|
|
41
|
+
/** Shown while nothing is picked. */
|
|
42
|
+
placeholder?: React.ReactNode;
|
|
43
|
+
/** Draws an option's content instead of its label and description. */
|
|
44
|
+
renderOption?: (option: SelectOption, state: SelectOptionState) => React.ReactNode;
|
|
45
|
+
/** Draws the picked option inside the button instead of its label. */
|
|
46
|
+
renderValue?: (option: SelectOption | undefined) => React.ReactNode;
|
|
47
|
+
/** Which edge of the button the list lines up with. */
|
|
48
|
+
align?: 'start' | 'end';
|
|
49
|
+
/** Can't be opened. */
|
|
50
|
+
disabled?: boolean;
|
|
51
|
+
/** Prefix for the ids that tie the button to its list and options. */
|
|
52
|
+
id?: string;
|
|
53
|
+
/** Custom class name. */
|
|
54
|
+
className?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Pick one value from a list: a button that opens a list of options (the
|
|
59
|
+
* WAI-ARIA select-only combobox). Focus stays on the button: ↓ ↑ move, Home
|
|
60
|
+
* and End jump, typing finds an option by its first letters, Enter or Space
|
|
61
|
+
* picks, Esc closes. Options can be grouped, and drawn your own way.
|
|
62
|
+
*/
|
|
63
|
+
export default function Select({
|
|
64
|
+
options = [],
|
|
65
|
+
value,
|
|
66
|
+
defaultValue,
|
|
67
|
+
onChange,
|
|
68
|
+
label,
|
|
69
|
+
placeholder = 'Choose…',
|
|
70
|
+
renderOption,
|
|
71
|
+
renderValue,
|
|
72
|
+
align = 'start',
|
|
73
|
+
disabled = false,
|
|
74
|
+
id = 'select',
|
|
75
|
+
className,
|
|
76
|
+
...restProps
|
|
77
|
+
}: SelectProps) {
|
|
78
|
+
const { groups, ordered } = React.useMemo(() => groupOptions(options), [options]);
|
|
79
|
+
const [inner, setInner] = React.useState(defaultValue);
|
|
80
|
+
const chosen = value !== undefined ? value : inner;
|
|
81
|
+
const selected = ordered.findIndex((o) => o.value === chosen);
|
|
82
|
+
const [open, setOpen] = React.useState(false);
|
|
83
|
+
const [active, setActive] = React.useState(-1);
|
|
84
|
+
const typed = React.useRef(typeahead());
|
|
85
|
+
const list = React.useRef<HTMLDivElement>(null);
|
|
86
|
+
const optionId = (index: number) => `${id}-option-${index}`;
|
|
87
|
+
|
|
88
|
+
// Keep the active option in view as the keys move through a long list.
|
|
89
|
+
React.useEffect(() => {
|
|
90
|
+
if (!open || active < 0 || !list.current) return;
|
|
91
|
+
const el = list.current.querySelector<HTMLElement>(`[data-index="${active}"]`);
|
|
92
|
+
if (el && el.scrollIntoView) el.scrollIntoView({ block: 'nearest' });
|
|
93
|
+
}, [open, active]);
|
|
94
|
+
|
|
95
|
+
const pick = (index: number) => {
|
|
96
|
+
const option = ordered[index];
|
|
97
|
+
if (!option || option.disabled) return;
|
|
98
|
+
if (value === undefined) setInner(option.value);
|
|
99
|
+
if (onChange && option.value !== chosen) onChange(option.value, option);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const apply = (action: SelectAction) => {
|
|
103
|
+
if (action.type === 'open') { setOpen(true); setActive(action.active); }
|
|
104
|
+
if (action.type === 'move') setActive(action.active);
|
|
105
|
+
if (action.type === 'pick') { pick(action.active); setOpen(false); }
|
|
106
|
+
if (action.type === 'close') setOpen(false);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
110
|
+
if (disabled) return;
|
|
111
|
+
const typing = isPrintable(event) && (event.key !== ' ' || typed.current.pending());
|
|
112
|
+
const action = typing ? null : selectKey(event.key, event.altKey, { open, active, selected }, ordered);
|
|
113
|
+
if (action) {
|
|
114
|
+
if (event.key !== 'Tab') event.preventDefault();
|
|
115
|
+
apply(action);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (typing) {
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
const found = matchOption(ordered, typed.current.add(event.key), open ? active : selected);
|
|
121
|
+
if (found >= 0) { setOpen(true); setActive(found); }
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const toggle = () => {
|
|
126
|
+
if (disabled) return;
|
|
127
|
+
if (open) setOpen(false);
|
|
128
|
+
else apply(selectKey('Enter', false, { open: false, active, selected }, ordered) as SelectAction);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const chosenOption = selected >= 0 ? ordered[selected] : undefined;
|
|
132
|
+
const drawOption = (option: SelectOption, index: number) => {
|
|
133
|
+
const state = { selected: index === selected, active: open && index === active };
|
|
134
|
+
return (
|
|
135
|
+
<div
|
|
136
|
+
key={ `${option.value}-${index}` }
|
|
137
|
+
className="select-option"
|
|
138
|
+
role="option"
|
|
139
|
+
id={ optionId(index) }
|
|
140
|
+
data-index={ index }
|
|
141
|
+
aria-selected={ state.selected }
|
|
142
|
+
aria-disabled={ option.disabled || undefined }
|
|
143
|
+
data-active={ state.active }
|
|
144
|
+
onMouseMove={ () => { if (!option.disabled && active !== index) setActive(index); } }
|
|
145
|
+
onClick={ () => { if (!option.disabled) apply({ type: 'pick', active: index }); } }
|
|
146
|
+
>
|
|
147
|
+
{ renderOption ? renderOption(option, state) : (
|
|
148
|
+
<span className="select-option-text">
|
|
149
|
+
<span className="select-option-label">{ option.label }</span>
|
|
150
|
+
{ option.description !== undefined && <span className="select-option-description">{ option.description }</span> }
|
|
151
|
+
</span>
|
|
152
|
+
) }
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<div
|
|
159
|
+
className={ classNames(className, 'select', CONSTANT.CLASS_PREFIX) }
|
|
160
|
+
data-open={ open }
|
|
161
|
+
data-align={ align }
|
|
162
|
+
data-disabled={ disabled || undefined }
|
|
163
|
+
{ ...restProps }
|
|
164
|
+
>
|
|
165
|
+
<div
|
|
166
|
+
className="select-trigger"
|
|
167
|
+
role="combobox"
|
|
168
|
+
id={ id }
|
|
169
|
+
tabIndex={ disabled ? -1 : 0 }
|
|
170
|
+
aria-label={ label }
|
|
171
|
+
aria-haspopup="listbox"
|
|
172
|
+
aria-expanded={ open }
|
|
173
|
+
aria-controls={ `${id}-list` }
|
|
174
|
+
aria-activedescendant={ open && active >= 0 ? optionId(active) : undefined }
|
|
175
|
+
aria-disabled={ disabled || undefined }
|
|
176
|
+
onClick={ toggle }
|
|
177
|
+
onKeyDown={ handleKeyDown }
|
|
178
|
+
onBlur={ () => setOpen(false) }
|
|
179
|
+
>
|
|
180
|
+
<span className="select-value" data-placeholder={ chosenOption ? undefined : true }>
|
|
181
|
+
{ chosenOption ? (renderValue ? renderValue(chosenOption) : chosenOption.label) : (renderValue ? renderValue(undefined) : placeholder) }
|
|
182
|
+
</span>
|
|
183
|
+
<span className="select-chevron" aria-hidden="true" />
|
|
184
|
+
</div>
|
|
185
|
+
{ /* mousedown would move focus off the button and close the list before the click lands */ }
|
|
186
|
+
<div className="select-list" role="listbox" id={ `${id}-list` } aria-label={ label } ref={ list } onMouseDown={ (e) => e.preventDefault() }>
|
|
187
|
+
{ groups.map((group, g) => (group.name ? (
|
|
188
|
+
<div key={ group.name } className="select-group" role="group" aria-labelledby={ `${id}-group-${g}` }>
|
|
189
|
+
<div className="select-group-label" id={ `${id}-group-${g}` } role="presentation">{ group.name }</div>
|
|
190
|
+
{ group.options.map(({ option, index }) => drawOption(option, index)) }
|
|
191
|
+
</div>
|
|
192
|
+
) : group.options.map(({ option, index }) => drawOption(option, index)))) }
|
|
193
|
+
</div>
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
@@ -183,10 +183,11 @@ const columns = [
|
|
|
183
183
|
textAlign: 'right'
|
|
184
184
|
},
|
|
185
185
|
render: (val, key, props) => {
|
|
186
|
-
|
|
186
|
+
// Theme roles, so the colors pass contrast in every theme.
|
|
187
|
+
let color = 'var(--nv-success)';
|
|
187
188
|
|
|
188
189
|
if (val < 0) {
|
|
189
|
-
color = '
|
|
190
|
+
color = 'var(--nv-danger)';
|
|
190
191
|
}
|
|
191
192
|
|
|
192
193
|
return (
|
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
border: 1px solid color-mix(in srgb, var(--tag-color) 45%, transparent);
|
|
10
10
|
border-radius: $--radius--full;
|
|
11
11
|
background: color-mix(in srgb, var(--tag-color) 10%, transparent);
|
|
12
|
-
color:
|
|
12
|
+
// The tone mixed with a fifth of the text color: on its own tint (and on a
|
|
13
|
+
// Panel's surface) every tone keeps 4.5:1 in every theme.
|
|
14
|
+
color: color-mix(in srgb, var(--tag-color) 80%, var(--nv-text));
|
|
13
15
|
font-size: 12px;
|
|
14
16
|
font-weight: 500;
|
|
15
17
|
line-height: 18px;
|
|
@@ -33,7 +33,7 @@ Using Text Overflow
|
|
|
33
33
|
```jsx
|
|
34
34
|
import RemoveRedEyeIcon from '@material-ui/icons/RemoveRedEye';
|
|
35
35
|
import AddIcon from '@material-ui/icons/Add';
|
|
36
|
-
import { Button, Layout,
|
|
36
|
+
import { Button, Layout, Text } from '@nurvus/ui';
|
|
37
37
|
|
|
38
38
|
const overflowStyle = {
|
|
39
39
|
textOverflow: 'ellipsis',
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
A list of rows to pick from, nested like a layers panel. Click a row, or use the keyboard: Tab reaches the active row, ↑ ↓ move, → goes to a child row, ← to its parent, Enter or Space picks.
|
|
2
|
+
```jsx
|
|
3
|
+
import { Tree } from '@nurvus/ui';
|
|
4
|
+
|
|
5
|
+
const items = [
|
|
6
|
+
{ label: 'Header', items: [{ label: 'Logo' }, { label: 'Navigation' }] },
|
|
7
|
+
{ label: 'Content', items: [{ label: 'Hero', items: [{ label: 'Title' }, { label: 'Button' }] }] },
|
|
8
|
+
{ label: 'Footer' },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const Layers = () => {
|
|
12
|
+
const [activePath, setActivePath] = React.useState([0, 1]);
|
|
13
|
+
return (
|
|
14
|
+
<div style={ { maxWidth: 280 } }>
|
|
15
|
+
<Tree
|
|
16
|
+
label="Layers"
|
|
17
|
+
activePath={ activePath }
|
|
18
|
+
items={ items }
|
|
19
|
+
onClick={ (e, path) => setActivePath(path) }
|
|
20
|
+
/>
|
|
21
|
+
</div>
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
<Layers />
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Draw your own rows with `itemRenderer`, e.g. a layer's name with its show and lock icons.
|
|
28
|
+
```jsx
|
|
29
|
+
import RemoveRedEyeIcon from '@material-ui/icons/RemoveRedEye';
|
|
30
|
+
import LockOpenIcon from '@material-ui/icons/LockOpen';
|
|
31
|
+
import { Layout, Text, Tree } from '@nurvus/ui';
|
|
32
|
+
|
|
33
|
+
const itemRenderer = ({ label }) => (
|
|
34
|
+
<Layout display="flex" alignItems="center" justifyContent="space-between">
|
|
35
|
+
<Text>{ label }</Text>
|
|
36
|
+
<Layout display="flex" alignItems="center" itemSpacing={ 4 }>
|
|
37
|
+
<RemoveRedEyeIcon fontSize="small" />
|
|
38
|
+
<LockOpenIcon fontSize="small" />
|
|
39
|
+
</Layout>
|
|
40
|
+
</Layout>
|
|
41
|
+
);
|
|
42
|
+
const items = [
|
|
43
|
+
{ label: 'Item 1', items: [{ label: 'Sub 1', items: [{ label: 'Sub Item 1' }, { label: 'Sub Item 2' }] }, { label: 'Sub 2' }] },
|
|
44
|
+
{ label: 'Item 2' },
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const MyApp = () => {
|
|
48
|
+
const [activePath, setActivePath] = React.useState([0, 0, 0]);
|
|
49
|
+
return (
|
|
50
|
+
<div style={ { maxWidth: 320 } }>
|
|
51
|
+
<Tree
|
|
52
|
+
label="Layers"
|
|
53
|
+
activePath={ activePath }
|
|
54
|
+
items={ items }
|
|
55
|
+
itemRenderer={ itemRenderer }
|
|
56
|
+
onClick={ (e, path) => setActivePath(path) }
|
|
57
|
+
/>
|
|
58
|
+
</div>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
<MyApp />
|
|
62
|
+
```
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
@import "../../styles/variables.scss";
|
|
2
|
+
@import "../../styles/tokens.scss";
|
|
3
|
+
|
|
4
|
+
.#{$cls-prefix}.tree {
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Indentation is token-driven, not a magic number in JS.
|
|
7
|
+
// Rows are FLAT siblings in the DOM (see Tree.tsx) — nesting is purely visual,
|
|
8
|
+
// produced by padding-left. Tree.tsx stamps `--nurvus-tree-depth` per row and
|
|
9
|
+
// clamps it with the `maxIndentDepth` prop, so a deep tree can never indent
|
|
10
|
+
// the label out of a narrow panel.
|
|
11
|
+
// Consumers retune the whole tree by overriding these two on .nurvus.tree.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
--nurvus-tree-indent-base: #{$--space--8};
|
|
14
|
+
--nurvus-tree-indent-step: #{$--space--12};
|
|
15
|
+
|
|
16
|
+
background: var(--nv-bg);
|
|
17
|
+
color: var(--nv-text);
|
|
18
|
+
padding: $--space--4;
|
|
19
|
+
|
|
20
|
+
// Allow the tree to shrink inside a flex/grid parent instead of forcing its
|
|
21
|
+
// intrinsic width and pushing the row content out.
|
|
22
|
+
min-width: 0;
|
|
23
|
+
|
|
24
|
+
.tree-item {
|
|
25
|
+
position: relative;
|
|
26
|
+
min-width: 0; // ...and let row content ellipsis rather than collapse to 0
|
|
27
|
+
padding: $--space--8 $--space--12;
|
|
28
|
+
padding-left: calc(
|
|
29
|
+
var(--nurvus-tree-indent-base) +
|
|
30
|
+
var(--nurvus-tree-depth, 0) * var(--nurvus-tree-indent-step)
|
|
31
|
+
);
|
|
32
|
+
border-radius: $--radius--4;
|
|
33
|
+
color: var(--nv-text);
|
|
34
|
+
cursor: pointer;
|
|
35
|
+
|
|
36
|
+
&:hover {
|
|
37
|
+
background: var(--nv-surface-hover);
|
|
38
|
+
color: var(--nv-text);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
&:focus-visible {
|
|
42
|
+
outline: 2px solid var(--nv-accent);
|
|
43
|
+
outline-offset: -2px;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Active row: 2px accent rail + a tinted wash, both from the theme's
|
|
47
|
+
// accent, so every theme keeps the same "you are here" language.
|
|
48
|
+
&[data-active="true"] {
|
|
49
|
+
background: var(--nv-accent-soft);
|
|
50
|
+
color: var(--nv-text);
|
|
51
|
+
|
|
52
|
+
&::before {
|
|
53
|
+
content: '';
|
|
54
|
+
position: absolute;
|
|
55
|
+
left: 0;
|
|
56
|
+
top: 0;
|
|
57
|
+
height: 100%;
|
|
58
|
+
width: $--space--2;
|
|
59
|
+
border-radius: $--radius--2;
|
|
60
|
+
background: var(--nv-accent);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
&:hover {
|
|
64
|
+
background: color-mix(in srgb, var(--nv-accent) 22%, transparent);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Nested groups read one step quieter than leaf rows.
|
|
69
|
+
&[data-type="group"] {
|
|
70
|
+
color: var(--nv-text-muted);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import { treeKeydown } from '../../tree';
|
|
5
|
+
import './Tree.scss';
|
|
6
|
+
|
|
7
|
+
export interface TreeItem {
|
|
8
|
+
/** What the row shows (unless itemRenderer draws it). */
|
|
9
|
+
label?: React.ReactNode;
|
|
10
|
+
/** Child rows, shown indented under this one. */
|
|
11
|
+
items?: TreeItem[];
|
|
12
|
+
/** Custom class name for this row. */
|
|
13
|
+
className?: string;
|
|
14
|
+
/** Inline style for this row. */
|
|
15
|
+
style?: React.CSSProperties;
|
|
16
|
+
/** Anything else your itemRenderer needs. */
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TreeProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick'> {
|
|
21
|
+
/** The rows to render; each can have child `items`. */
|
|
22
|
+
items?: TreeItem[];
|
|
23
|
+
/** The active row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
|
|
24
|
+
activePath?: number[];
|
|
25
|
+
/** Draws a row's content instead of its label. */
|
|
26
|
+
itemRenderer?: (item: TreeItem) => React.ReactNode;
|
|
27
|
+
/** Called when a row is clicked (or picked with Enter or Space), with the event and that row's path. */
|
|
28
|
+
onClick?: (event: React.MouseEvent<HTMLDivElement>, path: number[]) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Deepest level that still indents. Deeper rows stay at that indent, so the
|
|
31
|
+
* label keeps its room in a narrow panel.
|
|
32
|
+
*/
|
|
33
|
+
maxIndentDepth?: number;
|
|
34
|
+
/** Accessible name of the tree (screen readers say it), e.g. "Layers". */
|
|
35
|
+
label?: string;
|
|
36
|
+
/** Custom class name. */
|
|
37
|
+
className?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A nested list of rows to pick from, like a layers panel. Rows indent by
|
|
42
|
+
* depth, the active one is highlighted, and a click reports the row's path.
|
|
43
|
+
* By keyboard (the WAI-ARIA tree pattern): Tab reaches the active row, the
|
|
44
|
+
* arrow keys move between rows, Enter or Space picks one.
|
|
45
|
+
*/
|
|
46
|
+
export default function Tree({
|
|
47
|
+
activePath = [],
|
|
48
|
+
className,
|
|
49
|
+
itemRenderer,
|
|
50
|
+
items,
|
|
51
|
+
maxIndentDepth = 6,
|
|
52
|
+
onClick,
|
|
53
|
+
label = 'Tree',
|
|
54
|
+
onKeyDown,
|
|
55
|
+
children: _children,
|
|
56
|
+
...props
|
|
57
|
+
}: TreeProps) {
|
|
58
|
+
const handleClick = (event: React.MouseEvent<HTMLDivElement>, path: number[]) => {
|
|
59
|
+
event.stopPropagation();
|
|
60
|
+
if (onClick) onClick(event, path);
|
|
61
|
+
};
|
|
62
|
+
// One row is in the tab order: the active one, or else the first.
|
|
63
|
+
const activeKey = activePath.join();
|
|
64
|
+
const tabKey = activeKey && findPath(items, activePath) ? activeKey : '0';
|
|
65
|
+
|
|
66
|
+
const renderItems = (list: TreeItem[] | undefined, active: number[], path: number[] = []): React.ReactNode => {
|
|
67
|
+
if (!list) return null;
|
|
68
|
+
return list.map((item, i) => {
|
|
69
|
+
const { className: itemClassName, label: itemLabel, style } = item;
|
|
70
|
+
const type = path.length > 1 ? 'group' : '';
|
|
71
|
+
const isActive = active.length === 1 && active[0] === i;
|
|
72
|
+
const [first, ...remainingPath] = active;
|
|
73
|
+
const nextPath = [...path, i];
|
|
74
|
+
// Clamped so a deep tree cannot indent the label out of a narrow panel.
|
|
75
|
+
// The px maths lives in Tree.scss against spacing tokens; JS only supplies depth.
|
|
76
|
+
const depth = Math.min(path.length, maxIndentDepth);
|
|
77
|
+
const hasChildren = !!(item.items && item.items.length);
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<React.Fragment key={ i }>
|
|
81
|
+
<div
|
|
82
|
+
className={ classNames(itemClassName, 'tree-item', CONSTANT.CLASS_PREFIX) }
|
|
83
|
+
role="treeitem"
|
|
84
|
+
aria-level={ path.length + 1 }
|
|
85
|
+
aria-setsize={ list.length }
|
|
86
|
+
aria-posinset={ i + 1 }
|
|
87
|
+
aria-selected={ isActive }
|
|
88
|
+
aria-expanded={ hasChildren ? true : undefined }
|
|
89
|
+
tabIndex={ nextPath.join() === tabKey ? 0 : -1 }
|
|
90
|
+
data-active={ isActive }
|
|
91
|
+
data-type={ type }
|
|
92
|
+
onClick={ (e) => handleClick(e, nextPath) }
|
|
93
|
+
style={ { '--nurvus-tree-depth': String(depth), ...style } as React.CSSProperties }
|
|
94
|
+
>
|
|
95
|
+
{ itemRenderer ? itemRenderer(item) : itemLabel }
|
|
96
|
+
</div>
|
|
97
|
+
{ renderItems(item.items, first === i ? remainingPath : [], nextPath) }
|
|
98
|
+
</React.Fragment>
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<div
|
|
105
|
+
className={ classNames(className, 'tree', CONSTANT.CLASS_PREFIX) }
|
|
106
|
+
role="tree"
|
|
107
|
+
aria-label={ label }
|
|
108
|
+
{ ...props }
|
|
109
|
+
onKeyDown={ (e) => { treeKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
|
|
110
|
+
>
|
|
111
|
+
{ renderItems(items, activePath) }
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Whether `path` names a row in `items`. */
|
|
117
|
+
function findPath(items: TreeItem[] | undefined, path: number[]): boolean {
|
|
118
|
+
let list = items;
|
|
119
|
+
for (let i = 0; i < path.length; i += 1) {
|
|
120
|
+
const item = list && list[path[i]];
|
|
121
|
+
if (!item) return false;
|
|
122
|
+
list = item.items;
|
|
123
|
+
}
|
|
124
|
+
return path.length > 0;
|
|
125
|
+
}
|
package/src/css.ts
CHANGED
|
@@ -7,7 +7,9 @@ import './components/FullPageModal/FullPageModal.scss';
|
|
|
7
7
|
import './components/HistoryList/HistoryList.scss';
|
|
8
8
|
import './components/Layout/Layout.scss';
|
|
9
9
|
import './components/Link/Link.scss';
|
|
10
|
+
import './components/Tree/Tree.scss';
|
|
10
11
|
import './components/Menu/Menu.scss';
|
|
12
|
+
import './components/Select/Select.scss';
|
|
11
13
|
import './components/Panel/Panel.scss';
|
|
12
14
|
import './components/Progress/Progress.scss';
|
|
13
15
|
import './components/Table/Table.scss';
|
package/src/index.ts
CHANGED
|
@@ -22,8 +22,12 @@ export { default as Progress } from './components/Progress/Progress';
|
|
|
22
22
|
export type { ProgressProps, ProgressSize, ProgressStatus } from './components/Progress/Progress';
|
|
23
23
|
export { default as Layout } from './components/Layout/Layout';
|
|
24
24
|
export type { LayoutProps, Spacing, LegacySpacing } from './components/Layout/Layout';
|
|
25
|
+
export { default as Tree } from './components/Tree/Tree';
|
|
26
|
+
export type { TreeProps, TreeItem } from './components/Tree/Tree';
|
|
25
27
|
export { default as Menu } from './components/Menu/Menu';
|
|
26
|
-
export type { MenuProps, MenuItem } from './components/Menu/Menu';
|
|
28
|
+
export type { MenuProps, MenuItem, MenuSection } from './components/Menu/Menu';
|
|
29
|
+
export { default as Select } from './components/Select/Select';
|
|
30
|
+
export type { SelectProps, SelectOption, SelectOptionState } from './components/Select/Select';
|
|
27
31
|
export { default as FullPageModal } from './components/FullPageModal/FullPageModal';
|
|
28
32
|
export type { FullPageModalProps } from './components/FullPageModal/FullPageModal';
|
|
29
33
|
export { default as HistoryList } from './components/HistoryList/HistoryList';
|
package/src/menu.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Menu rules shared by the React and the Respawn Menu (no React here): the
|
|
2
|
+
// section ids, which sections start open, and the arrow keys.
|
|
3
|
+
|
|
4
|
+
/** A section's id: its own, or its title as a slug ("Get started" -> "get-started"). */
|
|
5
|
+
export const sectionId = (section: { id?: string; title: unknown }): string =>
|
|
6
|
+
section.id || String(section.title).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
7
|
+
|
|
8
|
+
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
9
|
+
export const startsOpen = (defaultOpen: boolean | Record<string, boolean> | undefined, id: string): boolean =>
|
|
10
|
+
typeof defaultOpen === 'boolean' ? defaultOpen : !defaultOpen || defaultOpen[id] !== false;
|
|
11
|
+
|
|
12
|
+
let warnedOldProps = false;
|
|
13
|
+
/** Menu used to be the nested, selectable list that is Tree now: say so once. */
|
|
14
|
+
export function warnOldMenuProps(props: { sections?: unknown; items?: unknown; activePath?: unknown }): boolean {
|
|
15
|
+
const old = !props.sections && (props.items !== undefined || props.activePath !== undefined);
|
|
16
|
+
if (old && !warnedOldProps && typeof console !== 'undefined') {
|
|
17
|
+
warnedOldProps = true;
|
|
18
|
+
console.warn('[@nurvus/ui] Menu is site navigation now (sections of links). The nested, selectable list it used to be is Tree: import { Tree } from \'@nurvus/ui\'. Drawing a Tree for now.');
|
|
19
|
+
}
|
|
20
|
+
return old;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The arrow keys on a Menu (the WAI-ARIA disclosure navigation pattern's
|
|
25
|
+
* optional keys). Tab still goes link by link; on top of that:
|
|
26
|
+
* - Up / Down: the previous / next row (section titles and the links in open
|
|
27
|
+
* sections), going round at the ends;
|
|
28
|
+
* - Home / End: the first / last row;
|
|
29
|
+
* - Right on a section: opens it, or (when open) goes to its first link;
|
|
30
|
+
* - Left on a section: closes it; Left on a link: goes up to its section.
|
|
31
|
+
* Returns true when it used the key.
|
|
32
|
+
*/
|
|
33
|
+
export function menuKeydown(event: KeyboardEvent, nav: HTMLElement): boolean {
|
|
34
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return false;
|
|
35
|
+
const target = event.target as HTMLElement;
|
|
36
|
+
const rows = Array.from(nav.querySelectorAll<HTMLElement>('.menu-title, .menu-item'))
|
|
37
|
+
.filter((row) => !row.closest('.menu-section[data-open="false"] .menu-items'));
|
|
38
|
+
if (!rows.length) return false;
|
|
39
|
+
const section = target.closest<HTMLElement>('.menu-section');
|
|
40
|
+
// The chevron beside a linked title counts as that title's row.
|
|
41
|
+
const onHeading = !!target.closest('.menu-heading');
|
|
42
|
+
const title = section && section.querySelector<HTMLElement>('.menu-title');
|
|
43
|
+
const at = rows.indexOf(onHeading && title ? title : target);
|
|
44
|
+
const focus = (row: HTMLElement | undefined) => { if (row) row.focus(); };
|
|
45
|
+
const toggle = () => { const button = section && section.querySelector<HTMLElement>('.menu-toggle'); if (button) button.click(); };
|
|
46
|
+
const isOpen = !!section && section.getAttribute('data-open') !== 'false';
|
|
47
|
+
|
|
48
|
+
switch (event.key) {
|
|
49
|
+
case 'ArrowDown': focus(rows[(at + 1) % rows.length]); break;
|
|
50
|
+
case 'ArrowUp': focus(rows[at <= 0 ? rows.length - 1 : at - 1]); break;
|
|
51
|
+
case 'Home': focus(rows[0]); break;
|
|
52
|
+
case 'End': focus(rows[rows.length - 1]); break;
|
|
53
|
+
case 'ArrowRight':
|
|
54
|
+
if (!onHeading || !section) return false;
|
|
55
|
+
if (!isOpen) toggle();
|
|
56
|
+
else focus(section.querySelector<HTMLElement>('.menu-item') || undefined);
|
|
57
|
+
break;
|
|
58
|
+
case 'ArrowLeft':
|
|
59
|
+
if (!section) return false;
|
|
60
|
+
if (onHeading) { if (isOpen) toggle(); } else focus(title || undefined);
|
|
61
|
+
break;
|
|
62
|
+
default: return false;
|
|
63
|
+
}
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
return true;
|
|
66
|
+
}
|