@nurvus/ui 0.1.0-alpha.13 → 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.
Files changed (49) hide show
  1. package/README.md +5 -0
  2. package/dist/index.js +1 -1
  3. package/dist/nurvus.css +6 -5
  4. package/dist/respawn.js +1 -1
  5. package/dist/types/components/Menu/Menu.d.ts +35 -27
  6. package/dist/types/components/Select/Select.d.ts +55 -0
  7. package/dist/types/components/Tree/Tree.d.ts +39 -0
  8. package/dist/types/index.d.ts +5 -3
  9. package/dist/types/menu.d.ts +24 -0
  10. package/dist/types/respawn/Menu.d.ts +36 -26
  11. package/dist/types/respawn/Select.d.ts +54 -0
  12. package/dist/types/respawn/Tree.d.ts +36 -0
  13. package/dist/types/respawn/index.d.ts +5 -3
  14. package/dist/types/select.d.ts +70 -0
  15. package/dist/types/tree.d.ts +9 -0
  16. package/package.json +2 -2
  17. package/src/components/Button/Button.scss +8 -2
  18. package/src/components/Link/Link.md +11 -2
  19. package/src/components/Menu/Menu.md +32 -126
  20. package/src/components/Menu/Menu.scss +133 -51
  21. package/src/components/Menu/Menu.tsx +118 -67
  22. package/src/components/Select/Select.md +72 -0
  23. package/src/components/Select/Select.scss +170 -0
  24. package/src/components/Select/Select.tsx +196 -0
  25. package/src/components/Table/Table.md +3 -2
  26. package/src/components/Tag/Tag.scss +3 -1
  27. package/src/components/Text/Text.md +1 -1
  28. package/src/components/Tree/Tree.md +62 -0
  29. package/src/components/Tree/Tree.scss +73 -0
  30. package/src/components/Tree/Tree.tsx +125 -0
  31. package/src/css.ts +2 -1
  32. package/src/index.ts +5 -3
  33. package/src/menu.ts +66 -0
  34. package/src/respawn/Menu.ts +113 -62
  35. package/src/respawn/Select.ts +212 -0
  36. package/src/respawn/Tree.ts +116 -0
  37. package/src/respawn/index.ts +5 -3
  38. package/src/select.ts +140 -0
  39. package/src/styles/theme.scss +1 -1
  40. package/src/styles/type.json +1 -1
  41. package/src/tree.ts +46 -0
  42. package/dist/types/components/NavList/NavList.d.ts +0 -43
  43. package/dist/types/navList.d.ts +0 -7
  44. package/dist/types/respawn/NavList.d.ts +0 -43
  45. package/src/components/NavList/NavList.md +0 -39
  46. package/src/components/NavList/NavList.scss +0 -149
  47. package/src/components/NavList/NavList.tsx +0 -129
  48. package/src/navList.ts +0 -9
  49. package/src/respawn/NavList.ts +0 -126
