@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.
Files changed (55) hide show
  1. package/README.md +13 -0
  2. package/dist/icons.svg +2 -0
  3. package/dist/index.js +1 -1
  4. package/dist/nurvus.css +6 -1
  5. package/dist/respawn.js +1 -1
  6. package/dist/types/anchor.d.ts +45 -0
  7. package/dist/types/components/Anchor/Anchor.d.ts +33 -0
  8. package/dist/types/components/Dropdown/Dropdown.d.ts +31 -0
  9. package/dist/types/components/Kbd/Kbd.d.ts +16 -0
  10. package/dist/types/components/Menu/Menu.d.ts +8 -1
  11. package/dist/types/components/NavToggle/NavToggle.d.ts +27 -0
  12. package/dist/types/components/Tooltip/Tooltip.d.ts +27 -0
  13. package/dist/types/dropdown.d.ts +9 -0
  14. package/dist/types/icons.d.ts +2 -0
  15. package/dist/types/index.d.ts +11 -0
  16. package/dist/types/respawn/Anchor.d.ts +30 -0
  17. package/dist/types/respawn/Dropdown.d.ts +27 -0
  18. package/dist/types/respawn/Kbd.d.ts +10 -0
  19. package/dist/types/respawn/Menu.d.ts +2 -0
  20. package/dist/types/respawn/NavToggle.d.ts +22 -0
  21. package/dist/types/respawn/Tooltip.d.ts +20 -0
  22. package/dist/types/respawn/index.d.ts +11 -0
  23. package/dist/types/tooltip.d.ts +4 -0
  24. package/package.json +1 -1
  25. package/src/anchor.ts +150 -0
  26. package/src/components/Anchor/Anchor.md +59 -0
  27. package/src/components/Anchor/Anchor.scss +88 -0
  28. package/src/components/Anchor/Anchor.tsx +114 -0
  29. package/src/components/Dropdown/Dropdown.md +48 -0
  30. package/src/components/Dropdown/Dropdown.scss +67 -0
  31. package/src/components/Dropdown/Dropdown.tsx +110 -0
  32. package/src/components/Kbd/Kbd.md +14 -0
  33. package/src/components/Kbd/Kbd.scss +30 -0
  34. package/src/components/Kbd/Kbd.tsx +28 -0
  35. package/src/components/Menu/Menu.md +1 -1
  36. package/src/components/Menu/Menu.scss +78 -0
  37. package/src/components/Menu/Menu.tsx +15 -3
  38. package/src/components/NavToggle/NavToggle.md +51 -0
  39. package/src/components/NavToggle/NavToggle.scss +162 -0
  40. package/src/components/NavToggle/NavToggle.tsx +68 -0
  41. package/src/components/Tooltip/Tooltip.md +19 -0
  42. package/src/components/Tooltip/Tooltip.scss +75 -0
  43. package/src/components/Tooltip/Tooltip.tsx +69 -0
  44. package/src/css.ts +5 -0
  45. package/src/dropdown.ts +24 -0
  46. package/src/icons.ts +3 -0
  47. package/src/index.ts +11 -0
  48. package/src/respawn/Anchor.ts +135 -0
  49. package/src/respawn/Dropdown.ts +90 -0
  50. package/src/respawn/Kbd.ts +22 -0
  51. package/src/respawn/Menu.ts +10 -7
  52. package/src/respawn/NavToggle.ts +78 -0
  53. package/src/respawn/Tooltip.ts +63 -0
  54. package/src/respawn/index.ts +11 -0
  55. package/src/tooltip.ts +14 -0
@@ -50,6 +50,13 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
50
50
  onToggle?: (id: string, open: boolean) => void;
51
51
  /** Navigation: called when any link is clicked, with its href (e.g. to close a drawer). */
52
52
  onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
53
+ /**
54
+ * Navigation: where each section's chevron sits. 'start' (the default): before
55
+ * the title, pointing right when closed and down when open, with a guide line
56
+ * down the open section's items (as Material UI draws it). 'end': after the
57
+ * title, down when closed and up when open.
58
+ */
59
+ chevron?: 'start' | 'end';
53
60
  /** Tree: the rows, each with optional child `items`. */
