@nurvus/ui 0.1.0-alpha.16 → 0.1.0-alpha.18
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 +13 -0
- package/dist/icons.svg +2 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +6 -1
- package/dist/respawn.js +1 -1
- package/dist/types/anchor.d.ts +45 -0
- package/dist/types/components/Anchor/Anchor.d.ts +33 -0
- package/dist/types/components/Dropdown/Dropdown.d.ts +31 -0
- package/dist/types/components/Kbd/Kbd.d.ts +16 -0
- package/dist/types/components/Menu/Menu.d.ts +8 -1
- package/dist/types/components/NavToggle/NavToggle.d.ts +27 -0
- package/dist/types/components/Tooltip/Tooltip.d.ts +27 -0
- package/dist/types/dropdown.d.ts +9 -0
- package/dist/types/icons.d.ts +2 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/respawn/Anchor.d.ts +30 -0
- package/dist/types/respawn/Dropdown.d.ts +27 -0
- package/dist/types/respawn/Kbd.d.ts +10 -0
- package/dist/types/respawn/Menu.d.ts +2 -0
- 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 +11 -0
- package/dist/types/tooltip.d.ts +4 -0
- package/package.json +1 -1
- package/src/anchor.ts +150 -0
- package/src/components/Anchor/Anchor.md +59 -0
- package/src/components/Anchor/Anchor.scss +88 -0
- package/src/components/Anchor/Anchor.tsx +114 -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/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 +1 -1
- package/src/components/Menu/Menu.scss +78 -0
- package/src/components/Menu/Menu.tsx +15 -3
- 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 +5 -0
- package/src/dropdown.ts +24 -0
- package/src/icons.ts +3 -0
- package/src/index.ts +11 -0
- package/src/respawn/Anchor.ts +135 -0
- package/src/respawn/Dropdown.ts +90 -0
- package/src/respawn/Kbd.ts +22 -0
- package/src/respawn/Menu.ts +10 -7
- package/src/respawn/NavToggle.ts +78 -0
- package/src/respawn/Tooltip.ts +63 -0
- package/src/respawn/index.ts +11 -0
- package/src/tooltip.ts +14 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { AnchorItem, anchorRail, anchorDash, anchorRange, anchorGo } from '../anchor';
|
|
4
|
+
import Icon from './Icon';
|
|
5
|
+
import { spawn, ElementProps } from './spawn';
|
|
6
|
+
import { updatable, NurvusElement } from './update';
|
|
7
|
+
import '../components/Anchor/Anchor.scss';
|
|
8
|
+
|
|
9
|
+
export type { AnchorItem };
|
|
10
|
+
|
|
11
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
12
|
+
|
|
13
|
+
export interface AnchorProps extends ElementProps {
|
|
14
|
+
/** The sections, in page order: each an "#id" href, a label and a level (1, 2…). */
|
|
15
|
+
items: AnchorItem[];
|
|
16
|
+
/** The heading above the list. */
|
|
17
|
+
title?: string;
|
|
18
|
+
/** Prefix for the ids inside (the title is "<id>-title"). Defaults to "anchor". */
|
|
19
|
+
id?: string;
|
|
20
|
+
/** How far below the top a section's heading must be scrolled to count as "in view", in pixels. */
|
|
21
|
+
offset?: number;
|
|
22
|
+
/** The section in view, set by you (its href). Left out, the Anchor follows the scrolling itself. */
|
|
23
|
+
current?: string;
|
|
24
|
+
/** Scroll-spy inside this element instead of the page: an element or a CSS selector. */
|
|
25
|
+
scrollRoot?: Element | string;
|
|
26
|
+
/** Writes the section's #id in the address bar when one is picked. Turn off for hash routers. */
|
|
27
|
+
updateHash?: boolean;
|
|
28
|
+
/** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
|
|
29
|
+
onNavigate?: (href: string, event: MouseEvent) => void;
|
|
30
|
+
/** A soft wash behind the row being read, fading to the right. */
|
|
31
|
+
fade?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* "On this page", the same markup as the React Anchor: the rail bends with the
|
|
36
|
+
* nesting and its highlight slides to the section in view, in place (the
|
|
37
|
+
* element stays the same while you scroll).
|
|
38
|
+
*/
|
|
39
|
+
export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, AnchorProps> {
|
|
40
|
+
return updatable(({
|
|
41
|
+
items, title = 'On this page', id = 'anchor', offset = 96, current, scrollRoot, updateHash = true, onNavigate, fade = false, className, ...rest
|
|
42
|
+
}: AnchorProps) => {
|
|
43
|
+
const controlled = current !== undefined;
|
|
44
|
+
const rail = anchorRail(items.map((item) => item.level || 1));
|
|
45
|
+
const fixed = controlled ? Math.max(0, items.findIndex((item) => item.href === current)) : 0;
|
|
46
|
+
let range: [number, number] = [fixed, fixed];
|
|
47
|
+
const links: HTMLElement[] = [];
|
|
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);
|
|
54
|
+
thumb.style.setProperty('stroke-dasharray', d['stroke-dasharray']);
|
|
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)); }
|
|
57
|
+
links.forEach((link, k) => {
|
|
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');
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const el = spawn<HTMLElement>({
|
|
65
|
+
tag: 'nav',
|
|
66
|
+
className: classNames(className, 'anchor', CONSTANT.CLASS_PREFIX),
|
|
67
|
+
'aria-labelledby': `${id}-title`,
|
|
68
|
+
'data-fade': fade || undefined,
|
|
69
|
+
...rest,
|
|
70
|
+
children: [
|
|
71
|
+
spawn({ tag: 'p', className: 'anchor-title', children: [Icon({ name: 'align-left', size: '1.125em' }), spawn({ tag: 'span', id: `${id}-title`, children: title })] }),
|
|
72
|
+
spawn({
|
|
73
|
+
className: 'anchor-body',
|
|
74
|
+
children: [
|
|
75
|
+
spawn<any>({
|
|
76
|
+
tag: 'svg',
|
|
77
|
+
namespace: SVG,
|
|
78
|
+
className: 'anchor-rail',
|
|
79
|
+
viewBox: `0 0 ${rail.width} ${rail.height}`,
|
|
80
|
+
style: { width: `${rail.width / 16}rem`, height: `${rail.height / 16}rem` },
|
|
81
|
+
'aria-hidden': true,
|
|
82
|
+
focusable: 'false',
|
|
83
|
+
children: [spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-track', d: rail.d }), thumb, ...caps],
|
|
84
|
+
}),
|
|
85
|
+
spawn({
|
|
86
|
+
tag: 'ul',
|
|
87
|
+
className: 'anchor-list',
|
|
88
|
+
children: items.map((item, i) => {
|
|
89
|
+
const link = spawn({
|
|
90
|
+
tag: 'a',
|
|
91
|
+
className: 'anchor-link',
|
|
92
|
+
href: item.href,
|
|
93
|
+
title: item.label,
|
|
94
|
+
'data-active': i === range[0],
|
|
95
|
+
'data-inview': i >= range[0] && i <= range[1],
|
|
96
|
+
'aria-current': i === range[0] ? 'location' : undefined,
|
|
97
|
+
events: {
|
|
98
|
+
click: (event: MouseEvent) => {
|
|
99
|
+
if (onNavigate) onNavigate(item.href, event);
|
|
100
|
+
if (event.defaultPrevented) return;
|
|
101
|
+
if (anchorGo(item.href, updateHash)) { event.preventDefault(); if (!controlled) mark([i, i]); }
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
children: item.label,
|
|
105
|
+
});
|
|
106
|
+
links.push(link);
|
|
107
|
+
return spawn({ tag: 'li', style: { '--anchor-level': String((item.level || 1) - 1) }, children: link });
|
|
108
|
+
}),
|
|
109
|
+
}),
|
|
110
|
+
],
|
|
111
|
+
}),
|
|
112
|
+
],
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Follow the scrolling once it is on the page (and stop once it is gone).
|
|
116
|
+
if (!controlled && typeof window !== 'undefined') {
|
|
117
|
+
let frame = 0;
|
|
118
|
+
let seen = false;
|
|
119
|
+
const root = () => (typeof scrollRoot === 'string' ? document.querySelector(scrollRoot) : scrollRoot || null);
|
|
120
|
+
const update = () => {
|
|
121
|
+
cancelAnimationFrame(frame);
|
|
122
|
+
frame = requestAnimationFrame(() => {
|
|
123
|
+
if (!el.isConnected) { if (seen) { window.removeEventListener('scroll', update, true); window.removeEventListener('resize', update); } return; }
|
|
124
|
+
seen = true;
|
|
125
|
+
const next = anchorRange(items, offset, root());
|
|
126
|
+
if (next[0] !== range[0] || next[1] !== range[1]) mark(next);
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
window.addEventListener('scroll', update, { passive: true, capture: true });
|
|
130
|
+
window.addEventListener('resize', update);
|
|
131
|
+
update();
|
|
132
|
+
}
|
|
133
|
+
return el;
|
|
134
|
+
}, props);
|
|
135
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -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
|
@@ -48,6 +48,8 @@ interface MenuBaseProps extends ElementProps {
|
|
|
48
48
|
label?: string;
|
|
49
49
|
/** Prefix for the ids inside (each toggle names its list). Defaults to "menu". */
|
|
50
50
|
id?: string;
|
|
51
|
+
/** Navigation: the chevron before the title ('start', the default: right when closed, down when open, a guide line down the items) or after it ('end'). */
|
|
52
|
+
chevron?: 'start' | 'end';
|
|
51
53
|
}
|
|
52
54
|
|
|
53
55
|
export interface MenuProps extends MenuBaseProps {
|
|
@@ -81,12 +83,12 @@ export type MenuTreeProps = TreeProps & { kind: 'tree' };
|
|
|
81
83
|
export default function Menu(props: MenuProps = { sections: [] }): NurvusElement<HTMLElement, MenuProps> {
|
|
82
84
|
const kind = menuKind(props || {});
|
|
83
85
|
if (kind === 'tree') {
|
|
84
|
-
const { kind: _kind, sections: _s, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, ...tree } = props;
|
|
86
|
+
const { kind: _kind, sections: _s, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, chevron: _ch, ...tree } = props;
|
|
85
87
|
return Tree(tree as TreeProps) as any;
|
|
86
88
|
}
|
|
87
89
|
if (kind === 'actions') return MenuActions(props as MenuListProps) as any;
|
|
88
90
|
return updatable<HTMLElement, MenuListProps>(({
|
|
89
|
-
kind: _kind, sections = [], current, defaultOpen = true, onToggle, onNavigate, label = 'Pages', id = 'menu', className, ...rest
|
|
91
|
+
kind: _kind, sections = [], current, defaultOpen = true, onToggle, onNavigate, chevron = 'start', label = 'Pages', id = 'menu', className, ...rest
|
|
90
92
|
}: MenuListProps, keep) => {
|
|
91
93
|
const open: Record<string, boolean> = Object.fromEntries(sections.map((s) => [sectionId(s), startsOpen(defaultOpen, sectionId(s))]));
|
|
92
94
|
// The current page's section always starts open, as in React.
|
|
@@ -112,11 +114,10 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
112
114
|
const toggleProps = (extra: Record<string, unknown>) => ({
|
|
113
115
|
tag: 'button', type: 'button', 'aria-expanded': open[sid], 'aria-controls': listId, events: { click: toggle }, ...extra,
|
|
114
116
|
});
|
|
117
|
+
const link = () => spawn({ tag: 'a', className: 'menu-title', href: section.href, 'data-active': current === section.href, events: navigate(section.href as string), children: section.title });
|
|
118
|
+
const chev = () => spawn(toggleProps({ className: 'menu-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${String(section.title)}` }));
|
|
115
119
|
const heading = section.href
|
|
116
|
-
? [
|
|
117
|
-
spawn({ tag: 'a', className: 'menu-title', href: section.href, 'data-active': current === section.href, events: navigate(section.href), children: section.title }),
|
|
118
|
-
spawn(toggleProps({ className: 'menu-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${String(section.title)}` })),
|
|
119
|
-
]
|
|
120
|
+
? (chevron === 'start' ? [chev(), link()] : [link(), chev()])
|
|
120
121
|
: [spawn(toggleProps({ className: 'menu-title menu-toggle', children: section.title }))];
|
|
121
122
|
heading.forEach((h) => { if (h.tagName === 'BUTTON') toggles.push(h); });
|
|
122
123
|
|
|
@@ -166,6 +167,7 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
166
167
|
className: classNames(className, 'menu', CONSTANT.CLASS_PREFIX),
|
|
167
168
|
'aria-label': label,
|
|
168
169
|
'data-kind': 'nav',
|
|
170
|
+
'data-chevron': chevron,
|
|
169
171
|
...others,
|
|
170
172
|
events: {
|
|
171
173
|
...events,
|
|
@@ -182,13 +184,14 @@ export default function Menu(props: MenuProps = { sections: [] }): NurvusElement
|
|
|
182
184
|
/** A menu of actions (role="menu"): one Tab stop that follows the focus, arrow keys, type to find. */
|
|
183
185
|
function MenuActions(props: MenuListProps): NurvusElement<HTMLElement, MenuListProps> {
|
|
184
186
|
return updatable(({
|
|
185
|
-
kind: _kind, sections = [], label = 'Actions', id = 'menu', className, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, ...rest
|
|
187
|
+
kind: _kind, sections = [], label = 'Actions', id = 'menu', className, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, chevron: _ch, ...rest
|
|
186
188
|
}: MenuListProps) => {
|
|
187
189
|
let first = true;
|
|
188
190
|
const { events, ...others } = rest;
|
|
189
191
|
return spawn<HTMLElement>({
|
|
190
192
|
className: classNames(className, 'menu', CONSTANT.CLASS_PREFIX),
|
|
191
193
|
role: 'menu',
|
|
194
|
+
id,
|
|
192
195
|
'aria-label': label,
|
|
193
196
|
'data-kind': 'actions',
|
|
194
197
|
...others,
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { keyboardFocus } from '../tooltip';
|
|
4
|
+
import Kbd from './Kbd';
|
|
5
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
6
|
+
import { updatable, NurvusElement } from './update';
|
|
7
|
+
import '../components/Tooltip/Tooltip.scss';
|
|
8
|
+
|
|
9
|
+
export interface TooltipProps extends ElementProps {
|
|
10
|
+
/** What the tooltip says. */
|
|
11
|
+
content: Child;
|
|
12
|
+
/** A keyboard shortcut to show after it, as key caps: ['⌘', 'B']. */
|
|
13
|
+
shortcut?: string[];
|
|
14
|
+
/** The tooltip's id: the control is described by it (aria-describedby). */
|
|
15
|
+
id: string;
|
|
16
|
+
/** Below the control (default) or above it. */
|
|
17
|
+
placement?: 'bottom' | 'top';
|
|
18
|
+
/** Which edge it lines up with: centered (default), or the control's start or end edge. */
|
|
19
|
+
align?: 'center' | 'start' | 'end';
|
|
20
|
+
/** How long the pointer must rest on the control before it shows, in ms. */
|
|
21
|
+
delay?: number;
|
|
22
|
+
/** The one control it describes (an element: it gets aria-describedby). */
|
|
23
|
+
children: HTMLElement;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A short label for a control, the same markup and behavior as the React Tooltip; it opens and closes in place. */
|
|
27
|
+
export default function Tooltip(props: TooltipProps): NurvusElement<HTMLSpanElement, TooltipProps> {
|
|
28
|
+
return updatable(({ content, shortcut, id, placement = 'bottom', align = 'center', delay = 300, children, className }: TooltipProps) => {
|
|
29
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
30
|
+
const esc = (event: KeyboardEvent) => { if (event.key === 'Escape') set(false); };
|
|
31
|
+
let el: HTMLSpanElement;
|
|
32
|
+
function set(open: boolean) {
|
|
33
|
+
clearTimeout(timer as any);
|
|
34
|
+
el.setAttribute('data-open', String(open));
|
|
35
|
+
if (open) document.addEventListener('keydown', esc); else document.removeEventListener('keydown', esc);
|
|
36
|
+
}
|
|
37
|
+
children.setAttribute('aria-describedby', id);
|
|
38
|
+
el = spawn<HTMLSpanElement>({
|
|
39
|
+
tag: 'span',
|
|
40
|
+
className: classNames(className, 'tooltip', CONSTANT.CLASS_PREFIX),
|
|
41
|
+
'data-open': false,
|
|
42
|
+
'data-placement': placement,
|
|
43
|
+
'data-align': align,
|
|
44
|
+
events: {
|
|
45
|
+
mouseenter: () => { clearTimeout(timer as any); timer = setTimeout(() => set(true), delay); },
|
|
46
|
+
mouseleave: () => set(false),
|
|
47
|
+
focusin: (event: FocusEvent) => { if (keyboardFocus(event.target)) set(true); },
|
|
48
|
+
focusout: () => set(false),
|
|
49
|
+
},
|
|
50
|
+
children: [
|
|
51
|
+
children,
|
|
52
|
+
spawn({
|
|
53
|
+
tag: 'span',
|
|
54
|
+
className: 'tooltip-bubble',
|
|
55
|
+
role: 'tooltip',
|
|
56
|
+
id,
|
|
57
|
+
children: [spawn({ tag: 'span', className: 'tooltip-text', children: content }), shortcut && shortcut.length ? Kbd({ keys: shortcut }) : null],
|
|
58
|
+
}),
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
return el;
|
|
62
|
+
}, props);
|
|
63
|
+
}
|
package/src/respawn/index.ts
CHANGED
|
@@ -53,6 +53,17 @@ export { BREAKPOINTS, isAtLeast } from '../breakpoints';
|
|
|
53
53
|
export type { Breakpoint, Responsive } from '../breakpoints';
|
|
54
54
|
export { default as Loader } from './Loader';
|
|
55
55
|
export type { LoaderProps } from './Loader';
|
|
56
|
+
export { default as Anchor } from './Anchor';
|
|
57
|
+
export type { AnchorProps, AnchorItem } from './Anchor';
|
|
58
|
+
export { default as Dropdown } from './Dropdown';
|
|
59
|
+
export type { DropdownProps } from './Dropdown';
|
|
60
|
+
export { default as Kbd } from './Kbd';
|
|
61
|
+
export type { KbdProps } from './Kbd';
|
|
62
|
+
export { default as Tooltip } from './Tooltip';
|
|
63
|
+
export type { TooltipProps } from './Tooltip';
|
|
64
|
+
export { default as NavToggle } from './NavToggle';
|
|
65
|
+
export type { NavToggleProps, NavToggleMorph } from './NavToggle';
|
|
66
|
+
export { modKey } from '../tooltip';
|
|
56
67
|
export { default as Menu } from './Menu';
|
|
57
68
|
export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './Menu';
|
|
58
69
|
export { default as Select } from './Select';
|
package/src/tooltip.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Tooltip rules shared by the React and the Respawn Tooltip (no React here).
|
|
2
|
+
|
|
3
|
+
/** True when focus on `el` came from the keyboard (the browser shows a focus ring). */
|
|
4
|
+
export const keyboardFocus = (el: EventTarget | null): boolean => {
|
|
5
|
+
try { return !!el && (el as Element).matches(':focus-visible'); } catch (e) { return true; }
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/** The platform's modifier for shortcuts: ⌘ on Apple devices, Ctrl elsewhere. */
|
|
9
|
+
export const modKey = (): '⌘' | 'Ctrl' => {
|
|
10
|
+
if (typeof navigator === 'undefined') return 'Ctrl';
|
|
11
|
+
const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
|
|
12
|
+
const platform = (nav.userAgentData && nav.userAgentData.platform) || navigator.platform || navigator.userAgent;
|
|
13
|
+
return /mac|iphone|ipad|ipod/i.test(platform) ? '⌘' : 'Ctrl';
|
|
14
|
+
};
|