@nurvus/ui 0.1.0-alpha.16 → 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 +6 -0
- package/dist/icons.svg +1 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +3 -1
- package/dist/respawn.js +1 -1
- package/dist/types/anchor.d.ts +37 -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/Menu/Menu.d.ts +8 -1
- package/dist/types/dropdown.d.ts +9 -0
- package/dist/types/icons.d.ts +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/respawn/Anchor.d.ts +28 -0
- package/dist/types/respawn/Dropdown.d.ts +27 -0
- package/dist/types/respawn/Menu.d.ts +2 -0
- package/dist/types/respawn/index.d.ts +4 -0
- package/package.json +1 -1
- package/src/anchor.ts +127 -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/Dropdown/Dropdown.md +48 -0
- package/src/components/Dropdown/Dropdown.scss +67 -0
- package/src/components/Dropdown/Dropdown.tsx +110 -0
- package/src/components/Menu/Menu.md +1 -1
- package/src/components/Menu/Menu.scss +78 -0
- package/src/components/Menu/Menu.tsx +15 -3
- package/src/css.ts +2 -0
- package/src/dropdown.ts +24 -0
- package/src/icons.ts +2 -0
- package/src/index.ts +4 -0
- package/src/respawn/Anchor.ts +126 -0
- package/src/respawn/Dropdown.ts +90 -0
- package/src/respawn/Menu.ts +10 -7
- package/src/respawn/index.ts +4 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Styles for Dropdown (React and Respawn: same markup). The popup opens
|
|
2
|
+
// under the button like Select's list.
|
|
3
|
+
@import "../../styles/variables.scss";
|
|
4
|
+
|
|
5
|
+
$dropdown-speed: 0.15s;
|
|
6
|
+
|
|
7
|
+
.#{$cls-prefix}.dropdown {
|
|
8
|
+
position: relative;
|
|
9
|
+
display: inline-block;
|
|
10
|
+
|
|
11
|
+
.dropdown-trigger {
|
|
12
|
+
display: inline-flex;
|
|
13
|
+
align-items: center;
|
|
14
|
+
gap: var(--space--8);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// A turned corner: down while closed, up while open.
|
|
18
|
+
.dropdown-chevron {
|
|
19
|
+
width: 0.375rem;
|
|
20
|
+
height: 0.375rem;
|
|
21
|
+
margin-left: var(--space--2);
|
|
22
|
+
border-right: 1.5px solid currentColor;
|
|
23
|
+
border-bottom: 1.5px solid currentColor;
|
|
24
|
+
transform: translateY(-2px) rotate(45deg);
|
|
25
|
+
transition: transform $dropdown-speed ease;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
&[data-open="true"] .dropdown-chevron { transform: translateY(2px) rotate(225deg); }
|
|
29
|
+
|
|
30
|
+
.dropdown-popup {
|
|
31
|
+
position: absolute;
|
|
32
|
+
z-index: 1000;
|
|
33
|
+
top: calc(100% + var(--space--4));
|
|
34
|
+
left: 0;
|
|
35
|
+
min-width: max(100%, 12rem);
|
|
36
|
+
max-width: 20rem;
|
|
37
|
+
padding: var(--space--4);
|
|
38
|
+
border: 1px solid var(--nv-border);
|
|
39
|
+
border-radius: 0.625rem;
|
|
40
|
+
background: var(--nv-surface);
|
|
41
|
+
box-shadow: 0 12px 32px rgb(0 0 0 / 0.24);
|
|
42
|
+
opacity: 0;
|
|
43
|
+
visibility: hidden;
|
|
44
|
+
transform: translateY(-0.25rem) scale(0.98);
|
|
45
|
+
transform-origin: top left;
|
|
46
|
+
transition: opacity $dropdown-speed ease, transform $dropdown-speed ease, visibility 0s $dropdown-speed;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
&[data-align="end"] .dropdown-popup { left: auto; right: 0; transform-origin: top right; }
|
|
50
|
+
|
|
51
|
+
&[data-open="true"] .dropdown-popup {
|
|
52
|
+
opacity: 1;
|
|
53
|
+
visibility: visible;
|
|
54
|
+
transform: none;
|
|
55
|
+
transition: opacity $dropdown-speed ease, transform $dropdown-speed ease, visibility 0s 0s;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.menu-section + .menu-section { margin-top: var(--space--4); padding-top: var(--space--4); border-top: 1px solid var(--nv-border); }
|
|
59
|
+
.menu-heading { padding: var(--space--4) 0 0; }
|
|
60
|
+
.menu-title { padding: var(--space--2) var(--space--8); }
|
|
61
|
+
.menu-item { min-height: 1.75rem; padding-left: var(--space--8); }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@media (prefers-reduced-motion: reduce) {
|
|
65
|
+
.#{$cls-prefix}.dropdown .dropdown-popup,
|
|
66
|
+
.#{$cls-prefix}.dropdown .dropdown-chevron { transition: none; }
|
|
67
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import Button, { ButtonSize, ButtonType } from '../Button/Button';
|
|
5
|
+
import Menu, { MenuItem, MenuSection } from '../Menu/Menu';
|
|
6
|
+
import { focusItem, triggerKey } from '../../dropdown';
|
|
7
|
+
import './Dropdown.scss';
|
|
8
|
+
|
|
9
|
+
export interface DropdownProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onSelect'> {
|
|
10
|
+
/** What the button says (text, or an Icon with a label for an icon-only button). */
|
|
11
|
+
label: React.ReactNode;
|
|
12
|
+
/** The actions, in groups (each may have a title). */
|
|
13
|
+
sections?: MenuSection[];
|
|
14
|
+
/** Or the actions as one flat list. */
|
|
15
|
+
items?: MenuItem[];
|
|
16
|
+
/** Which edge of the button the menu lines up with. */
|
|
17
|
+
align?: 'start' | 'end';
|
|
18
|
+
/** The button's look. Defaults to secondary. */
|
|
19
|
+
type?: ButtonType;
|
|
20
|
+
/** The button's size. */
|
|
21
|
+
size?: ButtonSize;
|
|
22
|
+
/** Stops the button working. */
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
/** Names the button and the menu ("<id>-button", "<id>-menu"). */
|
|
25
|
+
id: string;
|
|
26
|
+
/** Custom class name. */
|
|
27
|
+
className?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A button that opens a menu of actions (the WAI-ARIA menu button pattern).
|
|
32
|
+
* A click, Enter, Space or ↓ opens it on its first item, ↑ on its last; inside,
|
|
33
|
+
* the Menu's keys work (↑ ↓ Home End, type a letter); choosing an action runs
|
|
34
|
+
* it, closes the menu and puts focus back on the button; Esc does the same
|
|
35
|
+
* without choosing; Tab or a click outside closes it.
|
|
36
|
+
*/
|
|
37
|
+
export default function Dropdown({
|
|
38
|
+
label,
|
|
39
|
+
sections,
|
|
40
|
+
items,
|
|
41
|
+
align = 'start',
|
|
42
|
+
type = 'secondary',
|
|
43
|
+
size = 'medium',
|
|
44
|
+
disabled = false,
|
|
45
|
+
id,
|
|
46
|
+
className,
|
|
47
|
+
...restProps
|
|
48
|
+
}: DropdownProps) {
|
|
49
|
+
const [open, setOpen] = React.useState(false);
|
|
50
|
+
const root = React.useRef<HTMLDivElement>(null);
|
|
51
|
+
const button = React.useRef<HTMLButtonElement>(null);
|
|
52
|
+
const pending = React.useRef<'first' | 'last' | null>(null);
|
|
53
|
+
|
|
54
|
+
// Focus goes into the menu once it shows.
|
|
55
|
+
React.useEffect(() => {
|
|
56
|
+
if (open && pending.current && root.current) { focusItem(root.current, pending.current); pending.current = null; }
|
|
57
|
+
}, [open]);
|
|
58
|
+
|
|
59
|
+
// A press anywhere outside closes it.
|
|
60
|
+
React.useEffect(() => {
|
|
61
|
+
if (!open) return undefined;
|
|
62
|
+
const away = (event: MouseEvent) => { if (root.current && !root.current.contains(event.target as Node)) setOpen(false); };
|
|
63
|
+
document.addEventListener('mousedown', away);
|
|
64
|
+
return () => document.removeEventListener('mousedown', away);
|
|
65
|
+
}, [open]);
|
|
66
|
+
|
|
67
|
+
const show = (which: 'first' | 'last') => { pending.current = which; setOpen(true); };
|
|
68
|
+
const close = (refocus: boolean) => { setOpen(false); if (refocus && button.current) button.current.focus(); };
|
|
69
|
+
|
|
70
|
+
// Choosing an action runs it, then closes the menu with focus on the button.
|
|
71
|
+
const wrap = (list: MenuItem[]): MenuItem[] => list.map((item) => ({
|
|
72
|
+
...item,
|
|
73
|
+
onSelect: (event) => { if (item.onSelect) item.onSelect(event); close(true); },
|
|
74
|
+
}));
|
|
75
|
+
const groups: MenuSection[] = (sections || [{ items: items || [] }]).map((s) => ({ ...s, items: wrap(s.items) }));
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<div
|
|
79
|
+
ref={ root }
|
|
80
|
+
className={ classNames(className, 'dropdown', CONSTANT.CLASS_PREFIX) }
|
|
81
|
+
data-open={ open }
|
|
82
|
+
data-align={ align }
|
|
83
|
+
{ ...restProps }
|
|
84
|
+
onKeyDown={ (event) => {
|
|
85
|
+
if (event.key === 'Escape' && open) { event.preventDefault(); close(true); }
|
|
86
|
+
} }
|
|
87
|
+
onBlur={ (event) => { if (open && root.current && !root.current.contains(event.relatedTarget as Node)) setOpen(false); } }
|
|
88
|
+
>
|
|
89
|
+
<Button
|
|
90
|
+
ref={ button }
|
|
91
|
+
id={ `${id}-button` }
|
|
92
|
+
className="dropdown-trigger"
|
|
93
|
+
type={ type }
|
|
94
|
+
size={ size }
|
|
95
|
+
disabled={ disabled }
|
|
96
|
+
aria-haspopup="menu"
|
|
97
|
+
aria-expanded={ open }
|
|
98
|
+
aria-controls={ `${id}-menu` }
|
|
99
|
+
onClick={ () => (open ? close(false) : show('first')) }
|
|
100
|
+
onKeyDown={ (event) => { const which = triggerKey(event.nativeEvent); if (which) { event.preventDefault(); show(which); } } }
|
|
101
|
+
>
|
|
102
|
+
{ label }
|
|
103
|
+
<span className="dropdown-chevron" aria-hidden="true" />
|
|
104
|
+
</Button>
|
|
105
|
+
<div className="dropdown-popup">
|
|
106
|
+
<Menu kind="actions" id={ `${id}-menu` } aria-labelledby={ `${id}-button` } sections={ groups } />
|
|
107
|
+
</div>
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
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
10
|
|
|
11
|
-
**Navigation.** Sections open and close with the chevron (or the whole heading, when it isn't a link). The
|
|
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.
|
|
12
12
|
|
|
13
13
|
```jsx
|
|
14
14
|
import { Menu } from '@nurvus/ui';
|
|
@@ -155,6 +155,82 @@ $menu-speed: 0.2s;
|
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
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
|
+
|
|
158
234
|
// Actions: always open; section titles are labels, not buttons; items in the text color.
|
|
159
235
|
&[data-kind="actions"] {
|
|
160
236
|
.menu-title {
|
|
@@ -169,12 +245,14 @@ $menu-speed: 0.2s;
|
|
|
169
245
|
}
|
|
170
246
|
|
|
171
247
|
.menu-item { padding-left: 0.625rem; color: var(--nv-text); }
|
|
248
|
+
.menu-item[aria-disabled="true"] { color: var(--nv-text-subtle); }
|
|
172
249
|
li:last-child { margin-bottom: 0; }
|
|
173
250
|
}
|
|
174
251
|
|
|
175
252
|
@media (prefers-reduced-motion: reduce) {
|
|
176
253
|
.menu-items,
|
|
177
254
|
.menu-toggle::after,
|
|
255
|
+
.menu-toggle::before,
|
|
178
256
|
.menu-section[data-open="false"] ul { transition: none; }
|
|
179
257
|
}
|
|
180
258
|
}
|
|
@@ -50,6 +50,13 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
|
|
|
50
50
|
onToggle?: (id: string, open: boolean) => void;
|
|
51
51
|
/** Navigation: called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
52
52
|
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
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';
|
|
53
60
|
/** Tree: the rows, each with optional child `items`. */
|
|
54
61
|
items?: TreeItem[];
|
|
55
62
|
/** Tree: the picked row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
|
|
@@ -62,7 +69,7 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
|
|
|
62
69
|
maxIndentDepth?: number;
|
|
63
70
|
/** What screen readers call the menu ("Pages", "Actions", "Layers"). */
|
|
64
71
|
label?: string;
|
|
65
|
-
/** Prefix for the ids inside (each toggle names its list). Defaults to "menu". */
|
|
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). */
|
|
66
73
|
id?: string;
|
|
67
74
|
/** Custom class name. */
|
|
68
75
|
className?: string;
|
|
@@ -85,7 +92,7 @@ export type MenuTreeProps = TreeProps & { kind: 'tree' };
|
|
|
85
92
|
export default function Menu(props: MenuProps) {
|
|
86
93
|
const kind = menuKind(props);
|
|
87
94
|
if (kind === 'tree') {
|
|
88
|
-
const { kind: _kind, sections: _s, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, ...tree } = props;
|
|
95
|
+
const { kind: _kind, sections: _s, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, chevron: _ch, ...tree } = props;
|
|
89
96
|
return <Tree { ...(tree as TreeProps) } />;
|
|
90
97
|
}
|
|
91
98
|
const { label = kind === 'actions' ? 'Actions' : 'Pages', items: _i, activePath: _a, onClick: _o, itemRenderer: _r, maxIndentDepth: _m, ...rest } = props;
|
|
@@ -99,6 +106,7 @@ function MenuNav({
|
|
|
99
106
|
defaultOpen = true,
|
|
100
107
|
onToggle,
|
|
101
108
|
onNavigate,
|
|
109
|
+
chevron = 'start',
|
|
102
110
|
label,
|
|
103
111
|
id = 'menu',
|
|
104
112
|
className,
|
|
@@ -128,6 +136,7 @@ function MenuNav({
|
|
|
128
136
|
<nav className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) }
|
|
129
137
|
aria-label={ label }
|
|
130
138
|
data-kind="nav"
|
|
139
|
+
data-chevron={ chevron }
|
|
131
140
|
{ ...restProps }
|
|
132
141
|
onKeyDown={ (e) => { menuKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
|
|
133
142
|
>
|
|
@@ -146,10 +155,11 @@ function MenuNav({
|
|
|
146
155
|
<div className="menu-heading">
|
|
147
156
|
{ section.href ? (
|
|
148
157
|
<>
|
|
158
|
+
{ chevron === 'start' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } /> }
|
|
149
159
|
<a className="menu-title" href={ section.href } data-active={ current === section.href } onClick={ navigate(section.href) }>
|
|
150
160
|
{ section.title }
|
|
151
161
|
</a>
|
|
152
|
-
<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 } /> }
|
|
153
163
|
</>
|
|
154
164
|
) : (
|
|
155
165
|
<button className="menu-title menu-toggle" { ...toggleProps }>{ section.title }</button>
|
|
@@ -197,6 +207,7 @@ function MenuActions({
|
|
|
197
207
|
defaultOpen: _defaultOpen,
|
|
198
208
|
onToggle: _onToggle,
|
|
199
209
|
onNavigate: _onNavigate,
|
|
210
|
+
chevron: _chevron,
|
|
200
211
|
...restProps
|
|
201
212
|
}: MenuListProps) {
|
|
202
213
|
let first = true;
|
|
@@ -204,6 +215,7 @@ function MenuActions({
|
|
|
204
215
|
<div
|
|
205
216
|
className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) }
|
|
206
217
|
role="menu"
|
|
218
|
+
id={ id }
|
|
207
219
|
aria-label={ label }
|
|
208
220
|
data-kind="actions"
|
|
209
221
|
{ ...restProps }
|
package/src/css.ts
CHANGED
|
@@ -13,6 +13,8 @@ import './components/Icon/Icon.scss';
|
|
|
13
13
|
import './components/Loader/Loader.scss';
|
|
14
14
|
import './components/Tree/Tree.scss';
|
|
15
15
|
import './components/Menu/Menu.scss';
|
|
16
|
+
import './components/Dropdown/Dropdown.scss';
|
|
17
|
+
import './components/Anchor/Anchor.scss';
|
|
16
18
|
import './components/Select/Select.scss';
|
|
17
19
|
import './components/Panel/Panel.scss';
|
|
18
20
|
import './components/Progress/Progress.scss';
|
package/src/dropdown.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Dropdown rules shared by the React and the Respawn Dropdown (no React here):
|
|
2
|
+
// the WAI-ARIA menu button pattern's keys and focus.
|
|
3
|
+
|
|
4
|
+
/** The menu's items, in order. */
|
|
5
|
+
export const menuItems = (root: HTMLElement): HTMLElement[] => Array.from(root.querySelectorAll<HTMLElement>('.dropdown-popup [role="menuitem"]'));
|
|
6
|
+
|
|
7
|
+
/** Moves focus to the first or last item (and makes it the menu's Tab stop). */
|
|
8
|
+
export function focusItem(root: HTMLElement, which: 'first' | 'last'): void {
|
|
9
|
+
const items = menuItems(root);
|
|
10
|
+
const item = which === 'first' ? items[0] : items[items.length - 1];
|
|
11
|
+
if (!item) return;
|
|
12
|
+
items.forEach((i) => i.setAttribute('tabindex', i === item ? '0' : '-1'));
|
|
13
|
+
item.focus();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Keys on the button: ↓ (or Enter / Space, which click it) opens the menu on
|
|
18
|
+
* its first item, ↑ on its last. Returns which item to focus, or null.
|
|
19
|
+
*/
|
|
20
|
+
export function triggerKey(event: KeyboardEvent): 'first' | 'last' | null {
|
|
21
|
+
if (event.key === 'ArrowDown') return 'first';
|
|
22
|
+
if (event.key === 'ArrowUp') return 'last';
|
|
23
|
+
return null;
|
|
24
|
+
}
|
package/src/icons.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
/** One SVG element of an icon: its tag and attributes. */
|
|
11
11
|
export type IconNode = [tag: 'path' | 'circle' | 'rect', attrs: Record<string, string | number>];
|
|
12
|
+
// A shape may fill (fill: 'currentColor'): only `contrast` does, for its filled half.
|
|
12
13
|
|
|
13
14
|
const path = (d: string): IconNode => ['path', { d }];
|
|
14
15
|
const circle = (cx: number, cy: number, r: number): IconNode => ['circle', { cx, cy, r }];
|
|
@@ -88,6 +89,7 @@ const DEFS = {
|
|
|
88
89
|
// Theme
|
|
89
90
|
sun: [circle(12, 12, 4), path('M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41')],
|
|
90
91
|
moon: [path('M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5z')],
|
|
92
|
+
contrast: [ring, ['path', { d: 'M12 3a9 9 0 0 1 0 18z', fill: 'currentColor' }] as IconNode],
|
|
91
93
|
palette: [path('M12 3a9 9 0 0 0 0 18c1.1 0 1.6-.8 1.6-1.6 0-1-.9-1.4-.9-2.4 0-.9.7-1.5 1.6-1.5H16a5 5 0 0 0 5-5C21 6.4 17 3 12 3z'), dot(7.5, 11.5), dot(9.5, 7.5), dot(14.5, 7.5)],
|
|
92
94
|
sparkles: [path('M10 4l1.6 4.4L16 10l-4.4 1.6L10 16l-1.6-4.4L4 10l4.4-1.6z'), path('M18 14l.8 2.2L21 17l-2.2.8L18 20l-.8-2.2L15 17l2.2-.8z')],
|
|
93
95
|
zap: [path('M13 2.5L4.5 13.5H11l-1 8 8.5-11H12z')],
|
package/src/index.ts
CHANGED
|
@@ -36,6 +36,10 @@ export { default as Loader } from './components/Loader/Loader';
|
|
|
36
36
|
export type { LoaderProps } from './components/Loader/Loader';
|
|
37
37
|
export { default as Tree } from './components/Tree/Tree';
|
|
38
38
|
export type { TreeProps, TreeItem } from './components/Tree/Tree';
|
|
39
|
+
export { default as Anchor } from './components/Anchor/Anchor';
|
|
40
|
+
export type { AnchorProps, AnchorItem } from './components/Anchor/Anchor';
|
|
41
|
+
export { default as Dropdown } from './components/Dropdown/Dropdown';
|
|
42
|
+
export type { DropdownProps } from './components/Dropdown/Dropdown';
|
|
39
43
|
export { default as Menu } from './components/Menu/Menu';
|
|
40
44
|
export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './components/Menu/Menu';
|
|
41
45
|
export { default as Select } from './components/Select/Select';
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { AnchorItem, anchorRail, anchorDash, anchorActive, anchorGo } from '../anchor';
|
|
4
|
+
import { spawn, ElementProps } from './spawn';
|
|
5
|
+
import { updatable, NurvusElement } from './update';
|
|
6
|
+
import '../components/Anchor/Anchor.scss';
|
|
7
|
+
|
|
8
|
+
export type { AnchorItem };
|
|
9
|
+
|
|
10
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
11
|
+
|
|
12
|
+
export interface AnchorProps extends ElementProps {
|
|
13
|
+
/** The sections, in page order: each an "#id" href, a label and a level (1, 2…). */
|
|
14
|
+
items: AnchorItem[];
|
|
15
|
+
/** The heading above the list. */
|
|
16
|
+
title?: string;
|
|
17
|
+
/** Prefix for the ids inside (the title is "<id>-title"). Defaults to "anchor". */
|
|
18
|
+
id?: string;
|
|
19
|
+
/** How far below the top a section's heading must be scrolled to count as "in view", in pixels. */
|
|
20
|
+
offset?: number;
|
|
21
|
+
/** The section in view, set by you (its href). Left out, the Anchor follows the scrolling itself. */
|
|
22
|
+
current?: string;
|
|
23
|
+
/** Scroll-spy inside this element instead of the page: an element or a CSS selector. */
|
|
24
|
+
scrollRoot?: Element | string;
|
|
25
|
+
/** Writes the section's #id in the address bar when one is picked. Turn off for hash routers. */
|
|
26
|
+
updateHash?: boolean;
|
|
27
|
+
/** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
|
|
28
|
+
onNavigate?: (href: string, event: MouseEvent) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* "On this page", the same markup as the React Anchor: the rail bends with the
|
|
33
|
+
* nesting and its highlight slides to the section in view, in place (the
|
|
34
|
+
* element stays the same while you scroll).
|
|
35
|
+
*/
|
|
36
|
+
export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, AnchorProps> {
|
|
37
|
+
return updatable(({
|
|
38
|
+
items, title = 'On this page', id = 'anchor', offset = 96, current, scrollRoot, updateHash = true, onNavigate, className, ...rest
|
|
39
|
+
}: AnchorProps) => {
|
|
40
|
+
const controlled = current !== undefined;
|
|
41
|
+
const rail = anchorRail(items.map((item) => item.level || 1));
|
|
42
|
+
let active = controlled ? Math.max(0, items.findIndex((item) => item.href === current)) : 0;
|
|
43
|
+
const links: HTMLElement[] = [];
|
|
44
|
+
const dashOf = (i: number) => { const d = anchorDash(rail, i); return { 'stroke-dasharray': d.dasharray, 'stroke-dashoffset': String(d.dashoffset) }; };
|
|
45
|
+
const thumb = spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-thumb', d: rail.d, style: dashOf(active) });
|
|
46
|
+
const mark = (i: number) => {
|
|
47
|
+
active = i;
|
|
48
|
+
const d = dashOf(i);
|
|
49
|
+
thumb.style.setProperty('stroke-dasharray', d['stroke-dasharray']);
|
|
50
|
+
thumb.style.setProperty('stroke-dashoffset', d['stroke-dashoffset']);
|
|
51
|
+
links.forEach((link, k) => {
|
|
52
|
+
link.setAttribute('data-active', String(k === i));
|
|
53
|
+
if (k === i) link.setAttribute('aria-current', 'location'); else link.removeAttribute('aria-current');
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const el = spawn<HTMLElement>({
|
|
58
|
+
tag: 'nav',
|
|
59
|
+
className: classNames(className, 'anchor', CONSTANT.CLASS_PREFIX),
|
|
60
|
+
'aria-labelledby': `${id}-title`,
|
|
61
|
+
...rest,
|
|
62
|
+
children: [
|
|
63
|
+
spawn({ tag: 'p', className: 'anchor-title', id: `${id}-title`, children: title }),
|
|
64
|
+
spawn({
|
|
65
|
+
className: 'anchor-body',
|
|
66
|
+
children: [
|
|
67
|
+
spawn<any>({
|
|
68
|
+
tag: 'svg',
|
|
69
|
+
namespace: SVG,
|
|
70
|
+
className: 'anchor-rail',
|
|
71
|
+
viewBox: `0 0 ${rail.width} ${rail.height}`,
|
|
72
|
+
style: { width: `${rail.width / 16}rem`, height: `${rail.height / 16}rem` },
|
|
73
|
+
'aria-hidden': true,
|
|
74
|
+
focusable: 'false',
|
|
75
|
+
children: [spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-track', d: rail.d }), thumb],
|
|
76
|
+
}),
|
|
77
|
+
spawn({
|
|
78
|
+
tag: 'ul',
|
|
79
|
+
className: 'anchor-list',
|
|
80
|
+
children: items.map((item, i) => {
|
|
81
|
+
const link = spawn({
|
|
82
|
+
tag: 'a',
|
|
83
|
+
className: 'anchor-link',
|
|
84
|
+
href: item.href,
|
|
85
|
+
title: item.label,
|
|
86
|
+
'data-active': i === active,
|
|
87
|
+
'aria-current': i === active ? 'location' : undefined,
|
|
88
|
+
events: {
|
|
89
|
+
click: (event: MouseEvent) => {
|
|
90
|
+
if (onNavigate) onNavigate(item.href, event);
|
|
91
|
+
if (event.defaultPrevented) return;
|
|
92
|
+
if (anchorGo(item.href, updateHash)) { event.preventDefault(); if (!controlled) mark(i); }
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
children: item.label,
|
|
96
|
+
});
|
|
97
|
+
links.push(link);
|
|
98
|
+
return spawn({ tag: 'li', style: { '--anchor-level': String((item.level || 1) - 1) }, children: link });
|
|
99
|
+
}),
|
|
100
|
+
}),
|
|
101
|
+
],
|
|
102
|
+
}),
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Follow the scrolling once it is on the page (and stop once it is gone).
|
|
107
|
+
if (!controlled && typeof window !== 'undefined') {
|
|
108
|
+
let frame = 0;
|
|
109
|
+
let seen = false;
|
|
110
|
+
const root = () => (typeof scrollRoot === 'string' ? document.querySelector(scrollRoot) : scrollRoot || null);
|
|
111
|
+
const update = () => {
|
|
112
|
+
cancelAnimationFrame(frame);
|
|
113
|
+
frame = requestAnimationFrame(() => {
|
|
114
|
+
if (!el.isConnected) { if (seen) { window.removeEventListener('scroll', update, true); window.removeEventListener('resize', update); } return; }
|
|
115
|
+
seen = true;
|
|
116
|
+
const next = anchorActive(items, offset, root());
|
|
117
|
+
if (next !== active) mark(next);
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
window.addEventListener('scroll', update, { passive: true, capture: true });
|
|
121
|
+
window.addEventListener('resize', update);
|
|
122
|
+
update();
|
|
123
|
+
}
|
|
124
|
+
return el;
|
|
125
|
+
}, props);
|
|
126
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { focusItem, triggerKey } from '../dropdown';
|
|
4
|
+
import Button, { ButtonSize, ButtonType } from './Button';
|
|
5
|
+
import Menu, { MenuItem, MenuSection } from './Menu';
|
|
6
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
7
|
+
import { updatable, NurvusElement } from './update';
|
|
8
|
+
import '../components/Dropdown/Dropdown.scss';
|
|
9
|
+
|
|
10
|
+
export interface DropdownProps extends ElementProps {
|
|
11
|
+
/** What the button says (text, or an Icon with a label for an icon-only button). */
|
|
12
|
+
label: Child;
|
|
13
|
+
/** The actions, in groups (each may have a title). */
|
|
14
|
+
sections?: MenuSection[];
|
|
15
|
+
/** Or the actions as one flat list. */
|
|
16
|
+
items?: MenuItem[];
|
|
17
|
+
/** Which edge of the button the menu lines up with. */
|
|
18
|
+
align?: 'start' | 'end';
|
|
19
|
+
/** The button's look. Defaults to secondary. */
|
|
20
|
+
type?: ButtonType;
|
|
21
|
+
/** The button's size. */
|
|
22
|
+
size?: ButtonSize;
|
|
23
|
+
/** Stops the button working. */
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
/** Names the button and the menu ("<id>-button", "<id>-menu"). */
|
|
26
|
+
id: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A button that opens a menu of actions, the same markup and keys as the React
|
|
31
|
+
* Dropdown. Opening and closing change it in place (the element stays the same).
|
|
32
|
+
*/
|
|
33
|
+
export default function Dropdown(props: DropdownProps): NurvusElement<HTMLDivElement, DropdownProps> {
|
|
34
|
+
return updatable(({
|
|
35
|
+
label, sections, items, align = 'start', type = 'secondary', size = 'medium', disabled = false, id, className, ...rest
|
|
36
|
+
}: DropdownProps) => {
|
|
37
|
+
let open = false;
|
|
38
|
+
let root: HTMLDivElement;
|
|
39
|
+
let button: HTMLButtonElement;
|
|
40
|
+
const away = (event: MouseEvent) => { if (!root.contains(event.target as Node)) set(false); };
|
|
41
|
+
function set(next: boolean, which?: 'first' | 'last', refocus = false) {
|
|
42
|
+
open = next;
|
|
43
|
+
root.setAttribute('data-open', String(open));
|
|
44
|
+
button.setAttribute('aria-expanded', String(open));
|
|
45
|
+
if (open) {
|
|
46
|
+
document.addEventListener('mousedown', away);
|
|
47
|
+
if (which) focusItem(root, which);
|
|
48
|
+
} else {
|
|
49
|
+
document.removeEventListener('mousedown', away);
|
|
50
|
+
if (refocus) button.focus();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Choosing an action runs it, then closes the menu with focus on the button.
|
|
54
|
+
const wrap = (list: MenuItem[]): MenuItem[] => list.map((item) => ({
|
|
55
|
+
...item,
|
|
56
|
+
onSelect: (event: MouseEvent) => { if (item.onSelect) item.onSelect(event); set(false, undefined, true); },
|
|
57
|
+
}));
|
|
58
|
+
const groups: MenuSection[] = (sections || [{ items: items || [] }]).map((s) => ({ ...s, items: wrap(s.items) }));
|
|
59
|
+
|
|
60
|
+
button = Button({
|
|
61
|
+
id: `${id}-button`,
|
|
62
|
+
className: 'dropdown-trigger',
|
|
63
|
+
type,
|
|
64
|
+
size,
|
|
65
|
+
disabled,
|
|
66
|
+
'aria-haspopup': 'menu',
|
|
67
|
+
'aria-expanded': false,
|
|
68
|
+
'aria-controls': `${id}-menu`,
|
|
69
|
+
events: {
|
|
70
|
+
click: () => (open ? set(false) : set(true, 'first')),
|
|
71
|
+
keydown: (event: KeyboardEvent) => { const which = triggerKey(event); if (which) { event.preventDefault(); set(true, which); } },
|
|
72
|
+
},
|
|
73
|
+
children: [label, spawn({ tag: 'span', className: 'dropdown-chevron', 'aria-hidden': true })],
|
|
74
|
+
});
|
|
75
|
+
const { events, ...others } = rest;
|
|
76
|
+
root = spawn<HTMLDivElement>({
|
|
77
|
+
className: classNames(className, 'dropdown', CONSTANT.CLASS_PREFIX),
|
|
78
|
+
'data-open': false,
|
|
79
|
+
'data-align': align,
|
|
80
|
+
...others,
|
|
81
|
+
events: {
|
|
82
|
+
...(events as object),
|
|
83
|
+
keydown: (event: KeyboardEvent) => { if (event.key === 'Escape' && open) { event.preventDefault(); set(false, undefined, true); } },
|
|
84
|
+
focusout: (event: FocusEvent) => { if (open && !root.contains(event.relatedTarget as Node)) set(false); },
|
|
85
|
+
},
|
|
86
|
+
children: [button, spawn({ className: 'dropdown-popup', children: Menu({ kind: 'actions', id: `${id}-menu`, 'aria-labelledby': `${id}-button`, sections: groups }) })],
|
|
87
|
+
});
|
|
88
|
+
return root;
|
|
89
|
+
}, props);
|
|
90
|
+
}
|