@@ -0,0 +1,170 @@
1
+ @import "../../styles/variables.scss";
2
+
3
+ // Pick one value: a button and the list it opens. The list stays in the page
4
+ // (the button's aria-controls points at it) and fades and slides in.
5
+ $select-speed: 0.12s;
6
+
7
+ .#{$cls-prefix}.select {
8
+ position: relative;
9
+ display: inline-block;
10
+ max-width: 100%;
11
+ font-size: 14px;
12
+ line-height: 20px;
13
+
14
+ .select-trigger {
15
+ display: inline-flex;
16
+ align-items: center;
17
+ gap: var(--space--8);
18
+ max-width: 100%;
19
+ min-height: 32px;
20
+ padding: var(--space--4) var(--space--8) var(--space--4) var(--space--12);
21
+ border: 1px solid var(--nv-border-strong);
22
+ border-radius: 8px;
23
+ background: var(--nv-surface);
24
+ color: var(--nv-text);
25
+ cursor: pointer;
26
+ user-select: none;
27
+
28
+ &:hover { background: var(--nv-surface-hover); }
29
+ &:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: 2px; }
30
+ }
31
+
32
+ &[data-disabled] .select-trigger { opacity: 0.5; cursor: not-allowed; }
33
+
34
+ .select-value {
35
+ display: inline-flex;
36
+ align-items: center;
37
+ gap: var(--space--8);
38
+ min-width: 0;
39
+ overflow: hidden;
40
+ white-space: nowrap;
41
+ text-overflow: ellipsis;
42
+
43
+ &[data-placeholder] { color: var(--nv-text-muted); }
44
+ }
45
+
46
+ // The same turned-corner chevron as Menu: down when closed, up when open.
47
+ .select-chevron {
48
+ display: inline-flex;
49
+ flex: none;
50
+ width: 16px;
51
+ justify-content: center;
52
+
53
+ &::after {
54
+ content: '';
55
+ width: 6px;
56
+ height: 6px;
57
+ border-right: 1.5px solid currentColor;
58
+ border-bottom: 1.5px solid currentColor;
59
+ transform: translateY(-2px) rotate(45deg);
60
+ transition: transform $select-speed ease;
61
+ }
62
+ }
63
+
64
+ &[data-open="true"] .select-chevron::after { transform: translateY(2px) rotate(225deg); }
65
+
66
+ .select-list {
67
+ position: absolute;
68
+ z-index: 1000;
69
+ top: calc(100% + var(--space--4));
70
+ left: 0;
71
+ min-width: 100%;
72
+ max-height: 360px;
73
+ overflow: auto;
74
+ padding: var(--space--4);
75
+ border: 1px solid var(--nv-border);
76
+ border-radius: 10px;
77
+ background: var(--nv-surface);
78
+ color: var(--nv-text);
79
+ box-shadow: 0 12px 32px rgb(0 0 0 / 0.24);
80
+ opacity: 0;
81
+ visibility: hidden;
82
+ transform: translateY(-4px) scale(0.98);
83
+ transform-origin: top left;
84
+ transition: opacity $select-speed ease, transform $select-speed ease, visibility 0s $select-speed;
85
+ }
86
+
87
+ &[data-align="end"] .select-list { left: auto; right: 0; transform-origin: top right; }
88
+
89
+ &[data-open="true"] .select-list {
90
+ opacity: 1;
91
+ visibility: visible;
92
+ transform: none;
93
+ transition: opacity $select-speed ease, transform $select-speed ease, visibility 0s 0s;
94
+ }
95
+
96
+ .select-group + .select-group,
97
+ .select-option + .select-group {
98
+ margin-top: var(--space--4);
99
+ padding-top: var(--space--4);
100
+ border-top: 1px solid var(--nv-border);
101
+ }
102
+
103
+ .select-group-label {
104
+ padding: var(--space--4) var(--space--8);
105
+ color: var(--nv-text-muted);
106
+ font-size: 12px;
107
+ line-height: 16px;
108
+ font-weight: 600;
109
+ letter-spacing: 0.04em;
110
+ text-transform: uppercase;
111
+ }
112
+
113
+ .select-option {
114
+ position: relative;
115
+ display: flex;
116
+ align-items: center;
117
+ gap: var(--space--8);
118
+ padding: 6px 32px 6px var(--space--8); // room on the right for the check
119
+ border-radius: 6px;
120
+ cursor: pointer;
121
+ white-space: nowrap;
122
+
123
+ // Where the keys (or the pointer) are: a wash and an accent bar, so it
124
+ // reads as focus even though focus stays on the button.
125
+ &[data-active="true"] {
126
+ background: var(--nv-surface-hover);
127
+ box-shadow: inset 2px 0 0 var(--nv-accent);
128
+ }
129
+
130
+ // The picked value: tinted, with a check.
131
+ &[aria-selected="true"] {
132
+ background: var(--nv-accent-soft);
133
+ font-weight: 500;
134
+
135
+ &::after {
136
+ content: '';
137
+ position: absolute;
138
+ top: 50%;
139
+ right: 12px;
140
+ width: 5px;
141
+ height: 10px;
142
+ border-right: 2px solid var(--nv-accent);
143
+ border-bottom: 2px solid var(--nv-accent);
144
+ transform: translateY(-60%) rotate(45deg);
145
+ }
146
+ }
147
+
148
+ &[aria-selected="true"][data-active="true"] { background: color-mix(in srgb, var(--nv-accent) 22%, transparent); }
149
+ &[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }
150
+ }
151
+
152
+ .select-option-text {
153
+ display: flex;
154
+ flex-direction: column;
155
+ min-width: 0;
156
+ }
157
+
158
+ .select-option-description {
159
+ color: var(--nv-text-muted);
160
+ font-size: 12px;
161
+ line-height: 16px;
162
+ font-weight: 400;
163
+ }
164
+
165
+ @media (prefers-reduced-motion: reduce) {
166
+ .select-list,
167
+ .select-chevron::after,
168
+ &[data-open="true"] .select-list { transition: none; }
169
+ }
170
+ }
@@ -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
- let color = 'rgba(4, 170, 53, 1)';
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 = 'rgba(239, 2, 9, 1)';
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: var(--tag-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, Menu, Text } from '@nurvus/ui';
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,8 +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';
11
- import './components/NavList/NavList.scss';
12
+ import './components/Select/Select.scss';
12
13
  import './components/Panel/Panel.scss';
13
14
  import './components/Progress/Progress.scss';
14
15
  import './components/Table/Table.scss';