@olenbetong/synergi-react 2.6.0 → 2.7.0
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/es/components/AppMenu/AppMenu.css +0 -25
- package/es/components/AppMenu/AppMenu.js +1 -2
- package/es/components/AppMenu/index.d.ts +0 -1
- package/es/components/AppMenu/index.js +0 -1
- package/es/index.d.ts +1 -2
- package/es/index.js +1 -2
- package/package.json +3 -3
- package/src/colorScheme.ts +81 -81
- package/src/components/AppMenu/AppMenu.css +648 -546
- package/src/components/AppMenu/AppMenu.tsx +285 -478
- package/src/components/AppMenu/AppMenuColorScheme.tsx +41 -41
- package/src/components/AppMenu/AppMenuFavoriteButton.tsx +47 -0
- package/src/components/AppMenu/AppMenuHeader.tsx +79 -0
- package/src/components/AppMenu/AppMenuList.tsx +89 -0
- package/src/components/AppMenu/AppMenuRail.tsx +117 -0
- package/src/components/AppMenu/icons.tsx +76 -0
- package/src/components/AppMenu/index.ts +17 -12
- package/src/components/AppMenu/menuEntries.ts +128 -0
- package/src/components/AppMenu/types.ts +45 -35
- package/src/components/AppMenu/useFavorites.ts +40 -0
- package/src/components/AppMenu/useRovingTabIndex.ts +130 -118
- package/src/components/AppShell/index.tsx +287 -259
- package/src/components/LinkedCardList/index.scss +93 -93
- package/src/components/ProgressBar/index.scss +94 -94
- package/src/index.ts +61 -61
- package/es/colorScheme.d.ts +0 -17
- package/es/colorScheme.js +0 -54
- package/es/components/AppMenu/AppMenuColorScheme.d.ts +0 -2
- package/es/components/AppMenu/AppMenuColorScheme.js +0 -18
|
@@ -1,35 +1,45 @@
|
|
|
1
|
-
/** A single entry in the app menu's right hand pane or in the user menu. */
|
|
2
|
-
export type AppMenuLink = {
|
|
3
|
-
label: string;
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
1
|
+
/** A single entry in the app menu's right hand pane or in the user menu. */
|
|
2
|
+
export type AppMenuLink = {
|
|
3
|
+
label: string;
|
|
4
|
+
/**
|
|
5
|
+
* Stable identifier for the entry. Set for app entries (the version-less
|
|
6
|
+
* ArticleId), and used as the key when toggling the favorite state.
|
|
7
|
+
*/
|
|
8
|
+
id?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Current favorite state. A favorite toggle is only rendered for entries
|
|
11
|
+
* where this is a boolean and the menu got an `onToggleFavorite` handler.
|
|
12
|
+
*/
|
|
13
|
+
isFavorite?: boolean;
|
|
14
|
+
/** Renders an anchor. Without an href the entry is rendered as a button. */
|
|
15
|
+
href?: string;
|
|
16
|
+
target?: string;
|
|
17
|
+
rel?: string;
|
|
18
|
+
onClick?: () => void;
|
|
19
|
+
/** Extra attributes, e.g. `{ "data-action": "clear-cache" }` for site scripts. */
|
|
20
|
+
attributes?: Record<string, string>;
|
|
21
|
+
/** Keep the menu open when the entry is activated. */
|
|
22
|
+
keepOpen?: boolean;
|
|
23
|
+
/** Render a divider above this entry (user menu only). */
|
|
24
|
+
separatorBefore?: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** A titled block of links inside a category. */
|
|
28
|
+
export type AppMenuLinkGroup = {
|
|
29
|
+
title?: string;
|
|
30
|
+
items: AppMenuLink[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A category listed in the app menu rail that shows a fixed set of links
|
|
35
|
+
* instead of apps, e.g. the developer tools.
|
|
36
|
+
*/
|
|
37
|
+
export type AppMenuCategory = {
|
|
38
|
+
/** Unique key, used for selection. */
|
|
39
|
+
id: string;
|
|
40
|
+
/** Label shown in the rail. */
|
|
41
|
+
label: string;
|
|
42
|
+
/** Sub header in the rail that this category is listed under. */
|
|
43
|
+
section?: string;
|
|
44
|
+
groups: AppMenuLinkGroup[];
|
|
45
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import type { AppEntry } from "./menuEntries.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Tracks the favorite state the user has changed while the menu is open.
|
|
7
|
+
*
|
|
8
|
+
* The changes are applied optimistically on top of the article rows and rolled
|
|
9
|
+
* back if the handler rejects. The overrides are kept for the lifetime of the
|
|
10
|
+
* menu, since the article list is only loaded the first time it is opened and
|
|
11
|
+
* would otherwise show a stale state the second time around.
|
|
12
|
+
*/
|
|
13
|
+
export function useFavorites(onToggleFavorite?: (id: string, favorite: boolean) => void | Promise<unknown>) {
|
|
14
|
+
let [overrides, setOverrides] = useState<Record<string, boolean>>({});
|
|
15
|
+
|
|
16
|
+
let applyOverrides = useCallback(
|
|
17
|
+
(entries: AppEntry[]) => {
|
|
18
|
+
if (Object.keys(overrides).length === 0) return entries;
|
|
19
|
+
|
|
20
|
+
return entries.map((entry) => (entry.id in overrides ? { ...entry, isFavorite: overrides[entry.id] } : entry));
|
|
21
|
+
},
|
|
22
|
+
[overrides],
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
let toggleFavorite = useCallback(
|
|
26
|
+
async (id: string, favorite: boolean) => {
|
|
27
|
+
setOverrides((current) => ({ ...current, [id]: favorite }));
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
await onToggleFavorite?.(id, favorite);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error(error);
|
|
33
|
+
setOverrides((current) => ({ ...current, [id]: !favorite }));
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
[onToggleFavorite],
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
return { applyOverrides, toggleFavorite: onToggleFavorite ? toggleFavorite : undefined };
|
|
40
|
+
}
|
|
@@ -1,118 +1,130 @@
|
|
|
1
|
-
import { type KeyboardEvent, useEffect, useState } from "react";
|
|
2
|
-
|
|
3
|
-
const NEXT_KEYS = ["ArrowDown", "ArrowRight"];
|
|
4
|
-
const PREV_KEYS = ["ArrowUp", "ArrowLeft"];
|
|
5
|
-
const HORIZONTAL_KEYS = ["ArrowLeft", "ArrowRight"];
|
|
6
|
-
const ARROW_KEYS = [...HORIZONTAL_KEYS, "ArrowUp", "ArrowDown"];
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Find the item closest to `from` in the neighbouring visual row or column.
|
|
10
|
-
*
|
|
11
|
-
* The app list is a grid whose column count depends on the available width, so
|
|
12
|
-
* which row and column an item ends up in is only known after layout. Comparing
|
|
13
|
-
* client rectangles is therefore the only reliable way to move in two
|
|
14
|
-
* dimensions.
|
|
15
|
-
*/
|
|
16
|
-
function findAdjacent(items: readonly HTMLElement[], from: number, axis: "x" | "y", direction: 1 | -1) {
|
|
17
|
-
// `main` is the axis stepped along, `cross` the one used to pick the closest
|
|
18
|
-
// candidate within the target row/column.
|
|
19
|
-
let rects = items.map((item) => {
|
|
20
|
-
let rect = item.getBoundingClientRect();
|
|
21
|
-
return axis === "x" ? { main: rect.left, cross: rect.top } : { main: rect.top, cross: rect.left };
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
let origin = rects[from];
|
|
25
|
-
if (!origin) return null;
|
|
26
|
-
|
|
27
|
-
// The nearest row/column edge in the requested direction.
|
|
28
|
-
let target: number | null = null;
|
|
29
|
-
for (let rect of rects) {
|
|
30
|
-
let delta = rect.main - origin.main;
|
|
31
|
-
if (direction > 0 ? delta <= 1 : delta >= -1) continue;
|
|
32
|
-
if (target === null || Math.abs(rect.main - origin.main) < Math.abs(target - origin.main)) {
|
|
33
|
-
target = rect.main;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (target === null) return null;
|
|
38
|
-
|
|
39
|
-
let best: number | null = null;
|
|
40
|
-
let bestDistance = Number.POSITIVE_INFINITY;
|
|
41
|
-
for (let index = 0; index < rects.length; index++) {
|
|
42
|
-
let rect = rects[index];
|
|
43
|
-
if (Math.abs(rect.main - target) > 1) continue;
|
|
44
|
-
|
|
45
|
-
let distance = Math.abs(rect.cross - origin.cross);
|
|
46
|
-
if (distance < bestDistance) {
|
|
47
|
-
bestDistance = distance;
|
|
48
|
-
best = index;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return best;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export type RovingTabIndexOptions = {
|
|
56
|
-
/**
|
|
57
|
-
* When true, the arrow keys follow the visual layout of a wrapping grid
|
|
58
|
-
* instead of the DOM order: left/right move between columns and up/down
|
|
59
|
-
* between rows.
|
|
60
|
-
*/
|
|
61
|
-
columns?: boolean;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Roving tabindex: the container holds a single tab stop, and the arrow keys
|
|
66
|
-
* (plus Home/End) move focus between the items inside it.
|
|
67
|
-
*
|
|
68
|
-
* Spread `getItemProps(index)` on every focusable item and put `onKeyDown` on
|
|
69
|
-
* their common ancestor.
|
|
70
|
-
*/
|
|
71
|
-
export function useRovingTabIndex(itemCount: number, { columns = false }: RovingTabIndexOptions = {}) {
|
|
72
|
-
let [activeIndex, setActiveIndex] = useState(0);
|
|
73
|
-
|
|
74
|
-
// Keep the tab stop within the list when it shrinks (e.g. while searching).
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
setActiveIndex((index) => (index >= itemCount ? 0 : index));
|
|
77
|
-
}, [itemCount]);
|
|
78
|
-
|
|
79
|
-
function onKeyDown(event: KeyboardEvent<HTMLElement>) {
|
|
80
|
-
if (itemCount === 0) return;
|
|
81
|
-
|
|
82
|
-
let items = [...event.currentTarget.querySelectorAll<HTMLElement>("[data-roving-item]")];
|
|
83
|
-
let next: number | null;
|
|
84
|
-
|
|
85
|
-
if (columns && ARROW_KEYS.includes(event.key)) {
|
|
86
|
-
event.preventDefault();
|
|
87
|
-
|
|
88
|
-
let isHorizontal = HORIZONTAL_KEYS.includes(event.key);
|
|
89
|
-
let direction: 1 | -1 = NEXT_KEYS.includes(event.key) ? 1 : -1;
|
|
90
|
-
next = findAdjacent(items, activeIndex, isHorizontal ? "x" : "y", direction);
|
|
91
|
-
|
|
92
|
-
if (next === null) {
|
|
93
|
-
// Stay put at the first/last column, but let up/down continue into the
|
|
94
|
-
// neighbouring item so the whole list stays reachable.
|
|
95
|
-
if (isHorizontal) return;
|
|
96
|
-
next = (activeIndex + direction + itemCount) % itemCount;
|
|
97
|
-
}
|
|
98
|
-
} else if (NEXT_KEYS.includes(event.key)) next = (activeIndex + 1) % itemCount;
|
|
99
|
-
else if (PREV_KEYS.includes(event.key)) next = (activeIndex - 1 + itemCount) % itemCount;
|
|
100
|
-
else if (event.key === "Home") next = 0;
|
|
101
|
-
else if (event.key === "End") next = itemCount - 1;
|
|
102
|
-
else return;
|
|
103
|
-
|
|
104
|
-
event.preventDefault();
|
|
105
|
-
setActiveIndex(next);
|
|
106
|
-
items[next]?.focus();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function getItemProps(index: number) {
|
|
110
|
-
return {
|
|
111
|
-
"data-roving-item": "",
|
|
112
|
-
tabIndex: index === activeIndex ? 0 : -1,
|
|
113
|
-
onFocus: () => setActiveIndex(index),
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
1
|
+
import { type KeyboardEvent, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
const NEXT_KEYS = ["ArrowDown", "ArrowRight"];
|
|
4
|
+
const PREV_KEYS = ["ArrowUp", "ArrowLeft"];
|
|
5
|
+
const HORIZONTAL_KEYS = ["ArrowLeft", "ArrowRight"];
|
|
6
|
+
const ARROW_KEYS = [...HORIZONTAL_KEYS, "ArrowUp", "ArrowDown"];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Find the item closest to `from` in the neighbouring visual row or column.
|
|
10
|
+
*
|
|
11
|
+
* The app list is a grid whose column count depends on the available width, so
|
|
12
|
+
* which row and column an item ends up in is only known after layout. Comparing
|
|
13
|
+
* client rectangles is therefore the only reliable way to move in two
|
|
14
|
+
* dimensions.
|
|
15
|
+
*/
|
|
16
|
+
function findAdjacent(items: readonly HTMLElement[], from: number, axis: "x" | "y", direction: 1 | -1) {
|
|
17
|
+
// `main` is the axis stepped along, `cross` the one used to pick the closest
|
|
18
|
+
// candidate within the target row/column.
|
|
19
|
+
let rects = items.map((item) => {
|
|
20
|
+
let rect = item.getBoundingClientRect();
|
|
21
|
+
return axis === "x" ? { main: rect.left, cross: rect.top } : { main: rect.top, cross: rect.left };
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
let origin = rects[from];
|
|
25
|
+
if (!origin) return null;
|
|
26
|
+
|
|
27
|
+
// The nearest row/column edge in the requested direction.
|
|
28
|
+
let target: number | null = null;
|
|
29
|
+
for (let rect of rects) {
|
|
30
|
+
let delta = rect.main - origin.main;
|
|
31
|
+
if (direction > 0 ? delta <= 1 : delta >= -1) continue;
|
|
32
|
+
if (target === null || Math.abs(rect.main - origin.main) < Math.abs(target - origin.main)) {
|
|
33
|
+
target = rect.main;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (target === null) return null;
|
|
38
|
+
|
|
39
|
+
let best: number | null = null;
|
|
40
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
41
|
+
for (let index = 0; index < rects.length; index++) {
|
|
42
|
+
let rect = rects[index];
|
|
43
|
+
if (Math.abs(rect.main - target) > 1) continue;
|
|
44
|
+
|
|
45
|
+
let distance = Math.abs(rect.cross - origin.cross);
|
|
46
|
+
if (distance < bestDistance) {
|
|
47
|
+
bestDistance = distance;
|
|
48
|
+
best = index;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return best;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type RovingTabIndexOptions = {
|
|
56
|
+
/**
|
|
57
|
+
* When true, the arrow keys follow the visual layout of a wrapping grid
|
|
58
|
+
* instead of the DOM order: left/right move between columns and up/down
|
|
59
|
+
* between rows.
|
|
60
|
+
*/
|
|
61
|
+
columns?: boolean;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Roving tabindex: the container holds a single tab stop, and the arrow keys
|
|
66
|
+
* (plus Home/End) move focus between the items inside it.
|
|
67
|
+
*
|
|
68
|
+
* Spread `getItemProps(index)` on every focusable item and put `onKeyDown` on
|
|
69
|
+
* their common ancestor.
|
|
70
|
+
*/
|
|
71
|
+
export function useRovingTabIndex(itemCount: number, { columns = false }: RovingTabIndexOptions = {}) {
|
|
72
|
+
let [activeIndex, setActiveIndex] = useState(0);
|
|
73
|
+
|
|
74
|
+
// Keep the tab stop within the list when it shrinks (e.g. while searching).
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
setActiveIndex((index) => (index >= itemCount ? 0 : index));
|
|
77
|
+
}, [itemCount]);
|
|
78
|
+
|
|
79
|
+
function onKeyDown(event: KeyboardEvent<HTMLElement>) {
|
|
80
|
+
if (itemCount === 0) return;
|
|
81
|
+
|
|
82
|
+
let items = [...event.currentTarget.querySelectorAll<HTMLElement>("[data-roving-item]")];
|
|
83
|
+
let next: number | null;
|
|
84
|
+
|
|
85
|
+
if (columns && ARROW_KEYS.includes(event.key)) {
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
|
|
88
|
+
let isHorizontal = HORIZONTAL_KEYS.includes(event.key);
|
|
89
|
+
let direction: 1 | -1 = NEXT_KEYS.includes(event.key) ? 1 : -1;
|
|
90
|
+
next = findAdjacent(items, activeIndex, isHorizontal ? "x" : "y", direction);
|
|
91
|
+
|
|
92
|
+
if (next === null) {
|
|
93
|
+
// Stay put at the first/last column, but let up/down continue into the
|
|
94
|
+
// neighbouring item so the whole list stays reachable.
|
|
95
|
+
if (isHorizontal) return;
|
|
96
|
+
next = (activeIndex + direction + itemCount) % itemCount;
|
|
97
|
+
}
|
|
98
|
+
} else if (NEXT_KEYS.includes(event.key)) next = (activeIndex + 1) % itemCount;
|
|
99
|
+
else if (PREV_KEYS.includes(event.key)) next = (activeIndex - 1 + itemCount) % itemCount;
|
|
100
|
+
else if (event.key === "Home") next = 0;
|
|
101
|
+
else if (event.key === "End") next = itemCount - 1;
|
|
102
|
+
else return;
|
|
103
|
+
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
setActiveIndex(next);
|
|
106
|
+
items[next]?.focus();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function getItemProps(index: number) {
|
|
110
|
+
return {
|
|
111
|
+
"data-roving-item": "",
|
|
112
|
+
tabIndex: index === activeIndex ? 0 : -1,
|
|
113
|
+
onFocus: () => setActiveIndex(index),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Props for a secondary control belonging to item `index`, e.g. the favorite
|
|
119
|
+
* toggle next to an app link. It shares the item's tab stop — so Tab reaches
|
|
120
|
+
* it from the link — but the arrow keys skip it.
|
|
121
|
+
*/
|
|
122
|
+
function getSecondaryProps(index: number) {
|
|
123
|
+
return {
|
|
124
|
+
tabIndex: index === activeIndex ? 0 : -1,
|
|
125
|
+
onFocus: () => setActiveIndex(index),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { onKeyDown, getItemProps, getSecondaryProps };
|
|
130
|
+
}
|