@nurvus/ui 0.1.0-alpha.15 → 0.1.0-alpha.17
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 +17 -1
- package/dist/icons.svg +74 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +15 -10
- package/dist/respawn.js +1 -1
- package/dist/types/anchor.d.ts +37 -0
- package/dist/types/breakpoints.d.ts +14 -0
- package/dist/types/components/Anchor/Anchor.d.ts +30 -0
- package/dist/types/components/Dropdown/Dropdown.d.ts +31 -0
- package/dist/types/components/FullPageModal/FullPageModal.d.ts +11 -2
- package/dist/types/components/Grid/Grid.d.ts +26 -0
- package/dist/types/components/Loader/Loader.d.ts +21 -0
- package/dist/types/components/Menu/Menu.d.ts +61 -19
- package/dist/types/components/Progress/Progress.d.ts +19 -5
- package/dist/types/components/Stack/Stack.d.ts +26 -0
- package/dist/types/dialog.d.ts +12 -0
- package/dist/types/dropdown.d.ts +9 -0
- package/dist/types/icons.d.ts +29 -0
- package/dist/types/index.d.ts +13 -1
- package/dist/types/loader.d.ts +28 -0
- package/dist/types/logo.d.ts +5 -0
- package/dist/types/menu.d.ts +43 -14
- package/dist/types/progress.d.ts +11 -0
- package/dist/types/respawn/Anchor.d.ts +28 -0
- package/dist/types/respawn/Dropdown.d.ts +27 -0
- package/dist/types/respawn/FullPageModal.d.ts +7 -1
- package/dist/types/respawn/Grid.d.ts +21 -0
- package/dist/types/respawn/Loader.d.ts +18 -0
- package/dist/types/respawn/Menu.d.ts +47 -15
- package/dist/types/respawn/Progress.d.ts +18 -6
- package/dist/types/respawn/Stack.d.ts +21 -0
- package/dist/types/respawn/index.d.ts +13 -1
- package/package.json +1 -3
- package/src/anchor.ts +127 -0
- package/src/breakpoints.ts +28 -0
- package/src/components/Anchor/Anchor.md +59 -0
- package/src/components/Anchor/Anchor.scss +65 -0
- package/src/components/Anchor/Anchor.tsx +105 -0
- package/src/components/Button/Button.scss +20 -20
- package/src/components/Dropdown/Dropdown.md +48 -0
- package/src/components/Dropdown/Dropdown.scss +67 -0
- package/src/components/Dropdown/Dropdown.tsx +110 -0
- package/src/components/FullPageModal/FullPageModal.md +20 -17
- package/src/components/FullPageModal/FullPageModal.scss +5 -0
- package/src/components/FullPageModal/FullPageModal.tsx +37 -3
- package/src/components/Grid/Grid.md +37 -0
- package/src/components/Grid/Grid.scss +37 -0
- package/src/components/Grid/Grid.tsx +54 -0
- package/src/components/HistoryList/HistoryList.scss +2 -2
- package/src/components/Loader/Loader.md +30 -0
- package/src/components/Loader/Loader.scss +42 -0
- package/src/components/Loader/Loader.tsx +68 -0
- package/src/components/Menu/Menu.md +67 -3
- package/src/components/Menu/Menu.scss +126 -18
- package/src/components/Menu/Menu.tsx +159 -40
- package/src/components/Progress/Progress.md +27 -26
- package/src/components/Progress/Progress.scss +41 -36
- package/src/components/Progress/Progress.tsx +50 -6
- package/src/components/Select/Select.scss +20 -19
- package/src/components/Stack/Stack.md +43 -0
- package/src/components/Stack/Stack.scss +36 -0
- package/src/components/Stack/Stack.tsx +52 -0
- package/src/components/Table/Table.scss +1 -1
- package/src/components/Tag/Tag.scss +2 -2
- package/src/components/Title/Title.scss +6 -6
- package/src/components/Tree/Tree.md +1 -1
- package/src/css.ts +5 -0
- package/src/dialog.ts +47 -0
- package/src/dropdown.ts +24 -0
- package/src/icons.ts +31 -0
- package/src/index.ts +13 -1
- package/src/loader.ts +43 -0
- package/src/logo.ts +12 -0
- package/src/menu.ts +91 -21
- package/src/progress.ts +21 -0
- package/src/respawn/Anchor.ts +126 -0
- package/src/respawn/Dropdown.ts +90 -0
- package/src/respawn/FullPageModal.ts +30 -3
- package/src/respawn/Grid.ts +41 -0
- package/src/respawn/Loader.ts +77 -0
- package/src/respawn/Menu.ts +144 -37
- package/src/respawn/Progress.ts +56 -13
- package/src/respawn/Stack.ts +37 -0
- package/src/respawn/index.ts +13 -1
- package/src/styles/breakpoints.scss +3 -0
- package/src/styles/theme.scss +55 -55
- package/src/styles/type.json +9 -9
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import { loaderRings, LOGO_VIEWBOX } from '../../loader';
|
|
5
|
+
import './Loader.scss';
|
|
6
|
+
|
|
7
|
+
export interface LoaderProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
8
|
+
/** Width and height of the mark. Defaults to 2em (twice the text size). A number is pixels. */
|
|
9
|
+
size?: number | string;
|
|
10
|
+
/** What is loading, for screen readers ("Loading photos"). Defaults to "Loading". */
|
|
11
|
+
label?: string;
|
|
12
|
+
/** Shows the label beside the mark, not only to screen readers. */
|
|
13
|
+
showLabel?: boolean;
|
|
14
|
+
/** Fills the segments with the logo's gradients (for big loaders); their ids start with this. Without it, one color each. */
|
|
15
|
+
id?: string;
|
|
16
|
+
/** Custom class name. */
|
|
17
|
+
className?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Shows that something is loading: the NURVUS mark with a light running round
|
|
22
|
+
* it (the logo's Pulse). Screen readers hear the label once, politely
|
|
23
|
+
* (role="status"). The light keeps running with reduced motion: it only
|
|
24
|
+
* changes brightness, nothing moves, and a loader that stopped would look
|
|
25
|
+
* stuck (WCAG 2.2.2 counts a loading animation as essential).
|
|
26
|
+
*/
|
|
27
|
+
export default function Loader({
|
|
28
|
+
size = '2em',
|
|
29
|
+
label = 'Loading',
|
|
30
|
+
showLabel = false,
|
|
31
|
+
id,
|
|
32
|
+
className,
|
|
33
|
+
...restProps
|
|
34
|
+
}: LoaderProps) {
|
|
35
|
+
const { inner, outer, gradients } = loaderRings(id);
|
|
36
|
+
const ring = (cls: string, segments: typeof inner) => (
|
|
37
|
+
<g className={ `loader-ring ${cls}` }>
|
|
38
|
+
{ segments.map((s, i) => (
|
|
39
|
+
<path key={ i } className="loader-seg" d={ s.d } fill={ s.fill } style={ { '--nurvus-loader-at': String(s.angle) } as React.CSSProperties } />
|
|
40
|
+
)) }
|
|
41
|
+
</g>
|
|
42
|
+
);
|
|
43
|
+
return (
|
|
44
|
+
<div
|
|
45
|
+
className={ classNames(className, 'loader', CONSTANT.CLASS_PREFIX) }
|
|
46
|
+
role="status"
|
|
47
|
+
data-label={ showLabel ? 'shown' : 'hidden' }
|
|
48
|
+
id={ id }
|
|
49
|
+
{ ...restProps }
|
|
50
|
+
>
|
|
51
|
+
<svg className="loader-mark" viewBox={ LOGO_VIEWBOX } width={ size } height={ size } aria-hidden="true" focusable="false">
|
|
52
|
+
{ gradients.length > 0 && (
|
|
53
|
+
<defs>
|
|
54
|
+
{ gradients.map((g) => (
|
|
55
|
+
<linearGradient key={ g.id } id={ g.id } gradientUnits="userSpaceOnUse" x1={ g.x1 } y1={ g.y1 } x2={ g.x2 } y2={ g.y2 }>
|
|
56
|
+
<stop stopColor={ g.from } />
|
|
57
|
+
<stop offset="1" stopColor={ g.to } />
|
|
58
|
+
</linearGradient>
|
|
59
|
+
)) }
|
|
60
|
+
</defs>
|
|
61
|
+
) }
|
|
62
|
+
{ ring('loader-inner', inner) }
|
|
63
|
+
{ ring('loader-outer', outer) }
|
|
64
|
+
</svg>
|
|
65
|
+
<span className="loader-label">{ label }</span>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
**
|
|
1
|
+
**The items decide what it is**, and screen readers hear it that way:
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
| Items | It becomes | Screen readers say | Keys |
|
|
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 |
|
|
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
|
+
| 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
|
+
|
|
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"). Links take Enter only, as everywhere on the web (Space scrolls the page); buttons, chevrons and tree rows take Enter and Space.
|
|
10
|
+
|
|
11
|
+
**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.
|
|
4
12
|
|
|
5
|
-
By keyboard: Tab goes link by link as usual, and inside the menu ↑ ↓ move between rows (going round at the ends), Home and End jump to the first and last, → opens a section and ← closes it. Enter follows a link; Enter or Space opens and closes a section (its chevron is a button). Links take Enter only, as everywhere on the web: Space scrolls the page.
|
|
6
13
|
```jsx
|
|
7
14
|
import { Menu } from '@nurvus/ui';
|
|
8
15
|
|
|
@@ -41,3 +48,60 @@ import { Menu } from '@nurvus/ui';
|
|
|
41
48
|
/>
|
|
42
49
|
</div>
|
|
43
50
|
```
|
|
51
|
+
|
|
52
|
+
**Actions.** Items with `onSelect` and no `href`: a menu of commands. Tab reaches it once (the item you were last on), then ↑ ↓ move and typing a letter jumps to the next item that starts with it. A disabled item stays in the list, so people still find it, but does nothing.
|
|
53
|
+
```jsx
|
|
54
|
+
import { Menu, Text } from '@nurvus/ui';
|
|
55
|
+
|
|
56
|
+
const Actions = () => {
|
|
57
|
+
const [done, setDone] = React.useState('Nothing yet');
|
|
58
|
+
const run = (what) => () => setDone(what);
|
|
59
|
+
return (
|
|
60
|
+
<div style={ { maxWidth: 260 } }>
|
|
61
|
+
<Menu
|
|
62
|
+
id="layer-actions"
|
|
63
|
+
label="Layer actions"
|
|
64
|
+
sections={ [
|
|
65
|
+
{ title: 'Edit', items: [
|
|
66
|
+
{ label: 'Duplicate', onSelect: run('Duplicated') },
|
|
67
|
+
{ label: 'Rename', onSelect: run('Renamed') },
|
|
68
|
+
{ label: 'Group', onSelect: run('Grouped'), disabled: true },
|
|
69
|
+
] },
|
|
70
|
+
{ title: 'Arrange', items: [
|
|
71
|
+
{ label: 'Bring to front', onSelect: run('Brought to front') },
|
|
72
|
+
{ label: 'Send to back', onSelect: run('Sent to back') },
|
|
73
|
+
{ label: 'Delete', onSelect: run('Deleted'), divider: true },
|
|
74
|
+
] },
|
|
75
|
+
] }
|
|
76
|
+
/>
|
|
77
|
+
<Text variant="caption" tone="muted">{ done }</Text>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
<Actions />
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**A tree.** `kind="tree"` takes Tree's props: nested `items`, the `activePath` of the picked row, and `onClick(event, path)`.
|
|
85
|
+
```jsx
|
|
86
|
+
import { Menu } from '@nurvus/ui';
|
|
87
|
+
|
|
88
|
+
const Layers = () => {
|
|
89
|
+
const [path, setPath] = React.useState([1, 0]);
|
|
90
|
+
return (
|
|
91
|
+
<div style={ { maxWidth: 260 } }>
|
|
92
|
+
<Menu
|
|
93
|
+
kind="tree"
|
|
94
|
+
label="Layers"
|
|
95
|
+
activePath={ path }
|
|
96
|
+
onClick={ (event, next) => setPath(next) }
|
|
97
|
+
items={ [
|
|
98
|
+
{ label: 'Header' },
|
|
99
|
+
{ label: 'Hero', items: [{ label: 'Title' }, { label: 'Image' }] },
|
|
100
|
+
{ label: 'Footer' },
|
|
101
|
+
] }
|
|
102
|
+
/>
|
|
103
|
+
</div>
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
<Layers />
|
|
107
|
+
```
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
@import "../../styles/variables.scss";
|
|
2
2
|
|
|
3
|
-
//
|
|
4
|
-
//
|
|
3
|
+
// Menu: navigation (links; sections open and close by animating the list's
|
|
4
|
+
// row, grid-template-rows 1fr <-> 0fr: smooth, no height measuring) or a menu
|
|
5
|
+
// of actions (buttons, data-kind="actions").
|
|
5
6
|
$menu-speed: 0.2s;
|
|
6
7
|
|
|
7
8
|
.#{$cls-prefix}.menu {
|
|
8
|
-
font-size:
|
|
9
|
-
line-height:
|
|
9
|
+
font-size: 0.875rem;
|
|
10
|
+
line-height: 1.25rem;
|
|
10
11
|
|
|
11
12
|
.menu-section + .menu-section { margin-top: var(--space--8); }
|
|
12
13
|
|
|
@@ -19,9 +20,9 @@ $menu-speed: 0.2s;
|
|
|
19
20
|
.menu-title {
|
|
20
21
|
flex: 1;
|
|
21
22
|
min-width: 0;
|
|
22
|
-
padding: var(--space--4)
|
|
23
|
+
padding: var(--space--4) 0.625rem;
|
|
23
24
|
border: 0;
|
|
24
|
-
border-radius:
|
|
25
|
+
border-radius: 0.375rem;
|
|
25
26
|
background: none;
|
|
26
27
|
color: var(--nv-text);
|
|
27
28
|
font: inherit;
|
|
@@ -44,8 +45,8 @@ $menu-speed: 0.2s;
|
|
|
44
45
|
|
|
45
46
|
&::after {
|
|
46
47
|
content: '';
|
|
47
|
-
width:
|
|
48
|
-
height:
|
|
48
|
+
width: 0.375rem;
|
|
49
|
+
height: 0.375rem;
|
|
49
50
|
margin: 0 var(--space--4);
|
|
50
51
|
border-right: 1.5px solid currentColor;
|
|
51
52
|
border-bottom: 1.5px solid currentColor;
|
|
@@ -57,11 +58,11 @@ $menu-speed: 0.2s;
|
|
|
57
58
|
// The icon-only toggle beside a heading that is a link.
|
|
58
59
|
.menu-toggle:not(.menu-title) {
|
|
59
60
|
flex: none;
|
|
60
|
-
width:
|
|
61
|
-
height:
|
|
61
|
+
width: 1.5rem;
|
|
62
|
+
height: 1.5rem;
|
|
62
63
|
padding: 0;
|
|
63
64
|
border: 0;
|
|
64
|
-
border-radius:
|
|
65
|
+
border-radius: 0.375rem;
|
|
65
66
|
background: none;
|
|
66
67
|
color: var(--nv-text-muted);
|
|
67
68
|
cursor: pointer;
|
|
@@ -88,8 +89,8 @@ $menu-speed: 0.2s;
|
|
|
88
89
|
min-height: 0;
|
|
89
90
|
overflow: hidden;
|
|
90
91
|
// Room on the left for the current-page bar, which sits outside the item.
|
|
91
|
-
margin: 0 0 0 -
|
|
92
|
-
padding: 0 0 0
|
|
92
|
+
margin: 0 0 0 -0.75rem;
|
|
93
|
+
padding: 0 0 0 0.75rem; // no vertical padding: it would stay when the row closes to 0
|
|
93
94
|
list-style: none;
|
|
94
95
|
}
|
|
95
96
|
|
|
@@ -113,10 +114,22 @@ $menu-speed: 0.2s;
|
|
|
113
114
|
.menu-item {
|
|
114
115
|
position: relative;
|
|
115
116
|
display: block;
|
|
116
|
-
|
|
117
|
-
|
|
117
|
+
box-sizing: border-box;
|
|
118
|
+
width: 100%;
|
|
119
|
+
padding: var(--space--4) 0.625rem var(--space--4) 1.25rem;
|
|
120
|
+
border: 0;
|
|
121
|
+
border-radius: 0.375rem;
|
|
122
|
+
background: none;
|
|
118
123
|
color: var(--nv-text-muted);
|
|
124
|
+
font: inherit;
|
|
125
|
+
text-align: left;
|
|
119
126
|
text-decoration: none;
|
|
127
|
+
cursor: pointer;
|
|
128
|
+
|
|
129
|
+
&:disabled,
|
|
130
|
+
&[aria-disabled="true"] { color: var(--nv-text-subtle); cursor: not-allowed; }
|
|
131
|
+
&:disabled:hover,
|
|
132
|
+
&[aria-disabled="true"]:hover { background: none; color: var(--nv-text-subtle); }
|
|
120
133
|
|
|
121
134
|
&:hover { background: var(--nv-surface-hover); color: var(--nv-text); }
|
|
122
135
|
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
|
|
@@ -133,18 +146,113 @@ $menu-speed: 0.2s;
|
|
|
133
146
|
content: '';
|
|
134
147
|
position: absolute;
|
|
135
148
|
top: 50%;
|
|
136
|
-
left: -
|
|
137
|
-
width:
|
|
138
|
-
height:
|
|
149
|
+
left: -0.5rem;
|
|
150
|
+
width: 0.25rem;
|
|
151
|
+
height: 1.125rem;
|
|
139
152
|
border-radius: 2px;
|
|
140
153
|
background: var(--nv-accent);
|
|
141
154
|
transform: translateY(-50%);
|
|
142
155
|
}
|
|
143
156
|
}
|
|
144
157
|
|
|
158
|
+
// Chevron at the start (data-chevron="start", the default since 0.1.0-alpha.17;
|
|
159
|
+
// as Material UI draws it): before the title, › while the section is closed
|
|
160
|
+
// and ⌄ while it is open, a quarter turn between. Every chevron is centered
|
|
161
|
+
// 12px from the left, both title kinds start their text at 30px, and a
|
|
162
|
+
// hairline guide runs down an open section's items under the chevron; the
|
|
163
|
+
// current page's bar is drawn on that line.
|
|
164
|
+
&[data-chevron="start"] {
|
|
165
|
+
.menu-heading { gap: var(--space--4); }
|
|
166
|
+
|
|
167
|
+
.menu-toggle::after { transform: translateY(-1px) rotate(45deg); }
|
|
168
|
+
.menu-section[data-open="false"] .menu-toggle::after { transform: translateX(-1px) rotate(-45deg); }
|
|
169
|
+
|
|
170
|
+
// A linked title: the chevron button first, then the link.
|
|
171
|
+
.menu-toggle:not(.menu-title) + .menu-title { padding-left: 0.125rem; }
|
|
172
|
+
|
|
173
|
+
// A title that is the toggle: the chevron drawn before the words.
|
|
174
|
+
.menu-title.menu-toggle {
|
|
175
|
+
justify-content: flex-start;
|
|
176
|
+
padding-left: var(--space--4);
|
|
177
|
+
|
|
178
|
+
&::after { content: none; }
|
|
179
|
+
|
|
180
|
+
&::before {
|
|
181
|
+
content: "";
|
|
182
|
+
flex: none;
|
|
183
|
+
width: 0.375rem;
|
|
184
|
+
height: 0.375rem;
|
|
185
|
+
margin: 0 0.9375rem 0 0.3125rem;
|
|
186
|
+
border-right: 1.5px solid currentColor;
|
|
187
|
+
border-bottom: 1.5px solid currentColor;
|
|
188
|
+
transform: translateY(-1px) rotate(45deg);
|
|
189
|
+
transition: transform $menu-speed ease;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
.menu-section[data-open="false"] .menu-title.menu-toggle::before { transform: translateX(-1px) rotate(-45deg); }
|
|
194
|
+
|
|
195
|
+
ul {
|
|
196
|
+
position: relative;
|
|
197
|
+
margin: 0;
|
|
198
|
+
padding: 0;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// The guide line, under the chevrons.
|
|
202
|
+
ul::before {
|
|
203
|
+
content: "";
|
|
204
|
+
position: absolute;
|
|
205
|
+
top: 2px;
|
|
206
|
+
bottom: var(--space--8);
|
|
207
|
+
left: calc(0.75rem - 0.5px);
|
|
208
|
+
width: 1px;
|
|
209
|
+
background: var(--nv-border);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// A divider starts where the items do, right of the guide line.
|
|
213
|
+
li[data-divider] { margin-left: 1.25rem; }
|
|
214
|
+
li[data-divider] .menu-item { width: 100%; margin-left: 0; }
|
|
215
|
+
|
|
216
|
+
.menu-item {
|
|
217
|
+
width: calc(100% - 1.25rem);
|
|
218
|
+
margin-left: 1.25rem;
|
|
219
|
+
padding-left: var(--space--16);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The current page: a bar on the guide line, the item's full height.
|
|
223
|
+
.menu-item[data-active="true"]::before {
|
|
224
|
+
top: 2px;
|
|
225
|
+
bottom: 2px;
|
|
226
|
+
left: calc(-0.5rem - 1px);
|
|
227
|
+
width: 2px;
|
|
228
|
+
height: auto;
|
|
229
|
+
border-radius: 1px;
|
|
230
|
+
transform: none;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Actions: always open; section titles are labels, not buttons; items in the text color.
|
|
235
|
+
&[data-kind="actions"] {
|
|
236
|
+
.menu-title {
|
|
237
|
+
cursor: default;
|
|
238
|
+
color: var(--nv-text-muted);
|
|
239
|
+
font-size: 0.75rem;
|
|
240
|
+
line-height: 1rem;
|
|
241
|
+
letter-spacing: 0.04em;
|
|
242
|
+
text-transform: uppercase;
|
|
243
|
+
|
|
244
|
+
&:hover { background: none; }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
.menu-item { padding-left: 0.625rem; color: var(--nv-text); }
|
|
248
|
+
.menu-item[aria-disabled="true"] { color: var(--nv-text-subtle); }
|
|
249
|
+
li:last-child { margin-bottom: 0; }
|
|
250
|
+
}
|
|
251
|
+
|
|
145
252
|
@media (prefers-reduced-motion: reduce) {
|
|
146
253
|
.menu-items,
|
|
147
254
|
.menu-toggle::after,
|
|
255
|
+
.menu-toggle::before,
|
|
148
256
|
.menu-section[data-open="false"] ul { transition: none; }
|
|
149
257
|
}
|
|
150
258
|
}
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import classNames from 'classnames';
|
|
3
3
|
import CONSTANT from '../../constants';
|
|
4
|
-
import { sectionId, startsOpen, menuKeydown,
|
|
5
|
-
import Tree from '../Tree/Tree';
|
|
4
|
+
import { sectionId, startsOpen, menuKeydown, menuFocus, menuKind, MenuKind } from '../../menu';
|
|
5
|
+
import Tree, { TreeProps, TreeItem } from '../Tree/Tree';
|
|
6
6
|
import './Menu.scss';
|
|
7
7
|
|
|
8
|
+
export type { MenuKind };
|
|
9
|
+
|
|
8
10
|
export interface MenuItem {
|
|
9
|
-
/** What the
|
|
11
|
+
/** What the item says. */
|
|
10
12
|
label: React.ReactNode;
|
|
11
|
-
/**
|
|
12
|
-
href
|
|
13
|
+
/** Makes the item a link to this address. The item whose href equals `current` is marked as the current page. */
|
|
14
|
+
href?: string;
|
|
15
|
+
/** Makes the item a button that calls this (an action: "Duplicate", "Sign out"). */
|
|
16
|
+
onSelect?: (event: React.MouseEvent<HTMLElement>) => void;
|
|
17
|
+
/** Shows the item but stops it working (actions only). */
|
|
18
|
+
disabled?: boolean;
|
|
13
19
|
/** Draws a hairline above this item, to split a section into groups. */
|
|
14
20
|
divider?: boolean;
|
|
15
21
|
}
|
|
@@ -17,65 +23,105 @@ export interface MenuItem {
|
|
|
17
23
|
export interface MenuSection {
|
|
18
24
|
/** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
|
|
19
25
|
id?: string;
|
|
20
|
-
/** The section heading. */
|
|
21
|
-
title
|
|
26
|
+
/** The section heading. Optional for actions. */
|
|
27
|
+
title?: React.ReactNode;
|
|
22
28
|
/** Makes the heading a link too (e.g. to the section's overview page). */
|
|
23
29
|
href?: string;
|
|
24
|
-
/** The section's
|
|
30
|
+
/** The section's items. */
|
|
25
31
|
items: MenuItem[];
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onToggle'> {
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onToggle' | 'onClick'> {
|
|
35
|
+
/**
|
|
36
|
+
* What the menu is, which decides what screen readers hear and which keys
|
|
37
|
+
* work: 'nav' (links: a navigation landmark), 'actions' (commands: a menu
|
|
38
|
+
* of buttons) or 'tree' (rows to pick from, with Tree's props). Left out,
|
|
39
|
+
* it follows the props: navigation if any item has an href, actions if none
|
|
40
|
+
* has, a tree for `items`.
|
|
41
|
+
*/
|
|
42
|
+
kind?: MenuKind;
|
|
43
|
+
/** Navigation and actions: the sections, in order. */
|
|
44
|
+
sections?: MenuSection[];
|
|
45
|
+
/** Navigation: the href of the page being shown. Its link is marked, and its section opens. */
|
|
32
46
|
current?: string;
|
|
33
|
-
/**
|
|
47
|
+
/** Navigation: which sections start open: true (all), false (none) or a map of section id → open. Defaults to all. */
|
|
34
48
|
defaultOpen?: boolean | Record<string, boolean>;
|
|
35
|
-
/**
|
|
49
|
+
/** Navigation: called when a section opens or closes (e.g. to remember it). */
|
|
36
50
|
onToggle?: (id: string, open: boolean) => void;
|
|
37
|
-
/**
|
|
51
|
+
/** Navigation: called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
38
52
|
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
39
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Navigation: where each section's chevron sits. 'start' (the default): before
|
|
55
|
+
* the title, pointing right when closed and down when open, with a guide line
|
|
56
|
+
* down the open section's items (as Material UI draws it). 'end': after the
|
|
57
|
+
* title, down when closed and up when open.
|
|
58
|
+
*/
|
|
59
|
+
chevron?: 'start' | 'end';
|
|
60
|
+
/** Tree: the rows, each with optional child `items`. */
|
|
61
|
+
items?: TreeItem[];
|
|
62
|
+
/** Tree: the picked row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
|
|
63
|
+
activePath?: number[];
|
|
64
|
+
/** Tree: called when a row is picked (a click, Enter or Space), with the event and the row's path. */
|
|
65
|
+
onClick?: (event: React.MouseEvent<HTMLDivElement>, path: number[]) => void;
|
|
66
|
+
/** Tree: draws a row's content instead of its label. */
|
|
67
|
+
itemRenderer?: (item: TreeItem) => React.ReactNode;
|
|
68
|
+
/** Tree: the deepest level that still indents. */
|
|
69
|
+
maxIndentDepth?: number;
|
|
70
|
+
/** What screen readers call the menu ("Pages", "Actions", "Layers"). */
|
|
40
71
|
label?: string;
|
|
41
|
-
/** Prefix for the ids
|
|
72
|
+
/** Prefix for the ids inside (each toggle names its list). Defaults to "menu". A menu of actions also carries it (so a button can point at it with aria-controls). */
|
|
42
73
|
id?: string;
|
|
43
74
|
/** Custom class name. */
|
|
44
75
|
className?: string;
|
|
45
76
|
}
|
|
46
77
|
|
|
78
|
+
/** Menu's props for navigation and actions. */
|
|
79
|
+
export type MenuListProps = Omit<MenuProps, 'items' | 'activePath' | 'onClick' | 'itemRenderer' | 'maxIndentDepth'>;
|
|
80
|
+
/** Menu kind="tree": Tree's props (Tree is the same thing under its own name). */
|
|
81
|
+
export type MenuTreeProps = TreeProps & { kind: 'tree' };
|
|
82
|
+
|
|
47
83
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
84
|
+
* One component for lists of things to go to, do or pick. The items decide
|
|
85
|
+
* what it is, and screen readers hear it that way:
|
|
86
|
+
* - links: site navigation (sections that open and close, the current page
|
|
87
|
+
* marked), and Tab goes link by link;
|
|
88
|
+
* - actions: a menu of commands, one Tab stop, the arrow keys move;
|
|
89
|
+
* - kind="tree": a tree to pick a row from, like layers (see Tree).
|
|
90
|
+
* Up and Down move between items in all three.
|
|
51
91
|
*/
|
|
52
|
-
export default function Menu(
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
-
|
|
92
|
+
export default function Menu(props: MenuProps) {
|
|
93
|
+
const kind = menuKind(props);
|
|
94
|
+
if (kind === 'tree') {
|
|
95
|
+
const { kind: _kind, sections: _s, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, chevron: _ch, ...tree } = props;
|
|
96
|
+
return <Tree { ...(tree as TreeProps) } />;
|
|
97
|
+
}
|
|
98
|
+
const { label = kind === 'actions' ? 'Actions' : 'Pages', items: _i, activePath: _a, onClick: _o, itemRenderer: _r, maxIndentDepth: _m, ...rest } = props;
|
|
99
|
+
return kind === 'actions' ? <MenuActions label={ label } { ...rest } /> : <MenuNav label={ label } { ...rest } />;
|
|
56
100
|
}
|
|
57
101
|
|
|
58
102
|
function MenuNav({
|
|
103
|
+
kind: _kind,
|
|
59
104
|
sections = [],
|
|
60
105
|
current,
|
|
61
|
-
defaultOpen,
|
|
106
|
+
defaultOpen = true,
|
|
62
107
|
onToggle,
|
|
63
108
|
onNavigate,
|
|
109
|
+
chevron = 'start',
|
|
64
110
|
label,
|
|
65
111
|
id = 'menu',
|
|
66
112
|
className,
|
|
67
113
|
onKeyDown,
|
|
68
114
|
...restProps
|
|
69
|
-
}:
|
|
115
|
+
}: MenuListProps) {
|
|
70
116
|
// The current page's section starts open, whatever defaultOpen says.
|
|
71
117
|
const [open, setOpen] = React.useState<Record<string, boolean>>(() => {
|
|
72
|
-
const home = sections.find((s) => s.items.some((item) => item.href === current));
|
|
118
|
+
const home = sections.find((s) => s.items.some((item) => item.href !== undefined && item.href === current));
|
|
73
119
|
return Object.fromEntries(sections.map((s) => [sectionId(s), s === home || startsOpen(defaultOpen, sectionId(s))]));
|
|
74
120
|
});
|
|
75
121
|
|
|
76
122
|
// Navigating into a closed section opens it, so the current page is always visible.
|
|
77
123
|
React.useEffect(() => {
|
|
78
|
-
const home = sections.find((s) => s.items.some((item) => item.href === current));
|
|
124
|
+
const home = sections.find((s) => s.items.some((item) => item.href !== undefined && item.href === current));
|
|
79
125
|
if (home && open[sectionId(home)] === false) setOpen((was) => ({ ...was, [sectionId(home)]: true }));
|
|
80
126
|
}, [current]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
81
127
|
|
|
@@ -89,6 +135,8 @@ function MenuNav({
|
|
|
89
135
|
return (
|
|
90
136
|
<nav className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) }
|
|
91
137
|
aria-label={ label }
|
|
138
|
+
data-kind="nav"
|
|
139
|
+
data-chevron={ chevron }
|
|
92
140
|
{ ...restProps }
|
|
93
141
|
onKeyDown={ (e) => { menuKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
|
|
94
142
|
>
|
|
@@ -107,10 +155,11 @@ function MenuNav({
|
|
|
107
155
|
<div className="menu-heading">
|
|
108
156
|
{ section.href ? (
|
|
109
157
|
<>
|
|
158
|
+
{ chevron === 'start' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } /> }
|
|
110
159
|
<a className="menu-title" href={ section.href } data-active={ current === section.href } onClick={ navigate(section.href) }>
|
|
111
160
|
{ section.title }
|
|
112
161
|
</a>
|
|
113
|
-
<button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } />
|
|
162
|
+
{ chevron === 'end' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } /> }
|
|
114
163
|
</>
|
|
115
164
|
) : (
|
|
116
165
|
<button className="menu-title menu-toggle" { ...toggleProps }>{ section.title }</button>
|
|
@@ -118,17 +167,23 @@ function MenuNav({
|
|
|
118
167
|
</div>
|
|
119
168
|
<div className="menu-items" id={ listId }>
|
|
120
169
|
<ul>
|
|
121
|
-
{ section.items.map((item) => (
|
|
122
|
-
<li key={ item.href } data-divider={ item.divider || undefined }>
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
+
) }
|
|
132
187
|
</li>
|
|
133
188
|
)) }
|
|
134
189
|
</ul>
|
|
@@ -139,3 +194,67 @@ function MenuNav({
|
|
|
139
194
|
</nav>
|
|
140
195
|
);
|
|
141
196
|
}
|
|
197
|
+
|
|
198
|
+
function MenuActions({
|
|
199
|
+
kind: _kind,
|
|
200
|
+
sections = [],
|
|
201
|
+
label,
|
|
202
|
+
id = 'menu',
|
|
203
|
+
className,
|
|
204
|
+
onKeyDown,
|
|
205
|
+
onFocus,
|
|
206
|
+
current: _current,
|
|
207
|
+
defaultOpen: _defaultOpen,
|
|
208
|
+
onToggle: _onToggle,
|
|
209
|
+
onNavigate: _onNavigate,
|
|
210
|
+
chevron: _chevron,
|
|
211
|
+
...restProps
|
|
212
|
+
}: MenuListProps) {
|
|
213
|
+
let first = true;
|
|
214
|
+
return (
|
|
215
|
+
<div
|
|
216
|
+
className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) }
|
|
217
|
+
role="menu"
|
|
218
|
+
id={ id }
|
|
219
|
+
aria-label={ label }
|
|
220
|
+
data-kind="actions"
|
|
221
|
+
{ ...restProps }
|
|
222
|
+
onKeyDown={ (e) => { menuKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
|
|
223
|
+
onFocus={ (e) => { menuFocus(e.nativeEvent, e.currentTarget); if (onFocus) onFocus(e); } }
|
|
224
|
+
>
|
|
225
|
+
{ sections.map((section, s) => {
|
|
226
|
+
const sid = sectionId({ id: section.id, title: section.title ?? `section-${s + 1}` });
|
|
227
|
+
const titleId = `${id}-${sid}-title`;
|
|
228
|
+
return (
|
|
229
|
+
<div key={ sid } className="menu-section" data-section={ sid } role="group" aria-labelledby={ section.title ? titleId : undefined }>
|
|
230
|
+
{ section.title && (
|
|
231
|
+
<div className="menu-heading"><span className="menu-title" id={ titleId }>{ section.title }</span></div>
|
|
232
|
+
) }
|
|
233
|
+
<div className="menu-items">
|
|
234
|
+
<ul role="none">
|
|
235
|
+
{ section.items.map((item, i) => {
|
|
236
|
+
const tabIndex = first ? 0 : -1;
|
|
237
|
+
first = false;
|
|
238
|
+
return (
|
|
239
|
+
<li key={ i } role="none" data-divider={ item.divider || undefined }>
|
|
240
|
+
<button
|
|
241
|
+
type="button"
|
|
242
|
+
role="menuitem"
|
|
243
|
+
className="menu-item"
|
|
244
|
+
tabIndex={ tabIndex }
|
|
245
|
+
aria-disabled={ item.disabled || undefined }
|
|
246
|
+
onClick={ (event) => { if (!item.disabled && item.onSelect) item.onSelect(event); } }
|
|
247
|
+
>
|
|
248
|
+
{ item.label }
|
|
249
|
+
</button>
|
|
250
|
+
</li>
|
|
251
|
+
);
|
|
252
|
+
}) }
|
|
253
|
+
</ul>
|
|
254
|
+
</div>
|
|
255
|
+
</div>
|
|
256
|
+
);
|
|
257
|
+
}) }
|
|
258
|
+
</div>
|
|
259
|
+
);
|
|
260
|
+
}
|