@nurvus/ui 0.1.0-alpha.18 → 0.1.0-alpha.19
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 +8 -3
- package/dist/index.js +1 -1
- package/dist/nurvus.css +1 -1
- package/dist/respawn.js +1 -1
- package/dist/types/components/Menu/Menu.d.ts +13 -5
- package/dist/types/menu.d.ts +30 -6
- package/dist/types/respawn/Menu.d.ts +5 -1
- package/package.json +1 -1
- package/src/components/Menu/Menu.md +31 -2
- package/src/components/Menu/Menu.scss +24 -0
- package/src/components/Menu/Menu.tsx +72 -65
- package/src/menu.ts +58 -12
- package/src/respawn/Menu.ts +18 -16
|
@@ -13,6 +13,14 @@ export interface MenuItem {
|
|
|
13
13
|
disabled?: boolean;
|
|
14
14
|
/** Draws a hairline above this item, to split a section into groups. */
|
|
15
15
|
divider?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Navigation: child items, which make this item a nested group that opens
|
|
18
|
+
* and closes like a section, as deep as you like (Tree's shape). With an
|
|
19
|
+
* `href` its label is a link too.
|
|
20
|
+
*/
|
|
21
|
+
items?: MenuItem[];
|
|
22
|
+
/** Navigation, a nested group: a stable id for its open state. Defaults to its section's id and its label ("settings-advanced"). */
|
|
23
|
+
id?: string;
|
|
16
24
|
}
|
|
17
25
|
export interface MenuSection {
|
|
18
26
|
/** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
|
|
@@ -35,11 +43,11 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
|
|
|
35
43
|
kind?: MenuKind;
|
|
36
44
|
/** Navigation and actions: the sections, in order. */
|
|
37
45
|
sections?: MenuSection[];
|
|
38
|
-
/** Navigation: the href of the page being shown. Its link is marked, and its section opens. */
|
|
46
|
+
/** Navigation: the href of the page being shown. Its link is marked, and its section (and nested group) opens. */
|
|
39
47
|
current?: string;
|
|
40
|
-
/** Navigation: which sections start open: true (all), false (none) or a map of
|
|
48
|
+
/** Navigation: which sections and nested groups start open: true (all), false (none) or a map of id → open. Defaults to all. */
|
|
41
49
|
defaultOpen?: boolean | Record<string, boolean>;
|
|
42
|
-
/** Navigation: called when a section opens or closes (e.g. to remember it). */
|
|
50
|
+
/** Navigation: called when a section or nested group opens or closes (e.g. to remember it). */
|
|
43
51
|
onToggle?: (id: string, open: boolean) => void;
|
|
44
52
|
/** Navigation: called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
45
53
|
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
@@ -76,8 +84,8 @@ export type MenuTreeProps = TreeProps & {
|
|
|
76
84
|
/**
|
|
77
85
|
* One component for lists of things to go to, do or pick. The items decide
|
|
78
86
|
* what it is, and screen readers hear it that way:
|
|
79
|
-
* - links: site navigation (sections that open and close,
|
|
80
|
-
* marked), and Tab goes link by link;
|
|
87
|
+
* - links: site navigation (sections that open and close, nested groups as
|
|
88
|
+
* deep as you like, the current page marked), and Tab goes link by link;
|
|
81
89
|
* - actions: a menu of commands, one Tab stop, the arrow keys move;
|
|
82
90
|
* - kind="tree": a tree to pick a row from, like layers (see Tree).
|
|
83
91
|
* Up and Down move between items in all three.
|
package/dist/types/menu.d.ts
CHANGED
|
@@ -8,13 +8,18 @@
|
|
|
8
8
|
* "tree", each row "level 2, 3 of 5, selected". Drawn by Tree.
|
|
9
9
|
*/
|
|
10
10
|
export type MenuKind = 'nav' | 'actions' | 'tree';
|
|
11
|
+
/** An item as the rules see it: a link, and maybe child items (a nested group). */
|
|
12
|
+
interface NavNode {
|
|
13
|
+
id?: string;
|
|
14
|
+
label?: unknown;
|
|
15
|
+
href?: string;
|
|
16
|
+
items?: NavNode[];
|
|
17
|
+
}
|
|
11
18
|
interface KindProps {
|
|
12
19
|
kind?: MenuKind;
|
|
13
20
|
sections?: {
|
|
14
21
|
href?: string;
|
|
15
|
-
items?:
|
|
16
|
-
href?: string;
|
|
17
|
-
}[];
|
|
22
|
+
items?: NavNode[];
|
|
18
23
|
}[];
|
|
19
24
|
items?: unknown;
|
|
20
25
|
activePath?: unknown;
|
|
@@ -30,6 +35,24 @@ export declare const sectionId: (section: {
|
|
|
30
35
|
id?: string;
|
|
31
36
|
title?: unknown;
|
|
32
37
|
}) => string;
|
|
38
|
+
/** Whether a navigation item is a nested group: it has child items of its own. */
|
|
39
|
+
export declare const isGroup: (item: NavNode) => boolean;
|
|
40
|
+
/** A nested group's id: its own, or its parent's id and its label as a slug ("settings" + "Advanced" -> "settings-advanced"). */
|
|
41
|
+
export declare const groupId: (item: NavNode, parent: string) => string;
|
|
42
|
+
/** Whether `current` is the link of one of these items, at any depth. */
|
|
43
|
+
export declare const holdsCurrent: (items: NavNode[] | undefined, current: string | undefined) => boolean;
|
|
44
|
+
/** Every section and nested group of a navigation menu, by id, and whether the current page is inside it. */
|
|
45
|
+
export declare function menuGroups(sections: {
|
|
46
|
+
id?: string;
|
|
47
|
+
title?: unknown;
|
|
48
|
+
items?: NavNode[];
|
|
49
|
+
}[], current?: string): [string, boolean][];
|
|
50
|
+
/** Which sections and groups start open: as `defaultOpen` says, and always the ones the current page is in. */
|
|
51
|
+
export declare const menuOpenState: (sections: {
|
|
52
|
+
id?: string;
|
|
53
|
+
title?: unknown;
|
|
54
|
+
items?: NavNode[];
|
|
55
|
+
}[], current: string | undefined, defaultOpen: boolean | Record<string, boolean> | undefined) => Record<string, boolean>;
|
|
33
56
|
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
34
57
|
export declare const startsOpen: (defaultOpen: boolean | Record<string, boolean> | undefined, id: string) => boolean;
|
|
35
58
|
/** Makes `row` the one item of an actions menu that Tab reaches (roving tabindex). */
|
|
@@ -41,9 +64,10 @@ export declare function menuFocus(event: FocusEvent, menu: HTMLElement): void;
|
|
|
41
64
|
* at the ends) and Home / End to the first / last, in every kind.
|
|
42
65
|
* - Navigation (the WAI-ARIA disclosure navigation pattern, with its
|
|
43
66
|
* optional keys): Tab still goes link by link. The rows are the section
|
|
44
|
-
* titles and the links in open
|
|
45
|
-
* goes to its first
|
|
46
|
-
*
|
|
67
|
+
* and group titles and the links in open ones; Right on a section or group
|
|
68
|
+
* opens it, or goes to its first row; Left closes it, or goes up to the
|
|
69
|
+
* one it is in. Enter or Space follows a link (Space too, as in the
|
|
70
|
+
* WAI-ARIA navigation tree) and opens a section.
|
|
47
71
|
* - Actions (the WAI-ARIA menu pattern): one Tab stop that follows the
|
|
48
72
|
* focus; typing a letter goes to the next item starting with it; Enter or
|
|
49
73
|
* Space runs the item.
|
|
@@ -14,6 +14,10 @@ export interface MenuItem {
|
|
|
14
14
|
disabled?: boolean;
|
|
15
15
|
/** Draws a hairline above this item, to split a section into groups. */
|
|
16
16
|
divider?: boolean;
|
|
17
|
+
/** Navigation: child items, which make this item a nested group that opens and closes like a section, as deep as you like. With an `href` its label is a link too. */
|
|
18
|
+
items?: MenuItem[];
|
|
19
|
+
/** Navigation, a nested group: a stable id for its open state. Defaults to its section's id and its label ("settings-advanced"). */
|
|
20
|
+
id?: string;
|
|
17
21
|
}
|
|
18
22
|
export interface MenuSection {
|
|
19
23
|
/** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
|
|
@@ -31,7 +35,7 @@ interface MenuBaseProps extends ElementProps {
|
|
|
31
35
|
sections?: MenuSection[];
|
|
32
36
|
/** The href of the page being shown: its link is marked, and its section opens. */
|
|
33
37
|
current?: string;
|
|
34
|
-
/** Which sections start open: true (all), false (none) or a map of
|
|
38
|
+
/** Which sections and nested groups start open: true (all), false (none) or a map of id -> open. Defaults to all. */
|
|
35
39
|
defaultOpen?: boolean | Record<string, boolean>;
|
|
36
40
|
/** Called when a section opens or closes (e.g. to remember it). */
|
|
37
41
|
onToggle?: (id: string, open: boolean) => void;
|
package/package.json
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
| Items | It becomes | Screen readers say | Keys |
|
|
4
4
|
|---|---|---|---|
|
|
5
|
-
| Links (`href`) | Navigation: a `<nav>` landmark | "Pages, navigation… Billing, link, current page" | Tab goes link by link; ↑ ↓ Home End move too; → ← open and close sections |
|
|
5
|
+
| Links (`href`) | Navigation: a `<nav>` landmark | "Pages, navigation… Billing, link, current page" | Tab goes link by link; ↑ ↓ Home End move too; → ← open and close sections; Enter or Space follows a link |
|
|
6
6
|
| Actions (`onSelect`) | A menu of commands (`role="menu"`) | "Actions, menu… Duplicate, menu item" | One Tab stop; ↑ ↓ Home End move; type a letter to jump; Enter or Space runs it |
|
|
7
7
|
| Rows to pick, `kind="tree"` | A tree (the same as [Tree](#/Components/Tree)) | "Layers, tree… Header, level 1, 1 of 3, selected" | One Tab stop; ↑ ↓ move, → ← go in and out, Enter or Space picks |
|
|
8
8
|
|
|
9
|
-
Left out, `kind` follows the items: navigation when any item has an `href`, actions when none has. An item without an `href` inside navigation is a button there ("Sign out").
|
|
9
|
+
Left out, `kind` follows the items: navigation when any item has an `href`, actions when none has. An item without an `href` inside navigation is a button there ("Sign out").
|
|
10
|
+
|
|
11
|
+
**What each row is.** In navigation, a page is a link (`<a href>`): it opens in a new tab, copies, and is listed with the page's links by screen readers, so it stays a link. A section heading without a page, and every chevron, is a `<button>`. Buttons take Enter and Space everywhere; a link on the web takes only Enter (Space scrolls the page), but in a Menu **Space follows a link too**, as the WAI-ARIA navigation tree does, so every row answers the same two keys. Tree rows take Enter and Space.
|
|
10
12
|
|
|
11
13
|
**Navigation.** Sections open and close with the chevron (or the whole heading, when it isn't a link). The chevron sits before the title, as Material UI draws it: › while a section is closed, ⌄ while it is open, so it stays next to the words it belongs to (easier to find for people who zoom in) and every title lines up. An open section has a hairline guide down its items, and the current page's bar sits on it; the page also opens its section by itself. `chevron="end"` puts the chevron after the title instead (⌄ closed, ⌃ open), with no guide line.
|
|
12
14
|
|
|
@@ -32,6 +34,33 @@ import { Menu } from '@nurvus/ui';
|
|
|
32
34
|
</div>
|
|
33
35
|
```
|
|
34
36
|
|
|
37
|
+
**Nested groups.** Any item can have its own `items`, as deep as you like (Tree's shape): it becomes a group that opens and closes like a section, its chevron between the guide line and the words, its title lined up with the items around it, and its own guide line under it. With an `href` its title is a link too. → opens a group and ← closes it, or goes up to the one it is in. The current page opens every group it is in; `defaultOpen` and `onToggle` use the group's `id` (or its section's id and its label: "components-forms").
|
|
38
|
+
```jsx
|
|
39
|
+
import { Menu } from '@nurvus/ui';
|
|
40
|
+
|
|
41
|
+
<div style={ { maxWidth: 260 } }>
|
|
42
|
+
<Menu
|
|
43
|
+
id="nested-example"
|
|
44
|
+
label="Docs"
|
|
45
|
+
current="#/c/select"
|
|
46
|
+
onNavigate={ (href, event) => event.preventDefault() }
|
|
47
|
+
sections={ [
|
|
48
|
+
{ title: 'Components', items: [
|
|
49
|
+
{ label: 'Button', href: '#/c/button' },
|
|
50
|
+
{ label: 'Forms', items: [
|
|
51
|
+
{ label: 'Select', href: '#/c/select' },
|
|
52
|
+
{ label: 'Inputs', href: '#/c/inputs', items: [
|
|
53
|
+
{ label: 'Text field', href: '#/c/text-field' },
|
|
54
|
+
{ label: 'Checkbox', href: '#/c/checkbox' },
|
|
55
|
+
] },
|
|
56
|
+
] },
|
|
57
|
+
{ label: 'Table', href: '#/c/table' },
|
|
58
|
+
] },
|
|
59
|
+
] }
|
|
60
|
+
/>
|
|
61
|
+
</div>
|
|
62
|
+
```
|
|
63
|
+
|
|
35
64
|
Start with some sections closed: `defaultOpen` takes a map of section ids (the title as a slug, or the section's own `id`).
|
|
36
65
|
```jsx
|
|
37
66
|
import { Menu } from '@nurvus/ui';
|
|
@@ -155,6 +155,20 @@ $menu-speed: 0.2s;
|
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
+
// A nested group (an item with its own items, round 11): a row like the
|
|
159
|
+
// items around it, a little in, so its title reads as one of them.
|
|
160
|
+
li > .menu-section {
|
|
161
|
+
margin-left: 0.75rem;
|
|
162
|
+
|
|
163
|
+
> .menu-heading .menu-title {
|
|
164
|
+
color: var(--nv-text-muted);
|
|
165
|
+
font-weight: 400;
|
|
166
|
+
|
|
167
|
+
&:hover,
|
|
168
|
+
&[data-active="true"] { color: var(--nv-text); }
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
158
172
|
// Chevron at the start (data-chevron="start", the default since 0.1.0-alpha.17;
|
|
159
173
|
// as Material UI draws it): before the title, › while the section is closed
|
|
160
174
|
// and ⌄ while it is open, a quarter turn between. Every chevron is centered
|
|
@@ -209,8 +223,18 @@ $menu-speed: 0.2s;
|
|
|
209
223
|
background: var(--nv-border);
|
|
210
224
|
}
|
|
211
225
|
|
|
226
|
+
// A nested group, 0.75rem in: its chevron sits between the guide line and
|
|
227
|
+
// the items' words, its title starts where they do (it and the items
|
|
228
|
+
// around it line up), and its own guide line runs under its chevron.
|
|
229
|
+
li > .menu-section > .menu-heading {
|
|
230
|
+
gap: 0;
|
|
231
|
+
|
|
232
|
+
.menu-title.menu-toggle::before { margin-right: 0.5625rem; }
|
|
233
|
+
}
|
|
234
|
+
|
|
212
235
|
// A divider starts where the items do, right of the guide line.
|
|
213
236
|
li[data-divider] { margin-left: 1.25rem; }
|
|
237
|
+
li[data-divider] > .menu-section { margin-left: -0.5rem; }
|
|
214
238
|
li[data-divider] .menu-item { width: 100%; margin-left: 0; }
|
|
215
239
|
|
|
216
240
|
.menu-item {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import classNames from 'classnames';
|
|
3
3
|
import CONSTANT from '../../constants';
|
|
4
|
-
import { sectionId,
|
|
4
|
+
import { sectionId, groupId, isGroup, menuGroups, menuOpenState, menuKeydown, menuFocus, menuKind, MenuKind } from '../../menu';
|
|
5
5
|
import Tree, { TreeProps, TreeItem } from '../Tree/Tree';
|
|
6
6
|
import './Menu.scss';
|
|
7
7
|
|
|
@@ -18,6 +18,14 @@ export interface MenuItem {
|
|
|
18
18
|
disabled?: boolean;
|
|
19
19
|
/** Draws a hairline above this item, to split a section into groups. */
|
|
20
20
|
divider?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Navigation: child items, which make this item a nested group that opens
|
|
23
|
+
* and closes like a section, as deep as you like (Tree's shape). With an
|
|
24
|
+
* `href` its label is a link too.
|
|
25
|
+
*/
|
|
26
|
+
items?: MenuItem[];
|
|
27
|
+
/** Navigation, a nested group: a stable id for its open state. Defaults to its section's id and its label ("settings-advanced"). */
|
|
28
|
+
id?: string;
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
export interface MenuSection {
|
|
@@ -42,11 +50,11 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
|
|
|
42
50
|
kind?: MenuKind;
|
|
43
51
|
/** Navigation and actions: the sections, in order. */
|
|
44
52
|
sections?: MenuSection[];
|
|
45
|
-
/** Navigation: the href of the page being shown. Its link is marked, and its section opens. */
|
|
53
|
+
/** Navigation: the href of the page being shown. Its link is marked, and its section (and nested group) opens. */
|
|
46
54
|
current?: string;
|
|
47
|
-
/** Navigation: which sections start open: true (all), false (none) or a map of
|
|
55
|
+
/** Navigation: which sections and nested groups start open: true (all), false (none) or a map of id → open. Defaults to all. */
|
|
48
56
|
defaultOpen?: boolean | Record<string, boolean>;
|
|
49
|
-
/** Navigation: called when a section opens or closes (e.g. to remember it). */
|
|
57
|
+
/** Navigation: called when a section or nested group opens or closes (e.g. to remember it). */
|
|
50
58
|
onToggle?: (id: string, open: boolean) => void;
|
|
51
59
|
/** Navigation: called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
52
60
|
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
@@ -83,8 +91,8 @@ export type MenuTreeProps = TreeProps & { kind: 'tree' };
|
|
|
83
91
|
/**
|
|
84
92
|
* One component for lists of things to go to, do or pick. The items decide
|
|
85
93
|
* what it is, and screen readers hear it that way:
|
|
86
|
-
* - links: site navigation (sections that open and close,
|
|
87
|
-
* marked), and Tab goes link by link;
|
|
94
|
+
* - links: site navigation (sections that open and close, nested groups as
|
|
95
|
+
* deep as you like, the current page marked), and Tab goes link by link;
|
|
88
96
|
* - actions: a menu of commands, one Tab stop, the arrow keys move;
|
|
89
97
|
* - kind="tree": a tree to pick a row from, like layers (see Tree).
|
|
90
98
|
* Up and Down move between items in all three.
|
|
@@ -113,16 +121,13 @@ function MenuNav({
|
|
|
113
121
|
onKeyDown,
|
|
114
122
|
...restProps
|
|
115
123
|
}: MenuListProps) {
|
|
116
|
-
// The current page's section starts open, whatever defaultOpen says.
|
|
117
|
-
const [open, setOpen] = React.useState<Record<string, boolean>>(() =>
|
|
118
|
-
const home = sections.find((s) => s.items.some((item) => item.href !== undefined && item.href === current));
|
|
119
|
-
return Object.fromEntries(sections.map((s) => [sectionId(s), s === home || startsOpen(defaultOpen, sectionId(s))]));
|
|
120
|
-
});
|
|
124
|
+
// The current page's section (and group) starts open, whatever defaultOpen says.
|
|
125
|
+
const [open, setOpen] = React.useState<Record<string, boolean>>(() => menuOpenState(sections, current, defaultOpen));
|
|
121
126
|
|
|
122
|
-
// Navigating into a closed section opens it, so the current page is always visible.
|
|
127
|
+
// Navigating into a closed section or group opens it, so the current page is always visible.
|
|
123
128
|
React.useEffect(() => {
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
129
|
+
const closed = menuGroups(sections, current).filter(([gid, home]) => home && open[gid] === false);
|
|
130
|
+
if (closed.length) setOpen((was) => ({ ...was, ...Object.fromEntries(closed.map(([gid]) => [gid, true])) }));
|
|
126
131
|
}, [current]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
127
132
|
|
|
128
133
|
const toggle = (sid: string) => {
|
|
@@ -140,59 +145,61 @@ function MenuNav({
|
|
|
140
145
|
{ ...restProps }
|
|
141
146
|
onKeyDown={ (e) => { menuKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
|
|
142
147
|
>
|
|
143
|
-
{ sections.map((section) =>
|
|
144
|
-
const sid = sectionId(section);
|
|
145
|
-
const shown = open[sid] !== false;
|
|
146
|
-
const listId = `${id}-${sid}`;
|
|
147
|
-
const toggleProps = {
|
|
148
|
-
type: 'button' as const,
|
|
149
|
-
'aria-expanded': shown,
|
|
150
|
-
'aria-controls': listId,
|
|
151
|
-
onClick: () => toggle(sid),
|
|
152
|
-
};
|
|
153
|
-
return (
|
|
154
|
-
<div key={ sid } className="menu-section" data-section={ sid } data-open={ shown }>
|
|
155
|
-
<div className="menu-heading">
|
|
156
|
-
{ section.href ? (
|
|
157
|
-
<>
|
|
158
|
-
{ chevron === 'start' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } /> }
|
|
159
|
-
<a className="menu-title" href={ section.href } data-active={ current === section.href } onClick={ navigate(section.href) }>
|
|
160
|
-
{ section.title }
|
|
161
|
-
</a>
|
|
162
|
-
{ chevron === 'end' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } /> }
|
|
163
|
-
</>
|
|
164
|
-
) : (
|
|
165
|
-
<button className="menu-title menu-toggle" { ...toggleProps }>{ section.title }</button>
|
|
166
|
-
) }
|
|
167
|
-
</div>
|
|
168
|
-
<div className="menu-items" id={ listId }>
|
|
169
|
-
<ul>
|
|
170
|
-
{ section.items.map((item, i) => (
|
|
171
|
-
<li key={ item.href || i } data-divider={ item.divider || undefined }>
|
|
172
|
-
{ item.href !== undefined ? (
|
|
173
|
-
<a
|
|
174
|
-
className="menu-item"
|
|
175
|
-
href={ item.href }
|
|
176
|
-
data-active={ current === item.href }
|
|
177
|
-
aria-current={ current === item.href ? 'page' : undefined }
|
|
178
|
-
onClick={ navigate(item.href) }
|
|
179
|
-
>
|
|
180
|
-
{ item.label }
|
|
181
|
-
</a>
|
|
182
|
-
) : (
|
|
183
|
-
<button type="button" className="menu-item" disabled={ item.disabled } onClick={ item.onSelect }>
|
|
184
|
-
{ item.label }
|
|
185
|
-
</button>
|
|
186
|
-
) }
|
|
187
|
-
</li>
|
|
188
|
-
)) }
|
|
189
|
-
</ul>
|
|
190
|
-
</div>
|
|
191
|
-
</div>
|
|
192
|
-
);
|
|
193
|
-
}) }
|
|
148
|
+
{ sections.map((section) => renderGroup(sectionId(section), section.title, section.href, section.items)) }
|
|
194
149
|
</nav>
|
|
195
150
|
);
|
|
151
|
+
|
|
152
|
+
// A section, or a nested group (an item with items): a heading that opens and closes its list.
|
|
153
|
+
function renderGroup(gid: string, title: React.ReactNode, href: string | undefined, items: MenuItem[]): React.ReactNode {
|
|
154
|
+
const shown = open[gid] !== false;
|
|
155
|
+
const listId = `${id}-${gid}`;
|
|
156
|
+
const toggleProps = {
|
|
157
|
+
type: 'button' as const,
|
|
158
|
+
'aria-expanded': shown,
|
|
159
|
+
'aria-controls': listId,
|
|
160
|
+
onClick: () => toggle(gid),
|
|
161
|
+
};
|
|
162
|
+
return (
|
|
163
|
+
<div key={ gid } className="menu-section" data-section={ gid } data-open={ shown }>
|
|
164
|
+
<div className="menu-heading">
|
|
165
|
+
{ href ? (
|
|
166
|
+
<>
|
|
167
|
+
{ chevron === 'start' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(title)}` } { ...toggleProps } /> }
|
|
168
|
+
<a className="menu-title" href={ href } data-active={ current === href } aria-current={ current === href ? 'page' : undefined } onClick={ navigate(href) }>
|
|
169
|
+
{ title }
|
|
170
|
+
</a>
|
|
171
|
+
{ chevron === 'end' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(title)}` } { ...toggleProps } /> }
|
|
172
|
+
</>
|
|
173
|
+
) : (
|
|
174
|
+
<button className="menu-title menu-toggle" { ...toggleProps }>{ title }</button>
|
|
175
|
+
) }
|
|
176
|
+
</div>
|
|
177
|
+
<div className="menu-items" id={ listId }>
|
|
178
|
+
<ul>
|
|
179
|
+
{ items.map((item, i) => (
|
|
180
|
+
<li key={ item.href || i } data-divider={ item.divider || undefined }>
|
|
181
|
+
{ isGroup(item) ? renderGroup(groupId(item, gid), item.label, item.href, item.items as MenuItem[]) : item.href !== undefined ? (
|
|
182
|
+
<a
|
|
183
|
+
className="menu-item"
|
|
184
|
+
href={ item.href }
|
|
185
|
+
data-active={ current === item.href }
|
|
186
|
+
aria-current={ current === item.href ? 'page' : undefined }
|
|
187
|
+
onClick={ navigate(item.href) }
|
|
188
|
+
>
|
|
189
|
+
{ item.label }
|
|
190
|
+
</a>
|
|
191
|
+
) : (
|
|
192
|
+
<button type="button" className="menu-item" disabled={ item.disabled } onClick={ item.onSelect }>
|
|
193
|
+
{ item.label }
|
|
194
|
+
</button>
|
|
195
|
+
) }
|
|
196
|
+
</li>
|
|
197
|
+
)) }
|
|
198
|
+
</ul>
|
|
199
|
+
</div>
|
|
200
|
+
</div>
|
|
201
|
+
);
|
|
202
|
+
}
|
|
196
203
|
}
|
|
197
204
|
|
|
198
205
|
function MenuActions({
|
package/src/menu.ts
CHANGED
|
@@ -13,9 +13,12 @@
|
|
|
13
13
|
*/
|
|
14
14
|
export type MenuKind = 'nav' | 'actions' | 'tree';
|
|
15
15
|
|
|
16
|
+
/** An item as the rules see it: a link, and maybe child items (a nested group). */
|
|
17
|
+
interface NavNode { id?: string; label?: unknown; href?: string; items?: NavNode[] }
|
|
18
|
+
|
|
16
19
|
interface KindProps {
|
|
17
20
|
kind?: MenuKind;
|
|
18
|
-
sections?: { href?: string; items?:
|
|
21
|
+
sections?: { href?: string; items?: NavNode[] }[];
|
|
19
22
|
items?: unknown;
|
|
20
23
|
activePath?: unknown;
|
|
21
24
|
}
|
|
@@ -29,13 +32,46 @@ export function menuKind(props: KindProps): MenuKind {
|
|
|
29
32
|
if (props.kind) return props.kind;
|
|
30
33
|
if (!props.sections && (props.items !== undefined || props.activePath !== undefined)) return 'tree';
|
|
31
34
|
const sections = props.sections || [];
|
|
32
|
-
const
|
|
35
|
+
const linked = (items: NavNode[] | undefined): boolean => !!items && items.some((item) => item.href !== undefined || linked(item.items));
|
|
36
|
+
const links = sections.some((s) => s.href !== undefined || linked(s.items));
|
|
33
37
|
return links || !sections.length ? 'nav' : 'actions';
|
|
34
38
|
}
|
|
35
39
|
|
|
40
|
+
const slug = (text: unknown): string => String(text).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
41
|
+
|
|
36
42
|
/** A section's id: its own, or its title as a slug ("Get started" -> "get-started"). */
|
|
37
|
-
export const sectionId = (section: { id?: string; title?: unknown }): string =>
|
|
38
|
-
|
|
43
|
+
export const sectionId = (section: { id?: string; title?: unknown }): string => section.id || slug(section.title ?? 'section');
|
|
44
|
+
|
|
45
|
+
/** Whether a navigation item is a nested group: it has child items of its own. */
|
|
46
|
+
export const isGroup = (item: NavNode): boolean => !!(item.items && item.items.length);
|
|
47
|
+
|
|
48
|
+
/** A nested group's id: its own, or its parent's id and its label as a slug ("settings" + "Advanced" -> "settings-advanced"). */
|
|
49
|
+
export const groupId = (item: NavNode, parent: string): string => item.id || `${parent}-${slug(item.label)}`;
|
|
50
|
+
|
|
51
|
+
/** Whether `current` is the link of one of these items, at any depth. */
|
|
52
|
+
export const holdsCurrent = (items: NavNode[] | undefined, current: string | undefined): boolean =>
|
|
53
|
+
current !== undefined && !!items && items.some((item) => item.href === current || holdsCurrent(item.items, current));
|
|
54
|
+
|
|
55
|
+
/** Every section and nested group of a navigation menu, by id, and whether the current page is inside it. */
|
|
56
|
+
export function menuGroups(sections: { id?: string; title?: unknown; items?: NavNode[] }[], current?: string): [string, boolean][] {
|
|
57
|
+
const all: [string, boolean][] = [];
|
|
58
|
+
const walk = (items: NavNode[], parent: string) => items.forEach((item) => {
|
|
59
|
+
if (!isGroup(item)) return;
|
|
60
|
+
const gid = groupId(item, parent);
|
|
61
|
+
all.push([gid, holdsCurrent(item.items, current)]);
|
|
62
|
+
walk(item.items as NavNode[], gid);
|
|
63
|
+
});
|
|
64
|
+
sections.forEach((section) => {
|
|
65
|
+
const sid = sectionId(section);
|
|
66
|
+
all.push([sid, holdsCurrent(section.items, current)]);
|
|
67
|
+
walk(section.items || [], sid);
|
|
68
|
+
});
|
|
69
|
+
return all;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Which sections and groups start open: as `defaultOpen` says, and always the ones the current page is in. */
|
|
73
|
+
export const menuOpenState = (sections: { id?: string; title?: unknown; items?: NavNode[] }[], current: string | undefined, defaultOpen: boolean | Record<string, boolean> | undefined): Record<string, boolean> =>
|
|
74
|
+
Object.fromEntries(menuGroups(sections, current).map(([gid, home]) => [gid, home || startsOpen(defaultOpen, gid)]));
|
|
39
75
|
|
|
40
76
|
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
41
77
|
export const startsOpen = (defaultOpen: boolean | Record<string, boolean> | undefined, id: string): boolean =>
|
|
@@ -57,9 +93,10 @@ export function menuFocus(event: FocusEvent, menu: HTMLElement): void {
|
|
|
57
93
|
* at the ends) and Home / End to the first / last, in every kind.
|
|
58
94
|
* - Navigation (the WAI-ARIA disclosure navigation pattern, with its
|
|
59
95
|
* optional keys): Tab still goes link by link. The rows are the section
|
|
60
|
-
* titles and the links in open
|
|
61
|
-
* goes to its first
|
|
62
|
-
*
|
|
96
|
+
* and group titles and the links in open ones; Right on a section or group
|
|
97
|
+
* opens it, or goes to its first row; Left closes it, or goes up to the
|
|
98
|
+
* one it is in. Enter or Space follows a link (Space too, as in the
|
|
99
|
+
* WAI-ARIA navigation tree) and opens a section.
|
|
63
100
|
* - Actions (the WAI-ARIA menu pattern): one Tab stop that follows the
|
|
64
101
|
* focus; typing a letter goes to the next item starting with it; Enter or
|
|
65
102
|
* Space runs the item.
|
|
@@ -106,28 +143,37 @@ function navKeydown(event: KeyboardEvent, nav: HTMLElement): boolean {
|
|
|
106
143
|
const rows = Array.from(nav.querySelectorAll<HTMLElement>('.menu-title, .menu-item'))
|
|
107
144
|
.filter((row) => !row.closest('.menu-section[data-open="false"] .menu-items'));
|
|
108
145
|
if (!rows.length) return false;
|
|
146
|
+
// The section or nested group the key was pressed in (its own heading, or a row of its list).
|
|
109
147
|
const section = target.closest<HTMLElement>('.menu-section');
|
|
110
148
|
// The chevron beside a linked title counts as that title's row.
|
|
111
149
|
const onHeading = !!target.closest('.menu-heading');
|
|
112
|
-
const title = section && section.querySelector<HTMLElement>('.menu-title');
|
|
150
|
+
const title = section && section.querySelector<HTMLElement>(':scope > .menu-heading .menu-title');
|
|
113
151
|
const at = rows.indexOf(onHeading && title ? title : target);
|
|
114
|
-
const focus = (row:
|
|
115
|
-
const toggle = () => { const button = section && section.querySelector<HTMLElement>('.menu-toggle'); if (button) button.click(); };
|
|
152
|
+
const focus = (row: Element | null | undefined) => { if (row) (row as HTMLElement).focus(); };
|
|
153
|
+
const toggle = () => { const button = section && section.querySelector<HTMLElement>(':scope > .menu-heading .menu-toggle'); if (button) button.click(); };
|
|
116
154
|
const isOpen = !!section && section.getAttribute('data-open') !== 'false';
|
|
155
|
+
const outer = section && section.parentElement ? section.parentElement.closest<HTMLElement>('.menu-section') : null;
|
|
117
156
|
|
|
118
157
|
switch (event.key) {
|
|
119
158
|
case 'ArrowDown': focus(rows[(at + 1) % rows.length]); break;
|
|
120
159
|
case 'ArrowUp': focus(rows[at <= 0 ? rows.length - 1 : at - 1]); break;
|
|
121
160
|
case 'Home': focus(rows[0]); break;
|
|
122
161
|
case 'End': focus(rows[rows.length - 1]); break;
|
|
162
|
+
case ' ':
|
|
163
|
+
// Space follows a link as Enter does (buttons take Space by themselves).
|
|
164
|
+
if (target.tagName !== 'A') return false;
|
|
165
|
+
target.click();
|
|
166
|
+
break;
|
|
123
167
|
case 'ArrowRight':
|
|
124
168
|
if (!onHeading || !section) return false;
|
|
125
169
|
if (!isOpen) toggle();
|
|
126
|
-
else focus(section.querySelector
|
|
170
|
+
else focus(section.querySelector(':scope > .menu-items .menu-title, :scope > .menu-items .menu-item'));
|
|
127
171
|
break;
|
|
128
172
|
case 'ArrowLeft':
|
|
129
173
|
if (!section) return false;
|
|
130
|
-
if (onHeading)
|
|
174
|
+
if (!onHeading) focus(title);
|
|
175
|
+
else if (isOpen) toggle();
|
|
176
|
+
else if (outer) focus(outer.querySelector(':scope > .menu-heading .menu-title'));
|
|
131
177
|
break;
|
|
132
178
|
default: return false;
|
|
133
179
|
}
|
package/src/respawn/Menu.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import classNames from 'classnames';
|
|
2
2
|
import CONSTANT from '../constants';
|
|
3
|
-
import { sectionId,
|
|
3
|
+
import { sectionId, groupId, isGroup, menuOpenState, menuKeydown, menuFocus, menuKind, MenuKind } from '../menu';
|
|
4
4
|
import Tree, { TreeProps } from './Tree';
|
|
5
5
|
import { spawn, Child, ElementProps } from './spawn';
|
|
6
6
|
import { updatable, NurvusElement } from './update';
|
|
@@ -19,6 +19,10 @@ export interface MenuItem {
|
|
|
19
19
|
disabled?: boolean;
|
|
20
20
|
/** Draws a hairline above this item, to split a section into groups. */
|
|
21
21
|
divider?: boolean;
|
|
22
|
+
/** Navigation: child items, which make this item a nested group that opens and closes like a section, as deep as you like. With an `href` its label is a link too. */
|
|
23
|
+
items?: MenuItem[];
|
|
24
|
+
/** Navigation, a nested group: a stable id for its open state. Defaults to its section's id and its label ("settings-advanced"). */
|
|
25
|
+
id?: string;
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
export interface MenuSection {
|
|
@@ -38,7 +42,7 @@ interface MenuBaseProps extends ElementProps {
|
|
|
38
42
|
sections?: MenuSection[];
|
|
39
43
|
/** The href of the page being shown: its link is marked, and its section opens. */
|
|
40
44
|
current?: string;
|
|
41
|
-
/** Which sections start open: true (all), false (none) or a map of
|
|
45
|
+
/** Which sections and nested groups start open: true (all), false (none) or a map of id -> open. Defaults to all. */
|
|
42
46
|
defaultOpen?: boolean | Record<string, boolean>;
|
|
43
47
|
/** Called when a section opens or closes (e.g. to remember it). */
|
|
44
48
|
onToggle?: (id: string, open: boolean) => void;
|
|
@@ -90,15 +94,13 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
90
94
|
return updatable<HTMLElement, MenuListProps>(({
|
|
91
95
|
kind: _kind, sections = [], current, defaultOpen = true, onToggle, onNavigate, chevron = 'start', label = 'Pages', id = 'menu', className, ...rest
|
|
92
96
|
}: MenuListProps, keep) => {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const home = sections.find((s) => s.items.some((item) => item.href !== undefined && item.href === current));
|
|
96
|
-
if (home) open[sectionId(home)] = true;
|
|
97
|
+
// As in React: defaultOpen, and the current page's section and group always open.
|
|
98
|
+
const open: Record<string, boolean> = menuOpenState(sections, current, defaultOpen);
|
|
97
99
|
|
|
98
100
|
const navigate = (href: string) => ({ click: (event: MouseEvent) => { if (onNavigate) onNavigate(href, event); } });
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
// A section, or a nested group (an item with items): a heading that opens and closes its list.
|
|
103
|
+
const renderGroup = (sid: string, title: Child, href: string | undefined, items: MenuItem[]): HTMLElement => {
|
|
102
104
|
const listId = `${id}-${sid}`;
|
|
103
105
|
const toggles: HTMLElement[] = [];
|
|
104
106
|
const toggle = () => {
|
|
@@ -107,18 +109,18 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
107
109
|
el.setAttribute('data-open', String(open[sid]));
|
|
108
110
|
toggles.forEach((t) => {
|
|
109
111
|
t.setAttribute('aria-expanded', String(open[sid]));
|
|
110
|
-
if (t.hasAttribute('aria-label')) t.setAttribute('aria-label', `${open[sid] ? 'Hide' : 'Show'} ${String(
|
|
112
|
+
if (t.hasAttribute('aria-label')) t.setAttribute('aria-label', `${open[sid] ? 'Hide' : 'Show'} ${String(title)}`);
|
|
111
113
|
});
|
|
112
114
|
if (onToggle) onToggle(sid, open[sid]);
|
|
113
115
|
};
|
|
114
116
|
const toggleProps = (extra: Record<string, unknown>) => ({
|
|
115
117
|
tag: 'button', type: 'button', 'aria-expanded': open[sid], 'aria-controls': listId, events: { click: toggle }, ...extra,
|
|
116
118
|
});
|
|
117
|
-
const link = () => spawn({ tag: 'a', className: 'menu-title', href:
|
|
118
|
-
const chev = () => spawn(toggleProps({ className: 'menu-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${String(
|
|
119
|
-
const heading =
|
|
119
|
+
const link = () => spawn({ tag: 'a', className: 'menu-title', href, 'data-active': current === href, 'aria-current': current === href ? 'page' : undefined, events: navigate(href as string), children: title });
|
|
120
|
+
const chev = () => spawn(toggleProps({ className: 'menu-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${String(title)}` }));
|
|
121
|
+
const heading = href
|
|
120
122
|
? (chevron === 'start' ? [chev(), link()] : [link(), chev()])
|
|
121
|
-
: [spawn(toggleProps({ className: 'menu-title menu-toggle', children:
|
|
123
|
+
: [spawn(toggleProps({ className: 'menu-title menu-toggle', children: title }))];
|
|
122
124
|
heading.forEach((h) => { if (h.tagName === 'BUTTON') toggles.push(h); });
|
|
123
125
|
|
|
124
126
|
const el: HTMLElement = spawn({
|
|
@@ -132,10 +134,10 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
132
134
|
id: listId,
|
|
133
135
|
children: spawn({
|
|
134
136
|
tag: 'ul',
|
|
135
|
-
children:
|
|
137
|
+
children: items.map((item) => spawn({
|
|
136
138
|
tag: 'li',
|
|
137
139
|
'data-divider': item.divider || undefined,
|
|
138
|
-
children: item.href !== undefined
|
|
140
|
+
children: isGroup(item) ? renderGroup(groupId(item, sid), item.label, item.href, item.items as MenuItem[]) : item.href !== undefined
|
|
139
141
|
? spawn({
|
|
140
142
|
tag: 'a',
|
|
141
143
|
className: 'menu-item',
|
|
@@ -176,7 +178,7 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
176
178
|
if (events && events.keydown) events.keydown(event, el);
|
|
177
179
|
},
|
|
178
180
|
},
|
|
179
|
-
children: sections.map(
|
|
181
|
+
children: sections.map((section) => renderGroup(sectionId(section), section.title as Child, section.href, section.items)),
|
|
180
182
|
});
|
|
181
183
|
}, props as MenuListProps) as any;
|
|
182
184
|
}
|