@open-pioneer/react-utils 4.5.0-dev.20260226113620 → 4.5.0-dev.20260303103629
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/CHANGELOG.md +6 -5
- package/README.md +24 -3
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +3 -3
- package/roving-menu/RovingMenuState.d.ts +20 -2
- package/roving-menu/RovingMenuState.js +60 -12
- package/roving-menu/RovingMenuState.js.map +1 -1
- package/roving-menu/useMenuState.d.ts +4 -0
- package/roving-menu/useMenuState.js +18 -0
- package/roving-menu/useMenuState.js.map +1 -0
- package/roving-menu/useRovingMenu.d.ts +54 -8
- package/roving-menu/useRovingMenu.js +49 -10
- package/roving-menu/useRovingMenu.js.map +1 -1
- package/roving-menu/useRovingMenuItem.d.ts +16 -7
- package/roving-menu/useRovingMenuItem.js +10 -17
- package/roving-menu/useRovingMenuItem.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @open-pioneer/react-utils
|
|
2
2
|
|
|
3
|
-
## 4.5.0-dev.
|
|
3
|
+
## 4.5.0-dev.20260303103629
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
@@ -17,16 +17,17 @@
|
|
|
17
17
|
NOTE: Defining these values may overwrite existing behavior of a component if values cannot be merged.
|
|
18
18
|
For example, overriding an existing `data-` property may break a component.
|
|
19
19
|
|
|
20
|
+
- 8df39a2: Roving Menu - Add support for nested menus
|
|
20
21
|
- d523ade: Implement `mergeChakraProps` helper to merge multiple props objects.
|
|
21
|
-
- 604a589: Provide an implementation of the roving tab index pattern for menus.
|
|
22
|
+
- 604a589: Provide an implementation of the roving tab index pattern for menus ("Roving Menu").
|
|
22
23
|
This roving menu can be used to implement accessible menus with support for keyboard navigation.
|
|
23
24
|
|
|
24
25
|
### Patch Changes
|
|
25
26
|
|
|
26
|
-
- bb3f515:
|
|
27
|
+
- bb3f515: Roving Menu -Move focus correctly when an item unmounts while it is active.
|
|
27
28
|
- Updated dependencies [4e76893]
|
|
28
|
-
- @open-pioneer/core@4.5.0-dev.
|
|
29
|
-
- @open-pioneer/reactivity@4.5.0-dev.
|
|
29
|
+
- @open-pioneer/core@4.5.0-dev.20260303103629
|
|
30
|
+
- @open-pioneer/reactivity@4.5.0-dev.20260303103629
|
|
30
31
|
|
|
31
32
|
## 4.4.0
|
|
32
33
|
|
package/README.md
CHANGED
|
@@ -112,10 +112,31 @@ As an alternative, you can also completely unmount your component from a parent
|
|
|
112
112
|
Note that returning `null` or `undefined` from your component will interfere with focus handling.
|
|
113
113
|
You should always render your menu item's dom node, for as long as your item exists.
|
|
114
114
|
|
|
115
|
-
####
|
|
115
|
+
#### Nested menus
|
|
116
116
|
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
Roving menu supports limited nesting (2 levels deep).
|
|
118
|
+
|
|
119
|
+
For the outer menu, use the normal `useRovingMenu()` hook with a certain orientation.
|
|
120
|
+
For the child menu, use the `useNestedRovingMenu()` hook with the _opposite_ orientation.
|
|
121
|
+
|
|
122
|
+
The child menu will act as an item in its parent menu, so it requires a `value` as well.
|
|
123
|
+
|
|
124
|
+
Example for the child menu:
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
function NestedMenu() {
|
|
128
|
+
const { menuProps, menuState } = useNestedRovingMenu({
|
|
129
|
+
orientation: "horizontal",
|
|
130
|
+
value: "some-value"
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<HStack {...menuProps} justify="center" gap={5} padding={2}>
|
|
135
|
+
<RovingMenuRoot menuState={menuState}>{/* nested items ... */}</RovingMenuRoot>
|
|
136
|
+
</HStack>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
```
|
|
119
140
|
|
|
120
141
|
### Hooks
|
|
121
142
|
|
package/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export { useEvent } from "./useEvent";
|
|
|
18
18
|
export { type TitledSectionProps, TitledSection, type SectionHeadingProps, SectionHeading, type ConfigureTitledSectionProps, ConfigureTitledSection, type HeadingLevel, useHeadingLevel } from "./TitledSection";
|
|
19
19
|
export { type CommonComponentProps, type CommonComponentContainerProps, useCommonComponentProps } from "./useCommonComponentProps";
|
|
20
20
|
export { RovingMenuRoot, type RovingMenuRootProps } from "./roving-menu/RovingMenuRoot";
|
|
21
|
-
export { useRovingMenu, type RovingMenuProps, type RovingMenuResult } from "./roving-menu/useRovingMenu";
|
|
21
|
+
export { useRovingMenu, type RovingMenuProps, type RovingMenuResult, useNestedRovingMenu, type NestedRovingMenuProps, type NestedRovingMenuResult } from "./roving-menu/useRovingMenu";
|
|
22
22
|
export { useRovingMenuItem, type RovingMenuItemProps, type RovingMenuItemResult } from "./roving-menu/useRovingMenuItem";
|
|
23
23
|
export { type RovingMenuState } from "./roving-menu/RovingMenuState";
|
|
24
24
|
export { mergeChakraProps } from "./mergeChakraProps";
|
package/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export { useEvent } from './useEvent.js';
|
|
|
2
2
|
export { ConfigureTitledSection, SectionHeading, TitledSection, useHeadingLevel } from './TitledSection.js';
|
|
3
3
|
export { useCommonComponentProps } from './useCommonComponentProps.js';
|
|
4
4
|
export { RovingMenuRoot } from './roving-menu/RovingMenuRoot.js';
|
|
5
|
-
export { useRovingMenu } from './roving-menu/useRovingMenu.js';
|
|
5
|
+
export { useNestedRovingMenu, useRovingMenu } from './roving-menu/useRovingMenu.js';
|
|
6
6
|
export { useRovingMenuItem } from './roving-menu/useRovingMenuItem.js';
|
|
7
7
|
export { mergeChakraProps } from './mergeChakraProps.js';
|
|
8
8
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@open-pioneer/react-utils",
|
|
4
|
-
"version": "4.5.0-dev.
|
|
4
|
+
"version": "4.5.0-dev.20260303103629",
|
|
5
5
|
"description": "Contains common react utilities used by the open pioneer trails project",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"open-pioneer-trails"
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"@conterra/reactivity-core": "^0.8.1",
|
|
19
19
|
"classnames": "^2.5.1",
|
|
20
20
|
"react": "^19.2.4",
|
|
21
|
-
"@open-pioneer/
|
|
22
|
-
"@open-pioneer/
|
|
21
|
+
"@open-pioneer/reactivity": "4.5.0-dev.20260303103629",
|
|
22
|
+
"@open-pioneer/core": "4.5.0-dev.20260303103629"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
25
|
"./package.json": "./package.json",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { KeyboardEvent, RefObject } from "react";
|
|
1
|
+
import { KeyboardEvent, FocusEvent, RefObject } from "react";
|
|
2
2
|
export declare const MENU_ID_ATTR = "data-roving-menu-id";
|
|
3
3
|
export declare const MENU_OWNER_ATTR = "data-roving-menu-parent";
|
|
4
4
|
export declare const MENU_VALUE_ATTR = "data-roving-menu-value";
|
|
@@ -25,6 +25,14 @@ export interface RovingMenuState {
|
|
|
25
25
|
* @internal
|
|
26
26
|
*/
|
|
27
27
|
export declare function getInternalState(menuState: RovingMenuState): InternalMenuState;
|
|
28
|
+
export interface MenuStateOptions {
|
|
29
|
+
menuId: string;
|
|
30
|
+
orientation: "horizontal" | "vertical";
|
|
31
|
+
menuRef: RefObject<HTMLElement | null>;
|
|
32
|
+
wrap: boolean;
|
|
33
|
+
active: boolean;
|
|
34
|
+
isActiveInParent?: () => boolean;
|
|
35
|
+
}
|
|
28
36
|
/**
|
|
29
37
|
* Internal model class; created by the menu and accessible by the menu items.
|
|
30
38
|
*
|
|
@@ -34,13 +42,23 @@ export declare class InternalMenuState {
|
|
|
34
42
|
#private;
|
|
35
43
|
readonly menuId: string;
|
|
36
44
|
readonly orientation: "horizontal" | "vertical";
|
|
37
|
-
|
|
45
|
+
readonly wrap: boolean;
|
|
46
|
+
constructor(options: MenuStateOptions);
|
|
47
|
+
get active(): boolean;
|
|
48
|
+
set active(value: boolean);
|
|
38
49
|
/**
|
|
39
50
|
* Returns true if the menu item with the given value is currently active.
|
|
40
51
|
*
|
|
41
52
|
* The active menu item can receive the focus via keyboard navigation (tab).
|
|
42
53
|
*/
|
|
43
54
|
isActive(value: string): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Called when the menu itself is focused.
|
|
57
|
+
* The focus handler moves focus to the active child.
|
|
58
|
+
*
|
|
59
|
+
* This is used to implement nested menus.
|
|
60
|
+
*/
|
|
61
|
+
onFocus(e: FocusEvent): void;
|
|
44
62
|
/**
|
|
45
63
|
* Called when a key is pressed on the menu root element.
|
|
46
64
|
*/
|
|
@@ -8,6 +8,7 @@ const MENU_ID_ATTR = "data-roving-menu-id";
|
|
|
8
8
|
const MENU_OWNER_ATTR = "data-roving-menu-parent";
|
|
9
9
|
const MENU_VALUE_ATTR = "data-roving-menu-value";
|
|
10
10
|
const MENU_VALUE_JS = "rovingMenuValue";
|
|
11
|
+
const NOT_DISABLED_SELECTOR = ":not([disabled],[aria-disabled='true'])";
|
|
11
12
|
const MenuStateContext = createContext(void 0);
|
|
12
13
|
function getInternalState(menuState) {
|
|
13
14
|
if (!(menuState instanceof InternalMenuState)) {
|
|
@@ -16,15 +17,29 @@ function getInternalState(menuState) {
|
|
|
16
17
|
return menuState;
|
|
17
18
|
}
|
|
18
19
|
class InternalMenuState {
|
|
20
|
+
#active;
|
|
21
|
+
// True if the menu is currently active in its parent menu (used for nesting).
|
|
22
|
+
// Note: reactive function
|
|
23
|
+
#isActiveInParent;
|
|
19
24
|
#menuRef;
|
|
20
25
|
// Value of the currently active item
|
|
21
26
|
#current = reactive();
|
|
22
27
|
menuId;
|
|
23
28
|
orientation;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.menuId = menuId;
|
|
27
|
-
this.
|
|
29
|
+
wrap;
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.menuId = options.menuId;
|
|
32
|
+
this.wrap = options.wrap;
|
|
33
|
+
this.orientation = options.orientation;
|
|
34
|
+
this.#active = reactive(options.active);
|
|
35
|
+
this.#menuRef = options.menuRef;
|
|
36
|
+
this.#isActiveInParent = options.isActiveInParent ?? (() => true);
|
|
37
|
+
}
|
|
38
|
+
get active() {
|
|
39
|
+
return this.#active.value;
|
|
40
|
+
}
|
|
41
|
+
set active(value) {
|
|
42
|
+
this.#active.value = value;
|
|
28
43
|
}
|
|
29
44
|
get #currentValue() {
|
|
30
45
|
return this.#current.value;
|
|
@@ -35,22 +50,43 @@ class InternalMenuState {
|
|
|
35
50
|
* The active menu item can receive the focus via keyboard navigation (tab).
|
|
36
51
|
*/
|
|
37
52
|
isActive(value) {
|
|
38
|
-
return this.#currentValue === value;
|
|
53
|
+
return this.active && this.#isActiveInParent() && this.#currentValue === value;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Called when the menu itself is focused.
|
|
57
|
+
* The focus handler moves focus to the active child.
|
|
58
|
+
*
|
|
59
|
+
* This is used to implement nested menus.
|
|
60
|
+
*/
|
|
61
|
+
onFocus(e) {
|
|
62
|
+
if (e.target !== e.currentTarget) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const activeValue = this.#currentValue;
|
|
66
|
+
if (activeValue != null) {
|
|
67
|
+
const item = getMenuItem(this.#menuRef, this.menuId, activeValue);
|
|
68
|
+
if (item) {
|
|
69
|
+
LOG.debug("Forwarding menu focus to item", item);
|
|
70
|
+
item.focus();
|
|
71
|
+
} else {
|
|
72
|
+
LOG.debug("Failed to find menu item for active value");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
39
75
|
}
|
|
40
76
|
/**
|
|
41
77
|
* Called when a key is pressed on the menu root element.
|
|
42
78
|
*/
|
|
43
79
|
onKeyDown(event) {
|
|
80
|
+
if (event.defaultPrevented) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
44
83
|
const direction = getNavDirection(event, this.orientation);
|
|
45
84
|
if (!direction) {
|
|
46
85
|
return;
|
|
47
86
|
}
|
|
48
87
|
const items = getMenuItems(this.#menuRef, this.menuId);
|
|
49
|
-
const target = getFocusTarget(items, this.#currentValue, direction);
|
|
88
|
+
const target = getFocusTarget(items, this.#currentValue, direction, this.wrap);
|
|
50
89
|
if (!target) {
|
|
51
|
-
if (items.length > 1) {
|
|
52
|
-
LOG.warn("Failed to identify focus target for keyboard navigation");
|
|
53
|
-
}
|
|
54
90
|
return;
|
|
55
91
|
}
|
|
56
92
|
event.preventDefault();
|
|
@@ -94,7 +130,7 @@ class InternalMenuState {
|
|
|
94
130
|
const disabledIndex = findItemIndex(items, disabledValue);
|
|
95
131
|
let target;
|
|
96
132
|
if (disabledIndex === -1) {
|
|
97
|
-
target = getFocusTarget(items, -1, "home");
|
|
133
|
+
target = getFocusTarget(items, -1, "home", false);
|
|
98
134
|
} else {
|
|
99
135
|
target = getFocusTarget(items, disabledIndex, "forward", false) ?? getFocusTarget(items, disabledIndex, "backward", false);
|
|
100
136
|
}
|
|
@@ -152,7 +188,7 @@ function getNavDirection(event, orientation) {
|
|
|
152
188
|
return "end";
|
|
153
189
|
}
|
|
154
190
|
}
|
|
155
|
-
function getFocusTarget(items, current, direction, wrap
|
|
191
|
+
function getFocusTarget(items, current, direction, wrap) {
|
|
156
192
|
if (items.length === 0) {
|
|
157
193
|
return void 0;
|
|
158
194
|
}
|
|
@@ -226,11 +262,23 @@ function getMenuItems(menuRef, menuId, unmount = false, excludeDisabled = true)
|
|
|
226
262
|
const escapedId = CSS.escape(menuId);
|
|
227
263
|
let selector = `[${MENU_OWNER_ATTR}=${escapedId}]`;
|
|
228
264
|
if (excludeDisabled) {
|
|
229
|
-
selector +=
|
|
265
|
+
selector += NOT_DISABLED_SELECTOR;
|
|
230
266
|
}
|
|
231
267
|
const children = owner.querySelectorAll(selector);
|
|
232
268
|
return Array.from(children);
|
|
233
269
|
}
|
|
270
|
+
function getMenuItem(menuRef, menuId, value) {
|
|
271
|
+
const owner = menuRef.current;
|
|
272
|
+
if (!owner) {
|
|
273
|
+
return void 0;
|
|
274
|
+
}
|
|
275
|
+
const escapedMenuId = CSS.escape(menuId);
|
|
276
|
+
const escapedValue = CSS.escape(value);
|
|
277
|
+
const node = owner.querySelector(
|
|
278
|
+
`[${MENU_OWNER_ATTR}=${escapedMenuId}][${MENU_VALUE_ATTR}=${escapedValue}]${NOT_DISABLED_SELECTOR}`
|
|
279
|
+
);
|
|
280
|
+
return node ?? void 0;
|
|
281
|
+
}
|
|
234
282
|
function getItemValue(item) {
|
|
235
283
|
return item?.dataset[MENU_VALUE_JS] ?? void 0;
|
|
236
284
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RovingMenuState.js","sources":["RovingMenuState.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { reactive } from \"@conterra/reactivity-core\";\nimport { createLogger } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { createContext, KeyboardEvent, RefObject } from \"react\";\n\nconst LOG = createLogger(sourceId);\n\n// Mostly for debugging parent/child links. Not used for lookups.\nexport const MENU_ID_ATTR = \"data-roving-menu-id\";\n\n// Children of the menu have this attribute to signal which menu they belong to.\nexport const MENU_OWNER_ATTR = \"data-roving-menu-parent\";\n\n// Children of the menu use this as their unique id (in the context of the menu).\nexport const MENU_VALUE_ATTR = \"data-roving-menu-value\";\n\n// Same as above, but for access via node.dataset\nexport const MENU_VALUE_JS = \"rovingMenuValue\";\n\n/**\n * Shared context for Menu <--> MenuItem coordination.\n *\n * @internal\n */\nexport const MenuStateContext = createContext<RovingMenuState | undefined>(undefined);\n\ndeclare const MENU_STATE_BRAND: unique symbol;\n\n/**\n * The menu state represents the internal state of the menu.\n *\n * The internals are currently hidden.\n *\n * @group Roving menu\n */\nexport interface RovingMenuState {\n [MENU_STATE_BRAND]: void;\n}\n\n/**\n * Checked cast for the internal menu state.\n * @internal\n */\nexport function getInternalState(menuState: RovingMenuState): InternalMenuState {\n if (!(menuState instanceof InternalMenuState)) {\n throw new Error(\"Invalid menu state.\");\n }\n return menuState as unknown as InternalMenuState;\n}\n\n/**\n * Internal model class; created by the menu and accessible by the menu items.\n *\n * @internal\n */\nexport class InternalMenuState {\n #menuRef: RefObject<HTMLElement | null>;\n\n // Value of the currently active item\n #current = reactive<string | undefined>();\n\n readonly menuId: string;\n readonly orientation: \"horizontal\" | \"vertical\";\n\n constructor(\n menuId: string,\n orientation: \"horizontal\" | \"vertical\",\n menuRef: RefObject<HTMLElement | null>\n ) {\n this.#menuRef = menuRef;\n this.menuId = menuId;\n this.orientation = orientation;\n }\n\n get #currentValue(): string | undefined {\n return this.#current.value;\n }\n\n /**\n * Returns true if the menu item with the given value is currently active.\n *\n * The active menu item can receive the focus via keyboard navigation (tab).\n */\n isActive(value: string): boolean {\n return this.#currentValue === value;\n }\n\n /**\n * Called when a key is pressed on the menu root element.\n */\n onKeyDown(event: KeyboardEvent): void {\n const direction = getNavDirection(event, this.orientation);\n if (!direction) {\n return;\n }\n\n const items = getMenuItems(this.#menuRef, this.menuId);\n const target = getFocusTarget(items, this.#currentValue, direction);\n if (!target) {\n if (items.length > 1) {\n LOG.warn(\"Failed to identify focus target for keyboard navigation\");\n }\n return;\n }\n\n event.preventDefault();\n this.#navigateToItem(target);\n target.focus();\n }\n\n /**\n * Called by items when they mount.\n *\n * This will activate the first item to ensure that at least one item is focusable.\n */\n onItemMount(value: string): void {\n if (!this.#currentValue) {\n this.#activateItem(value);\n }\n }\n\n /**\n * Called by items when they unmount.\n * Also used to handle the case when an item becomes disabled while focused.\n *\n * This will move the active value to another item to ensure that one item is focusable.\n */\n onItemUnmount(value: string, hadFocus: boolean): void {\n if (!this.isActive(value)) {\n return;\n }\n\n this.#navigateToNextFocusableItem(value, hadFocus);\n }\n\n /**\n * Called by items when they receive focus.\n *\n * This is redundant when using arrow key navigation, but its important if\n * the item receives focus from the outside (e.g. programmatically) to sync it\n * with the menu's state.\n */\n onItemFocus(value: string): void {\n this.#activateItem(value);\n }\n\n #navigateToNextFocusableItem(disabledValue: string, hadFocus: boolean): void {\n const items = getMenuItems(this.#menuRef, this.menuId, true, false);\n const disabledIndex = findItemIndex(items, disabledValue);\n\n let target;\n if (disabledIndex === -1) {\n target = getFocusTarget(items, -1, \"home\");\n } else {\n // Attempt to move focus to something sensible\n target =\n getFocusTarget(items, disabledIndex, \"forward\", false) ??\n getFocusTarget(items, disabledIndex, \"backward\", false);\n }\n\n if (target) {\n this.#navigateToItem(target);\n if (hadFocus) {\n LOG.debug(\"Restoring focus within menu to\", target);\n requestAnimationFrame(() => target.focus());\n }\n return;\n }\n this.#activateItem(undefined);\n }\n\n #navigateToItem(target: HTMLElement) {\n const value = getItemValue(target);\n if (!value) {\n LOG.warn(\"Menu item without a value\", value);\n return;\n }\n this.#activateItem(value);\n }\n\n #activateItem(value: string | undefined): void {\n this.#current.value = value;\n }\n}\n\ntype NavDirection = \"backward\" | \"forward\" | \"home\" | \"end\";\n\nfunction getNavDirection(\n event: KeyboardEvent,\n orientation: \"horizontal\" | \"vertical\"\n): NavDirection | undefined {\n switch (event.key) {\n case \"ArrowUp\": {\n if (orientation === \"vertical\") {\n return \"backward\";\n }\n break;\n }\n case \"ArrowDown\": {\n if (orientation === \"vertical\") {\n return \"forward\";\n }\n break;\n }\n case \"ArrowLeft\": {\n if (orientation === \"horizontal\") {\n return \"backward\";\n }\n break;\n }\n case \"ArrowRight\": {\n if (orientation === \"horizontal\") {\n return \"forward\";\n }\n break;\n }\n case \"Home\":\n return \"home\";\n case \"End\":\n return \"end\";\n }\n}\n\nfunction getFocusTarget(\n items: HTMLElement[],\n current: string | number | undefined,\n direction: NavDirection,\n wrap = true\n): HTMLElement | undefined {\n if (items.length === 0) {\n return undefined;\n }\n\n let currentIndex = -1;\n if (typeof current === \"number\") {\n currentIndex = current;\n } else if (typeof current === \"string\") {\n currentIndex = findItemIndex(items, current);\n }\n\n if (currentIndex === -1 || direction === \"home\") {\n const index = items.findIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n if (direction === \"end\") {\n const index = items.findLastIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n\n if (direction === \"forward\") {\n for (let nextIndex = currentIndex + 1; ; ) {\n if (nextIndex >= items.length) {\n if (!wrap) {\n break;\n }\n nextIndex = 0;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex += 1;\n }\n } else {\n for (let nextIndex = currentIndex - 1; ; ) {\n if (nextIndex < 0) {\n if (!wrap) {\n break;\n }\n nextIndex = items.length - 1;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex -= 1;\n }\n }\n return undefined;\n}\n\nfunction findItemIndex(items: HTMLElement[], value: string) {\n return items.findIndex((item) => getItemValue(item) === value);\n}\n\nfunction getMenuItems(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n unmount = false,\n excludeDisabled = true\n): HTMLElement[] {\n const owner = menuRef.current;\n if (!owner) {\n if (unmount) {\n // List may no longer be in the DOM\n return [];\n } else {\n throw new Error(\n \"Menu parent not found in DOM. Was the 'ref' prop applied correctly to the container?\"\n );\n }\n }\n\n const escapedId = CSS.escape(menuId);\n let selector = `[${MENU_OWNER_ATTR}=${escapedId}]`;\n if (excludeDisabled) {\n selector += \":not([disabled],[aria-disabled='true'])\";\n }\n const children = owner.querySelectorAll(selector);\n return Array.from(children) as HTMLElement[];\n}\n\nfunction getItemValue(item: HTMLElement | undefined): string | undefined {\n return item?.dataset[MENU_VALUE_JS] ?? undefined;\n}\n\nfunction isDisabled(item: HTMLElement): boolean {\n // eslint-disable-next-line @typescript-eslint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EAAO;AACR,IAAA,IAAI,OAAA,EAAS;AAET,MAAA,OAAO,EAAC;AAAA,IACZ,CAAA,MAAO;AACH,MAAA,MAAM,IAAI,KAAA;AAAA,QACN;AAAA,OACJ;AAAA,IACJ;AAAA,EACJ;AAEA,EAAA,MAAM,SAAA,GAAY,GAAA,CAAI,MAAA,CAAO,MAAM,CAAA;AACnC,EAAA,IAAI,QAAA,GAAW,CAAA,CAAA,EAAI,eAAe,CAAA,CAAA,EAAI,SAAS,CAAA,CAAA,CAAA;AAC/C,EAAA,IAAI,eAAA,EAAiB;AACjB,IAAA,QAAA,IAAY,yCAAA;AAAA,EAChB;AACA,EAAA,MAAM,QAAA,GAAW,KAAA,CAAM,gBAAA,CAAiB,QAAQ,CAAA;AAChD,EAAA,OAAO,KAAA,CAAM,KAAK,QAAQ,CAAA;AAC9B;AAEA,SAAS,aAAa,IAAA,EAAmD;AACrE,EAAA,OAAO,IAAA,EAAM,OAAA,CAAQ,aAAa,CAAA,IAAK,MAAA;AAC3C;AAEA,SAAS,WAAW,IAAA,EAA4B;AAE5C,EAAA,OAAO,CAAC,CAAE,IAAA,CAAa,QAAA,IAAY,KAAK,YAAA,KAAiB,MAAA;AAC7D;;;;"}
|
|
1
|
+
{"version":3,"file":"RovingMenuState.js","sources":["RovingMenuState.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Reactive, reactive } from \"@conterra/reactivity-core\";\nimport { createLogger } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { createContext, KeyboardEvent, FocusEvent, RefObject } from \"react\";\n\nconst LOG = createLogger(sourceId);\n\n// Mostly for debugging parent/child links. Not used for lookups.\nexport const MENU_ID_ATTR = \"data-roving-menu-id\";\n\n// Children of the menu have this attribute to signal which menu they belong to.\nexport const MENU_OWNER_ATTR = \"data-roving-menu-parent\";\n\n// Children of the menu use this as their unique id (in the context of the menu).\nexport const MENU_VALUE_ATTR = \"data-roving-menu-value\";\n\n// Same as above, but for access via node.dataset\nexport const MENU_VALUE_JS = \"rovingMenuValue\";\n\nconst NOT_DISABLED_SELECTOR = \":not([disabled],[aria-disabled='true'])\";\n\n/**\n * Shared context for Menu <--> MenuItem coordination.\n *\n * @internal\n */\nexport const MenuStateContext = createContext<RovingMenuState | undefined>(undefined);\n\ndeclare const MENU_STATE_BRAND: unique symbol;\n\n/**\n * The menu state represents the internal state of the menu.\n *\n * The internals are currently hidden.\n *\n * @group Roving menu\n */\nexport interface RovingMenuState {\n [MENU_STATE_BRAND]: void;\n}\n\n/**\n * Checked cast for the internal menu state.\n * @internal\n */\nexport function getInternalState(menuState: RovingMenuState): InternalMenuState {\n if (!(menuState instanceof InternalMenuState)) {\n throw new Error(\"Invalid menu state.\");\n }\n return menuState as unknown as InternalMenuState;\n}\n\nexport interface MenuStateOptions {\n menuId: string;\n orientation: \"horizontal\" | \"vertical\";\n menuRef: RefObject<HTMLElement | null>;\n wrap: boolean;\n active: boolean;\n isActiveInParent?: () => boolean;\n}\n\n/**\n * Internal model class; created by the menu and accessible by the menu items.\n *\n * @internal\n */\nexport class InternalMenuState {\n #active: Reactive<boolean>;\n\n // True if the menu is currently active in its parent menu (used for nesting).\n // Note: reactive function\n #isActiveInParent: () => boolean;\n\n #menuRef: RefObject<HTMLElement | null>;\n\n // Value of the currently active item\n #current = reactive<string | undefined>();\n\n readonly menuId: string;\n readonly orientation: \"horizontal\" | \"vertical\";\n readonly wrap: boolean;\n\n constructor(options: MenuStateOptions) {\n this.menuId = options.menuId;\n this.wrap = options.wrap;\n this.orientation = options.orientation;\n this.#active = reactive(options.active);\n this.#menuRef = options.menuRef;\n this.#isActiveInParent = options.isActiveInParent ?? (() => true);\n }\n\n get active(): boolean {\n return this.#active.value;\n }\n\n set active(value: boolean) {\n this.#active.value = value;\n }\n\n get #currentValue(): string | undefined {\n return this.#current.value;\n }\n\n /**\n * Returns true if the menu item with the given value is currently active.\n *\n * The active menu item can receive the focus via keyboard navigation (tab).\n */\n isActive(value: string): boolean {\n return this.active && this.#isActiveInParent() && this.#currentValue === value;\n }\n\n /**\n * Called when the menu itself is focused.\n * The focus handler moves focus to the active child.\n *\n * This is used to implement nested menus.\n */\n onFocus(e: FocusEvent) {\n if (e.target !== e.currentTarget) {\n return; // Focus event from child\n }\n\n const activeValue = this.#currentValue;\n if (activeValue != null) {\n const item = getMenuItem(this.#menuRef, this.menuId, activeValue);\n if (item) {\n LOG.debug(\"Forwarding menu focus to item\", item);\n item.focus();\n } else {\n LOG.debug(\"Failed to find menu item for active value\");\n }\n }\n }\n\n /**\n * Called when a key is pressed on the menu root element.\n */\n onKeyDown(event: KeyboardEvent): void {\n if (event.defaultPrevented) {\n return; // Keyboard event from child\n }\n\n const direction = getNavDirection(event, this.orientation);\n if (!direction) {\n return;\n }\n\n const items = getMenuItems(this.#menuRef, this.menuId);\n const target = getFocusTarget(items, this.#currentValue, direction, this.wrap);\n if (!target) {\n // No candidate in the requested direction.\n return;\n }\n\n event.preventDefault();\n this.#navigateToItem(target);\n target.focus();\n }\n\n /**\n * Called by items when they mount.\n *\n * This will activate the first item to ensure that at least one item is focusable.\n */\n onItemMount(value: string): void {\n if (!this.#currentValue) {\n this.#activateItem(value);\n }\n }\n\n /**\n * Called by items when they unmount.\n * Also used to handle the case when an item becomes disabled while focused.\n *\n * This will move the active value to another item to ensure that one item is focusable.\n */\n onItemUnmount(value: string, hadFocus: boolean): void {\n if (!this.isActive(value)) {\n return;\n }\n\n this.#navigateToNextFocusableItem(value, hadFocus);\n }\n\n /**\n * Called by items when they receive focus.\n *\n * This is redundant when using arrow key navigation, but its important if\n * the item receives focus from the outside (e.g. programmatically) to sync it\n * with the menu's state.\n */\n onItemFocus(value: string): void {\n this.#activateItem(value);\n }\n\n #navigateToNextFocusableItem(disabledValue: string, hadFocus: boolean): void {\n const items = getMenuItems(this.#menuRef, this.menuId, true, false);\n const disabledIndex = findItemIndex(items, disabledValue);\n\n let target;\n if (disabledIndex === -1) {\n target = getFocusTarget(items, -1, \"home\", false);\n } else {\n // Attempt to move focus to something sensible\n target =\n getFocusTarget(items, disabledIndex, \"forward\", false) ??\n getFocusTarget(items, disabledIndex, \"backward\", false);\n }\n\n if (target) {\n this.#navigateToItem(target);\n if (hadFocus) {\n LOG.debug(\"Restoring focus within menu to\", target);\n requestAnimationFrame(() => target.focus());\n }\n return;\n }\n this.#activateItem(undefined);\n }\n\n #navigateToItem(target: HTMLElement) {\n const value = getItemValue(target);\n if (!value) {\n LOG.warn(\"Menu item without a value\", value);\n return;\n }\n this.#activateItem(value);\n }\n\n #activateItem(value: string | undefined): void {\n this.#current.value = value;\n }\n}\n\ntype NavDirection = \"backward\" | \"forward\" | \"home\" | \"end\";\n\nfunction getNavDirection(\n event: KeyboardEvent,\n orientation: \"horizontal\" | \"vertical\"\n): NavDirection | undefined {\n switch (event.key) {\n case \"ArrowUp\": {\n if (orientation === \"vertical\") {\n return \"backward\";\n }\n break;\n }\n case \"ArrowDown\": {\n if (orientation === \"vertical\") {\n return \"forward\";\n }\n break;\n }\n case \"ArrowLeft\": {\n if (orientation === \"horizontal\") {\n return \"backward\";\n }\n break;\n }\n case \"ArrowRight\": {\n if (orientation === \"horizontal\") {\n return \"forward\";\n }\n break;\n }\n case \"Home\":\n return \"home\";\n case \"End\":\n return \"end\";\n }\n}\n\nfunction getFocusTarget(\n items: HTMLElement[],\n current: string | number | undefined,\n direction: NavDirection,\n wrap: boolean\n): HTMLElement | undefined {\n if (items.length === 0) {\n return undefined;\n }\n\n let currentIndex = -1;\n if (typeof current === \"number\") {\n currentIndex = current;\n } else if (typeof current === \"string\") {\n currentIndex = findItemIndex(items, current);\n }\n\n if (currentIndex === -1 || direction === \"home\") {\n const index = items.findIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n if (direction === \"end\") {\n const index = items.findLastIndex((item) => !isDisabled(item));\n const el = items[index];\n return el;\n }\n\n if (direction === \"forward\") {\n for (let nextIndex = currentIndex + 1; ; ) {\n if (nextIndex >= items.length) {\n if (!wrap) {\n break;\n }\n nextIndex = 0;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex += 1;\n }\n } else {\n for (let nextIndex = currentIndex - 1; ; ) {\n if (nextIndex < 0) {\n if (!wrap) {\n break;\n }\n nextIndex = items.length - 1;\n }\n if (nextIndex === currentIndex) {\n break;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const item = items[nextIndex]!;\n if (!isDisabled(item)) {\n return item;\n }\n nextIndex -= 1;\n }\n }\n return undefined;\n}\n\nfunction findItemIndex(items: HTMLElement[], value: string) {\n return items.findIndex((item) => getItemValue(item) === value);\n}\n\nfunction getMenuItems(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n unmount = false,\n excludeDisabled = true\n): HTMLElement[] {\n const owner = menuRef.current;\n if (!owner) {\n if (unmount) {\n // List may no longer be in the DOM\n return [];\n } else {\n throw new Error(\n \"Menu parent not found in DOM. Was the 'ref' prop applied correctly to the container?\"\n );\n }\n }\n\n const escapedId = CSS.escape(menuId);\n let selector = `[${MENU_OWNER_ATTR}=${escapedId}]`;\n if (excludeDisabled) {\n selector += NOT_DISABLED_SELECTOR;\n }\n const children = owner.querySelectorAll(selector);\n return Array.from(children) as HTMLElement[];\n}\n\nfunction getMenuItem(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n value: string\n): HTMLElement | undefined {\n const owner = menuRef.current;\n if (!owner) {\n return undefined;\n }\n\n const escapedMenuId = CSS.escape(menuId);\n const escapedValue = CSS.escape(value);\n const node = owner.querySelector(\n `[${MENU_OWNER_ATTR}=${escapedMenuId}][${MENU_VALUE_ATTR}=${escapedValue}]${NOT_DISABLED_SELECTOR}`\n );\n return (node ?? undefined) as HTMLElement | undefined;\n}\n\nfunction getItemValue(item: HTMLElement | undefined): string | undefined {\n return item?.dataset[MENU_VALUE_JS] ?? undefined;\n}\n\nfunction isDisabled(item: HTMLElement): boolean {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return !!(item as any).disabled || item.ariaDisabled === \"true\";\n}\n"],"names":[],"mappings":";;;;;AAOA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAG1B,MAAM,YAAA,GAAe;AAGrB,MAAM,eAAA,GAAkB;AAGxB,MAAM,eAAA,GAAkB;AAGxB,MAAM,aAAA,GAAgB,iBAAA;AAE7B,MAAM,qBAAA,GAAwB,yCAAA;AAOvB,MAAM,gBAAA,GAAmB,cAA2C,MAAS;AAmB7E,SAAS,iBAAiB,SAAA,EAA+C;AAC5E,EAAA,IAAI,EAAE,qBAAqB,iBAAA,CAAA,EAAoB;AAC3C,IAAA,MAAM,IAAI,MAAM,qBAAqB,CAAA;AAAA,EACzC;AACA,EAAA,OAAO,SAAA;AACX;AAgBO,MAAM,iBAAA,CAAkB;AAAA,EAC3B,OAAA;AAAA;AAAA;AAAA,EAIA,iBAAA;AAAA,EAEA,QAAA;AAAA;AAAA,EAGA,WAAW,QAAA,EAA6B;AAAA,EAE/B,MAAA;AAAA,EACA,WAAA;AAAA,EACA,IAAA;AAAA,EAET,YAAY,OAAA,EAA2B;AACnC,IAAA,IAAA,CAAK,SAAS,OAAA,CAAQ,MAAA;AACtB,IAAA,IAAA,CAAK,OAAO,OAAA,CAAQ,IAAA;AACpB,IAAA,IAAA,CAAK,cAAc,OAAA,CAAQ,WAAA;AAC3B,IAAA,IAAA,CAAK,OAAA,GAAU,QAAA,CAAS,OAAA,CAAQ,MAAM,CAAA;AACtC,IAAA,IAAA,CAAK,WAAW,OAAA,CAAQ,OAAA;AACxB,IAAA,IAAA,CAAK,iBAAA,GAAoB,OAAA,CAAQ,gBAAA,KAAqB,MAAM,IAAA,CAAA;AAAA,EAChE;AAAA,EAEA,IAAI,MAAA,GAAkB;AAClB,IAAA,OAAO,KAAK,OAAA,C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;;"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { InternalMenuState } from "./RovingMenuState";
|
|
2
|
+
export declare function useMenuState(): InternalMenuState;
|
|
3
|
+
export declare function useMenuState(required: true): InternalMenuState;
|
|
4
|
+
export declare function useMenuState(required: boolean): InternalMenuState | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { MenuStateContext, getInternalState } from './RovingMenuState.js';
|
|
3
|
+
|
|
4
|
+
function useMenuState(required = true) {
|
|
5
|
+
const state = useContext(MenuStateContext);
|
|
6
|
+
if (!state) {
|
|
7
|
+
if (required) {
|
|
8
|
+
throw new Error(
|
|
9
|
+
"Failed to find the outer menu. Is the menu item surrounded by the RovingMenuRoot?"
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
return getInternalState(state);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { useMenuState };
|
|
18
|
+
//# sourceMappingURL=useMenuState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMenuState.js","sources":["useMenuState.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { useContext } from \"react\";\nimport { InternalMenuState, MenuStateContext, getInternalState } from \"./RovingMenuState\";\n\nexport function useMenuState(): InternalMenuState;\nexport function useMenuState(required: true): InternalMenuState;\nexport function useMenuState(required: boolean): InternalMenuState | undefined;\nexport function useMenuState(required = true): InternalMenuState | undefined {\n const state = useContext(MenuStateContext);\n if (!state) {\n if (required) {\n throw new Error(\n \"Failed to find the outer menu. Is the menu item surrounded by the RovingMenuRoot?\"\n );\n }\n return undefined;\n }\n return getInternalState(state);\n}\n"],"names":[],"mappings":";;;AAQO,SAAS,YAAA,CAAa,WAAW,IAAA,EAAqC;AACzE,EAAA,MAAM,KAAA,GAAQ,WAAW,gBAAgB,CAAA;AACzC,EAAA,IAAI,CAAC,KAAA,EAAO;AACR,IAAA,IAAI,QAAA,EAAU;AACV,MAAA,MAAM,IAAI,KAAA;AAAA,QACN;AAAA,OACJ;AAAA,IACJ;AACA,IAAA,OAAO,MAAA;AAAA,EACX;AACA,EAAA,OAAO,iBAAiB,KAAK,CAAA;AACjC;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { KeyboardEvent, RefObject } from "react";
|
|
1
|
+
import { KeyboardEvent, FocusEvent, RefObject } from "react";
|
|
2
2
|
import { MENU_ID_ATTR, RovingMenuState } from "./RovingMenuState";
|
|
3
|
+
import { RovingMenuItemDomProps } from "./useRovingMenuItem";
|
|
3
4
|
/**
|
|
4
5
|
* Properties supported when creating a new menu via {@link useRovingMenu}.
|
|
5
6
|
*
|
|
@@ -12,6 +13,29 @@ export interface RovingMenuProps {
|
|
|
12
13
|
* Horizontal menus can be navigated using left/right, vertical menus use up/down.
|
|
13
14
|
*/
|
|
14
15
|
orientation?: "vertical" | "horizontal";
|
|
16
|
+
/**
|
|
17
|
+
* Wrap around when pressing an arrow key at the start or the end of the menu.
|
|
18
|
+
*
|
|
19
|
+
* Default: `true`.
|
|
20
|
+
*/
|
|
21
|
+
wrap?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether the menu is active.
|
|
24
|
+
* Only active menus are focusable by the user through keyboard navigation.
|
|
25
|
+
*
|
|
26
|
+
* Default: `true`
|
|
27
|
+
*/
|
|
28
|
+
active?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** @internal */
|
|
31
|
+
export interface RovingMenuDomProps {
|
|
32
|
+
ref: RefObject<any>;
|
|
33
|
+
role: string;
|
|
34
|
+
tabIndex: number;
|
|
35
|
+
[MENU_ID_ATTR]: string;
|
|
36
|
+
"aria-orientation": "horizontal" | "vertical";
|
|
37
|
+
onKeyDown: (event: KeyboardEvent) => void;
|
|
38
|
+
onFocus: (event: FocusEvent) => void;
|
|
15
39
|
}
|
|
16
40
|
/**
|
|
17
41
|
* The return value of {@link useRovingMenu}.
|
|
@@ -24,13 +48,7 @@ export interface RovingMenuResult {
|
|
|
24
48
|
*
|
|
25
49
|
* Note that other properties may be added in a future release.
|
|
26
50
|
*/
|
|
27
|
-
menuProps:
|
|
28
|
-
ref: RefObject<any>;
|
|
29
|
-
role: string;
|
|
30
|
-
[MENU_ID_ATTR]: string;
|
|
31
|
-
"aria-orientation": "horizontal" | "vertical";
|
|
32
|
-
onKeyDown: (event: KeyboardEvent) => void;
|
|
33
|
-
};
|
|
51
|
+
menuProps: RovingMenuDomProps;
|
|
34
52
|
/**
|
|
35
53
|
* Properties that should be passed to the {@link RovingMenuRoot}.
|
|
36
54
|
*
|
|
@@ -48,3 +66,31 @@ export interface RovingMenuResult {
|
|
|
48
66
|
* @expandType RovingMenuProps
|
|
49
67
|
*/
|
|
50
68
|
export declare function useRovingMenu(props?: RovingMenuProps): RovingMenuResult;
|
|
69
|
+
/**
|
|
70
|
+
* Properties supported when creating a new nested menu via {@link useNestedRovingMenu}.
|
|
71
|
+
*
|
|
72
|
+
* @group Roving menu
|
|
73
|
+
*/
|
|
74
|
+
export interface NestedRovingMenuProps extends RovingMenuProps {
|
|
75
|
+
/**
|
|
76
|
+
* The value of the menu within its parent menu.
|
|
77
|
+
*/
|
|
78
|
+
value: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The return value of {@link useNestedRovingMenu}.
|
|
82
|
+
*
|
|
83
|
+
* @group Roving menu
|
|
84
|
+
*/
|
|
85
|
+
export interface NestedRovingMenuResult extends RovingMenuResult {
|
|
86
|
+
menuProps: RovingMenuDomProps & RovingMenuItemDomProps;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Like {@link useRovingMenu}, but suitable for nested menus.
|
|
90
|
+
*
|
|
91
|
+
* Only a single level of nested is supported at this time (i.e. horizontal in vertical, or the other way around).
|
|
92
|
+
*
|
|
93
|
+
* @group Roving menu
|
|
94
|
+
* @expandType NestedRovingMenuProps
|
|
95
|
+
*/
|
|
96
|
+
export declare function useNestedRovingMenu(props: NestedRovingMenuProps): NestedRovingMenuResult;
|
|
@@ -1,36 +1,75 @@
|
|
|
1
|
-
import { useId, useRef,
|
|
1
|
+
import { useCallback, useMemo, useId, useRef, useEffect } from 'react';
|
|
2
2
|
import { useEvent } from '../useEvent.js';
|
|
3
3
|
import { InternalMenuState, MENU_ID_ATTR } from './RovingMenuState.js';
|
|
4
|
+
import { useRovingMenuItemImpl } from './useRovingMenuItem.js';
|
|
5
|
+
import { useMenuState } from './useMenuState.js';
|
|
4
6
|
|
|
5
7
|
function useRovingMenu(props = {}) {
|
|
6
|
-
|
|
8
|
+
return useRovingMenuImpl(props);
|
|
9
|
+
}
|
|
10
|
+
function useNestedRovingMenu(props) {
|
|
11
|
+
const parentMenuState = useMenuState();
|
|
12
|
+
const { menuProps, menuState } = useRovingMenuImpl(
|
|
13
|
+
props,
|
|
14
|
+
useCallback(() => parentMenuState.isActive(props.value), [parentMenuState, props.value])
|
|
15
|
+
);
|
|
16
|
+
const { itemProps } = useRovingMenuItemImpl(props, "nested");
|
|
17
|
+
return useMemo(() => {
|
|
18
|
+
return {
|
|
19
|
+
menuProps: {
|
|
20
|
+
...menuProps,
|
|
21
|
+
...itemProps,
|
|
22
|
+
onFocus(e) {
|
|
23
|
+
itemProps.onFocus(e);
|
|
24
|
+
menuProps.onFocus(e);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
menuState
|
|
28
|
+
};
|
|
29
|
+
}, [menuProps, menuState, itemProps]);
|
|
30
|
+
}
|
|
31
|
+
function useRovingMenuImpl(props, isActiveInParent) {
|
|
32
|
+
const { orientation = "horizontal", active = true, wrap = true } = props;
|
|
7
33
|
const menuId = useId();
|
|
8
34
|
const menuRef = useRef(null);
|
|
35
|
+
const initialActive = useRef(active);
|
|
9
36
|
const state = useMemo(
|
|
10
|
-
() => new InternalMenuState(
|
|
11
|
-
|
|
37
|
+
() => new InternalMenuState({
|
|
38
|
+
menuId,
|
|
39
|
+
orientation,
|
|
40
|
+
menuRef,
|
|
41
|
+
wrap,
|
|
42
|
+
active: initialActive.current,
|
|
43
|
+
isActiveInParent
|
|
44
|
+
}),
|
|
45
|
+
[menuRef, orientation, menuId, wrap, isActiveInParent]
|
|
12
46
|
);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
state.active = active;
|
|
49
|
+
}, [state, active]);
|
|
13
50
|
const onKeyDown = useEvent((event) => {
|
|
14
|
-
if (event.defaultPrevented) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
51
|
state.onKeyDown(event);
|
|
18
52
|
});
|
|
53
|
+
const onFocus = useEvent((event) => {
|
|
54
|
+
state.onFocus(event);
|
|
55
|
+
});
|
|
19
56
|
const result = useMemo(
|
|
20
57
|
() => ({
|
|
21
58
|
menuProps: {
|
|
22
59
|
ref: menuRef,
|
|
23
60
|
role: "toolbar",
|
|
24
61
|
"aria-orientation": orientation,
|
|
62
|
+
tabIndex: -1,
|
|
25
63
|
[MENU_ID_ATTR]: menuId,
|
|
26
|
-
onKeyDown
|
|
64
|
+
onKeyDown,
|
|
65
|
+
onFocus
|
|
27
66
|
},
|
|
28
67
|
menuState: state
|
|
29
68
|
}),
|
|
30
|
-
[menuId, orientation, onKeyDown, state]
|
|
69
|
+
[menuId, orientation, onKeyDown, onFocus, state]
|
|
31
70
|
);
|
|
32
71
|
return result;
|
|
33
72
|
}
|
|
34
73
|
|
|
35
|
-
export { useRovingMenu };
|
|
74
|
+
export { useNestedRovingMenu, useRovingMenu };
|
|
36
75
|
//# sourceMappingURL=useRovingMenu.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useRovingMenu.js","sources":["useRovingMenu.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { KeyboardEvent, RefObject, useId, useMemo, useRef } from \"react\";\nimport { useEvent } from \"../useEvent\";\nimport { type RovingMenuRoot } from \"./RovingMenuRoot\";\nimport { InternalMenuState, MENU_ID_ATTR, RovingMenuState } from \"./RovingMenuState\";\n\n/**\n * Properties supported when creating a new menu via {@link useRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuProps {\n /**\n * Configures the orientation of the menu (default: `horizontal`).\n *\n * Horizontal menus can be navigated using left/right, vertical menus use up/down.\n */\n orientation?: \"vertical\" | \"horizontal\";\n
|
|
1
|
+
{"version":3,"file":"useRovingMenu.js","sources":["useRovingMenu.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { KeyboardEvent, FocusEvent, RefObject, useCallback, useId, useMemo, useRef } from \"react\";\nimport { useEvent } from \"../useEvent\";\nimport { type RovingMenuRoot } from \"./RovingMenuRoot\";\nimport { InternalMenuState, MENU_ID_ATTR, RovingMenuState } from \"./RovingMenuState\";\nimport { RovingMenuItemDomProps, useRovingMenuItemImpl } from \"./useRovingMenuItem\";\nimport { useMenuState } from \"./useMenuState\";\nimport { useEffect } from \"react\";\n\n/**\n * Properties supported when creating a new menu via {@link useRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuProps {\n /**\n * Configures the orientation of the menu (default: `horizontal`).\n *\n * Horizontal menus can be navigated using left/right, vertical menus use up/down.\n */\n orientation?: \"vertical\" | \"horizontal\";\n\n /**\n * Wrap around when pressing an arrow key at the start or the end of the menu.\n *\n * Default: `true`.\n */\n wrap?: boolean;\n\n /**\n * Whether the menu is active.\n * Only active menus are focusable by the user through keyboard navigation.\n *\n * Default: `true`\n */\n active?: boolean;\n}\n\n/** @internal */\nexport interface RovingMenuDomProps {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ref: RefObject<any>;\n role: string;\n tabIndex: number;\n [MENU_ID_ATTR]: string;\n \"aria-orientation\": \"horizontal\" | \"vertical\";\n onKeyDown: (event: KeyboardEvent) => void;\n onFocus: (event: FocusEvent) => void;\n}\n\n/**\n * The return value of {@link useRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuResult {\n /**\n * DOM-Properties that must be applied to the menu's dom element.\n *\n * Note that other properties may be added in a future release.\n */\n menuProps: RovingMenuDomProps;\n\n /**\n * Properties that should be passed to the {@link RovingMenuRoot}.\n *\n * The specific internals of this object are an implementation detail.\n */\n menuState: RovingMenuState;\n}\n\n/**\n * The roving menu is a render-less component for lists that support keyboard navigation.\n * It implements the basics for an accessible, keyboard navigable menu using the \"roving tab index\" pattern.\n *\n * Items can be navigated using arrow keys and home/end keys.\n *\n * @group Roving menu\n * @expandType RovingMenuProps\n */\nexport function useRovingMenu(props: RovingMenuProps = {}): RovingMenuResult {\n return useRovingMenuImpl(props);\n}\n\n/**\n * Properties supported when creating a new nested menu via {@link useNestedRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface NestedRovingMenuProps extends RovingMenuProps {\n /**\n * The value of the menu within its parent menu.\n */\n value: string;\n}\n\n/**\n * The return value of {@link useNestedRovingMenu}.\n *\n * @group Roving menu\n */\nexport interface NestedRovingMenuResult extends RovingMenuResult {\n menuProps: RovingMenuDomProps & RovingMenuItemDomProps;\n}\n\n/**\n * Like {@link useRovingMenu}, but suitable for nested menus.\n *\n * Only a single level of nested is supported at this time (i.e. horizontal in vertical, or the other way around).\n *\n * @group Roving menu\n * @expandType NestedRovingMenuProps\n */\nexport function useNestedRovingMenu(props: NestedRovingMenuProps): NestedRovingMenuResult {\n const parentMenuState = useMenuState();\n const { menuProps, menuState } = useRovingMenuImpl(\n props,\n useCallback(() => parentMenuState.isActive(props.value), [parentMenuState, props.value])\n );\n const { itemProps } = useRovingMenuItemImpl(props, \"nested\");\n\n return useMemo(() => {\n return {\n menuProps: {\n ...menuProps,\n ...itemProps,\n onFocus(e: FocusEvent) {\n // Tracks active state in parent\n itemProps.onFocus(e);\n\n // Focuses the correct child\n menuProps.onFocus(e);\n }\n },\n menuState\n };\n }, [menuProps, menuState, itemProps]);\n}\n\nfunction useRovingMenuImpl(\n props: RovingMenuProps,\n isActiveInParent?: () => boolean\n): RovingMenuResult {\n const { orientation = \"horizontal\", active = true, wrap = true } = props;\n const menuId = useId();\n const menuRef = useRef<HTMLElement>(null);\n\n const initialActive = useRef(active);\n\n // Shared state between items and root\n const state = useMemo(\n () =>\n new InternalMenuState({\n menuId,\n orientation,\n menuRef,\n wrap,\n active: initialActive.current,\n isActiveInParent\n }),\n [menuRef, orientation, menuId, wrap, isActiveInParent]\n );\n\n // Fine grained updates without losing state\n useEffect(() => {\n state.active = active;\n }, [state, active]);\n\n const onKeyDown = useEvent((event: KeyboardEvent) => {\n state.onKeyDown(event);\n });\n const onFocus = useEvent((event: FocusEvent) => {\n state.onFocus(event);\n });\n const result = useMemo(\n (): RovingMenuResult => ({\n menuProps: {\n ref: menuRef,\n role: \"toolbar\",\n \"aria-orientation\": orientation,\n tabIndex: -1,\n [MENU_ID_ATTR]: menuId,\n onKeyDown,\n onFocus\n },\n menuState: state as unknown as RovingMenuState\n }),\n [menuId, orientation, onKeyDown, onFocus, state]\n );\n return result;\n}\n"],"names":[],"mappings":";;;;;;AAiFO,SAAS,aAAA,CAAc,KAAA,GAAyB,EAAC,EAAqB;AACzE,EAAA,OAAO,kBAAkB,KAAK,CAAA;AAClC;AA+BO,SAAS,oBAAoB,KAAA,EAAsD;AACtF,EAAA,MAAM,kBAAkB,YAAA,EAAa;AACrC,EAAA,MAAM,EAAE,SAAA,EAAW,SAAA,EAAU,GAAI,iBAAA;AAAA,IAC7B,KAAA;AAAA,IACA,WAAA,CAAY,MAAM,eAAA,CAAgB,QAAA,CAAS,KAAA,CAAM,KAAK,CAAA,EAAG,CAAC,eAAA,EAAiB,KAAA,CAAM,KAAK,CAAC;AAAA,GAC3F;AACA,EAAA,MAAM,EAAE,SAAA,EAAU,GAAI,qBAAA,CAAsB,OAAO,QAAQ,CAAA;AAE3D,EAAA,OAAO,QAAQ,MA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AA,CAAM,QAAQ,KAAK,CAAA;AAAA,EACvB,CAAC,CAAA;AACD,EAAA,MAAM,MAAA,GAAS,OAAA;AAAA,IACX,OAAyB;AAAA,MACrB,SAAA,EAAW;AAAA,QACP,GAAA,EAAK,OAAA;AAAA,QACL,IAAA,EAAM,SAAA;AAAA,QACN,kBAAA,EAAoB,WAAA;AAAA,QACpB,QAAA,EAAU,EAAA;AAAA,QACV,CAAC,YAAY,GAAG,MAAA;AAAA,QAChB,SAAA;AAAA,QACA;AAAA,OACJ;AAAA,MACA,SAAA,EAAW;AAAA,KACf,CAAA;AAAA,IACA,CAAC,MAAA,EAAQ,WAAA,EAAa,SAAA,EAAW,SAAS,KAAK;AAAA,GACnD;AACA,EAAA,OAAO,MAAA;AACX;;;;"}
|
|
@@ -27,6 +27,14 @@ export interface RovingMenuItemProps {
|
|
|
27
27
|
*/
|
|
28
28
|
required?: boolean;
|
|
29
29
|
}
|
|
30
|
+
/** @internal */
|
|
31
|
+
export interface RovingMenuItemDomProps {
|
|
32
|
+
[MENU_OWNER_ATTR]: string;
|
|
33
|
+
[MENU_VALUE_ATTR]: string;
|
|
34
|
+
tabIndex: number;
|
|
35
|
+
onFocus: FocusEventHandler;
|
|
36
|
+
onBlur: FocusEventHandler;
|
|
37
|
+
}
|
|
30
38
|
/**
|
|
31
39
|
* The return value of {@link useRovingMenuItem}.
|
|
32
40
|
*
|
|
@@ -41,13 +49,7 @@ export interface RovingMenuItemResult {
|
|
|
41
49
|
*
|
|
42
50
|
* Note that other properties may be added in a future release.
|
|
43
51
|
*/
|
|
44
|
-
itemProps:
|
|
45
|
-
[MENU_OWNER_ATTR]: string;
|
|
46
|
-
[MENU_VALUE_ATTR]: string;
|
|
47
|
-
tabIndex: number;
|
|
48
|
-
onFocus: FocusEventHandler;
|
|
49
|
-
onBlur: FocusEventHandler;
|
|
50
|
-
};
|
|
52
|
+
itemProps: RovingMenuItemDomProps;
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
55
|
* Declares a new menu item using the given `props`.
|
|
@@ -58,3 +60,10 @@ export declare function useRovingMenuItem(props: RovingMenuItemProps & {
|
|
|
58
60
|
required?: true;
|
|
59
61
|
}): RovingMenuItemResult;
|
|
60
62
|
export declare function useRovingMenuItem(props: RovingMenuItemProps): RovingMenuItemResult | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
export declare function useRovingMenuItemImpl(props: RovingMenuItemProps & {
|
|
67
|
+
required?: true;
|
|
68
|
+
}, context: "item" | "nested"): RovingMenuItemResult;
|
|
69
|
+
export declare function useRovingMenuItemImpl(props: RovingMenuItemProps, context: "item" | "nested"): RovingMenuItemResult | undefined;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { useReactiveSnapshot } from '@open-pioneer/reactivity';
|
|
2
|
-
import { useRef, useLayoutEffect, useMemo
|
|
3
|
-
import { MENU_VALUE_ATTR, MENU_OWNER_ATTR
|
|
2
|
+
import { useRef, useLayoutEffect, useMemo } from 'react';
|
|
3
|
+
import { MENU_VALUE_ATTR, MENU_OWNER_ATTR } from './RovingMenuState.js';
|
|
4
|
+
import { useMenuState } from './useMenuState.js';
|
|
4
5
|
|
|
5
6
|
function useRovingMenuItem(props) {
|
|
7
|
+
return useRovingMenuItemImpl(props, "item");
|
|
8
|
+
}
|
|
9
|
+
function useRovingMenuItemImpl(props, context) {
|
|
6
10
|
const { value, disabled = false, required = true } = props;
|
|
7
11
|
const state = useMenuState(required);
|
|
8
12
|
const hasFocus = useRef(false);
|
|
@@ -24,7 +28,8 @@ function useRovingMenuItem(props) {
|
|
|
24
28
|
itemProps: {
|
|
25
29
|
[MENU_OWNER_ATTR]: menuId,
|
|
26
30
|
[MENU_VALUE_ATTR]: value,
|
|
27
|
-
|
|
31
|
+
// items can be reached via user tab, nested menus can't
|
|
32
|
+
tabIndex: context === "item" && isActive ? 0 : -1,
|
|
28
33
|
onFocus: (_event) => {
|
|
29
34
|
hasFocus.current = true;
|
|
30
35
|
state?.onItemFocus(value);
|
|
@@ -34,21 +39,9 @@ function useRovingMenuItem(props) {
|
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
41
|
};
|
|
37
|
-
}, [state, value, isActive]);
|
|
42
|
+
}, [state, value, isActive, context]);
|
|
38
43
|
return result;
|
|
39
44
|
}
|
|
40
|
-
function useMenuState(required) {
|
|
41
|
-
const state = useContext(MenuStateContext);
|
|
42
|
-
if (!state) {
|
|
43
|
-
if (required) {
|
|
44
|
-
throw new Error(
|
|
45
|
-
"Failed to find the outer menu. Is the menu item surrounded by the RovingMenuRoot?"
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
return void 0;
|
|
49
|
-
}
|
|
50
|
-
return getInternalState(state);
|
|
51
|
-
}
|
|
52
45
|
|
|
53
|
-
export { useRovingMenuItem };
|
|
46
|
+
export { useRovingMenuItem, useRovingMenuItemImpl };
|
|
54
47
|
//# sourceMappingURL=useRovingMenuItem.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useRovingMenuItem.js","sources":["useRovingMenuItem.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { useReactiveSnapshot } from \"@open-pioneer/reactivity\";\nimport { FocusEventHandler,
|
|
1
|
+
{"version":3,"file":"useRovingMenuItem.js","sources":["useRovingMenuItem.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { useReactiveSnapshot } from \"@open-pioneer/reactivity\";\nimport { FocusEventHandler, useLayoutEffect, useMemo, useRef } from \"react\";\nimport { MENU_OWNER_ATTR, MENU_VALUE_ATTR } from \"./RovingMenuState\";\nimport { useMenuState } from \"./useMenuState\";\n\n/**\n * Properties support when creating a new menu item via {@link useRovingMenuItem}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuItemProps {\n /**\n * A unique value for the current item (in the context of this menu).\n */\n value: string;\n\n /**\n * Set this to true if your item is disabled.\n *\n * This tells the menu that the item cannot be focused.\n */\n disabled?: boolean;\n\n /**\n * By default, a menu item parent is required.\n *\n * Set this to `false` to make the parent optional.\n * The hook will become inactive and will return `undefined` if no parent is present.\n * This is useful for components that may or may not be used within a menu.\n *\n * Default: `true`\n */\n required?: boolean;\n}\n\n/** @internal */\nexport interface RovingMenuItemDomProps {\n [MENU_OWNER_ATTR]: string;\n [MENU_VALUE_ATTR]: string;\n tabIndex: number;\n onFocus: FocusEventHandler;\n onBlur: FocusEventHandler;\n}\n\n/**\n * The return value of {@link useRovingMenuItem}.\n *\n * @group Roving menu\n */\nexport interface RovingMenuItemResult {\n /**\n * DOM-Properties that must be applied to the menu item's dom element.\n *\n * Most importantly, this will apply the item's tab index and data attributes\n * that allow the menu to find the item in the DOM.\n *\n * Note that other properties may be added in a future release.\n */\n itemProps: RovingMenuItemDomProps;\n}\n\n/**\n * Declares a new menu item using the given `props`.\n * @group Roving menu\n * @expandType RovingMenuItemProps\n */\nexport function useRovingMenuItem(\n props: RovingMenuItemProps & { required?: true }\n): RovingMenuItemResult;\nexport function useRovingMenuItem(props: RovingMenuItemProps): RovingMenuItemResult | undefined;\nexport function useRovingMenuItem(props: RovingMenuItemProps): RovingMenuItemResult | undefined {\n return useRovingMenuItemImpl(props, \"item\");\n}\n\n/**\n * @internal\n */\nexport function useRovingMenuItemImpl(\n props: RovingMenuItemProps & { required?: true },\n context: \"item\" | \"nested\"\n): RovingMenuItemResult;\nexport function useRovingMenuItemImpl(\n props: RovingMenuItemProps,\n context: \"item\" | \"nested\"\n): RovingMenuItemResult | undefined;\nexport function useRovingMenuItemImpl(\n props: RovingMenuItemProps,\n context: \"item\" | \"nested\"\n): RovingMenuItemResult | undefined {\n const { value, disabled = false, required = true } = props;\n const state = useMenuState(required);\n\n const hasFocus = useRef(false);\n\n useLayoutEffect(() => {\n if (!state || disabled) {\n return;\n }\n state.onItemMount(value);\n return () => state.onItemUnmount(value, hasFocus.current);\n }, [state, value, disabled]);\n\n const isActiveValue = useReactiveSnapshot(() => state?.isActive(value), [state, value]);\n const isActive = !disabled && isActiveValue;\n const result = useMemo((): RovingMenuItemResult | undefined => {\n if (!state) {\n return undefined;\n }\n const menuId = state.menuId;\n return {\n itemProps: {\n [MENU_OWNER_ATTR]: menuId,\n [MENU_VALUE_ATTR]: value,\n\n // items can be reached via user tab, nested menus can't\n tabIndex: context === \"item\" && isActive ? 0 : -1,\n\n onFocus: (_event) => {\n hasFocus.current = true;\n state?.onItemFocus(value);\n },\n onBlur: (_event) => {\n hasFocus.current = false;\n }\n }\n };\n }, [state, value, isActive, context]);\n return result;\n}\n"],"names":[],"mappings":";;;;;AAwEO,SAAS,kBAAkB,KAAA,EAA8D;AAC5F,EAAA,OAAO,qBAAA,CAAsB,OAAO,MAAM,CAAA;AAC9C;AAaO,SAAS,qBAAA,CACZ,OACA,OAAA,EACgC;AAChC,EAAA,MAAM,EAAE,KAAA,EAAO,QAAA,GAAW,KAAA,EAAO,QAAA,GAAW,MAAK,GAAI,KAAA;AACrD,EAAA,MAAM,KAAA,GAAQ,aAAa,QAAQ,CAAA;AAEnC,EAAA,MAAM,QAAA,GAAW,OAAO,KAAK,CAAA;AAE7B,EAAA,eAAA,CAAgB,MAAM;AAClB,IAAA,IAAI,CAAC,SAAS,QAAA,EAAU;AACpB,MAAA;AAAA,IACJ;AACA,IAAA,KAAA,CAAM,YAAY,KAAK,CAAA;AACvB,IAAA,OAAO,MAAM,KAAA,CAAM,aAAA,CAAc,KAAA,EAAO,SAAS,OAAO,CAAA;AAAA,EAC5D,CAAA,EAAG,CAAC,KAAA,EAAO,KAAA,EAAO,QAAQ,CAAC,CAAA;AAE3B,EAAA,MAAM,aAAA,GAAgB,mBAAA,CAAoB,MAAM,KAAA,EAAO,QAAA,CAAS,KAAK,CAAA,EAAG,CAAC,KAAA,EAAO,KAAK,CAAC,CAAA;AACtF,EAAA,MAAM,Q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