@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.
Files changed (41) hide show
  1. package/README.md +5 -0
  2. package/dist/index.js +1 -1
  3. package/dist/nurvus.css +6 -4
  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 -1
  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 -1
  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 -0
  32. package/src/index.ts +5 -1
  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 -1
  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
@@ -1,86 +1,137 @@
1
1
  import classNames from 'classnames';
2
2
  import CONSTANT from '../constants';
3
- import { spawn, Child, ElementProps, Style } from './spawn';
3
+ import { sectionId, startsOpen, menuKeydown, warnOldMenuProps } from '../menu';
4
+ import Tree from './Tree';
5
+ import { spawn, Child, ElementProps } from './spawn';
4
6
  import { updatable, NurvusElement } from './update';
5
7
  import '../components/Menu/Menu.scss';
6
8
 
7
9
  export interface MenuItem {
8
- /** What the row shows (unless itemRenderer draws it). */
9
- label?: Child;
10
- /** Child rows, shown indented under this one. */
11
- items?: MenuItem[];
12
- /** Custom class name for this row. */
13
- className?: string;
14
- /** Inline style for this row. */
15
- style?: Style;
16
- /** Anything else your itemRenderer needs. */
17
- [key: string]: any;
10
+ /** What the link says. */
11
+ label: Child;
12
+ /** Where it goes. The item whose href equals `current` is marked as the current page. */
13
+ href: string;
14
+ /** Draws a hairline above this item, to split a section into groups. */
15
+ divider?: boolean;
16
+ }
17
+
18
+ export interface MenuSection {
19
+ /** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
20
+ id?: string;
21
+ /** The section heading. */
22
+ title: string;
23
+ /** Makes the heading a link too (e.g. to the section's overview page). */
24
+ href?: string;
25
+ /** The section's links. */
26
+ items: MenuItem[];
18
27
  }
19
28
 
20
29
  export interface MenuProps extends ElementProps {
21
- /** The rows to render; each can have child `items`. */
22
- items?: MenuItem[];
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: MenuItem) => Child;
27
- /** Called on a row click with the event and that row's path, after the row is marked active. */
28
- onClick?: (event: MouseEvent, path: number[]) => void;
29
- /** Deepest level that still indents. Deeper rows stay at that indent. */
30
- maxIndentDepth?: number;
30
+ /** The sections, in order. */
31
+ sections: MenuSection[];
32
+ /** The href of the page being shown: its link is marked, and its section opens. */
33
+ current?: string;
34
+ /** Which sections start open: true (all), false (none) or a map of section id -> open. Defaults to all. */
35
+ defaultOpen?: boolean | Record<string, boolean>;
36
+ /** Called when a section opens or closes (e.g. to remember it). */
37
+ onToggle?: (id: string, open: boolean) => void;
38
+ /** Called when any link is clicked, with its href (e.g. to close a drawer). */
39
+ onNavigate?: (href: string, event: MouseEvent) => void;
40
+ /** Accessible name of the navigation landmark. */
41
+ label?: string;
42
+ /** Prefix for the ids that link each toggle to its list. Defaults to "menu". */
43
+ id?: string;
31
44
  }
32
45
 
33
46
  /**
34
- * A nested list of rows, like a layers panel. Rows indent by depth and the
35
- * active one is highlighted. A click marks that row active (in place: the
36
- * element stays the same) and calls onClick(event, path). To set the active
37
- * row from code: menu.update({ activePath: [1] }).
47
+ * Site navigation: sections of links that open and close (an accordion, with
48
+ * a smooth animation), and the current page marked by a bar beside it. A
49
+ * click on a toggle opens or closes its section in place (the element stays
50
+ * the same); update({ current }) marks another page and opens its section.
38
51
  */
