@nurvus/ui 0.1.0-alpha.17 → 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 +7 -0
- package/dist/icons.svg +1 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +4 -1
- 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/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/respawn/Anchor.d.ts +2 -0
- package/dist/types/respawn/Kbd.d.ts +10 -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 +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/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/respawn/Anchor.ts +26 -17
- package/src/respawn/Kbd.ts +22 -0
- 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,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.
|
|
@@ -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/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
|
+
}
|
|
@@ -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
|
+
}
|