@olenbetong/synergi-react 2.6.0 → 2.7.1

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 (43) hide show
  1. package/es/components/AppMenu/AppMenu.css +648 -546
  2. package/es/components/AppMenu/AppMenu.d.ts +17 -8
  3. package/es/components/AppMenu/AppMenu.js +44 -123
  4. package/es/components/AppMenu/AppMenuColorScheme.js +1 -1
  5. package/es/components/AppMenu/AppMenuFavoriteButton.d.ts +18 -0
  6. package/es/components/AppMenu/AppMenuFavoriteButton.js +11 -0
  7. package/es/components/AppMenu/AppMenuHeader.d.ts +24 -0
  8. package/es/components/AppMenu/AppMenuHeader.js +18 -0
  9. package/es/components/AppMenu/AppMenuList.d.ts +18 -0
  10. package/es/components/AppMenu/AppMenuList.js +39 -0
  11. package/es/components/AppMenu/AppMenuRail.d.ts +26 -0
  12. package/es/components/AppMenu/AppMenuRail.js +30 -0
  13. package/es/components/AppMenu/icons.d.ts +15 -0
  14. package/es/components/AppMenu/icons.js +13 -0
  15. package/es/components/AppMenu/index.d.ts +6 -1
  16. package/es/components/AppMenu/index.js +4 -0
  17. package/es/components/AppMenu/menuEntries.d.ts +51 -0
  18. package/es/components/AppMenu/menuEntries.js +93 -0
  19. package/es/components/AppMenu/types.d.ts +10 -0
  20. package/es/components/AppMenu/useFavorites.d.ts +13 -0
  21. package/es/components/AppMenu/useFavorites.js +28 -0
  22. package/es/components/AppMenu/useRovingTabIndex.d.ts +4 -0
  23. package/es/components/AppMenu/useRovingTabIndex.js +12 -1
  24. package/es/components/AppShell/index.js +23 -2
  25. package/package.json +3 -3
  26. package/src/colorScheme.ts +81 -81
  27. package/src/components/AppMenu/AppMenu.css +648 -546
  28. package/src/components/AppMenu/AppMenu.tsx +285 -478
  29. package/src/components/AppMenu/AppMenuColorScheme.tsx +1 -0
  30. package/src/components/AppMenu/AppMenuFavoriteButton.tsx +47 -0
  31. package/src/components/AppMenu/AppMenuHeader.tsx +79 -0
  32. package/src/components/AppMenu/AppMenuList.tsx +89 -0
  33. package/src/components/AppMenu/AppMenuRail.tsx +117 -0
  34. package/src/components/AppMenu/icons.tsx +76 -0
  35. package/src/components/AppMenu/index.ts +17 -12
  36. package/src/components/AppMenu/menuEntries.ts +128 -0
  37. package/src/components/AppMenu/types.ts +45 -35
  38. package/src/components/AppMenu/useFavorites.ts +40 -0
  39. package/src/components/AppMenu/useRovingTabIndex.ts +130 -118
  40. package/src/components/AppShell/index.tsx +287 -259
  41. package/src/components/LinkedCardList/index.scss +93 -93
  42. package/src/components/ProgressBar/index.scss +94 -94
  43. package/src/index.ts +61 -61
@@ -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
- return { onKeyDown, getItemProps };
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
+ }