39
- export default function Menu(props: MenuProps = {}): NurvusElement<HTMLDivElement, MenuProps> {
52
+ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement<HTMLElement, MenuProps> {
53
+ // The props Menu took before it was navigation (Tree's) still draw a Tree, with a warning.
54
+ if (warnOldMenuProps(props || {})) return Tree(props as any) as any;
40
55
  return updatable(({
41
- activePath = [], className, itemRenderer, items, maxIndentDepth = 6, onClick, children: _children, ...rest
56
+ sections = [], current, defaultOpen = true, onToggle, onNavigate, label = 'Pages', id = 'menu', className, ...rest
42
57
  }: MenuProps, keep) => {
43
- const rows: { el: HTMLElement; path: string }[] = [];
44
- const markActive = (path: number[]) => {
45
- const key = path.join();
46
- rows.forEach((row) => row.el.setAttribute('data-active', String(row.path === key)));
47
- };
58
+ const open: Record<string, boolean> = Object.fromEntries(sections.map((s) => [sectionId(s), startsOpen(defaultOpen, sectionId(s))]));
59
+ // The current page's section always starts open, as in React.
60
+ const home = sections.find((s) => s.items.some((item) => item.href === current));
61
+ if (home) open[sectionId(home)] = true;
48
62
 
49
- const renderItems = (list: MenuItem[] | undefined, active: number[], path: number[] = []): HTMLElement[] => {
50
- if (!list) return [];
51
- return list.flatMap((item, i) => {
52
- const { className: itemClassName, label, style } = item;
53
- const type = path.length > 1 ? 'group' : '';
54
- const isActive = active.length === 1 && active[0] === i;
55
- const [first, ...remainingPath] = active;
56
- const nextPath = [...path, i];
57
- // Clamped so a deep tree cannot indent the label out of a narrow panel (see Menu.scss).
58
- const depth = Math.min(path.length, maxIndentDepth);
59
- const el = spawn({
60
- className: classNames(itemClassName, 'menu-item', CONSTANT.CLASS_PREFIX),
61
- 'data-active': isActive,
62
- 'data-type': type,
63
- style: { '--nurvus-menu-depth': String(depth), ...style },
64
- events: {
65
- click: (event) => {
66
- event.stopPropagation();
67
- keep({ activePath: nextPath });
68
- markActive(nextPath);
69
- if (onClick) onClick(event, nextPath);
70
- },
71
- },
72
- children: itemRenderer ? itemRenderer(item) : label,
63
+ const navigate = (href: string) => ({ click: (event: MouseEvent) => { if (onNavigate) onNavigate(href, event); } });
64
+
65
+ const renderSection = (section: MenuSection): HTMLElement => {
66
+ const sid = sectionId(section);
67
+ const listId = `${id}-${sid}`;
68
+ const toggles: HTMLElement[] = [];
69
+ const toggle = () => {
70
+ open[sid] = !open[sid];
71
+ keep({ defaultOpen: { ...open } });
72
+ el.setAttribute('data-open', String(open[sid]));
73
+ toggles.forEach((t) => {
74
+ t.setAttribute('aria-expanded', String(open[sid]));
75
+ if (t.hasAttribute('aria-label')) t.setAttribute('aria-label', `${open[sid] ? 'Hide' : 'Show'} ${section.title}`);
73
76
  });
74
- rows.push({ el, path: nextPath.join() });
75
- return [el, ...renderItems(item.items, first === i ? remainingPath : [], nextPath)];
77
+ if (onToggle) onToggle(sid, open[sid]);
78
+ };
79
+ const toggleProps = (extra: Record<string, unknown>) => ({
80
+ tag: 'button', type: 'button', 'aria-expanded': open[sid], 'aria-controls': listId, events: { click: toggle }, ...extra,
81
+ });
82
+ const heading = section.href
83
+ ? [
84
+ spawn({ tag: 'a', className: 'menu-title', href: section.href, 'data-active': current === section.href, events: navigate(section.href), children: section.title }),
85
+ spawn(toggleProps({ className: 'menu-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${section.title}` })),
86
+ ]
87
+ : [spawn(toggleProps({ className: 'menu-title menu-toggle', children: section.title }))];
88
+ heading.forEach((h) => { if (h.tagName === 'BUTTON') toggles.push(h); });
89
+
90
+ const el: HTMLElement = spawn({
91
+ className: 'menu-section',
92
+ 'data-section': sid,
93
+ 'data-open': open[sid],
94
+ children: [
95
+ spawn({ className: 'menu-heading', children: heading }),
96
+ spawn({
97
+ className: 'menu-items',
98
+ id: listId,
99
+ children: spawn({
100
+ tag: 'ul',
101
+ children: section.items.map((item) => spawn({
102
+ tag: 'li',
103
+ 'data-divider': item.divider || undefined,
104
+ children: spawn({
105
+ tag: 'a',
106
+ className: 'menu-item',
107
+ href: item.href,
108
+ 'data-active': current === item.href,
109
+ 'aria-current': current === item.href ? 'page' : undefined,
110
+ events: navigate(item.href),
111
+ children: item.label,
112
+ }),
113
+ })),
114
+ }),
115
+ }),
116
+ ],
76
117
  });
118
+ return el;
77
119
  };
78
120
 
79
- return spawn<HTMLDivElement>({
121
+ const { events, ...others } = rest;
122
+ return spawn<HTMLElement>({
123
+ tag: 'nav',
80
124
  className: classNames(className, 'menu', CONSTANT.CLASS_PREFIX),
81
- ...rest,
82
- tag: 'div',
83
- children: renderItems(items, activePath),
125
+ 'aria-label': label,
126
+ ...others,
127
+ events: {
128
+ ...events,
129
+ keydown: (event: KeyboardEvent, el: HTMLElement) => {
130
+ menuKeydown(event, el);
131
+ if (events && events.keydown) events.keydown(event, el);
132
+ },
133
+ },
134
+ children: sections.map(renderSection),
84
135
  });
85
136
  }, props);
86
137
  }
@@ -0,0 +1,212 @@
1
+ import classNames from 'classnames';
2
+ import CONSTANT from '../constants';
3
+ import { groupOptions, selectKey, matchOption, typeahead, isPrintable, SelectAction } from '../select';
4
+ import { spawn, Child, ElementProps } from './spawn';
5
+ import { updatable, NurvusElement } from './update';
6
+ import '../components/Select/Select.scss';
7
+
8
+ export interface SelectOption {
9
+ /** The value onChange reports. */
10
+ value: string;
11
+ /** What the option says. */
12
+ label: Child;
13
+ /** A second, quieter line under the label. */
14
+ description?: Child;
15
+ /** Options with the same group are listed together, under the group as a heading. */
16
+ group?: string;
17
+ /** Shown, but can't be picked. */
18
+ disabled?: boolean;
19
+ /** The text typing matches, when the label isn't plain text. Defaults to the label. */
20
+ textValue?: string;
21
+ }
22
+
23
+ /** Where an option stands when it is drawn. */
24
+ export interface SelectOptionState {
25
+ /** It is the picked value. */
26
+ selected: boolean;
27
+ /** The keys or the pointer are on it. */
28
+ active: boolean;
29
+ }
30
+
31
+ export interface SelectProps extends ElementProps {
32
+ /** The options, in order (grouped ones are listed together under their group). */
33
+ options: SelectOption[];
34
+ /** The picked value. */
35
+ value?: string;
36
+ /** The value it starts with (the same as value here: the element keeps its own value). */
37
+ defaultValue?: string;
38
+ /** Called when an option is picked (not when the same one is picked again), after it is shown as picked. */
39
+ onChange?: (value: string, option: SelectOption) => void;
40
+ /** Accessible name (screen readers say it), e.g. "Theme". */
41
+ label: string;
42
+ /** Shown while nothing is picked. */
43
+ placeholder?: Child;
44
+ /** Draws an option's content instead of its label and description (once, when the element is built). */
45
+ renderOption?: (option: SelectOption, state: SelectOptionState) => Child;
46
+ /** Draws the picked option inside the button instead of its label. */
47
+ renderValue?: (option: SelectOption | undefined) => Child;
48
+ /** Which edge of the button the list lines up with. */
49
+ align?: 'start' | 'end';
50
+ /** Can't be opened. */
51
+ disabled?: boolean;
52
+ /** Prefix for the ids that tie the button to its list and options. */
53
+ id?: string;
54
+ }
55
+
56
+ /**
57
+ * Pick one value from a list: a button that opens a list of options (the
58
+ * WAI-ARIA select-only combobox), with the same keys as the React Select.
59
+ * Opening, moving and picking change the element in place; picking calls
60
+ * onChange(value, option). To set the value from code: select.update({ value }).
61
+ */
62
+ export default function Select(props: SelectProps): NurvusElement<HTMLDivElement, SelectProps> {
63
+ return updatable(({
64
+ options = [], value, defaultValue, onChange, label, placeholder = 'Choose…', renderOption, renderValue,
65
+ align = 'start', disabled = false, id = 'select', className, ...rest
66
+ }: SelectProps, keep) => {
67
+ const { groups, ordered } = groupOptions(options);
68
+ let chosen = value !== undefined ? value : defaultValue;
69
+ let selected = ordered.findIndex((o) => o.value === chosen);
70
+ let open = false;
71
+ let active = -1;
72
+ const typed = typeahead();
73
+ const optionId = (index: number) => `${id}-option-${index}`;
74
+ const optionEls: HTMLElement[] = [];
75
+
76
+ const drawValue = () => {
77
+ const option = selected >= 0 ? ordered[selected] : undefined;
78
+ return spawn({
79
+ tag: 'span',
80
+ className: 'select-value',
81
+ 'data-placeholder': option ? undefined : true,
82
+ children: option ? (renderValue ? renderValue(option) : option.label) : (renderValue ? renderValue(undefined) : placeholder),
83
+ });
84
+ };
85
+
86
+ const setActive = (index: number) => {
87
+ active = index;
88
+ optionEls.forEach((el, i) => el.setAttribute('data-active', String(open && i === active)));
89
+ if (open && active >= 0) {
90
+ trigger.setAttribute('aria-activedescendant', optionId(active));
91
+ const el = optionEls[active];
92
+ if (el && el.scrollIntoView) el.scrollIntoView({ block: 'nearest' });
93
+ } else trigger.removeAttribute('aria-activedescendant');
94
+ };
95
+ const setOpen = (next: boolean) => {
96
+ open = next;
97
+ root.setAttribute('data-open', String(open));
98
+ trigger.setAttribute('aria-expanded', String(open));
99
+ setActive(active);
100
+ };
101
+ const pick = (index: number) => {
102
+ const option = ordered[index];
103
+ if (!option || option.disabled) return;
104
+ const changed = option.value !== chosen;
105
+ chosen = option.value;
106
+ selected = index;
107
+ keep({ value: chosen });
108
+ optionEls.forEach((el, i) => el.setAttribute('aria-selected', String(i === selected)));
109
+ valueEl.replaceWith(valueEl = drawValue());
110
+ if (onChange && changed) onChange(option.value, option);
111
+ };
112
+ const apply = (action: SelectAction) => {
113
+ if (action.type === 'open') { active = action.active; setOpen(true); }
114
+ if (action.type === 'move') setActive(action.active);
115
+ if (action.type === 'pick') { pick(action.active); setOpen(false); }
116
+ if (action.type === 'close') setOpen(false);
117
+ };
118
+
119
+ const drawOption = (option: SelectOption, index: number) => {
120
+ const el = spawn({
121
+ className: 'select-option',
122
+ role: 'option',
123
+ id: optionId(index),
124
+ 'data-index': index,
125
+ 'aria-selected': index === selected,
126
+ 'aria-disabled': option.disabled || undefined,
127
+ 'data-active': false,
128
+ events: {
129
+ mousemove: () => { if (!option.disabled && active !== index) setActive(index); },
130
+ click: () => { if (!option.disabled) apply({ type: 'pick', active: index }); },
131
+ },
132
+ children: renderOption ? renderOption(option, { selected: index === selected, active: false }) : spawn({
133
+ tag: 'span',
134
+ className: 'select-option-text',
135
+ children: [
136
+ spawn({ tag: 'span', className: 'select-option-label', children: option.label }),
137
+ option.description !== undefined ? spawn({ tag: 'span', className: 'select-option-description', children: option.description }) : null,
138
+ ],
139
+ }),
140
+ });
141
+ optionEls[index] = el;
142
+ return el;
143
+ };
144
+
145
+ let valueEl = drawValue();
146
+ const trigger: HTMLElement = spawn({
147
+ className: 'select-trigger',
148
+ role: 'combobox',
149
+ id,
150
+ tabIndex: disabled ? -1 : 0,
151
+ 'aria-label': label,
152
+ 'aria-haspopup': 'listbox',
153
+ 'aria-expanded': false,
154
+ 'aria-controls': `${id}-list`,
155
+ 'aria-disabled': disabled || undefined,
156
+ events: {
157
+ click: () => {
158
+ if (disabled) return;
159
+ if (open) setOpen(false);
160
+ else apply(selectKey('Enter', false, { open: false, active, selected }, ordered) as SelectAction);
161
+ },
162
+ keydown: (event: KeyboardEvent) => {
163
+ if (disabled) return;
164
+ const typing = isPrintable(event) && (event.key !== ' ' || typed.pending());
165
+ const action = typing ? null : selectKey(event.key, event.altKey, { open, active, selected }, ordered);
166
+ if (action) {
167
+ if (event.key !== 'Tab') event.preventDefault();
168
+ apply(action);
169
+ return;
170
+ }
171
+ if (typing) {
172
+ event.preventDefault();
173
+ const found = matchOption(ordered, typed.add(event.key), open ? active : selected);
174
+ if (found >= 0) { active = found; setOpen(true); }
175
+ }
176
+ },
177
+ blur: () => { if (open) setOpen(false); },
178
+ },
179
+ children: [valueEl, spawn({ tag: 'span', className: 'select-chevron', 'aria-hidden': true })],
180
+ });
181
+
182
+ const root: HTMLDivElement = spawn<HTMLDivElement>({
183
+ className: classNames(className, 'select', CONSTANT.CLASS_PREFIX),
184
+ 'data-open': false,
185
+ 'data-align': align,
186
+ 'data-disabled': disabled || undefined,
187
+ ...rest,
188
+ tag: 'div',
189
+ children: [
190
+ trigger,
191
+ spawn({
192
+ className: 'select-list',
193
+ role: 'listbox',
194
+ id: `${id}-list`,
195
+ 'aria-label': label,
196
+ // mousedown would move focus off the button and close the list before the click lands
197
+ events: { mousedown: (event: MouseEvent) => event.preventDefault() },
198
+ children: groups.map((group, g) => (group.name ? spawn({
199
+ className: 'select-group',
200
+ role: 'group',
201
+ 'aria-labelledby': `${id}-group-${g}`,
202
+ children: [
203
+ spawn({ className: 'select-group-label', id: `${id}-group-${g}`, role: 'presentation', children: group.name }),
204
+ ...group.options.map(({ option, index }) => drawOption(option, index)),
205
+ ],
206
+ }) : group.options.map(({ option, index }) => drawOption(option, index)))),
207
+ }),
208
+ ],
209
+ });
210
+ return root;
211
+ }, props);
212
+ }
@@ -0,0 +1,116 @@
1
+ import classNames from 'classnames';
2
+ import CONSTANT from '../constants';
3
+ import { treeKeydown } from '../tree';
4
+ import { spawn, Child, ElementProps, Style } from './spawn';
5
+ import { updatable, NurvusElement } from './update';
6
+ import '../components/Tree/Tree.scss';
7
+
8
+ export interface TreeItem {
9
+ /** What the row shows (unless itemRenderer draws it). */
10
+ label?: Child;
11
+ /** Child rows, shown indented under this one. */
12
+ items?: TreeItem[];
13
+ /** Custom class name for this row. */
14
+ className?: string;
15
+ /** Inline style for this row. */
16
+ style?: Style;
17
+ /** Anything else your itemRenderer needs. */
18
+ [key: string]: any;
19
+ }
20
+
21
+ export interface TreeProps extends ElementProps {
22
+ /** The rows to render; each can have child `items`. */
23
+ items?: TreeItem[];
24
+ /** The active row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
25
+ activePath?: number[];
26
+ /** Draws a row's content instead of its label. */
27
+ itemRenderer?: (item: TreeItem) => Child;
28
+ /** Called when a row is clicked (or picked with Enter or Space), after it is marked active, with the event and its path. */
29
+ onClick?: (event: MouseEvent, path: number[]) => void;
30
+ /** Deepest level that still indents. Deeper rows stay at that indent. */
31
+ maxIndentDepth?: number;
32
+ /** Accessible name of the tree (screen readers say it), e.g. "Layers". */
33
+ label?: string;
34
+ }
35
+
36
+ /**
37
+ * A nested list of rows to pick from, like a layers panel. Rows indent by
38
+ * depth and the active one is highlighted. A click (or Enter / Space) marks
39
+ * that row active (in place: the element stays the same) and calls
40
+ * onClick(event, path); the arrow keys move between rows. To set the active
41
+ * row from code: tree.update({ activePath: [1] }).
42
+ */
43
+ export default function Tree(props: TreeProps = {}): NurvusElement<HTMLDivElement, TreeProps> {
44
+ return updatable(({
45
+ activePath = [], className, itemRenderer, items, maxIndentDepth = 6, onClick, label = 'Tree', children: _children, ...rest
46
+ }: TreeProps, keep) => {
47
+ const rows: { el: HTMLElement; path: string }[] = [];
48
+ const markActive = (path: number[]) => {
49
+ const key = path.join();
50
+ rows.forEach((row) => {
51
+ row.el.setAttribute('data-active', String(row.path === key));
52
+ row.el.setAttribute('aria-selected', String(row.path === key));
53
+ row.el.setAttribute('tabindex', row.path === key ? '0' : '-1');
54
+ });
55
+ };
56
+
57
+ const renderItems = (list: TreeItem[] | undefined, active: number[], path: number[] = []): HTMLElement[] => {
58
+ if (!list) return [];
59
+ return list.flatMap((item, i) => {
60
+ const { className: itemClassName, label: itemLabel, style } = item;
61
+ const type = path.length > 1 ? 'group' : '';
62
+ const isActive = active.length === 1 && active[0] === i;
63
+ const [first, ...remainingPath] = active;
64
+ const nextPath = [...path, i];
65
+ // Clamped so a deep tree cannot indent the label out of a narrow panel (see Tree.scss).
66
+ const depth = Math.min(path.length, maxIndentDepth);
67
+ const el = spawn({
68
+ className: classNames(itemClassName, 'tree-item', CONSTANT.CLASS_PREFIX),
69
+ role: 'treeitem',
70
+ 'aria-level': path.length + 1,
71
+ 'aria-setsize': list.length,
72
+ 'aria-posinset': i + 1,
73
+ 'aria-selected': isActive,
74
+ 'aria-expanded': item.items && item.items.length ? true : undefined,
75
+ tabIndex: -1,
76
+ 'data-active': isActive,
77
+ 'data-type': type,
78
+ style: { '--nurvus-tree-depth': String(depth), ...style },
79
+ events: {
80
+ click: (event) => {
81
+ event.stopPropagation();
82
+ keep({ activePath: nextPath });
83
+ markActive(nextPath);
84
+ if (onClick) onClick(event, nextPath);
85
+ },
86
+ },
87
+ children: itemRenderer ? itemRenderer(item) : itemLabel,
88
+ });
89
+ rows.push({ el, path: nextPath.join() });
90
+ return [el, ...renderItems(item.items, first === i ? remainingPath : [], nextPath)];
91
+ });
92
+ };
93
+
94
+ const children = renderItems(items, activePath);
95
+ // One row is in the tab order: the active one, or else the first (as in React).
96
+ const stop = rows.find((row) => row.path === activePath.join() && activePath.length) || rows[0];
97
+ if (stop) stop.el.setAttribute('tabindex', '0');
98
+
99
+ const { events, ...others } = rest;
100
+ return spawn<HTMLDivElement>({
101
+ className: classNames(className, 'tree', CONSTANT.CLASS_PREFIX),
102
+ role: 'tree',
103
+ 'aria-label': label,
104
+ ...others,
105
+ events: {
106
+ ...events,
107
+ keydown: (event: KeyboardEvent, el: HTMLElement) => {
108
+ treeKeydown(event, el);
109
+ if (events && events.keydown) events.keydown(event, el);
110
+ },
111
+ },
112
+ tag: 'div',
113
+ children,
114
+ });
115
+ }, props);
116
+ }
@@ -39,8 +39,12 @@ export { default as Progress } from './Progress';
39
39
  export type { ProgressProps, ProgressSize, ProgressStatus } from './Progress';
40
40
  export { default as Layout } from './Layout';
41
41
  export type { LayoutProps, Spacing, LegacySpacing } from './Layout';
42
+ export { default as Tree } from './Tree';
43
+ export type { TreeProps, TreeItem } from './Tree';
42
44
  export { default as Menu } from './Menu';
43
- export type { MenuProps, MenuItem } from './Menu';
45
+ export type { MenuProps, MenuItem, MenuSection } from './Menu';
46
+ export { default as Select } from './Select';
47
+ export type { SelectProps, SelectOption, SelectOptionState } from './Select';
44
48
  export { default as FullPageModal } from './FullPageModal';
45
49
  export type { FullPageModalProps } from './FullPageModal';
46
50
  export { default as HistoryList } from './HistoryList';