@nurvus/ui 0.1.0-alpha.17 → 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 +15 -3
- package/dist/icons.svg +1 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +5 -2
- package/dist/respawn.js +1 -1
- package/dist/types/anchor.d.ts +10 -2
- package/dist/types/components/Anchor/Anchor.d.ts +8 -5
- package/dist/types/components/Kbd/Kbd.d.ts +16 -0
- package/dist/types/components/Menu/Menu.d.ts +13 -5
- package/dist/types/components/NavToggle/NavToggle.d.ts +27 -0
- package/dist/types/components/Tooltip/Tooltip.d.ts +27 -0
- package/dist/types/icons.d.ts +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/menu.d.ts +30 -6
- package/dist/types/respawn/Anchor.d.ts +2 -0
- package/dist/types/respawn/Kbd.d.ts +10 -0
- package/dist/types/respawn/Menu.d.ts +5 -1
- package/dist/types/respawn/NavToggle.d.ts +22 -0
- package/dist/types/respawn/Tooltip.d.ts +20 -0
- package/dist/types/respawn/index.d.ts +7 -0
- package/dist/types/tooltip.d.ts +4 -0
- package/package.json +1 -1
- package/src/anchor.ts +30 -7
- package/src/components/Anchor/Anchor.scss +26 -3
- package/src/components/Anchor/Anchor.tsx +23 -14
- package/src/components/Kbd/Kbd.md +14 -0
- package/src/components/Kbd/Kbd.scss +30 -0
- package/src/components/Kbd/Kbd.tsx +28 -0
- 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/components/NavToggle/NavToggle.md +51 -0
- package/src/components/NavToggle/NavToggle.scss +162 -0
- package/src/components/NavToggle/NavToggle.tsx +68 -0
- package/src/components/Tooltip/Tooltip.md +19 -0
- package/src/components/Tooltip/Tooltip.scss +75 -0
- package/src/components/Tooltip/Tooltip.tsx +69 -0
- package/src/css.ts +3 -0
- package/src/icons.ts +1 -0
- package/src/index.ts +7 -0
- package/src/menu.ts +58 -12
- package/src/respawn/Anchor.ts +26 -17
- package/src/respawn/Kbd.ts +22 -0
- package/src/respawn/Menu.ts +18 -16
- package/src/respawn/NavToggle.ts +78 -0
- package/src/respawn/Tooltip.ts +63 -0
- package/src/respawn/index.ts +7 -0
- package/src/tooltip.ts +14 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import './Kbd.scss';
|
|
5
|
+
|
|
6
|
+
export interface KbdProps extends React.HTMLAttributes<HTMLElement> {
|
|
7
|
+
/** The keys of a shortcut, in order: ['⌘', 'B']. Each is drawn as a key cap. */
|
|
8
|
+
keys?: string[];
|
|
9
|
+
/** One key, when there is only one (instead of keys). */
|
|
10
|
+
children?: React.ReactNode;
|
|
11
|
+
/** Custom class name. */
|
|
12
|
+
className?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A keyboard key, or a shortcut of several (⌘ B), drawn as key caps. The HTML
|
|
17
|
+
* way: a <kbd> for the shortcut holding a <kbd> per key, so screen readers and
|
|
18
|
+
* search engines know it is keyboard input. Sized in em, so it matches the
|
|
19
|
+
* text around it.
|
|
20
|
+
*/
|
|
21
|
+
export default function Kbd({ keys, children, className, ...restProps }: KbdProps) {
|
|
22
|
+
const caps = keys && keys.length ? keys : [children];
|
|
23
|
+
return (
|
|
24
|
+
<kbd className={ classNames(className, 'kbd', CONSTANT.CLASS_PREFIX) } { ...restProps }>
|
|
25
|
+
{ caps.map((key, i) => <kbd key={ i } className="kbd-key">{ key }</kbd>) }
|
|
26
|
+
</kbd>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
@@ -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({
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Which look? The **hamburger** for navigation that covers the page (a phone's drawer, a menu), the **sidebar icon** for a sidebar that sits beside the page and folds away (as VS Code, shadcn/ui and Keyframery do). The docs header uses the hamburger with Slide and asymmetric lines; the sidebar icon is kept here for a docked sidebar. Click each one.
|
|
2
|
+
|
|
3
|
+
```jsx
|
|
4
|
+
import { NavToggle, Text } from '@nurvus/ui';
|
|
5
|
+
|
|
6
|
+
const MORPHS = [
|
|
7
|
+
['slide', 'Slide: a cross, the middle bar sliding out to the left (the docs header)'],
|
|
8
|
+
['cross', 'Cross: the middle bar fades'],
|
|
9
|
+
['squeeze', 'Squeeze: in to the middle, then a cross'],
|
|
10
|
+
['elastic', 'Elastic: a cross on a spring'],
|
|
11
|
+
['spin', 'Spin: a cross, turning half a circle'],
|
|
12
|
+
['arrow', 'Arrow: points at the sidebar'],
|
|
13
|
+
['chevron', 'Chevron: a ‹, slide it away'],
|
|
14
|
+
['collapse', 'Collapse: three bars become one'],
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const One = ({ morph, about }) => {
|
|
18
|
+
const [open, setOpen] = React.useState(false);
|
|
19
|
+
return (
|
|
20
|
+
<div style={ { display: 'flex', alignItems: 'center', gap: 12 } }>
|
|
21
|
+
<NavToggle open={ open } morph={ morph } lines="asymmetric" label={ `navigation (${morph})` } onClick={ () => setOpen(!open) } />
|
|
22
|
+
<Text variant="caption" tone="muted">{ about }</Text>
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
<div style={ { display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(16rem, 1fr))', gap: 12 } }>
|
|
28
|
+
{ MORPHS.map(([morph, about]) => <One key={ morph } morph={ morph } about={ about } />) }
|
|
29
|
+
</div>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Lines: even (three of one length) or asymmetric (long, short, middle), which line up when pointed at and while open. And the sidebar icon: its pane fills while the sidebar shows; closed, it shrinks away and the divider slides left.
|
|
33
|
+
```jsx
|
|
34
|
+
import { NavToggle, Layout, Text } from '@nurvus/ui';
|
|
35
|
+
|
|
36
|
+
const Pair = () => {
|
|
37
|
+
const [open, setOpen] = React.useState(true);
|
|
38
|
+
const flip = () => setOpen(!open);
|
|
39
|
+
return (
|
|
40
|
+
<Layout display="flex" alignItems="center" itemSpacing={ 24 }>
|
|
41
|
+
<NavToggle open={ open } lines="even" onClick={ flip } label="menu (even)" />
|
|
42
|
+
<NavToggle open={ open } lines="asymmetric" onClick={ flip } label="menu (asymmetric)" />
|
|
43
|
+
<NavToggle open={ open } variant="sidebar" onClick={ flip } label="sidebar" />
|
|
44
|
+
<Text variant="caption" tone="muted">{ open ? 'Open' : 'Closed' }: click any</Text>
|
|
45
|
+
</Layout>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
<Pair />
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Give it `controls` (the navigation's id) so screen readers can follow it there, and when your app has a shortcut for it, say so with `aria-keyshortcuts` and a [Tooltip](#/Components/Tooltip) with the keys. The docs header does: <kbd>⌘</kbd> <kbd>B</kbd>.
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// Styles for NavToggle (React and Respawn: same markup). Every shape is a
|
|
2
|
+
// transition of the same parts, so a change is one smooth motion.
|
|
3
|
+
@import "../../styles/variables.scss";
|
|
4
|
+
|
|
5
|
+
$nav-ease: cubic-bezier(0.2, 0.8, 0.2, 1); // fast out, gentle settle (250ms)
|
|
6
|
+
$nav-speed: 0.25s;
|
|
7
|
+
$bar-gap: 0.375rem; // 6px at the default text size: the bars sit 0, 6 and 12px down
|
|
8
|
+
|
|
9
|
+
.#{$cls-prefix}.nav-toggle {
|
|
10
|
+
display: inline-flex;
|
|
11
|
+
flex: none;
|
|
12
|
+
align-items: center;
|
|
13
|
+
justify-content: center;
|
|
14
|
+
width: 2rem;
|
|
15
|
+
height: 2rem;
|
|
16
|
+
padding: 0;
|
|
17
|
+
border: 0;
|
|
18
|
+
border-radius: 0.625rem;
|
|
19
|
+
background: transparent;
|
|
20
|
+
color: var(--nv-text);
|
|
21
|
+
cursor: pointer;
|
|
22
|
+
transition: background-color 0.15s ease, color 0.15s ease;
|
|
23
|
+
|
|
24
|
+
&:hover { background: color-mix(in srgb, var(--nv-text) 8%, transparent); color: var(--nv-accent); }
|
|
25
|
+
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: 2px; }
|
|
26
|
+
|
|
27
|
+
// The hamburger: three 2px bars in a 1.125rem x 0.875rem box (an icon's 20px live area).
|
|
28
|
+
.nav-toggle-bars {
|
|
29
|
+
position: relative;
|
|
30
|
+
width: 1.125rem;
|
|
31
|
+
height: 0.875rem;
|
|
32
|
+
transition: transform 0.35s $nav-ease;
|
|
33
|
+
|
|
34
|
+
span {
|
|
35
|
+
position: absolute;
|
|
36
|
+
left: 0;
|
|
37
|
+
width: 100%;
|
|
38
|
+
height: 2px;
|
|
39
|
+
border-radius: 1px;
|
|
40
|
+
background: currentColor;
|
|
41
|
+
transform-origin: left center;
|
|
42
|
+
transition: transform $nav-speed $nav-ease, opacity 0.2s ease, width $nav-speed $nav-ease;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
span:nth-child(1) { top: 0; }
|
|
46
|
+
span:nth-child(2) { top: $bar-gap; }
|
|
47
|
+
span:nth-child(3) { top: calc(#{$bar-gap} * 2); }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Asymmetric lines: long, short, middle, lined up on the left; they even out
|
|
51
|
+
// when pointed at, and while open (a cross needs equal bars).
|
|
52
|
+
&[data-lines="asymmetric"] .nav-toggle-bars {
|
|
53
|
+
span:nth-child(2) { width: 62%; }
|
|
54
|
+
span:nth-child(3) { width: 82%; }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
&[data-lines="asymmetric"]:hover .nav-toggle-bars span,
|
|
58
|
+
&[data-lines="asymmetric"][data-open="true"] .nav-toggle-bars span { width: 100%; }
|
|
59
|
+
|
|
60
|
+
// --- Open: the morphs ---
|
|
61
|
+
|
|
62
|
+
// Slide (Jonathan Suh's "Slider"): a cross while the middle bar slides out left and fades.
|
|
63
|
+
&[data-open="true"][data-morph="slide"] .nav-toggle-bars {
|
|
64
|
+
span { transform-origin: center; }
|
|
65
|
+
span:nth-child(1) { transform: translateY($bar-gap) rotate(45deg); }
|
|
66
|
+
span:nth-child(2) { opacity: 0; transform: translateX(-0.625rem); transition-duration: 0.15s; }
|
|
67
|
+
span:nth-child(3) { transform: translateY(-$bar-gap) rotate(-45deg); }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Cross: the outer bars meet in the middle; the middle one fades. Spin turns half a circle too.
|
|
71
|
+
&[data-open="true"][data-morph="cross"],
|
|
72
|
+
&[data-open="true"][data-morph="spin"] {
|
|
73
|
+
.nav-toggle-bars span { transform-origin: center; }
|
|
74
|
+
.nav-toggle-bars span:nth-child(1) { transform: translateY($bar-gap) rotate(45deg); }
|
|
75
|
+
.nav-toggle-bars span:nth-child(2) { opacity: 0; transform: scaleX(0.2); }
|
|
76
|
+
.nav-toggle-bars span:nth-child(3) { transform: translateY(-$bar-gap) rotate(-45deg); }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
&[data-open="true"][data-morph="spin"] .nav-toggle-bars { transform: rotate(180deg); }
|
|
80
|
+
|
|
81
|
+
// Squeeze: the outer bars close in on the middle first, then turn (and back the other way).
|
|
82
|
+
&[data-morph="squeeze"] .nav-toggle-bars span {
|
|
83
|
+
transform-origin: center;
|
|
84
|
+
transition: translate 0.12s ease 0.12s, rotate 0.12s ease, opacity 0.1s ease 0.1s, width $nav-speed $nav-ease;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
&[data-open="true"][data-morph="squeeze"] .nav-toggle-bars {
|
|
88
|
+
span { transition: translate 0.12s ease, rotate 0.12s ease 0.12s, opacity 0.1s ease, width $nav-speed $nav-ease; }
|
|
89
|
+
span:nth-child(1) { translate: 0 $bar-gap; rotate: 45deg; }
|
|
90
|
+
span:nth-child(2) { opacity: 0; }
|
|
91
|
+
span:nth-child(3) { translate: 0 (-$bar-gap); rotate: -45deg; }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Elastic: the cross on a spring that swings past and settles.
|
|
95
|
+
&[data-morph="elastic"] .nav-toggle-bars span { transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55), opacity 0.2s ease, width $nav-speed $nav-ease; }
|
|
96
|
+
|
|
97
|
+
&[data-open="true"][data-morph="elastic"] .nav-toggle-bars {
|
|
98
|
+
span { transform-origin: center; }
|
|
99
|
+
span:nth-child(1) { transform: translateY($bar-gap) rotate(135deg); }
|
|
100
|
+
span:nth-child(2) { opacity: 0; transform: scaleX(0); }
|
|
101
|
+
span:nth-child(3) { transform: translateY(-$bar-gap) rotate(-135deg); }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Arrow (←): the outer bars swing in from the left end to make the head.
|
|
105
|
+
&[data-open="true"][data-morph="arrow"] .nav-toggle-bars {
|
|
106
|
+
span:nth-child(1) { width: 55%; transform: translate(0, $bar-gap) rotate(-45deg); }
|
|
107
|
+
span:nth-child(3) { width: 55%; transform: translate(0, -$bar-gap) rotate(45deg); }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Chevron (‹): the outer bars fold to a point on the left; the middle fades.
|
|
111
|
+
&[data-open="true"][data-morph="chevron"] .nav-toggle-bars {
|
|
112
|
+
span:nth-child(1) { width: 50%; transform: translate(0.25rem, $bar-gap) rotate(-45deg); }
|
|
113
|
+
span:nth-child(2) { opacity: 0; transform: scaleX(0.2); }
|
|
114
|
+
span:nth-child(3) { width: 50%; transform: translate(0.25rem, -$bar-gap) rotate(45deg); }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Collapse (—): the outer bars slide into the middle one.
|
|
118
|
+
&[data-open="true"][data-morph="collapse"] .nav-toggle-bars {
|
|
119
|
+
span:nth-child(1) { transform: translateY($bar-gap); }
|
|
120
|
+
span:nth-child(3) { transform: translateY(-$bar-gap); }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The sidebar icon (after a motion study for NURVUS): the pane fills while
|
|
124
|
+
// the sidebar shows; closed, it shrinks away and the divider slides left.
|
|
125
|
+
.nav-toggle-panel {
|
|
126
|
+
width: 1.25rem;
|
|
127
|
+
height: 1.25rem;
|
|
128
|
+
overflow: visible;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
.nav-toggle-outline,
|
|
132
|
+
.nav-toggle-divider {
|
|
133
|
+
fill: none;
|
|
134
|
+
stroke: currentColor;
|
|
135
|
+
stroke-width: 2;
|
|
136
|
+
stroke-linecap: round;
|
|
137
|
+
stroke-linejoin: round;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.nav-toggle-fill {
|
|
141
|
+
fill: currentColor;
|
|
142
|
+
opacity: 0.3;
|
|
143
|
+
transform-box: fill-box;
|
|
144
|
+
transform-origin: left center;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.nav-toggle-fill,
|
|
148
|
+
.nav-toggle-divider { transition: transform $nav-speed $nav-ease, opacity $nav-speed $nav-ease; }
|
|
149
|
+
|
|
150
|
+
&[data-variant="sidebar"][data-open="false"] {
|
|
151
|
+
.nav-toggle-divider { transform: translateX(-4px); opacity: 0.55; }
|
|
152
|
+
.nav-toggle-fill { transform: scaleX(0.12); opacity: 0; }
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
@media (prefers-reduced-motion: reduce) {
|
|
157
|
+
.#{$cls-prefix}.nav-toggle,
|
|
158
|
+
.#{$cls-prefix}.nav-toggle .nav-toggle-bars,
|
|
159
|
+
.#{$cls-prefix}.nav-toggle .nav-toggle-bars span,
|
|
160
|
+
.#{$cls-prefix}.nav-toggle .nav-toggle-fill,
|
|
161
|
+
.#{$cls-prefix}.nav-toggle .nav-toggle-divider { transition: none !important; }
|
|
162
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import './NavToggle.scss';
|
|
5
|
+
|
|
6
|
+
export type NavToggleMorph = 'slide' | 'cross' | 'squeeze' | 'elastic' | 'spin' | 'arrow' | 'chevron' | 'collapse';
|
|
7
|
+
|
|
8
|
+
export interface NavToggleProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type'> {
|
|
9
|
+
/** Whether the navigation it controls is showing. */
|
|
10
|
+
open: boolean;
|
|
11
|
+
/** 'hamburger' (three bars, for a menu that covers the page) or 'sidebar' (a window with a side pane, for a sidebar beside the page). */
|
|
12
|
+
variant?: 'hamburger' | 'sidebar';
|
|
13
|
+
/** Hamburger: how the bars change while open. 'slide' (default): a cross, the middle bar sliding out to the left. */
|
|
14
|
+
morph?: NavToggleMorph;
|
|
15
|
+
/** Hamburger: three bars of one length, or 'asymmetric' (long, short, middle) that line up when pointed at or open. */
|
|
16
|
+
lines?: 'even' | 'asymmetric';
|
|
17
|
+
/** What it shows and hides, for its name: "Show navigation" / "Hide navigation". */
|
|
18
|
+
label?: string;
|
|
19
|
+
/** The id of the navigation it controls (aria-controls). */
|
|
20
|
+
controls?: string;
|
|
21
|
+
/** Custom class name. */
|
|
22
|
+
className?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The button that shows and hides navigation. Two looks: the hamburger, whose
|
|
27
|
+
* bars move into another shape while the navigation is open (eight morphs),
|
|
28
|
+
* and the sidebar icon, a window whose side pane fills while the sidebar shows
|
|
29
|
+
* and empties as its divider slides away. The shapes move, they aren't
|
|
30
|
+
* swapped; instantly for reduced motion. It says what it does ("Hide
|
|
31
|
+
* navigation") and whether it is open (aria-expanded).
|
|
32
|
+
*/
|
|
33
|
+
export default function NavToggle({
|
|
34
|
+
open,
|
|
35
|
+
variant = 'hamburger',
|
|
36
|
+
morph = 'slide',
|
|
37
|
+
lines = 'even',
|
|
38
|
+
label = 'navigation',
|
|
39
|
+
controls,
|
|
40
|
+
className,
|
|
41
|
+
...restProps
|
|
42
|
+
}: NavToggleProps) {
|
|
43
|
+
const sidebar = variant === 'sidebar';
|
|
44
|
+
return (
|
|
45
|
+
<button
|
|
46
|
+
type="button"
|
|
47
|
+
className={ classNames(className, 'nav-toggle', CONSTANT.CLASS_PREFIX) }
|
|
48
|
+
data-variant={ variant }
|
|
49
|
+
data-morph={ sidebar ? undefined : morph }
|
|
50
|
+
data-lines={ sidebar ? undefined : lines }
|
|
51
|
+
data-open={ open }
|
|
52
|
+
aria-expanded={ open }
|
|
53
|
+
aria-controls={ controls }
|
|
54
|
+
aria-label={ `${open ? 'Hide' : 'Show'} ${label}` }
|
|
55
|
+
{ ...restProps }
|
|
56
|
+
>
|
|
57
|
+
{ sidebar ? (
|
|
58
|
+
<svg className="nav-toggle-panel" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
|
59
|
+
<rect className="nav-toggle-outline" x="2.5" y="4" width="19" height="16" rx="2.5" />
|
|
60
|
+
<rect className="nav-toggle-fill" x="4" y="5.5" width="5" height="13" rx="0.5" />
|
|
61
|
+
<path className="nav-toggle-divider" d="M10 4V20" />
|
|
62
|
+
</svg>
|
|
63
|
+
) : (
|
|
64
|
+
<span className="nav-toggle-bars" aria-hidden="true"><span /><span /><span /></span>
|
|
65
|
+
) }
|
|
66
|
+
</button>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
A short label for a control, most useful on icon-only buttons; with `shortcut`, its keyboard shortcut as key caps. Point at a button and rest, or Tab to it: the tooltip shows. Press Esc to hide it; move the pointer onto it and it stays.
|
|
2
|
+
|
|
3
|
+
```jsx
|
|
4
|
+
import { Button, Icon, Layout, Tooltip } from '@nurvus/ui';
|
|
5
|
+
|
|
6
|
+
<Layout display="flex" itemSpacing={ 16 } style={ { padding: '8px 0 48px' } }>
|
|
7
|
+
<Tooltip id="tip-save" content="Save" shortcut={ ['⌘', 'S'] }>
|
|
8
|
+
<Button size="small" type="secondary" aria-label="Save"><Icon name="download" /></Button>
|
|
9
|
+
</Tooltip>
|
|
10
|
+
<Tooltip id="tip-share" content="Share a link">
|
|
11
|
+
<Button size="small" type="secondary" aria-label="Share"><Icon name="share" /></Button>
|
|
12
|
+
</Tooltip>
|
|
13
|
+
<Tooltip id="tip-settings" content="Settings" shortcut={ ['⌘', ','] } placement="bottom" align="start">
|
|
14
|
+
<Button size="small" type="secondary" aria-label="Settings"><Icon name="settings" /></Button>
|
|
15
|
+
</Tooltip>
|
|
16
|
+
</Layout>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The tooltip describes the control (`aria-describedby`): screen readers say the control's own name first ("Save, button"), then the tooltip. So a tooltip never replaces a name: an icon-only button still needs its `aria-label`. Keep tooltips to a few words, and never put links or buttons in them: nothing inside a tooltip can be reached by keyboard.
|