@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,75 @@
|
|
|
1
|
+
// Styles for Tooltip (React and Respawn: same markup). Inverted colors (the
|
|
2
|
+
// theme's text as the background), so it stands out in every theme.
|
|
3
|
+
@import "../../styles/variables.scss";
|
|
4
|
+
|
|
5
|
+
$tooltip-gap: 0.5rem;
|
|
6
|
+
|
|
7
|
+
.#{$cls-prefix}.tooltip {
|
|
8
|
+
position: relative;
|
|
9
|
+
display: inline-flex;
|
|
10
|
+
|
|
11
|
+
.tooltip-bubble {
|
|
12
|
+
position: absolute;
|
|
13
|
+
z-index: 1100;
|
|
14
|
+
top: calc(100% + #{$tooltip-gap});
|
|
15
|
+
left: 50%;
|
|
16
|
+
display: inline-flex;
|
|
17
|
+
align-items: center;
|
|
18
|
+
gap: var(--space--8);
|
|
19
|
+
padding: var(--space--4) var(--space--8);
|
|
20
|
+
border-radius: 0.375rem;
|
|
21
|
+
background: var(--nv-text);
|
|
22
|
+
color: var(--nv-bg);
|
|
23
|
+
box-shadow: 0 6px 20px rgb(0 0 0 / 0.22);
|
|
24
|
+
font-size: 0.8125rem;
|
|
25
|
+
font-weight: 500;
|
|
26
|
+
line-height: 1.25rem;
|
|
27
|
+
white-space: nowrap;
|
|
28
|
+
opacity: 0;
|
|
29
|
+
visibility: hidden;
|
|
30
|
+
pointer-events: none;
|
|
31
|
+
transform: translate(-50%, -0.25rem);
|
|
32
|
+
transition: opacity 0.15s ease, transform 0.15s ease, visibility 0s 0.15s;
|
|
33
|
+
|
|
34
|
+
// A bridge over the gap, so the pointer can move onto the tooltip.
|
|
35
|
+
&::before {
|
|
36
|
+
content: "";
|
|
37
|
+
position: absolute;
|
|
38
|
+
left: 0;
|
|
39
|
+
right: 0;
|
|
40
|
+
top: -$tooltip-gap;
|
|
41
|
+
height: $tooltip-gap;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
&[data-open="true"] .tooltip-bubble {
|
|
46
|
+
opacity: 1;
|
|
47
|
+
visibility: visible;
|
|
48
|
+
pointer-events: auto;
|
|
49
|
+
transform: translate(-50%, 0);
|
|
50
|
+
transition: opacity 0.15s ease, transform 0.15s ease, visibility 0s 0s;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
&[data-align="start"] .tooltip-bubble { left: 0; transform: translate(0, -0.25rem); }
|
|
54
|
+
&[data-align="start"][data-open="true"] .tooltip-bubble { transform: none; }
|
|
55
|
+
&[data-align="end"] .tooltip-bubble { left: auto; right: 0; transform: translate(0, -0.25rem); }
|
|
56
|
+
&[data-align="end"][data-open="true"] .tooltip-bubble { transform: none; }
|
|
57
|
+
|
|
58
|
+
&[data-placement="top"] .tooltip-bubble {
|
|
59
|
+
top: auto;
|
|
60
|
+
bottom: calc(100% + #{$tooltip-gap});
|
|
61
|
+
|
|
62
|
+
&::before { top: auto; bottom: -$tooltip-gap; }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Key caps on the inverted background.
|
|
66
|
+
.tooltip-bubble .kbd-key {
|
|
67
|
+
border-color: color-mix(in srgb, var(--nv-bg) 40%, transparent);
|
|
68
|
+
background: color-mix(in srgb, var(--nv-bg) 12%, transparent);
|
|
69
|
+
color: var(--nv-bg);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@media (prefers-reduced-motion: reduce) {
|
|
74
|
+
.#{$cls-prefix}.tooltip .tooltip-bubble { transition: none; }
|
|
75
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import Kbd from '../Kbd/Kbd';
|
|
5
|
+
import { keyboardFocus } from '../../tooltip';
|
|
6
|
+
import './Tooltip.scss';
|
|
7
|
+
|
|
8
|
+
export interface TooltipProps {
|
|
9
|
+
/** What the tooltip says. */
|
|
10
|
+
content: React.ReactNode;
|
|
11
|
+
/** A keyboard shortcut to show after it, as key caps: ['⌘', 'B']. */
|
|
12
|
+
shortcut?: string[];
|
|
13
|
+
/** The tooltip's id: the control is described by it (aria-describedby). */
|
|
14
|
+
id: string;
|
|
15
|
+
/** Below the control (default) or above it. */
|
|
16
|
+
placement?: 'bottom' | 'top';
|
|
17
|
+
/** Which edge it lines up with: centered (default), or the control's start or end edge (near the screen's edges). */
|
|
18
|
+
align?: 'center' | 'start' | 'end';
|
|
19
|
+
/** How long the pointer must rest on the control before it shows, in ms. Keyboard focus shows it at once. */
|
|
20
|
+
delay?: number;
|
|
21
|
+
/** The one control it describes. */
|
|
22
|
+
children: React.ReactElement;
|
|
23
|
+
/** Custom class name. */
|
|
24
|
+
className?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A short label for a control, shown while the pointer rests on it or it has
|
|
29
|
+
* keyboard focus; optionally with its keyboard shortcut as key caps. It meets
|
|
30
|
+
* WCAG 1.4.13: Esc hides it without moving focus, the pointer can move onto
|
|
31
|
+
* it, and it stays until the pointer or focus leaves. Screen readers get it as
|
|
32
|
+
* the control's description.
|
|
33
|
+
*/
|
|
34
|
+
export default function Tooltip({ content, shortcut, id, placement = 'bottom', align = 'center', delay = 300, children, className }: TooltipProps) {
|
|
35
|
+
const [open, setOpen] = React.useState(false);
|
|
36
|
+
const timer = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
37
|
+
const hide = () => { clearTimeout(timer.current as any); setOpen(false); };
|
|
38
|
+
const show = (now: boolean) => {
|
|
39
|
+
clearTimeout(timer.current as any);
|
|
40
|
+
if (now) setOpen(true); else timer.current = setTimeout(() => setOpen(true), delay);
|
|
41
|
+
};
|
|
42
|
+
React.useEffect(() => () => clearTimeout(timer.current as any), []);
|
|
43
|
+
// Esc hides it (focus stays where it is).
|
|
44
|
+
React.useEffect(() => {
|
|
45
|
+
if (!open) return undefined;
|
|
46
|
+
const esc = (event: KeyboardEvent) => { if (event.key === 'Escape') hide(); };
|
|
47
|
+
document.addEventListener('keydown', esc);
|
|
48
|
+
return () => document.removeEventListener('keydown', esc);
|
|
49
|
+
}, [open]);
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<span
|
|
53
|
+
className={ classNames(className, 'tooltip', CONSTANT.CLASS_PREFIX) }
|
|
54
|
+
data-open={ open }
|
|
55
|
+
data-placement={ placement }
|
|
56
|
+
data-align={ align }
|
|
57
|
+
onMouseEnter={ () => show(false) }
|
|
58
|
+
onMouseLeave={ hide }
|
|
59
|
+
onFocus={ (event) => { if (keyboardFocus(event.target)) show(true); } }
|
|
60
|
+
onBlur={ hide }
|
|
61
|
+
>
|
|
62
|
+
{ React.cloneElement(children, { 'aria-describedby': id }) }
|
|
63
|
+
<span className="tooltip-bubble" role="tooltip" id={ id }>
|
|
64
|
+
<span className="tooltip-text">{ content }</span>
|
|
65
|
+
{ shortcut && shortcut.length > 0 && <Kbd keys={ shortcut } /> }
|
|
66
|
+
</span>
|
|
67
|
+
</span>
|
|
68
|
+
);
|
|
69
|
+
}
|
package/src/css.ts
CHANGED
|
@@ -15,6 +15,9 @@ import './components/Tree/Tree.scss';
|
|
|
15
15
|
import './components/Menu/Menu.scss';
|
|
16
16
|
import './components/Dropdown/Dropdown.scss';
|
|
17
17
|
import './components/Anchor/Anchor.scss';
|
|
18
|
+
import './components/Kbd/Kbd.scss';
|
|
19
|
+
import './components/Tooltip/Tooltip.scss';
|
|
20
|
+
import './components/NavToggle/NavToggle.scss';
|
|
18
21
|
import './components/Select/Select.scss';
|
|
19
22
|
import './components/Panel/Panel.scss';
|
|
20
23
|
import './components/Progress/Progress.scss';
|
package/src/icons.ts
CHANGED
|
@@ -85,6 +85,7 @@ const DEFS = {
|
|
|
85
85
|
heart: [path('M12 20s-8-4.6-8-10.2A4.3 4.3 0 0 1 12 7.4a4.3 4.3 0 0 1 8 2.4C20 15.4 12 20 12 20z')],
|
|
86
86
|
grid: [rect(4, 4, 6, 6, 1), rect(14, 4, 6, 6, 1), rect(4, 14, 6, 6, 1), rect(14, 14, 6, 6, 1)],
|
|
87
87
|
list: [path('M9 6h11M9 12h11M9 18h11'), dot(4.5, 6), dot(4.5, 12), dot(4.5, 18)],
|
|
88
|
+
'align-left': [path('M4 6h16M4 12h10M4 18h13')],
|
|
88
89
|
link: [path('M10 14a4 4 0 0 0 5.66 0l3-3a4 4 0 0 0-5.66-5.66l-1 1'), path('M14 10a4 4 0 0 0-5.66 0l-3 3a4 4 0 0 0 5.66 5.66l1-1')],
|
|
89
90
|
// Theme
|
|
90
91
|
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')],
|
package/src/index.ts
CHANGED
|
@@ -40,6 +40,13 @@ export { default as Anchor } from './components/Anchor/Anchor';
|
|
|
40
40
|
export type { AnchorProps, AnchorItem } from './components/Anchor/Anchor';
|
|
41
41
|
export { default as Dropdown } from './components/Dropdown/Dropdown';
|
|
42
42
|
export type { DropdownProps } from './components/Dropdown/Dropdown';
|
|
43
|
+
export { default as Kbd } from './components/Kbd/Kbd';
|
|
44
|
+
export type { KbdProps } from './components/Kbd/Kbd';
|
|
45
|
+
export { default as Tooltip } from './components/Tooltip/Tooltip';
|
|
46
|
+
export type { TooltipProps } from './components/Tooltip/Tooltip';
|
|
47
|
+
export { default as NavToggle } from './components/NavToggle/NavToggle';
|
|
48
|
+
export type { NavToggleProps, NavToggleMorph } from './components/NavToggle/NavToggle';
|
|
49
|
+
export { modKey } from './tooltip';
|
|
43
50
|
export { default as Menu } from './components/Menu/Menu';
|
|
44
51
|
export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './components/Menu/Menu';
|
|
45
52
|
export { default as Select } from './components/Select/Select';
|
package/src/menu.ts
CHANGED
|
@@ -13,9 +13,12 @@
|
|
|
13
13
|
*/
|
|
14
14
|
export type MenuKind = 'nav' | 'actions' | 'tree';
|
|
15
15
|
|
|
16
|
+
/** An item as the rules see it: a link, and maybe child items (a nested group). */
|
|
17
|
+
interface NavNode { id?: string; label?: unknown; href?: string; items?: NavNode[] }
|
|
18
|
+
|
|
16
19
|
interface KindProps {
|
|
17
20
|
kind?: MenuKind;
|
|
18
|
-
sections?: { href?: string; items?:
|
|
21
|
+
sections?: { href?: string; items?: NavNode[] }[];
|
|
19
22
|
items?: unknown;
|
|
20
23
|
activePath?: unknown;
|
|
21
24
|
}
|
|
@@ -29,13 +32,46 @@ export function menuKind(props: KindProps): MenuKind {
|
|
|
29
32
|
if (props.kind) return props.kind;
|
|
30
33
|
if (!props.sections && (props.items !== undefined || props.activePath !== undefined)) return 'tree';
|
|
31
34
|
const sections = props.sections || [];
|
|
32
|
-
const
|
|
35
|
+
const linked = (items: NavNode[] | undefined): boolean => !!items && items.some((item) => item.href !== undefined || linked(item.items));
|
|
36
|
+
const links = sections.some((s) => s.href !== undefined || linked(s.items));
|
|
33
37
|
return links || !sections.length ? 'nav' : 'actions';
|
|
34
38
|
}
|
|
35
39
|
|
|
40
|
+
const slug = (text: unknown): string => String(text).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
41
|
+
|
|
36
42
|
/** A section's id: its own, or its title as a slug ("Get started" -> "get-started"). */
|
|
37
|
-
export const sectionId = (section: { id?: string; title?: unknown }): string =>
|
|
38
|
-
|
|
43
|
+
export const sectionId = (section: { id?: string; title?: unknown }): string => section.id || slug(section.title ?? 'section');
|
|
44
|
+
|
|
45
|
+
/** Whether a navigation item is a nested group: it has child items of its own. */
|
|
46
|
+
export const isGroup = (item: NavNode): boolean => !!(item.items && item.items.length);
|
|
47
|
+
|
|
48
|
+
/** A nested group's id: its own, or its parent's id and its label as a slug ("settings" + "Advanced" -> "settings-advanced"). */
|
|
49
|
+
export const groupId = (item: NavNode, parent: string): string => item.id || `${parent}-${slug(item.label)}`;
|
|
50
|
+
|
|
51
|
+
/** Whether `current` is the link of one of these items, at any depth. */
|
|
52
|
+
export const holdsCurrent = (items: NavNode[] | undefined, current: string | undefined): boolean =>
|
|
53
|
+
current !== undefined && !!items && items.some((item) => item.href === current || holdsCurrent(item.items, current));
|
|
54
|
+
|
|
55
|
+
/** Every section and nested group of a navigation menu, by id, and whether the current page is inside it. */
|
|
56
|
+
export function menuGroups(sections: { id?: string; title?: unknown; items?: NavNode[] }[], current?: string): [string, boolean][] {
|
|
57
|
+
const all: [string, boolean][] = [];
|
|
58
|
+
const walk = (items: NavNode[], parent: string) => items.forEach((item) => {
|
|
59
|
+
if (!isGroup(item)) return;
|
|
60
|
+
const gid = groupId(item, parent);
|
|
61
|
+
all.push([gid, holdsCurrent(item.items, current)]);
|
|
62
|
+
walk(item.items as NavNode[], gid);
|
|
63
|
+
});
|
|
64
|
+
sections.forEach((section) => {
|
|
65
|
+
const sid = sectionId(section);
|
|
66
|
+
all.push([sid, holdsCurrent(section.items, current)]);
|
|
67
|
+
walk(section.items || [], sid);
|
|
68
|
+
});
|
|
69
|
+
return all;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Which sections and groups start open: as `defaultOpen` says, and always the ones the current page is in. */
|
|
73
|
+
export const menuOpenState = (sections: { id?: string; title?: unknown; items?: NavNode[] }[], current: string | undefined, defaultOpen: boolean | Record<string, boolean> | undefined): Record<string, boolean> =>
|
|
74
|
+
Object.fromEntries(menuGroups(sections, current).map(([gid, home]) => [gid, home || startsOpen(defaultOpen, gid)]));
|
|
39
75
|
|
|
40
76
|
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
41
77
|
export const startsOpen = (defaultOpen: boolean | Record<string, boolean> | undefined, id: string): boolean =>
|
|
@@ -57,9 +93,10 @@ export function menuFocus(event: FocusEvent, menu: HTMLElement): void {
|
|
|
57
93
|
* at the ends) and Home / End to the first / last, in every kind.
|
|
58
94
|
* - Navigation (the WAI-ARIA disclosure navigation pattern, with its
|
|
59
95
|
* optional keys): Tab still goes link by link. The rows are the section
|
|
60
|
-
* titles and the links in open
|
|
61
|
-
* goes to its first
|
|
62
|
-
*
|
|
96
|
+
* and group titles and the links in open ones; Right on a section or group
|
|
97
|
+
* opens it, or goes to its first row; Left closes it, or goes up to the
|
|
98
|
+
* one it is in. Enter or Space follows a link (Space too, as in the
|
|
99
|
+
* WAI-ARIA navigation tree) and opens a section.
|
|
63
100
|
* - Actions (the WAI-ARIA menu pattern): one Tab stop that follows the
|
|
64
101
|
* focus; typing a letter goes to the next item starting with it; Enter or
|
|
65
102
|
* Space runs the item.
|
|
@@ -106,28 +143,37 @@ function navKeydown(event: KeyboardEvent, nav: HTMLElement): boolean {
|
|
|
106
143
|
const rows = Array.from(nav.querySelectorAll<HTMLElement>('.menu-title, .menu-item'))
|
|
107
144
|
.filter((row) => !row.closest('.menu-section[data-open="false"] .menu-items'));
|
|
108
145
|
if (!rows.length) return false;
|
|
146
|
+
// The section or nested group the key was pressed in (its own heading, or a row of its list).
|
|
109
147
|
const section = target.closest<HTMLElement>('.menu-section');
|
|
110
148
|
// The chevron beside a linked title counts as that title's row.
|
|
111
149
|
const onHeading = !!target.closest('.menu-heading');
|
|
112
|
-
const title = section && section.querySelector<HTMLElement>('.menu-title');
|
|
150
|
+
const title = section && section.querySelector<HTMLElement>(':scope > .menu-heading .menu-title');
|
|
113
151
|
const at = rows.indexOf(onHeading && title ? title : target);
|
|
114
|
-
const focus = (row:
|
|
115
|
-
const toggle = () => { const button = section && section.querySelector<HTMLElement>('.menu-toggle'); if (button) button.click(); };
|
|
152
|
+
const focus = (row: Element | null | undefined) => { if (row) (row as HTMLElement).focus(); };
|
|
153
|
+
const toggle = () => { const button = section && section.querySelector<HTMLElement>(':scope > .menu-heading .menu-toggle'); if (button) button.click(); };
|
|
116
154
|
const isOpen = !!section && section.getAttribute('data-open') !== 'false';
|
|
155
|
+
const outer = section && section.parentElement ? section.parentElement.closest<HTMLElement>('.menu-section') : null;
|
|
117
156
|
|
|
118
157
|
switch (event.key) {
|
|
119
158
|
case 'ArrowDown': focus(rows[(at + 1) % rows.length]); break;
|
|
120
159
|
case 'ArrowUp': focus(rows[at <= 0 ? rows.length - 1 : at - 1]); break;
|
|
121
160
|
case 'Home': focus(rows[0]); break;
|
|
122
161
|
case 'End': focus(rows[rows.length - 1]); break;
|
|
162
|
+
case ' ':
|
|
163
|
+
// Space follows a link as Enter does (buttons take Space by themselves).
|
|
164
|
+
if (target.tagName !== 'A') return false;
|
|
165
|
+
target.click();
|
|
166
|
+
break;
|
|
123
167
|
case 'ArrowRight':
|
|
124
168
|
if (!onHeading || !section) return false;
|
|
125
169
|
if (!isOpen) toggle();
|
|
126
|
-
else focus(section.querySelector
|
|
170
|
+
else focus(section.querySelector(':scope > .menu-items .menu-title, :scope > .menu-items .menu-item'));
|
|
127
171
|
break;
|
|
128
172
|
case 'ArrowLeft':
|
|
129
173
|
if (!section) return false;
|
|
130
|
-
if (onHeading)
|
|
174
|
+
if (!onHeading) focus(title);
|
|
175
|
+
else if (isOpen) toggle();
|
|
176
|
+
else if (outer) focus(outer.querySelector(':scope > .menu-heading .menu-title'));
|
|
131
177
|
break;
|
|
132
178
|
default: return false;
|
|
133
179
|
}
|
package/src/respawn/Anchor.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import classNames from 'classnames';
|
|
2
2
|
import CONSTANT from '../constants';
|
|
3
|
-
import { AnchorItem, anchorRail, anchorDash,
|
|
3
|
+
import { AnchorItem, anchorRail, anchorDash, anchorRange, anchorGo } from '../anchor';
|
|
4
|
+
import Icon from './Icon';
|
|
4
5
|
import { spawn, ElementProps } from './spawn';
|
|
5
6
|
import { updatable, NurvusElement } from './update';
|
|
6
7
|
import '../components/Anchor/Anchor.scss';
|
|
@@ -26,6 +27,8 @@ export interface AnchorProps extends ElementProps {
|
|
|
26
27
|
updateHash?: boolean;
|
|
27
28
|
/** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
|
|
28
29
|
onNavigate?: (href: string, event: MouseEvent) => void;
|
|
30
|
+
/** A soft wash behind the row being read, fading to the right. */
|
|
31
|
+
fade?: boolean;
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
/**
|
|
@@ -35,22 +38,26 @@ export interface AnchorProps extends ElementProps {
|
|
|
35
38
|
*/
|
|
36
39
|
export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, AnchorProps> {
|
|
37
40
|
return updatable(({
|
|
38
|
-
items, title = 'On this page', id = 'anchor', offset = 96, current, scrollRoot, updateHash = true, onNavigate, className, ...rest
|
|
41
|
+
items, title = 'On this page', id = 'anchor', offset = 96, current, scrollRoot, updateHash = true, onNavigate, fade = false, className, ...rest
|
|
39
42
|
}: AnchorProps) => {
|
|
40
43
|
const controlled = current !== undefined;
|
|
41
44
|
const rail = anchorRail(items.map((item) => item.level || 1));
|
|
42
|
-
|
|
45
|
+
const fixed = controlled ? Math.max(0, items.findIndex((item) => item.href === current)) : 0;
|
|
46
|
+
let range: [number, number] = [fixed, fixed];
|
|
43
47
|
const links: HTMLElement[] = [];
|
|
44
|
-
const dashOf = (
|
|
45
|
-
const thumb = spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-thumb', d: rail.d, style: dashOf(
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
const dashOf = (r: [number, number]) => { const d = anchorDash(rail, r[0], r[1]); return { 'stroke-dasharray': d.dasharray, 'stroke-dashoffset': String(d.dashoffset) }; };
|
|
49
|
+
const thumb = spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-thumb', d: rail.d, style: dashOf(range) });
|
|
50
|
+
const caps = items.length ? [0, 1].map((k) => spawn<any>({ tag: 'circle', namespace: SVG, className: 'anchor-cap', cx: rail.caps[k][0], cy: rail.caps[k][1], r: '2.25', 'data-lit': k === 0 ? range[0] === 0 : range[1] === items.length - 1 })) : [];
|
|
51
|
+
const mark = (r: [number, number]) => {
|
|
52
|
+
range = r;
|
|
53
|
+
const d = dashOf(r);
|
|
49
54
|
thumb.style.setProperty('stroke-dasharray', d['stroke-dasharray']);
|
|
50
55
|
thumb.style.setProperty('stroke-dashoffset', d['stroke-dashoffset']);
|
|
56
|
+
if (caps.length) { caps[0].setAttribute('data-lit', String(r[0] === 0)); caps[1].setAttribute('data-lit', String(r[1] === items.length - 1)); }
|
|
51
57
|
links.forEach((link, k) => {
|
|
52
|
-
link.setAttribute('data-active', String(k ===
|
|
53
|
-
|
|
58
|
+
link.setAttribute('data-active', String(k === r[0]));
|
|
59
|
+
link.setAttribute('data-inview', String(k >= r[0] && k <= r[1]));
|
|
60
|
+
if (k === r[0]) link.setAttribute('aria-current', 'location'); else link.removeAttribute('aria-current');
|
|
54
61
|
});
|
|
55
62
|
};
|
|
56
63
|
|
|
@@ -58,9 +65,10 @@ export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, A
|
|
|
58
65
|
tag: 'nav',
|
|
59
66
|
className: classNames(className, 'anchor', CONSTANT.CLASS_PREFIX),
|
|
60
67
|
'aria-labelledby': `${id}-title`,
|
|
68
|
+
'data-fade': fade || undefined,
|
|
61
69
|
...rest,
|
|
62
70
|
children: [
|
|
63
|
-
spawn({ tag: 'p', className: 'anchor-title', id: `${id}-title`, children: title }),
|
|
71
|
+
spawn({ tag: 'p', className: 'anchor-title', children: [Icon({ name: 'align-left', size: '1.125em' }), spawn({ tag: 'span', id: `${id}-title`, children: title })] }),
|
|
64
72
|
spawn({
|
|
65
73
|
className: 'anchor-body',
|
|
66
74
|
children: [
|
|
@@ -72,7 +80,7 @@ export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, A
|
|
|
72
80
|
style: { width: `${rail.width / 16}rem`, height: `${rail.height / 16}rem` },
|
|
73
81
|
'aria-hidden': true,
|
|
74
82
|
focusable: 'false',
|
|
75
|
-
children: [spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-track', d: rail.d }), thumb],
|
|
83
|
+
children: [spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-track', d: rail.d }), thumb, ...caps],
|
|
76
84
|
}),
|
|
77
85
|
spawn({
|
|
78
86
|
tag: 'ul',
|
|
@@ -83,13 +91,14 @@ export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, A
|
|
|
83
91
|
className: 'anchor-link',
|
|
84
92
|
href: item.href,
|
|
85
93
|
title: item.label,
|
|
86
|
-
'data-active': i ===
|
|
87
|
-
'
|
|
94
|
+
'data-active': i === range[0],
|
|
95
|
+
'data-inview': i >= range[0] && i <= range[1],
|
|
96
|
+
'aria-current': i === range[0] ? 'location' : undefined,
|
|
88
97
|
events: {
|
|
89
98
|
click: (event: MouseEvent) => {
|
|
90
99
|
if (onNavigate) onNavigate(item.href, event);
|
|
91
100
|
if (event.defaultPrevented) return;
|
|
92
|
-
if (anchorGo(item.href, updateHash)) { event.preventDefault(); if (!controlled) mark(i); }
|
|
101
|
+
if (anchorGo(item.href, updateHash)) { event.preventDefault(); if (!controlled) mark([i, i]); }
|
|
93
102
|
},
|
|
94
103
|
},
|
|
95
104
|
children: item.label,
|
|
@@ -113,8 +122,8 @@ export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, A
|
|
|
113
122
|
frame = requestAnimationFrame(() => {
|
|
114
123
|
if (!el.isConnected) { if (seen) { window.removeEventListener('scroll', update, true); window.removeEventListener('resize', update); } return; }
|
|
115
124
|
seen = true;
|
|
116
|
-
const next =
|
|
117
|
-
if (next !==
|
|
125
|
+
const next = anchorRange(items, offset, root());
|
|
126
|
+
if (next[0] !== range[0] || next[1] !== range[1]) mark(next);
|
|
118
127
|
});
|
|
119
128
|
};
|
|
120
129
|
window.addEventListener('scroll', update, { passive: true, capture: true });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/Kbd/Kbd.scss';
|
|
6
|
+
|
|
7
|
+
export interface KbdProps extends ElementProps {
|
|
8
|
+
/** The keys of a shortcut, in order: ['⌘', 'B']. Each is drawn as a key cap. */
|
|
9
|
+
keys?: string[];
|
|
10
|
+
/** One key, when there is only one (instead of keys). */
|
|
11
|
+
children?: Child;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A keyboard key or shortcut as key caps: the same markup as the React Kbd. */
|
|
15
|
+
export default function Kbd(props: KbdProps = {}): NurvusElement<HTMLElement, KbdProps> {
|
|
16
|
+
return updatable(({ keys, children, className, ...rest }: KbdProps) => spawn<HTMLElement>({
|
|
17
|
+
tag: 'kbd',
|
|
18
|
+
className: classNames(className, 'kbd', CONSTANT.CLASS_PREFIX),
|
|
19
|
+
...rest,
|
|
20
|
+
children: (keys && keys.length ? keys : [children]).map((key) => spawn({ tag: 'kbd', className: 'kbd-key', children: key as Child })),
|
|
21
|
+
}), props);
|
|
22
|
+
}
|
package/src/respawn/Menu.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import classNames from 'classnames';
|
|
2
2
|
import CONSTANT from '../constants';
|
|
3
|
-
import { sectionId,
|
|
3
|
+
import { sectionId, groupId, isGroup, menuOpenState, menuKeydown, menuFocus, menuKind, MenuKind } from '../menu';
|
|
4
4
|
import Tree, { TreeProps } from './Tree';
|
|
5
5
|
import { spawn, Child, ElementProps } from './spawn';
|
|
6
6
|
import { updatable, NurvusElement } from './update';
|
|
@@ -19,6 +19,10 @@ export interface MenuItem {
|
|
|
19
19
|
disabled?: boolean;
|
|
20
20
|
/** Draws a hairline above this item, to split a section into groups. */
|
|
21
21
|
divider?: boolean;
|
|
22
|
+
/** Navigation: child items, which make this item a nested group that opens and closes like a section, as deep as you like. With an `href` its label is a link too. */
|
|
23
|
+
items?: MenuItem[];
|
|
24
|
+
/** Navigation, a nested group: a stable id for its open state. Defaults to its section's id and its label ("settings-advanced"). */
|
|
25
|
+
id?: string;
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
export interface MenuSection {
|
|
@@ -38,7 +42,7 @@ interface MenuBaseProps extends ElementProps {
|
|
|
38
42
|
sections?: MenuSection[];
|
|
39
43
|
/** The href of the page being shown: its link is marked, and its section opens. */
|
|
40
44
|
current?: string;
|
|
41
|
-
/** Which sections start open: true (all), false (none) or a map of
|
|
45
|
+
/** Which sections and nested groups start open: true (all), false (none) or a map of id -> open. Defaults to all. */
|
|
42
46
|
defaultOpen?: boolean | Record<string, boolean>;
|
|
43
47
|
/** Called when a section opens or closes (e.g. to remember it). */
|
|
44
48
|
onToggle?: (id: string, open: boolean) => void;
|
|
@@ -90,15 +94,13 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
90
94
|
return updatable<HTMLElement, MenuListProps>(({
|
|
91
95
|
kind: _kind, sections = [], current, defaultOpen = true, onToggle, onNavigate, chevron = 'start', label = 'Pages', id = 'menu', className, ...rest
|
|
92
96
|
}: MenuListProps, keep) => {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const home = sections.find((s) => s.items.some((item) => item.href !== undefined && item.href === current));
|
|
96
|
-
if (home) open[sectionId(home)] = true;
|
|
97
|
+
// As in React: defaultOpen, and the current page's section and group always open.
|
|
98
|
+
const open: Record<string, boolean> = menuOpenState(sections, current, defaultOpen);
|
|
97
99
|
|
|
98
100
|
const navigate = (href: string) => ({ click: (event: MouseEvent) => { if (onNavigate) onNavigate(href, event); } });
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
// A section, or a nested group (an item with items): a heading that opens and closes its list.
|
|
103
|
+
const renderGroup = (sid: string, title: Child, href: string | undefined, items: MenuItem[]): HTMLElement => {
|
|
102
104
|
const listId = `${id}-${sid}`;
|
|
103
105
|
const toggles: HTMLElement[] = [];
|
|
104
106
|
const toggle = () => {
|
|
@@ -107,18 +109,18 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
107
109
|
el.setAttribute('data-open', String(open[sid]));
|
|
108
110
|
toggles.forEach((t) => {
|
|
109
111
|
t.setAttribute('aria-expanded', String(open[sid]));
|
|
110
|
-
if (t.hasAttribute('aria-label')) t.setAttribute('aria-label', `${open[sid] ? 'Hide' : 'Show'} ${String(
|
|
112
|
+
if (t.hasAttribute('aria-label')) t.setAttribute('aria-label', `${open[sid] ? 'Hide' : 'Show'} ${String(title)}`);
|
|
111
113
|
});
|
|
112
114
|
if (onToggle) onToggle(sid, open[sid]);
|
|
113
115
|
};
|
|
114
116
|
const toggleProps = (extra: Record<string, unknown>) => ({
|
|
115
117
|
tag: 'button', type: 'button', 'aria-expanded': open[sid], 'aria-controls': listId, events: { click: toggle }, ...extra,
|
|
116
118
|
});
|
|
117
|
-
const link = () => spawn({ tag: 'a', className: 'menu-title', href:
|
|
118
|
-
const chev = () => spawn(toggleProps({ className: 'menu-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${String(
|
|
119
|
-
const heading =
|
|
119
|
+
const link = () => spawn({ tag: 'a', className: 'menu-title', href, 'data-active': current === href, 'aria-current': current === href ? 'page' : undefined, events: navigate(href as string), children: title });
|
|
120
|
+
const chev = () => spawn(toggleProps({ className: 'menu-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${String(title)}` }));
|
|
121
|
+
const heading = href
|
|
120
122
|
? (chevron === 'start' ? [chev(), link()] : [link(), chev()])
|
|
121
|
-
: [spawn(toggleProps({ className: 'menu-title menu-toggle', children:
|
|
123
|
+
: [spawn(toggleProps({ className: 'menu-title menu-toggle', children: title }))];
|
|
122
124
|
heading.forEach((h) => { if (h.tagName === 'BUTTON') toggles.push(h); });
|
|
123
125
|
|
|
124
126
|
const el: HTMLElement = spawn({
|
|
@@ -132,10 +134,10 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
132
134
|
id: listId,
|
|
133
135
|
children: spawn({
|
|
134
136
|
tag: 'ul',
|
|
135
|
-
children:
|
|
137
|
+
children: items.map((item) => spawn({
|
|
136
138
|
tag: 'li',
|
|
137
139
|
'data-divider': item.divider || undefined,
|
|
138
|
-
children: item.href !== undefined
|
|
140
|
+
children: isGroup(item) ? renderGroup(groupId(item, sid), item.label, item.href, item.items as MenuItem[]) : item.href !== undefined
|
|
139
141
|
? spawn({
|
|
140
142
|
tag: 'a',
|
|
141
143
|
className: 'menu-item',
|
|
@@ -176,7 +178,7 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
176
178
|
if (events && events.keydown) events.keydown(event, el);
|
|
177
179
|
},
|
|
178
180
|
},
|
|
179
|
-
children: sections.map(
|
|
181
|
+
children: sections.map((section) => renderGroup(sectionId(section), section.title as Child, section.href, section.items)),
|
|
180
182
|
});
|
|
181
183
|
}, props as MenuListProps) as any;
|
|
182
184
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, ElementProps } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/NavToggle/NavToggle.scss';
|
|
6
|
+
|
|
7
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
8
|
+
|
|
9
|
+
export type NavToggleMorph = 'slide' | 'cross' | 'squeeze' | 'elastic' | 'spin' | 'arrow' | 'chevron' | 'collapse';
|
|
10
|
+
|
|
11
|
+
export interface NavToggleProps extends ElementProps {
|
|
12
|
+
/** Whether the navigation it controls is showing. */
|
|
13
|
+
open: boolean;
|
|
14
|
+
/** 'hamburger' (three bars) or 'sidebar' (a window with a side pane). */
|
|
15
|
+
variant?: 'hamburger' | 'sidebar';
|
|
16
|
+
/** Hamburger: how the bars change while open. */
|
|
17
|
+
morph?: NavToggleMorph;
|
|
18
|
+
/** Hamburger: bars of one length, or 'asymmetric'. */
|
|
19
|
+
lines?: 'even' | 'asymmetric';
|
|
20
|
+
/** What it shows and hides, for its name. */
|
|
21
|
+
label?: string;
|
|
22
|
+
/** The id of the navigation it controls (aria-controls). */
|
|
23
|
+
controls?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The show/hide navigation button, the same markup as the React NavToggle.
|
|
28
|
+
* toggle.update({ open }) changes it in place, so the shapes animate.
|
|
29
|
+
*/
|
|
30
|
+
export default function NavToggle(props: NavToggleProps): NurvusElement<HTMLButtonElement, NavToggleProps> {
|
|
31
|
+
const draw = ({ open, variant = 'hamburger', morph = 'slide', lines = 'even', label = 'navigation', controls, className, ...rest }: NavToggleProps) => {
|
|
32
|
+
const sidebar = variant === 'sidebar';
|
|
33
|
+
return spawn<HTMLButtonElement>({
|
|
34
|
+
tag: 'button',
|
|
35
|
+
type: 'button',
|
|
36
|
+
className: classNames(className, 'nav-toggle', CONSTANT.CLASS_PREFIX),
|
|
37
|
+
'data-variant': variant,
|
|
38
|
+
'data-morph': sidebar ? undefined : morph,
|
|
39
|
+
'data-lines': sidebar ? undefined : lines,
|
|
40
|
+
'data-open': open,
|
|
41
|
+
'aria-expanded': open,
|
|
42
|
+
'aria-controls': controls,
|
|
43
|
+
'aria-label': `${open ? 'Hide' : 'Show'} ${label}`,
|
|
44
|
+
...rest,
|
|
45
|
+
children: sidebar
|
|
46
|
+
? spawn<any>({
|
|
47
|
+
tag: 'svg',
|
|
48
|
+
namespace: SVG,
|
|
49
|
+
className: 'nav-toggle-panel',
|
|
50
|
+
viewBox: '0 0 24 24',
|
|
51
|
+
'aria-hidden': true,
|
|
52
|
+
focusable: 'false',
|
|
53
|
+
children: [
|
|
54
|
+
spawn<any>({ tag: 'rect', namespace: SVG, className: 'nav-toggle-outline', x: '2.5', y: '4', width: '19', height: '16', rx: '2.5' }),
|
|
55
|
+
spawn<any>({ tag: 'rect', namespace: SVG, className: 'nav-toggle-fill', x: '4', y: '5.5', width: '5', height: '13', rx: '0.5' }),
|
|
56
|
+
spawn<any>({ tag: 'path', namespace: SVG, className: 'nav-toggle-divider', d: 'M10 4V20' }),
|
|
57
|
+
],
|
|
58
|
+
})
|
|
59
|
+
: spawn({ tag: 'span', className: 'nav-toggle-bars', 'aria-hidden': true, children: [spawn({ tag: 'span' }), spawn({ tag: 'span' }), spawn({ tag: 'span' })] }),
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
// Opening and closing change the element in place (so the bars move); other changes rebuild it, keeping the open state.
|
|
63
|
+
let current = props;
|
|
64
|
+
const wire = (el: NurvusElement<HTMLButtonElement, NavToggleProps>): NurvusElement<HTMLButtonElement, NavToggleProps> => {
|
|
65
|
+
const rebuild = el.update;
|
|
66
|
+
el.update = (changes) => {
|
|
67
|
+
current = { ...current, ...changes };
|
|
68
|
+
if (!Object.keys(changes).every((k) => k === 'open')) return wire(rebuild({ ...changes, open: current.open }));
|
|
69
|
+
const open = !!current.open;
|
|
70
|
+
el.setAttribute('data-open', String(open));
|
|
71
|
+
el.setAttribute('aria-expanded', String(open));
|
|
72
|
+
el.setAttribute('aria-label', `${open ? 'Hide' : 'Show'} ${current.label || 'navigation'}`);
|
|
73
|
+
return el;
|
|
74
|
+
};
|
|
75
|
+
return el;
|
|
76
|
+
};
|
|
77
|
+
return wire(updatable(draw, props));
|
|
78
|
+
}
|