@nurvus/ui 0.1.0-alpha.15 → 0.1.0-alpha.17
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 +17 -1
- package/dist/icons.svg +74 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +15 -10
- package/dist/respawn.js +1 -1
- package/dist/types/anchor.d.ts +37 -0
- package/dist/types/breakpoints.d.ts +14 -0
- package/dist/types/components/Anchor/Anchor.d.ts +30 -0
- package/dist/types/components/Dropdown/Dropdown.d.ts +31 -0
- package/dist/types/components/FullPageModal/FullPageModal.d.ts +11 -2
- package/dist/types/components/Grid/Grid.d.ts +26 -0
- package/dist/types/components/Loader/Loader.d.ts +21 -0
- package/dist/types/components/Menu/Menu.d.ts +61 -19
- package/dist/types/components/Progress/Progress.d.ts +19 -5
- package/dist/types/components/Stack/Stack.d.ts +26 -0
- package/dist/types/dialog.d.ts +12 -0
- package/dist/types/dropdown.d.ts +9 -0
- package/dist/types/icons.d.ts +29 -0
- package/dist/types/index.d.ts +13 -1
- package/dist/types/loader.d.ts +28 -0
- package/dist/types/logo.d.ts +5 -0
- package/dist/types/menu.d.ts +43 -14
- package/dist/types/progress.d.ts +11 -0
- package/dist/types/respawn/Anchor.d.ts +28 -0
- package/dist/types/respawn/Dropdown.d.ts +27 -0
- package/dist/types/respawn/FullPageModal.d.ts +7 -1
- package/dist/types/respawn/Grid.d.ts +21 -0
- package/dist/types/respawn/Loader.d.ts +18 -0
- package/dist/types/respawn/Menu.d.ts +47 -15
- package/dist/types/respawn/Progress.d.ts +18 -6
- package/dist/types/respawn/Stack.d.ts +21 -0
- package/dist/types/respawn/index.d.ts +13 -1
- package/package.json +1 -3
- package/src/anchor.ts +127 -0
- package/src/breakpoints.ts +28 -0
- package/src/components/Anchor/Anchor.md +59 -0
- package/src/components/Anchor/Anchor.scss +65 -0
- package/src/components/Anchor/Anchor.tsx +105 -0
- package/src/components/Button/Button.scss +20 -20
- 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/FullPageModal/FullPageModal.md +20 -17
- package/src/components/FullPageModal/FullPageModal.scss +5 -0
- package/src/components/FullPageModal/FullPageModal.tsx +37 -3
- package/src/components/Grid/Grid.md +37 -0
- package/src/components/Grid/Grid.scss +37 -0
- package/src/components/Grid/Grid.tsx +54 -0
- package/src/components/HistoryList/HistoryList.scss +2 -2
- package/src/components/Loader/Loader.md +30 -0
- package/src/components/Loader/Loader.scss +42 -0
- package/src/components/Loader/Loader.tsx +68 -0
- package/src/components/Menu/Menu.md +67 -3
- package/src/components/Menu/Menu.scss +126 -18
- package/src/components/Menu/Menu.tsx +159 -40
- package/src/components/Progress/Progress.md +27 -26
- package/src/components/Progress/Progress.scss +41 -36
- package/src/components/Progress/Progress.tsx +50 -6
- package/src/components/Select/Select.scss +20 -19
- package/src/components/Stack/Stack.md +43 -0
- package/src/components/Stack/Stack.scss +36 -0
- package/src/components/Stack/Stack.tsx +52 -0
- package/src/components/Table/Table.scss +1 -1
- package/src/components/Tag/Tag.scss +2 -2
- package/src/components/Title/Title.scss +6 -6
- package/src/components/Tree/Tree.md +1 -1
- package/src/css.ts +5 -0
- package/src/dialog.ts +47 -0
- package/src/dropdown.ts +24 -0
- package/src/icons.ts +31 -0
- package/src/index.ts +13 -1
- package/src/loader.ts +43 -0
- package/src/logo.ts +12 -0
- package/src/menu.ts +91 -21
- package/src/progress.ts +21 -0
- package/src/respawn/Anchor.ts +126 -0
- package/src/respawn/Dropdown.ts +90 -0
- package/src/respawn/FullPageModal.ts +30 -3
- package/src/respawn/Grid.ts +41 -0
- package/src/respawn/Loader.ts +77 -0
- package/src/respawn/Menu.ts +144 -37
- package/src/respawn/Progress.ts +56 -13
- package/src/respawn/Stack.ts +37 -0
- package/src/respawn/index.ts +13 -1
- package/src/styles/breakpoints.scss +3 -0
- package/src/styles/theme.scss +55 -55
- package/src/styles/type.json +9 -9
package/src/icons.ts
CHANGED
|
@@ -9,12 +9,14 @@
|
|
|
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 }];
|
|
15
16
|
const rect = (x: number, y: number, width: number, height: number, rx = 2): IconNode => ['rect', { x, y, width, height, rx }];
|
|
16
17
|
const ring = circle(12, 12, 9);
|
|
17
18
|
const dot = (x: number, y: number) => path(`M${x} ${y}h.01`);
|
|
19
|
+
const bead = (x: number, y: number) => circle(x, y, 1); // a bigger dot: r 1 + the 2-unit line = 4 across
|
|
18
20
|
|
|
19
21
|
const DEFS = {
|
|
20
22
|
// Navigation
|
|
@@ -29,6 +31,12 @@ const DEFS = {
|
|
|
29
31
|
'arrow-up': [path('M12 19V5M6 11l6-6 6 6')],
|
|
30
32
|
'arrow-down': [path('M12 5v14M6 13l6 6 6-6')],
|
|
31
33
|
'external-link': [path('M14 4h6v6M20 4l-9 9M18 14v4a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4')],
|
|
34
|
+
home: [path('M4 11l8-7 8 7v8a2 2 0 0 1-2 2h-3v-6H9v6H6a2 2 0 0 1-2-2z')],
|
|
35
|
+
'panel-left': [rect(3, 4, 18, 16), path('M9 4v16')],
|
|
36
|
+
'more-horizontal': [bead(5, 12), bead(12, 12), bead(19, 12)],
|
|
37
|
+
'more-vertical': [bead(12, 5), bead(12, 12), bead(12, 19)],
|
|
38
|
+
maximize: [path('M14 4h6v6M10 20H4v-6M20 4l-6 6M4 20l6-6')],
|
|
39
|
+
minimize: [path('M4 14h6v6M20 10h-6V4M14 10l6-6M4 20l6-6')],
|
|
32
40
|
// Actions
|
|
33
41
|
add: [path('M12 5v14M5 12h14')],
|
|
34
42
|
remove: [path('M5 12h14')],
|
|
@@ -43,6 +51,11 @@ const DEFS = {
|
|
|
43
51
|
refresh: [path('M19 12a7 7 0 1 1-7-7'), path('M9.5 2.5L12 5l-2.5 2.5')],
|
|
44
52
|
undo: [path('M9 14L4 9l5-5M4 9h10.5a5.5 5.5 0 0 1 0 11H11')],
|
|
45
53
|
redo: [path('M15 14l5-5-5-5M20 9H9.5a5.5 5.5 0 0 0 0 11H13')],
|
|
54
|
+
settings: [path('M10.01 5.29L10.51 2.62L13.49 2.62L13.99 5.29L15.34 5.85L17.58 4.31L19.69 6.42L18.15 8.66L18.71 10.01L21.38 10.51L21.38 13.49L18.71 13.99L18.15 15.34L19.69 17.58L17.58 19.69L15.34 18.15L13.99 18.71L13.49 21.38L10.51 21.38L10.01 18.71L8.66 18.15L6.42 19.69L4.31 17.58L5.85 15.34L5.29 13.99L2.62 13.49L2.62 10.51L5.29 10.01L5.85 8.66L4.31 6.42L6.42 4.31L8.66 5.85z'), circle(12, 12, 3)],
|
|
55
|
+
share: [circle(18, 5, 2.5), circle(6, 12, 2.5), circle(18, 19, 2.5), path('M8.2 13.3l7.6 4.4M15.8 6.3l-7.6 4.4')],
|
|
56
|
+
play: [path('M7 4.5l12.5 7.5L7 19.5z')],
|
|
57
|
+
pause: [path('M8 5v14M16 5v14')],
|
|
58
|
+
'sign-out': [path('M10 20H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h4M15 16l4-4-4-4M19 12H9')],
|
|
46
59
|
// Status
|
|
47
60
|
check: [path('M5 12.5l4.5 4.5L19 7.5')],
|
|
48
61
|
info: [ring, path('M12 11v5'), dot(12, 8)],
|
|
@@ -58,10 +71,28 @@ const DEFS = {
|
|
|
58
71
|
'eye-off': [path('M3 3l18 18'), path('M10.6 6.1A9.7 9.7 0 0 1 12 6c6.4 0 10 6 10 6a17 17 0 0 1-2.6 3.4M6.6 6.6C3.9 8.4 2 12 2 12s3.6 6 10 6a9.7 9.7 0 0 0 5.4-1.6'), path('M9.9 9.9a3 3 0 0 0 4.2 4.2')],
|
|
59
72
|
lock: [rect(5, 11, 14, 10), path('M8 11V8a4 4 0 0 1 8 0v3')],
|
|
60
73
|
unlock: [rect(5, 11, 14, 10), path('M8 11V8a4 4 0 0 1 7.75-1.4')],
|
|
74
|
+
user: [circle(12, 8, 4), path('M4 21a8 8 0 0 1 16 0')],
|
|
75
|
+
users: [circle(9, 8, 3.5), path('M2 20a7 7 0 0 1 14 0'), path('M15.5 4.6a3.5 3.5 0 0 1 0 6.8M18 13.6a7 7 0 0 1 4 6.4')],
|
|
76
|
+
mail: [rect(3, 5, 18, 14), path('M3 7.5l9 6 9-6')],
|
|
77
|
+
bell: [path('M18 16v-5a6 6 0 0 0-12 0v5l-2 2h16z'), path('M10 21a2 2 0 0 0 4 0')],
|
|
78
|
+
calendar: [rect(3, 5, 18, 16), path('M3 10h18M8 3v4M16 3v4')],
|
|
79
|
+
clock: [ring, path('M12 7v5l3 2')],
|
|
80
|
+
globe: [ring, path('M3 12h18'), path('M12 3c-2.5 2.5-3.8 5.5-3.8 9s1.3 6.5 3.8 9c2.5-2.5 3.8-5.5 3.8-9S14.5 5.5 12 3z')],
|
|
81
|
+
'map-pin': [path('M12 21s-7-6-7-11.5a7 7 0 0 1 14 0C19 15 12 21 12 21z'), circle(12, 9.5, 2.5)],
|
|
82
|
+
tag: [path('M3 12V5a2 2 0 0 1 2-2h7l9 9-9 9z'), dot(8, 8)],
|
|
83
|
+
bookmark: [path('M6 5a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v16l-6-4-6 4z')],
|
|
84
|
+
star: [path('M12 3.3L14.47 9.4L21.04 9.86L15.99 14.1L17.58 20.49L12 17L6.42 20.49L8.01 14.1L2.96 9.86L9.53 9.4z')],
|
|
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
|
+
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
|
+
list: [path('M9 6h11M9 12h11M9 18h11'), dot(4.5, 6), dot(4.5, 12), dot(4.5, 18)],
|
|
61
88
|
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')],
|
|
62
89
|
// Theme
|
|
63
90
|
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')],
|
|
64
91
|
moon: [path('M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5z')],
|
|
92
|
+
contrast: [ring, ['path', { d: 'M12 3a9 9 0 0 1 0 18z', fill: 'currentColor' }] as IconNode],
|
|
93
|
+
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)],
|
|
94
|
+
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')],
|
|
95
|
+
zap: [path('M13 2.5L4.5 13.5H11l-1 8 8.5-11H12z')],
|
|
65
96
|
// Code
|
|
66
97
|
code: [path('M8 7l-5 5 5 5M16 7l5 5-5 5')],
|
|
67
98
|
terminal: [path('M4 17l6-5-6-5M12 19h8')],
|
package/src/index.ts
CHANGED
|
@@ -22,14 +22,26 @@ export { default as Progress } from './components/Progress/Progress';
|
|
|
22
22
|
export type { ProgressProps, ProgressSize, ProgressStatus } from './components/Progress/Progress';
|
|
23
23
|
export { default as Layout } from './components/Layout/Layout';
|
|
24
24
|
export type { LayoutProps, Spacing, LegacySpacing } from './components/Layout/Layout';
|
|
25
|
+
export { default as Stack } from './components/Stack/Stack';
|
|
26
|
+
export type { StackProps, StackDirection } from './components/Stack/Stack';
|
|
27
|
+
export { default as Grid } from './components/Grid/Grid';
|
|
28
|
+
export type { GridProps, GridColumns } from './components/Grid/Grid';
|
|
29
|
+
export { BREAKPOINTS, isAtLeast } from './breakpoints';
|
|
30
|
+
export type { Breakpoint, Responsive } from './breakpoints';
|
|
25
31
|
export { default as Icon } from './components/Icon/Icon';
|
|
26
32
|
export type { IconProps } from './components/Icon/Icon';
|
|
27
33
|
export { ICONS, ICON_NAMES } from './icons';
|
|
28
34
|
export type { IconName, IconNode } from './icons';
|
|
35
|
+
export { default as Loader } from './components/Loader/Loader';
|
|
36
|
+
export type { LoaderProps } from './components/Loader/Loader';
|
|
29
37
|
export { default as Tree } from './components/Tree/Tree';
|
|
30
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';
|
|
31
43
|
export { default as Menu } from './components/Menu/Menu';
|
|
32
|
-
export type { MenuProps, MenuItem, MenuSection } from './components/Menu/Menu';
|
|
44
|
+
export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './components/Menu/Menu';
|
|
33
45
|
export { default as Select } from './components/Select/Select';
|
|
34
46
|
export type { SelectProps, SelectOption, SelectOptionState } from './components/Select/Select';
|
|
35
47
|
export { default as FullPageModal } from './components/FullPageModal/FullPageModal';
|
package/src/loader.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Loader's drawing, shared by the React and the Respawn Loader (no React here).
|
|
2
|
+
import { LOGO_INNER, LOGO_OUTER, LogoSegment } from './logo';
|
|
3
|
+
|
|
4
|
+
export { LOGO_VIEWBOX } from './logo';
|
|
5
|
+
|
|
6
|
+
const hex = (h: string) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16));
|
|
7
|
+
/** The middle of a segment's gradient: its one color when there are no gradients. */
|
|
8
|
+
const middle = (from: string, to: string) => `#${hex(from).map((v, i) => Math.round((v + hex(to)[i]) / 2).toString(16).padStart(2, '0')).join('')}`;
|
|
9
|
+
|
|
10
|
+
export interface LoaderSegment {
|
|
11
|
+
d: string;
|
|
12
|
+
/** "url(#<id>-<n>)" with gradients, else one color. */
|
|
13
|
+
fill: string;
|
|
14
|
+
/** Where it sits round the ring, 0 to 1 from the top, clockwise: the pulse reaches it then. */
|
|
15
|
+
angle: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface LoaderGradient {
|
|
19
|
+
id: string;
|
|
20
|
+
x1: number; y1: number; x2: number; y2: number;
|
|
21
|
+
from: string; to: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The two rings' segments. With an `id`, each segment is filled with its own
|
|
26
|
+
* gradient (ids "<id>-0", "<id>-1"…), as in the logo; without one, with its
|
|
27
|
+
* gradient's middle color, so a page can show any number of loaders with no
|
|
28
|
+
* ids at all. They look the same up to about 64 pixels.
|
|
29
|
+
*/
|
|
30
|
+
export function loaderRings(id?: string): { inner: LoaderSegment[]; outer: LoaderSegment[]; gradients: LoaderGradient[] } {
|
|
31
|
+
const gradients: LoaderGradient[] = [];
|
|
32
|
+
let n = 0;
|
|
33
|
+
const seg = ([d, from, to, [x1, y1, x2, y2], angle]: LogoSegment): LoaderSegment => {
|
|
34
|
+
if (!id) return { d, fill: middle(from, to), angle };
|
|
35
|
+
const gid = `${id}-${n}`;
|
|
36
|
+
n += 1;
|
|
37
|
+
gradients.push({ id: gid, x1, y1, x2, y2, from, to });
|
|
38
|
+
return { d, fill: `url(#${gid})`, angle };
|
|
39
|
+
};
|
|
40
|
+
const inner = LOGO_INNER.map(seg);
|
|
41
|
+
const outer = LOGO_OUTER.map(seg);
|
|
42
|
+
return { inner, outer, gradients };
|
|
43
|
+
}
|
package/src/logo.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// GENERATED from docs/public/nurvus-logo.svg (via docs/src/components/logoData.js) by
|
|
2
|
+
// tools/build-logo.mjs. The NURVUS mark, for Loader: two rings of segments.
|
|
3
|
+
// Each segment: [path, gradient from, gradient to, [x1, y1, x2, y2] of the gradient, angle round the ring (0 at the top, 0..1 clockwise)].
|
|
4
|
+
|
|
5
|
+
export type LogoSegment = [d: string, from: string, to: string, line: [number, number, number, number], angle: number];
|
|
6
|
+
|
|
7
|
+
/** The artwork's viewBox around the mark alone. */
|
|
8
|
+
export const LOGO_VIEWBOX = '492 149 540 540';
|
|
9
|
+
|
|
10
|
+
export const LOGO_INNER: LogoSegment[] = [["M696 237 C670 244 639 264 620 298 C605 320 607 335 615 342 C632 336 647 329 661 309 C675 293 689 286 705 279 C696 266 693 251 696 237 Z","#141934","#244f70",[707,229,619,338],0.894],["M705 229 C718 222 734 218 746 221 C763 242 785 257 806 267 C776 271 750 285 727 274 C709 266 701 249 705 229 Z","#20174e","#344eaa",[744,221,760,277],0.981],["M787 232 C838 231 894 267 922 305 C932 318 928 328 915 340 C906 348 896 348 891 336 C874 293 845 257 787 232 Z","#453be8","#6284e7",[785,234,915,343],0.12],["M896 343 C901 347 907 347 912 344 C915 358 934 373 943 390 C957 439 942 490 905 532 C924 500 932 461 923 433 C913 409 899 391 896 371 Z","#6686e7","#4cbaf5",[907,344,928,529],0.239],["M725 557 C677 541 639 512 649 469 C653 447 642 434 627 435 C611 436 605 448 610 476 C619 521 665 550 723 562 Z","#30c3b6","#42e2c9",[612,444,708,560],0.658],["M655 575 C622 552 613 527 608 499 C605 482 596 473 585 477 C570 482 573 498 585 518 C601 546 627 563 655 575 Z","#64dfdf","#259cea",[584,479,651,575],0.657],["M680 580 C684 567 708 568 718 576 C731 587 720 599 704 597 C690 596 680 588 680 580 Z","#5ebee8","#4367e8",[697,571,707,597],0.557],["M807 547 C783 550 770 545 752 551 C733 557 727 570 736 581 C746 594 769 592 781 580 C789 571 798 557 807 547 Z","#50ded1","#26a6d8",[744,548,786,589],0.491],["M797 572 C839 550 852 527 860 497 C865 476 878 466 891 472 C912 481 905 505 889 525 C864 552 835 567 797 572 Z","#3c7de7","#20c9e8",[887,471,819,571],0.376],["M755 210 C758 194 785 193 795 206 C805 218 789 227 775 226 C761 225 753 219 755 210 Z","#d5f3f3","#c8b5eb",[761,202,792,225],0.009],["M932 334 C945 326 960 344 964 359 C969 378 951 385 940 370 C930 357 926 341 932 334 Z","#e5b5e9","#92b0f2",[937,336,959,378],0.195]];
|
|
11
|
+
|
|
12
|
+
export const LOGO_OUTER: LogoSegment[] = [["M557 294 C587 234 662 173 718 162 C734 163 744 182 736 198 C725 224 680 231 645 242 C609 253 581 278 568 292 C565 295 561 295 557 294 Z","#78e8de","#5471ee",[600,165,654,300],0.906],["M534 283 C547 307 583 305 609 289 L639 270 C613 302 604 343 601 383 C599 420 606 454 595 464 C577 476 545 443 533 418 C513 379 515 313 534 283 Z","#79e8df","#4664ee",[521,300,625,432],0.798],["M506 382 C509 421 529 452 551 469 C551 526 592 577 659 598 C697 610 699 639 669 655 C579 619 511 540 504 449 C502 420 503 399 506 382 Z","#59cbe9","#5141e9",[508,401,650,654],0.681],["M687 657 L697 646 C736 653 781 641 814 620 L733 625 C787 599 833 572 890 577 C902 578 902 594 894 605 C850 655 771 684 687 657 Z","#3333df","#12a9e7",[750,649,890,566],0.472],["M970 394 C1000 460 989 531 951 580 C937 598 922 610 913 610 C889 583 919 553 942 526 C972 490 983 444 970 394 Z","#82f6dc","#2296d9",[979,419,914,610],0.329],["M975 341 C970 333 988 322 996 327 C1000 329 1002 335 1005 344 C1024 397 1019 449 1000 499 C1012 442 1009 392 975 341 Z","#923ce9","#ed80df",[985,334,1013,493],0.22],["M744 167 C829 154 920 205 974 277 C987 295 979 315 966 322 C947 332 921 316 922 279 C880 207 807 176 744 167 Z","#fff9ef","#ee74b8",[755,167,958,308],0.105]];
|
package/src/menu.ts
CHANGED
|
@@ -1,37 +1,107 @@
|
|
|
1
|
-
// Menu rules shared by the React and the Respawn Menu (no React here):
|
|
2
|
-
// section ids, which sections start open,
|
|
1
|
+
// Menu rules shared by the React and the Respawn Menu (no React here): which
|
|
2
|
+
// kind of menu the props make, the section ids, which sections start open,
|
|
3
|
+
// and the keys.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What a Menu is, and so what screen readers hear:
|
|
7
|
+
* - 'nav': navigation. A <nav> landmark of links ("link", "current page");
|
|
8
|
+
* an item without an href is a button in it ("button").
|
|
9
|
+
* - 'actions': a menu of commands (the WAI-ARIA menu pattern): role="menu",
|
|
10
|
+
* each item a "menu item"; one Tab stop, the arrow keys move.
|
|
11
|
+
* - 'tree': a tree to pick from (the WAI-ARIA tree pattern), like layers:
|
|
12
|
+
* "tree", each row "level 2, 3 of 5, selected". Drawn by Tree.
|
|
13
|
+
*/
|
|
14
|
+
export type MenuKind = 'nav' | 'actions' | 'tree';
|
|
15
|
+
|
|
16
|
+
interface KindProps {
|
|
17
|
+
kind?: MenuKind;
|
|
18
|
+
sections?: { href?: string; items?: { href?: string }[] }[];
|
|
19
|
+
items?: unknown;
|
|
20
|
+
activePath?: unknown;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The kind to draw: `kind` when given; else a tree for Tree's props (items,
|
|
25
|
+
* activePath), navigation when any item is a link, a menu of actions when
|
|
26
|
+
* none is.
|
|
27
|
+
*/
|
|
28
|
+
export function menuKind(props: KindProps): MenuKind {
|
|
29
|
+
if (props.kind) return props.kind;
|
|
30
|
+
if (!props.sections && (props.items !== undefined || props.activePath !== undefined)) return 'tree';
|
|
31
|
+
const sections = props.sections || [];
|
|
32
|
+
const links = sections.some((s) => s.href !== undefined || (s.items || []).some((item) => item.href !== undefined));
|
|
33
|
+
return links || !sections.length ? 'nav' : 'actions';
|
|
34
|
+
}
|
|
3
35
|
|
|
4
36
|
/** A section's id: its own, or its title as a slug ("Get started" -> "get-started"). */
|
|
5
|
-
export const sectionId = (section: { id?: string; title
|
|
6
|
-
section.id || String(section.title).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
37
|
+
export const sectionId = (section: { id?: string; title?: unknown }): string =>
|
|
38
|
+
section.id || String(section.title ?? 'section').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
7
39
|
|
|
8
40
|
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
9
41
|
export const startsOpen = (defaultOpen: boolean | Record<string, boolean> | undefined, id: string): boolean =>
|
|
10
42
|
typeof defaultOpen === 'boolean' ? defaultOpen : !defaultOpen || defaultOpen[id] !== false;
|
|
11
43
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
44
|
+
/** Makes `row` the one item of an actions menu that Tab reaches (roving tabindex). */
|
|
45
|
+
export function roveTo(menu: HTMLElement, row: HTMLElement): void {
|
|
46
|
+
menu.querySelectorAll<HTMLElement>('[role="menuitem"]').forEach((item) => item.setAttribute('tabindex', item === row ? '0' : '-1'));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Focus moved onto an item of an actions menu (a click, say): it becomes the Tab stop. */
|
|
50
|
+
export function menuFocus(event: FocusEvent, menu: HTMLElement): void {
|
|
51
|
+
const row = (event.target as HTMLElement).closest<HTMLElement>('[role="menuitem"]');
|
|
52
|
+
if (row && menu.getAttribute('role') === 'menu') roveTo(menu, row);
|
|
21
53
|
}
|
|
22
54
|
|
|
23
55
|
/**
|
|
24
|
-
* The
|
|
25
|
-
*
|
|
26
|
-
* -
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
56
|
+
* The keys on a Menu. Up / Down move to the previous / next item (going round
|
|
57
|
+
* at the ends) and Home / End to the first / last, in every kind.
|
|
58
|
+
* - Navigation (the WAI-ARIA disclosure navigation pattern, with its
|
|
59
|
+
* optional keys): Tab still goes link by link. The rows are the section
|
|
60
|
+
* titles and the links in open sections; Right on a section opens it, or
|
|
61
|
+
* goes to its first link; Left closes it, or goes from a link up to its
|
|
62
|
+
* section. Enter follows a link; Enter or Space opens a section.
|
|
63
|
+
* - Actions (the WAI-ARIA menu pattern): one Tab stop that follows the
|
|
64
|
+
* focus; typing a letter goes to the next item starting with it; Enter or
|
|
65
|
+
* Space runs the item.
|
|
31
66
|
* Returns true when it used the key.
|
|
32
67
|
*/
|
|
33
68
|
export function menuKeydown(event: KeyboardEvent, nav: HTMLElement): boolean {
|
|
34
|
-
if (event.altKey || event.ctrlKey || event.metaKey
|
|
69
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return false;
|
|
70
|
+
return nav.getAttribute('role') === 'menu' ? actionsKeydown(event, nav) : navKeydown(event, nav);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function actionsKeydown(event: KeyboardEvent, menu: HTMLElement): boolean {
|
|
74
|
+
const rows = Array.from(menu.querySelectorAll<HTMLElement>('[role="menuitem"]'));
|
|
75
|
+
const target = (event.target as HTMLElement).closest<HTMLElement>('[role="menuitem"]');
|
|
76
|
+
if (!rows.length || !target) return false;
|
|
77
|
+
const at = rows.indexOf(target);
|
|
78
|
+
let next: HTMLElement | undefined;
|
|
79
|
+
switch (event.key) {
|
|
80
|
+
case 'ArrowDown': next = rows[(at + 1) % rows.length]; break;
|
|
81
|
+
case 'ArrowUp': next = rows[at <= 0 ? rows.length - 1 : at - 1]; break;
|
|
82
|
+
case 'Home': next = rows[0]; break;
|
|
83
|
+
case 'End': next = rows[rows.length - 1]; break;
|
|
84
|
+
default:
|
|
85
|
+
// Type to find: the next item whose text starts with the letter.
|
|
86
|
+
if (event.key.length === 1 && !event.shiftKey && /\S/.test(event.key)) {
|
|
87
|
+
const key = event.key.toLowerCase();
|
|
88
|
+
for (let i = 1; i <= rows.length; i += 1) {
|
|
89
|
+
const row = rows[(at + i) % rows.length];
|
|
90
|
+
if ((row.textContent || '').trim().toLowerCase().startsWith(key)) { next = row; break; }
|
|
91
|
+
}
|
|
92
|
+
if (!next) return false;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
roveTo(menu, next);
|
|
99
|
+
next.focus();
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function navKeydown(event: KeyboardEvent, nav: HTMLElement): boolean {
|
|
104
|
+
if (event.shiftKey) return false;
|
|
35
105
|
const target = event.target as HTMLElement;
|
|
36
106
|
const rows = Array.from(nav.querySelectorAll<HTMLElement>('.menu-title, .menu-item'))
|
|
37
107
|
.filter((row) => !row.closest('.menu-section[data-open="false"] .menu-items'));
|
package/src/progress.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Progress rules shared by the React and the Respawn Progress (no React here).
|
|
2
|
+
import type { IconName } from './icons';
|
|
3
|
+
|
|
4
|
+
export type ProgressStatus = 'active' | 'success' | 'warning' | 'critical';
|
|
5
|
+
|
|
6
|
+
/** What each status means, in words and as an icon, so it never rests on color alone (WCAG 1.4.1). */
|
|
7
|
+
export const PROGRESS_STATUS: Record<ProgressStatus, { word: string; icon?: IconName }> = {
|
|
8
|
+
active: { word: '' },
|
|
9
|
+
success: { word: 'Done', icon: 'check' },
|
|
10
|
+
warning: { word: 'Needs attention', icon: 'warning' },
|
|
11
|
+
critical: { word: 'Failed', icon: 'error' },
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** 0 to 100, whole numbers; anything else (NaN, -5, 140) is pulled into range. */
|
|
15
|
+
export const clampPercent = (percent: number): number => (Number.isFinite(percent) ? Math.round(Math.min(100, Math.max(0, percent))) : 0);
|
|
16
|
+
|
|
17
|
+
/** What screen readers say for the value: "40%", or "100%, Done". */
|
|
18
|
+
export const progressValueText = (percent: number, status: ProgressStatus): string => {
|
|
19
|
+
const word = (PROGRESS_STATUS[status] || PROGRESS_STATUS.active).word;
|
|
20
|
+
return word ? `${clampPercent(percent)}%, ${word}` : `${clampPercent(percent)}%`;
|
|
21
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { AnchorItem, anchorRail, anchorDash, anchorActive, anchorGo } from '../anchor';
|
|
4
|
+
import { spawn, ElementProps } from './spawn';
|
|
5
|
+
import { updatable, NurvusElement } from './update';
|
|
6
|
+
import '../components/Anchor/Anchor.scss';
|
|
7
|
+
|
|
8
|
+
export type { AnchorItem };
|
|
9
|
+
|
|
10
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
11
|
+
|
|
12
|
+
export interface AnchorProps extends ElementProps {
|
|
13
|
+
/** The sections, in page order: each an "#id" href, a label and a level (1, 2…). */
|
|
14
|
+
items: AnchorItem[];
|
|
15
|
+
/** The heading above the list. */
|
|
16
|
+
title?: string;
|
|
17
|
+
/** Prefix for the ids inside (the title is "<id>-title"). Defaults to "anchor". */
|
|
18
|
+
id?: string;
|
|
19
|
+
/** How far below the top a section's heading must be scrolled to count as "in view", in pixels. */
|
|
20
|
+
offset?: number;
|
|
21
|
+
/** The section in view, set by you (its href). Left out, the Anchor follows the scrolling itself. */
|
|
22
|
+
current?: string;
|
|
23
|
+
/** Scroll-spy inside this element instead of the page: an element or a CSS selector. */
|
|
24
|
+
scrollRoot?: Element | string;
|
|
25
|
+
/** Writes the section's #id in the address bar when one is picked. Turn off for hash routers. */
|
|
26
|
+
updateHash?: boolean;
|
|
27
|
+
/** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
|
|
28
|
+
onNavigate?: (href: string, event: MouseEvent) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* "On this page", the same markup as the React Anchor: the rail bends with the
|
|
33
|
+
* nesting and its highlight slides to the section in view, in place (the
|
|
34
|
+
* element stays the same while you scroll).
|
|
35
|
+
*/
|
|
36
|
+
export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, AnchorProps> {
|
|
37
|
+
return updatable(({
|
|
38
|
+
items, title = 'On this page', id = 'anchor', offset = 96, current, scrollRoot, updateHash = true, onNavigate, className, ...rest
|
|
39
|
+
}: AnchorProps) => {
|
|
40
|
+
const controlled = current !== undefined;
|
|
41
|
+
const rail = anchorRail(items.map((item) => item.level || 1));
|
|
42
|
+
let active = controlled ? Math.max(0, items.findIndex((item) => item.href === current)) : 0;
|
|
43
|
+
const links: HTMLElement[] = [];
|
|
44
|
+
const dashOf = (i: number) => { const d = anchorDash(rail, i); return { 'stroke-dasharray': d.dasharray, 'stroke-dashoffset': String(d.dashoffset) }; };
|
|
45
|
+
const thumb = spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-thumb', d: rail.d, style: dashOf(active) });
|
|
46
|
+
const mark = (i: number) => {
|
|
47
|
+
active = i;
|
|
48
|
+
const d = dashOf(i);
|
|
49
|
+
thumb.style.setProperty('stroke-dasharray', d['stroke-dasharray']);
|
|
50
|
+
thumb.style.setProperty('stroke-dashoffset', d['stroke-dashoffset']);
|
|
51
|
+
links.forEach((link, k) => {
|
|
52
|
+
link.setAttribute('data-active', String(k === i));
|
|
53
|
+
if (k === i) link.setAttribute('aria-current', 'location'); else link.removeAttribute('aria-current');
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const el = spawn<HTMLElement>({
|
|
58
|
+
tag: 'nav',
|
|
59
|
+
className: classNames(className, 'anchor', CONSTANT.CLASS_PREFIX),
|
|
60
|
+
'aria-labelledby': `${id}-title`,
|
|
61
|
+
...rest,
|
|
62
|
+
children: [
|
|
63
|
+
spawn({ tag: 'p', className: 'anchor-title', id: `${id}-title`, children: title }),
|
|
64
|
+
spawn({
|
|
65
|
+
className: 'anchor-body',
|
|
66
|
+
children: [
|
|
67
|
+
spawn<any>({
|
|
68
|
+
tag: 'svg',
|
|
69
|
+
namespace: SVG,
|
|
70
|
+
className: 'anchor-rail',
|
|
71
|
+
viewBox: `0 0 ${rail.width} ${rail.height}`,
|
|
72
|
+
style: { width: `${rail.width / 16}rem`, height: `${rail.height / 16}rem` },
|
|
73
|
+
'aria-hidden': true,
|
|
74
|
+
focusable: 'false',
|
|
75
|
+
children: [spawn<any>({ tag: 'path', namespace: SVG, className: 'anchor-track', d: rail.d }), thumb],
|
|
76
|
+
}),
|
|
77
|
+
spawn({
|
|
78
|
+
tag: 'ul',
|
|
79
|
+
className: 'anchor-list',
|
|
80
|
+
children: items.map((item, i) => {
|
|
81
|
+
const link = spawn({
|
|
82
|
+
tag: 'a',
|
|
83
|
+
className: 'anchor-link',
|
|
84
|
+
href: item.href,
|
|
85
|
+
title: item.label,
|
|
86
|
+
'data-active': i === active,
|
|
87
|
+
'aria-current': i === active ? 'location' : undefined,
|
|
88
|
+
events: {
|
|
89
|
+
click: (event: MouseEvent) => {
|
|
90
|
+
if (onNavigate) onNavigate(item.href, event);
|
|
91
|
+
if (event.defaultPrevented) return;
|
|
92
|
+
if (anchorGo(item.href, updateHash)) { event.preventDefault(); if (!controlled) mark(i); }
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
children: item.label,
|
|
96
|
+
});
|
|
97
|
+
links.push(link);
|
|
98
|
+
return spawn({ tag: 'li', style: { '--anchor-level': String((item.level || 1) - 1) }, children: link });
|
|
99
|
+
}),
|
|
100
|
+
}),
|
|
101
|
+
],
|
|
102
|
+
}),
|
|
103
|
+
],
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Follow the scrolling once it is on the page (and stop once it is gone).
|
|
107
|
+
if (!controlled && typeof window !== 'undefined') {
|
|
108
|
+
let frame = 0;
|
|
109
|
+
let seen = false;
|
|
110
|
+
const root = () => (typeof scrollRoot === 'string' ? document.querySelector(scrollRoot) : scrollRoot || null);
|
|
111
|
+
const update = () => {
|
|
112
|
+
cancelAnimationFrame(frame);
|
|
113
|
+
frame = requestAnimationFrame(() => {
|
|
114
|
+
if (!el.isConnected) { if (seen) { window.removeEventListener('scroll', update, true); window.removeEventListener('resize', update); } return; }
|
|
115
|
+
seen = true;
|
|
116
|
+
const next = anchorActive(items, offset, root());
|
|
117
|
+
if (next !== active) mark(next);
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
window.addEventListener('scroll', update, { passive: true, capture: true });
|
|
121
|
+
window.addEventListener('resize', update);
|
|
122
|
+
update();
|
|
123
|
+
}
|
|
124
|
+
return el;
|
|
125
|
+
}, props);
|
|
126
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -4,6 +4,7 @@ import { spawn, Child, ElementProps } from './spawn';
|
|
|
4
4
|
import { NurvusElement } from './update';
|
|
5
5
|
import Layout from './Layout';
|
|
6
6
|
import Title from './Title';
|
|
7
|
+
import { dialogKeydown, focusDialog, restoreFocus } from '../dialog';
|
|
7
8
|
import '../components/FullPageModal/FullPageModal.scss';
|
|
8
9
|
|
|
9
10
|
export interface FullPageModalProps extends ElementProps {
|
|
@@ -17,10 +18,16 @@ export interface FullPageModalProps extends ElementProps {
|
|
|
17
18
|
children?: Child;
|
|
18
19
|
/** Appends the modal to this element when it is made, e.g. document.body. */
|
|
19
20
|
mountEl?: Element;
|
|
21
|
+
/** Called when someone presses Esc. Close the modal in it: modal.update({ visible: false }). */
|
|
22
|
+
onClose?: () => void;
|
|
23
|
+
/** Names the modal and its title: the title gets the id "<id>-title", and the modal is labelled by it. */
|
|
24
|
+
id?: string;
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
/**
|
|
23
|
-
* Shows content over the whole screen
|
|
28
|
+
* Shows content over the whole screen: a modal dialog, like the React one
|
|
29
|
+
* (focus moves in when it opens and back when it closes, Tab stays inside,
|
|
30
|
+
* Esc calls onClose). Returns the modal's outer element
|
|
24
31
|
* (`<div class="portal nurvus">`, the one React puts into `<body>` itself):
|
|
25
32
|
* mount it on document.body, or pass `mountEl: document.body`.
|
|
26
33
|
*
|
|
@@ -42,6 +49,7 @@ export default function FullPageModal(props: FullPageModalProps = {}): NurvusEle
|
|
|
42
49
|
// The React component's two states.
|
|
43
50
|
let isVisible = Boolean(current.visible);
|
|
44
51
|
let animationState = '';
|
|
52
|
+
let opener: Element | null = null;
|
|
45
53
|
|
|
46
54
|
const portal = spawn<HTMLDivElement>({ className: `portal ${CONSTANT.CLASS_PREFIX}` }) as NurvusElement<HTMLDivElement, FullPageModalProps>;
|
|
47
55
|
let mask: HTMLElement | null = null;
|
|
@@ -80,10 +88,24 @@ export default function FullPageModal(props: FullPageModalProps = {}): NurvusEle
|
|
|
80
88
|
};
|
|
81
89
|
|
|
82
90
|
const buildModal = () => {
|
|
83
|
-
const { children, className, headerActions, title, visible: _visible, mountEl: _mountEl, ...rest } = current;
|
|
91
|
+
const { children, className, headerActions, title, visible: _visible, mountEl: _mountEl, onClose, id, events, ...rest } = current;
|
|
92
|
+
const titleId = id && title ? `${id}-title` : undefined;
|
|
84
93
|
const el = spawn({
|
|
85
94
|
className: classNames(className, 'modal', CONSTANT.CLASS_PREFIX),
|
|
95
|
+
role: 'dialog',
|
|
96
|
+
'aria-modal': 'true',
|
|
97
|
+
'aria-labelledby': titleId,
|
|
98
|
+
'aria-label': !id && typeof title === 'string' ? title : undefined,
|
|
99
|
+
tabindex: -1,
|
|
100
|
+
id,
|
|
86
101
|
...rest,
|
|
102
|
+
events: {
|
|
103
|
+
...(events as object),
|
|
104
|
+
keydown: (event: KeyboardEvent, node: HTMLElement) => {
|
|
105
|
+
dialogKeydown(event, node, current.onClose);
|
|
106
|
+
if (events && (events as any).keydown) (events as any).keydown(event, node);
|
|
107
|
+
},
|
|
108
|
+
},
|
|
87
109
|
tag: 'div',
|
|
88
110
|
children: [
|
|
89
111
|
spawn({
|
|
@@ -92,7 +114,7 @@ export default function FullPageModal(props: FullPageModalProps = {}): NurvusEle
|
|
|
92
114
|
display: 'flex',
|
|
93
115
|
alignItems: 'center',
|
|
94
116
|
justifyContent: 'space-between',
|
|
95
|
-
children: [Title({ children: title }), spawn({ className: 'header-actions', children: headerActions })],
|
|
117
|
+
children: [Title({ id: titleId, children: title }), spawn({ className: 'header-actions', children: headerActions })],
|
|
96
118
|
}),
|
|
97
119
|
}),
|
|
98
120
|
children,
|
|
@@ -117,9 +139,14 @@ export default function FullPageModal(props: FullPageModalProps = {}): NurvusEle
|
|
|
117
139
|
const visible = Boolean(current.visible);
|
|
118
140
|
if (visible !== was) start(visible);
|
|
119
141
|
sync();
|
|
142
|
+
// Focus in on open; back to the opener on close.
|
|
143
|
+
if (visible && !was) { opener = document.activeElement; focusDialog(modal); }
|
|
144
|
+
if (!visible && was) { restoreFocus(opener); opener = null; }
|
|
120
145
|
return portal;
|
|
121
146
|
};
|
|
122
147
|
|
|
123
148
|
if (mountEl) mountEl.appendChild(portal);
|
|
149
|
+
// Open from the start: focus goes in once it is on the page.
|
|
150
|
+
if (current.visible) { opener = document.activeElement; focusDialog(modal); }
|
|
124
151
|
return portal;
|
|
125
152
|
}
|