54
61
  items?: TreeItem[];
55
62
  /** Tree: the picked row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
@@ -62,7 +69,7 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
62
69
  maxIndentDepth?: number;
63
70
  /** What screen readers call the menu ("Pages", "Actions", "Layers"). */
64
71
  label?: string;
65
- /** Prefix for the ids inside (each toggle names its list). Defaults to "menu". */
72
+ /** Prefix for the ids inside (each toggle names its list). Defaults to "menu". A menu of actions also carries it (so a button can point at it with aria-controls). */
66
73
  id?: string;
67
74
  /** Custom class name. */
68
75
  className?: string;
@@ -85,7 +92,7 @@ export type MenuTreeProps = TreeProps & { kind: 'tree' };
85
92
  export default function Menu(props: MenuProps) {
86
93
  const kind = menuKind(props);
87
94
  if (kind === 'tree') {
88
- const { kind: _kind, sections: _s, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, ...tree } = props;
95
+ const { kind: _kind, sections: _s, current: _c, defaultOpen: _d, onToggle: _t, onNavigate: _n, chevron: _ch, ...tree } = props;
89
96
  return <Tree { ...(tree as TreeProps) } />;
90
97
  }
91
98
  const { label = kind === 'actions' ? 'Actions' : 'Pages', items: _i, activePath: _a, onClick: _o, itemRenderer: _r, maxIndentDepth: _m, ...rest } = props;
@@ -99,6 +106,7 @@ function MenuNav({
99
106
  defaultOpen = true,
100
107
  onToggle,
101
108
  onNavigate,
109
+ chevron = 'start',
102
110
  label,
103
111
  id = 'menu',
104
112
  className,
@@ -128,6 +136,7 @@ function MenuNav({
128
136
  <nav className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) }
129
137
  aria-label={ label }
130
138
  data-kind="nav"
139
+ data-chevron={ chevron }
131
140
  { ...restProps }
132
141
  onKeyDown={ (e) => { menuKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
133
142
  >
@@ -146,10 +155,11 @@ function MenuNav({
146
155
  <div className="menu-heading">
147
156
  { section.href ? (
148
157
  <>
158
+ { chevron === 'start' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } /> }
149
159
  <a className="menu-title" href={ section.href } data-active={ current === section.href } onClick={ navigate(section.href) }>
150
160
  { section.title }
151
161
  </a>
152
- <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } />
162
+ { chevron === 'end' && <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } /> }
153
163
  </>
154
164
  ) : (
155
165
  <button className="menu-title menu-toggle" { ...toggleProps }>{ section.title }</button>
@@ -197,6 +207,7 @@ function MenuActions({
197
207
  defaultOpen: _defaultOpen,
198
208
  onToggle: _onToggle,
199
209
  onNavigate: _onNavigate,
210
+ chevron: _chevron,
200
211
  ...restProps
201
212
  }: MenuListProps) {
202
213
  let first = true;
@@ -204,6 +215,7 @@ function MenuActions({
204
215
  <div
205
216
  className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) }
206
217
  role="menu"
218
+ id={ id }
207
219
  aria-label={ label }
208
220
  data-kind="actions"
209
221
  { ...restProps }
@@ -0,0 +1,51 @@
1
+ Which look? The **hamburger** for navigation that covers the page (a phone's drawer, a menu), the **sidebar icon** for a sidebar that sits beside the page and folds away (as VS Code, shadcn/ui and Keyframery do). The docs header uses the hamburger with Slide and asymmetric lines; the sidebar icon is kept here for a docked sidebar. Click each one.
2
+
3
+ ```jsx
4
+ import { NavToggle, Text } from '@nurvus/ui';
5
+
6
+ const MORPHS = [
7
+ ['slide', 'Slide: a cross, the middle bar sliding out to the left (the docs header)'],
8
+ ['cross', 'Cross: the middle bar fades'],
9
+ ['squeeze', 'Squeeze: in to the middle, then a cross'],
10
+ ['elastic', 'Elastic: a cross on a spring'],
11
+ ['spin', 'Spin: a cross, turning half a circle'],
12
+ ['arrow', 'Arrow: points at the sidebar'],
13
+ ['chevron', 'Chevron: a ‹, slide it away'],
14
+ ['collapse', 'Collapse: three bars become one'],
15
+ ];
16
+
17
+ const One = ({ morph, about }) => {
18
+ const [open, setOpen] = React.useState(false);
19
+ return (
20
+ <div style={ { display: 'flex', alignItems: 'center', gap: 12 } }>
21
+ <NavToggle open={ open } morph={ morph } lines="asymmetric" label={ `navigation (${morph})` } onClick={ () => setOpen(!open) } />
22
+ <Text variant="caption" tone="muted">{ about }</Text>
23
+ </div>
24
+ );
25
+ };
26
+
27
+ <div style={ { display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(16rem, 1fr))', gap: 12 } }>
28
+ { MORPHS.map(([morph, about]) => <One key={ morph } morph={ morph } about={ about } />) }
29
+ </div>
30
+ ```
31
+
32
+ Lines: even (three of one length) or asymmetric (long, short, middle), which line up when pointed at and while open. And the sidebar icon: its pane fills while the sidebar shows; closed, it shrinks away and the divider slides left.
33
+ ```jsx
34
+ import { NavToggle, Layout, Text } from '@nurvus/ui';
35
+
36
+ const Pair = () => {
37
+ const [open, setOpen] = React.useState(true);
38
+ const flip = () => setOpen(!open);
39
+ return (
40
+ <Layout display="flex" alignItems="center" itemSpacing={ 24 }>
41
+ <NavToggle open={ open } lines="even" onClick={ flip } label="menu (even)" />
42
+ <NavToggle open={ open } lines="asymmetric" onClick={ flip } label="menu (asymmetric)" />
43
+ <NavToggle open={ open } variant="sidebar" onClick={ flip } label="sidebar" />
44
+ <Text variant="caption" tone="muted">{ open ? 'Open' : 'Closed' }: click any</Text>
45
+ </Layout>
46
+ );
47
+ };
48
+ <Pair />
49
+ ```
50
+
51
+ Give it `controls` (the navigation's id) so screen readers can follow it there, and when your app has a shortcut for it, say so with `aria-keyshortcuts` and a [Tooltip](#/Components/Tooltip) with the keys. The docs header does: <kbd>⌘</kbd> <kbd>B</kbd>.
@@ -0,0 +1,162 @@
1
+ // Styles for NavToggle (React and Respawn: same markup). Every shape is a
2
+ // transition of the same parts, so a change is one smooth motion.
3
+ @import "../../styles/variables.scss";
4
+
5
+ $nav-ease: cubic-bezier(0.2, 0.8, 0.2, 1); // fast out, gentle settle (250ms)
6
+ $nav-speed: 0.25s;
7
+ $bar-gap: 0.375rem; // 6px at the default text size: the bars sit 0, 6 and 12px down
8
+
9
+ .#{$cls-prefix}.nav-toggle {
10
+ display: inline-flex;
11
+ flex: none;
12
+ align-items: center;
13
+ justify-content: center;
14
+ width: 2rem;
15
+ height: 2rem;
16
+ padding: 0;
17
+ border: 0;
18
+ border-radius: 0.625rem;
19
+ background: transparent;
20
+ color: var(--nv-text);
21
+ cursor: pointer;
22
+ transition: background-color 0.15s ease, color 0.15s ease;
23
+
24
+ &:hover { background: color-mix(in srgb, var(--nv-text) 8%, transparent); color: var(--nv-accent); }
25
+ &:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: 2px; }
26
+
27
+ // The hamburger: three 2px bars in a 1.125rem x 0.875rem box (an icon's 20px live area).
28
+ .nav-toggle-bars {
29
+ position: relative;
30
+ width: 1.125rem;
31
+ height: 0.875rem;
32
+ transition: transform 0.35s $nav-ease;
33
+
34
+ span {
35
+ position: absolute;
36
+ left: 0;
37
+ width: 100%;
38
+ height: 2px;
39
+ border-radius: 1px;
40
+ background: currentColor;
41
+ transform-origin: left center;
42
+ transition: transform $nav-speed $nav-ease, opacity 0.2s ease, width $nav-speed $nav-ease;
43
+ }
44
+
45
+ span:nth-child(1) { top: 0; }
46
+ span:nth-child(2) { top: $bar-gap; }
47
+ span:nth-child(3) { top: calc(#{$bar-gap} * 2); }
48
+ }
49
+
50
+ // Asymmetric lines: long, short, middle, lined up on the left; they even out
51
+ // when pointed at, and while open (a cross needs equal bars).
52
+ &[data-lines="asymmetric"] .nav-toggle-bars {
53
+ span:nth-child(2) { width: 62%; }
54
+ span:nth-child(3) { width: 82%; }
55
+ }
56
+
57
+ &[data-lines="asymmetric"]:hover .nav-toggle-bars span,
58
+ &[data-lines="asymmetric"][data-open="true"] .nav-toggle-bars span { width: 100%; }
59
+
60
+ // --- Open: the morphs ---
61
+
62
+ // Slide (Jonathan Suh's "Slider"): a cross while the middle bar slides out left and fades.
63
+ &[data-open="true"][data-morph="slide"] .nav-toggle-bars {
64
+ span { transform-origin: center; }
65
+ span:nth-child(1) { transform: translateY($bar-gap) rotate(45deg); }
66
+ span:nth-child(2) { opacity: 0; transform: translateX(-0.625rem); transition-duration: 0.15s; }
67
+ span:nth-child(3) { transform: translateY(-$bar-gap) rotate(-45deg); }
68
+ }
69
+
70
+ // Cross: the outer bars meet in the middle; the middle one fades. Spin turns half a circle too.
71
+ &[data-open="true"][data-morph="cross"],
72
+ &[data-open="true"][data-morph="spin"] {
73
+ .nav-toggle-bars span { transform-origin: center; }
74
+ .nav-toggle-bars span:nth-child(1) { transform: translateY($bar-gap) rotate(45deg); }
75
+ .nav-toggle-bars span:nth-child(2) { opacity: 0; transform: scaleX(0.2); }
76
+ .nav-toggle-bars span:nth-child(3) { transform: translateY(-$bar-gap) rotate(-45deg); }
77
+ }
78
+
79
+ &[data-open="true"][data-morph="spin"] .nav-toggle-bars { transform: rotate(180deg); }
80
+
81
+ // Squeeze: the outer bars close in on the middle first, then turn (and back the other way).
82
+ &[data-morph="squeeze"] .nav-toggle-bars span {
83
+ transform-origin: center;
84
+ transition: translate 0.12s ease 0.12s, rotate 0.12s ease, opacity 0.1s ease 0.1s, width $nav-speed $nav-ease;
85
+ }
86
+
87
+ &[data-open="true"][data-morph="squeeze"] .nav-toggle-bars {
88
+ span { transition: translate 0.12s ease, rotate 0.12s ease 0.12s, opacity 0.1s ease, width $nav-speed $nav-ease; }
89
+ span:nth-child(1) { translate: 0 $bar-gap; rotate: 45deg; }
90
+ span:nth-child(2) { opacity: 0; }
91
+ span:nth-child(3) { translate: 0 (-$bar-gap); rotate: -45deg; }
92
+ }
93
+
94
+ // Elastic: the cross on a spring that swings past and settles.
95
+ &[data-morph="elastic"] .nav-toggle-bars span { transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55), opacity 0.2s ease, width $nav-speed $nav-ease; }
96
+
97
+ &[data-open="true"][data-morph="elastic"] .nav-toggle-bars {
98
+ span { transform-origin: center; }
99
+ span:nth-child(1) { transform: translateY($bar-gap) rotate(135deg); }
100
+ span:nth-child(2) { opacity: 0; transform: scaleX(0); }
101
+ span:nth-child(3) { transform: translateY(-$bar-gap) rotate(-135deg); }
102
+ }
103
+
104
+ // Arrow (←): the outer bars swing in from the left end to make the head.
105
+ &[data-open="true"][data-morph="arrow"] .nav-toggle-bars {
106
+ span:nth-child(1) { width: 55%; transform: translate(0, $bar-gap) rotate(-45deg); }
107
+ span:nth-child(3) { width: 55%; transform: translate(0, -$bar-gap) rotate(45deg); }
108
+ }
109
+
110
+ // Chevron (‹): the outer bars fold to a point on the left; the middle fades.
111
+ &[data-open="true"][data-morph="chevron"] .nav-toggle-bars {
112
+ span:nth-child(1) { width: 50%; transform: translate(0.25rem, $bar-gap) rotate(-45deg); }
113
+ span:nth-child(2) { opacity: 0; transform: scaleX(0.2); }
114
+ span:nth-child(3) { width: 50%; transform: translate(0.25rem, -$bar-gap) rotate(45deg); }
115
+ }
116
+
117
+ // Collapse (—): the outer bars slide into the middle one.
118
+ &[data-open="true"][data-morph="collapse"] .nav-toggle-bars {
119
+ span:nth-child(1) { transform: translateY($bar-gap); }
120
+ span:nth-child(3) { transform: translateY(-$bar-gap); }
121
+ }
122
+
123
+ // The sidebar icon (after a motion study for NURVUS): the pane fills while
124
+ // the sidebar shows; closed, it shrinks away and the divider slides left.
125
+ .nav-toggle-panel {
126
+ width: 1.25rem;
127
+ height: 1.25rem;
128
+ overflow: visible;
129
+ }
130
+
131
+ .nav-toggle-outline,
132
+ .nav-toggle-divider {
133
+ fill: none;
134
+ stroke: currentColor;
135
+ stroke-width: 2;
136
+ stroke-linecap: round;
137
+ stroke-linejoin: round;
138
+ }
139
+
140
+ .nav-toggle-fill {
141
+ fill: currentColor;
142
+ opacity: 0.3;
143
+ transform-box: fill-box;
144
+ transform-origin: left center;
145
+ }
146
+
147
+ .nav-toggle-fill,
148
+ .nav-toggle-divider { transition: transform $nav-speed $nav-ease, opacity $nav-speed $nav-ease; }
149
+
150
+ &[data-variant="sidebar"][data-open="false"] {
151
+ .nav-toggle-divider { transform: translateX(-4px); opacity: 0.55; }
152
+ .nav-toggle-fill { transform: scaleX(0.12); opacity: 0; }
153
+ }
154
+ }
155
+
156
+ @media (prefers-reduced-motion: reduce) {
157
+ .#{$cls-prefix}.nav-toggle,
158
+ .#{$cls-prefix}.nav-toggle .nav-toggle-bars,
159
+ .#{$cls-prefix}.nav-toggle .nav-toggle-bars span,
160
+ .#{$cls-prefix}.nav-toggle .nav-toggle-fill,
161
+ .#{$cls-prefix}.nav-toggle .nav-toggle-divider { transition: none !important; }
162
+ }
@@ -0,0 +1,68 @@
1
+ import React from 'react';
2
+ import classNames from 'classnames';
3
+ import CONSTANT from '../../constants';
4
+ import './NavToggle.scss';
5
+
6
+ export type NavToggleMorph = 'slide' | 'cross' | 'squeeze' | 'elastic' | 'spin' | 'arrow' | 'chevron' | 'collapse';
7
+
8
+ export interface NavToggleProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type'> {
9
+ /** Whether the navigation it controls is showing. */
10
+ open: boolean;
11
+ /** 'hamburger' (three bars, for a menu that covers the page) or 'sidebar' (a window with a side pane, for a sidebar beside the page). */
12
+ variant?: 'hamburger' | 'sidebar';
13
+ /** Hamburger: how the bars change while open. 'slide' (default): a cross, the middle bar sliding out to the left. */
14
+ morph?: NavToggleMorph;
15
+ /** Hamburger: three bars of one length, or 'asymmetric' (long, short, middle) that line up when pointed at or open. */
16
+ lines?: 'even' | 'asymmetric';
17
+ /** What it shows and hides, for its name: "Show navigation" / "Hide navigation". */
18
+ label?: string;
19
+ /** The id of the navigation it controls (aria-controls). */
20
+ controls?: string;
21
+ /** Custom class name. */
22
+ className?: string;
23
+ }
24
+
25
+ /**
26
+ * The button that shows and hides navigation. Two looks: the hamburger, whose
27
+ * bars move into another shape while the navigation is open (eight morphs),
28
+ * and the sidebar icon, a window whose side pane fills while the sidebar shows
29
+ * and empties as its divider slides away. The shapes move, they aren't
30
+ * swapped; instantly for reduced motion. It says what it does ("Hide
31
+ * navigation") and whether it is open (aria-expanded).
32
+ */
33
+ export default function NavToggle({
34
+ open,
35
+ variant = 'hamburger',
36
+ morph = 'slide',
37
+ lines = 'even',
38
+ label = 'navigation',
39
+ controls,
40
+ className,
41
+ ...restProps
42
+ }: NavToggleProps) {
43
+ const sidebar = variant === 'sidebar';
44
+ return (
45
+ <button
46
+ type="button"
47
+ className={ classNames(className, 'nav-toggle', CONSTANT.CLASS_PREFIX) }
48
+ data-variant={ variant }
49
+ data-morph={ sidebar ? undefined : morph }
50
+ data-lines={ sidebar ? undefined : lines }
51
+ data-open={ open }
52
+ aria-expanded={ open }
53
+ aria-controls={ controls }
54
+ aria-label={ `${open ? 'Hide' : 'Show'} ${label}` }
55
+ { ...restProps }
56
+ >
57
+ { sidebar ? (
58
+ <svg className="nav-toggle-panel" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
59
+ <rect className="nav-toggle-outline" x="2.5" y="4" width="19" height="16" rx="2.5" />
60
+ <rect className="nav-toggle-fill" x="4" y="5.5" width="5" height="13" rx="0.5" />
61
+ <path className="nav-toggle-divider" d="M10 4V20" />
62
+ </svg>
63
+ ) : (
64
+ <span className="nav-toggle-bars" aria-hidden="true"><span /><span /><span /></span>
65
+ ) }
66
+ </button>
67
+ );
68
+ }
@@ -0,0 +1,19 @@
1
+ A short label for a control, most useful on icon-only buttons; with `shortcut`, its keyboard shortcut as key caps. Point at a button and rest, or Tab to it: the tooltip shows. Press Esc to hide it; move the pointer onto it and it stays.
2
+
3
+ ```jsx
4
+ import { Button, Icon, Layout, Tooltip } from '@nurvus/ui';
5
+
6
+ <Layout display="flex" itemSpacing={ 16 } style={ { padding: '8px 0 48px' } }>
7
+ <Tooltip id="tip-save" content="Save" shortcut={ ['⌘', 'S'] }>
8
+ <Button size="small" type="secondary" aria-label="Save"><Icon name="download" /></Button>
9
+ </Tooltip>
10
+ <Tooltip id="tip-share" content="Share a link">
11
+ <Button size="small" type="secondary" aria-label="Share"><Icon name="share" /></Button>
12
+ </Tooltip>
13
+ <Tooltip id="tip-settings" content="Settings" shortcut={ ['⌘', ','] } placement="bottom" align="start">
14
+ <Button size="small" type="secondary" aria-label="Settings"><Icon name="settings" /></Button>
15
+ </Tooltip>
16
+ </Layout>
17
+ ```
18
+
19
+ The tooltip describes the control (`aria-describedby`): screen readers say the control's own name first ("Save, button"), then the tooltip. So a tooltip never replaces a name: an icon-only button still needs its `aria-label`. Keep tooltips to a few words, and never put links or buttons in them: nothing inside a tooltip can be reached by keyboard.
@@ -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
@@ -13,6 +13,11 @@ import './components/Icon/Icon.scss';
13
13
  import './components/Loader/Loader.scss';
14
14
  import './components/Tree/Tree.scss';
15
15
  import './components/Menu/Menu.scss';
16
+ import './components/Dropdown/Dropdown.scss';
17
+ import './components/Anchor/Anchor.scss';
18
+ import './components/Kbd/Kbd.scss';
19
+ import './components/Tooltip/Tooltip.scss';
20
+ import './components/NavToggle/NavToggle.scss';
16
21
  import './components/Select/Select.scss';
17
22
  import './components/Panel/Panel.scss';
18
23
  import './components/Progress/Progress.scss';
@@ -0,0 +1,24 @@
1
+ // Dropdown rules shared by the React and the Respawn Dropdown (no React here):
2
+ // the WAI-ARIA menu button pattern's keys and focus.
3
+
4
+ /** The menu's items, in order. */
5
+ export const menuItems = (root: HTMLElement): HTMLElement[] => Array.from(root.querySelectorAll<HTMLElement>('.dropdown-popup [role="menuitem"]'));
6
+
7
+ /** Moves focus to the first or last item (and makes it the menu's Tab stop). */
8
+ export function focusItem(root: HTMLElement, which: 'first' | 'last'): void {
9
+ const items = menuItems(root);
10
+ const item = which === 'first' ? items[0] : items[items.length - 1];
11
+ if (!item) return;
12
+ items.forEach((i) => i.setAttribute('tabindex', i === item ? '0' : '-1'));
13
+ item.focus();
14
+ }
15
+
16
+ /**
17
+ * Keys on the button: ↓ (or Enter / Space, which click it) opens the menu on
18
+ * its first item, ↑ on its last. Returns which item to focus, or null.
19
+ */
20
+ export function triggerKey(event: KeyboardEvent): 'first' | 'last' | null {
21
+ if (event.key === 'ArrowDown') return 'first';
22
+ if (event.key === 'ArrowUp') return 'last';
23
+ return null;
24
+ }
package/src/icons.ts CHANGED
@@ -9,6 +9,7 @@
9
9
 
10
10
  /** One SVG element of an icon: its tag and attributes. */
11
11
  export type IconNode = [tag: 'path' | 'circle' | 'rect', attrs: Record<string, string | number>];
12
+ // A shape may fill (fill: 'currentColor'): only `contrast` does, for its filled half.
12
13
 
13
14
  const path = (d: string): IconNode => ['path', { d }];
14
15
  const circle = (cx: number, cy: number, r: number): IconNode => ['circle', { cx, cy, r }];
@@ -84,10 +85,12 @@ const DEFS = {
84
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')],
85
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)],
86
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')],
87
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')],
88
90
  // Theme
89
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')],
90
92
  moon: [path('M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5z')],
93
+ contrast: [ring, ['path', { d: 'M12 3a9 9 0 0 1 0 18z', fill: 'currentColor' }] as IconNode],
91
94
  palette: [path('M12 3a9 9 0 0 0 0 18c1.1 0 1.6-.8 1.6-1.6 0-1-.9-1.4-.9-2.4 0-.9.7-1.5 1.6-1.5H16a5 5 0 0 0 5-5C21 6.4 17 3 12 3z'), dot(7.5, 11.5), dot(9.5, 7.5), dot(14.5, 7.5)],
92
95
  sparkles: [path('M10 4l1.6 4.4L16 10l-4.4 1.6L10 16l-1.6-4.4L4 10l4.4-1.6z'), path('M18 14l.8 2.2L21 17l-2.2.8L18 20l-.8-2.2L15 17l2.2-.8z')],
93
96
  zap: [path('M13 2.5L4.5 13.5H11l-1 8 8.5-11H12z')],
package/src/index.ts CHANGED
@@ -36,6 +36,17 @@ export { default as Loader } from './components/Loader/Loader';
36
36
  export type { LoaderProps } from './components/Loader/Loader';
37
37
  export { default as Tree } from './components/Tree/Tree';
38
38
  export type { TreeProps, TreeItem } from './components/Tree/Tree';
39
+ export { default as Anchor } from './components/Anchor/Anchor';
40
+ export type { AnchorProps, AnchorItem } from './components/Anchor/Anchor';
41
+ export { default as Dropdown } from './components/Dropdown/Dropdown';
42
+ export type { DropdownProps } from './components/Dropdown/Dropdown';
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';
39
50
  export { default as Menu } from './components/Menu/Menu';
40
51
  export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './components/Menu/Menu';
41
52
  export { default as Select } from './components/Select/Select';