@open-pioneer/react-utils 4.5.0-dev.20260211105402 → 4.5.0-dev.20260225121659
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 +4 -3
- package/README.md +11 -0
- package/package.json +3 -3
- package/roving-menu/RovingMenuState.d.ts +7 -2
- package/roving-menu/RovingMenuState.js +41 -28
- package/roving-menu/RovingMenuState.js.map +1 -1
- package/roving-menu/useRovingMenuItem.d.ts +1 -0
- package/roving-menu/useRovingMenuItem.js +10 -4
- 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.20260225121659
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
@@ -23,9 +23,10 @@
|
|
|
23
23
|
|
|
24
24
|
### Patch Changes
|
|
25
25
|
|
|
26
|
+
- bb3f515: roving-menu - Move focus correctly when an item unmounts while it is active.
|
|
26
27
|
- Updated dependencies [4e76893]
|
|
27
|
-
- @open-pioneer/core@4.5.0-dev.
|
|
28
|
-
- @open-pioneer/reactivity@4.5.0-dev.
|
|
28
|
+
- @open-pioneer/core@4.5.0-dev.20260225121659
|
|
29
|
+
- @open-pioneer/reactivity@4.5.0-dev.20260225121659
|
|
29
30
|
|
|
30
31
|
## 4.4.0
|
|
31
32
|
|
package/README.md
CHANGED
|
@@ -101,6 +101,17 @@ function MenuItem(props: { value: string }) {
|
|
|
101
101
|
Use the `disabled` prop of `useRovingMenuItem` to indicate that your menu item is currently disabled.
|
|
102
102
|
Disabled items will lose focus and will be skipped during keyboard navigation.
|
|
103
103
|
|
|
104
|
+
To mark your items as disabled for your users, prefer `aria-disabled` over HTML's `disabled` attribute.
|
|
105
|
+
The `disabled` attribute completely disables focus and any browser events, which makes moving the focus to a neighboring item difficult to implement.
|
|
106
|
+
|
|
107
|
+
#### Unmounting / hiding menu items
|
|
108
|
+
|
|
109
|
+
`display: none` in combination with a `disabled: true` in `useRovingMenuItem` works well.
|
|
110
|
+
As an alternative, you can also completely unmount your component from a parent of your menu item.
|
|
111
|
+
|
|
112
|
+
Note that returning `null` or `undefined` from your component will interfere with focus handling.
|
|
113
|
+
You should always render your menu item's dom node, for as long as your item exists.
|
|
114
|
+
|
|
104
115
|
#### Limitations
|
|
105
116
|
|
|
106
117
|
- Items must be focusable HTML elements, such as buttons.
|
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.20260225121659",
|
|
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/core": "4.5.0-dev.
|
|
22
|
-
"@open-pioneer/reactivity": "4.5.0-dev.
|
|
21
|
+
"@open-pioneer/core": "4.5.0-dev.20260225121659",
|
|
22
|
+
"@open-pioneer/reactivity": "4.5.0-dev.20260225121659"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
25
|
"./package.json": "./package.json",
|
|
@@ -35,7 +35,11 @@ export declare class InternalMenuState {
|
|
|
35
35
|
readonly menuId: string;
|
|
36
36
|
readonly orientation: "horizontal" | "vertical";
|
|
37
37
|
constructor(menuId: string, orientation: "horizontal" | "vertical", menuRef: RefObject<HTMLElement | null>);
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Returns true if the menu item with the given value is currently active.
|
|
40
|
+
*
|
|
41
|
+
* The active menu item can receive the focus via keyboard navigation (tab).
|
|
42
|
+
*/
|
|
39
43
|
isActive(value: string): boolean;
|
|
40
44
|
/**
|
|
41
45
|
* Called when a key is pressed on the menu root element.
|
|
@@ -49,10 +53,11 @@ export declare class InternalMenuState {
|
|
|
49
53
|
onItemMount(value: string): void;
|
|
50
54
|
/**
|
|
51
55
|
* Called by items when they unmount.
|
|
56
|
+
* Also used to handle the case when an item becomes disabled while focused.
|
|
52
57
|
*
|
|
53
58
|
* This will move the active value to another item to ensure that one item is focusable.
|
|
54
59
|
*/
|
|
55
|
-
onItemUnmount(value: string): void;
|
|
60
|
+
onItemUnmount(value: string, hadFocus: boolean): void;
|
|
56
61
|
/**
|
|
57
62
|
* Called by items when they receive focus.
|
|
58
63
|
*
|
|
@@ -17,6 +17,7 @@ function getInternalState(menuState) {
|
|
|
17
17
|
}
|
|
18
18
|
class InternalMenuState {
|
|
19
19
|
#menuRef;
|
|
20
|
+
// Value of the currently active item
|
|
20
21
|
#current = reactive();
|
|
21
22
|
menuId;
|
|
22
23
|
orientation;
|
|
@@ -25,11 +26,16 @@ class InternalMenuState {
|
|
|
25
26
|
this.menuId = menuId;
|
|
26
27
|
this.orientation = orientation;
|
|
27
28
|
}
|
|
28
|
-
get
|
|
29
|
+
get #currentValue() {
|
|
29
30
|
return this.#current.value;
|
|
30
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns true if the menu item with the given value is currently active.
|
|
34
|
+
*
|
|
35
|
+
* The active menu item can receive the focus via keyboard navigation (tab).
|
|
36
|
+
*/
|
|
31
37
|
isActive(value) {
|
|
32
|
-
return this
|
|
38
|
+
return this.#currentValue === value;
|
|
33
39
|
}
|
|
34
40
|
/**
|
|
35
41
|
* Called when a key is pressed on the menu root element.
|
|
@@ -40,9 +46,11 @@ class InternalMenuState {
|
|
|
40
46
|
return;
|
|
41
47
|
}
|
|
42
48
|
const items = getMenuItems(this.#menuRef, this.menuId);
|
|
43
|
-
const target = getFocusTarget(items, this
|
|
49
|
+
const target = getFocusTarget(items, this.#currentValue, direction);
|
|
44
50
|
if (!target) {
|
|
45
|
-
|
|
51
|
+
if (items.length > 1) {
|
|
52
|
+
LOG.warn("Failed to identify focus target for keyboard navigation");
|
|
53
|
+
}
|
|
46
54
|
return;
|
|
47
55
|
}
|
|
48
56
|
event.preventDefault();
|
|
@@ -55,49 +63,50 @@ class InternalMenuState {
|
|
|
55
63
|
* This will activate the first item to ensure that at least one item is focusable.
|
|
56
64
|
*/
|
|
57
65
|
onItemMount(value) {
|
|
58
|
-
if (!this
|
|
66
|
+
if (!this.#currentValue) {
|
|
59
67
|
this.#activateItem(value);
|
|
60
68
|
}
|
|
61
69
|
}
|
|
62
70
|
/**
|
|
63
71
|
* Called by items when they unmount.
|
|
72
|
+
* Also used to handle the case when an item becomes disabled while focused.
|
|
64
73
|
*
|
|
65
74
|
* This will move the active value to another item to ensure that one item is focusable.
|
|
66
75
|
*/
|
|
67
|
-
onItemUnmount(value) {
|
|
76
|
+
onItemUnmount(value, hadFocus) {
|
|
68
77
|
if (!this.isActive(value)) {
|
|
69
78
|
return;
|
|
70
79
|
}
|
|
80
|
+
this.#navigateToNextFocusableItem(value, hadFocus);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Called by items when they receive focus.
|
|
84
|
+
*
|
|
85
|
+
* This is redundant when using arrow key navigation, but its important if
|
|
86
|
+
* the item receives focus from the outside (e.g. programmatically) to sync it
|
|
87
|
+
* with the menu's state.
|
|
88
|
+
*/
|
|
89
|
+
onItemFocus(value) {
|
|
90
|
+
this.#activateItem(value);
|
|
91
|
+
}
|
|
92
|
+
#navigateToNextFocusableItem(disabledValue, hadFocus) {
|
|
71
93
|
const items = getMenuItems(this.#menuRef, this.menuId, true, false);
|
|
72
|
-
const disabledIndex = findItemIndex(items,
|
|
94
|
+
const disabledIndex = findItemIndex(items, disabledValue);
|
|
73
95
|
let target;
|
|
74
|
-
let focus;
|
|
75
96
|
if (disabledIndex === -1) {
|
|
76
97
|
target = getFocusTarget(items, -1, "home");
|
|
77
98
|
} else {
|
|
78
|
-
const disabledItem = items[disabledIndex];
|
|
79
|
-
focus = hasFocus(disabledItem);
|
|
80
99
|
target = getFocusTarget(items, disabledIndex, "forward", false) ?? getFocusTarget(items, disabledIndex, "backward", false);
|
|
81
100
|
}
|
|
82
101
|
if (target) {
|
|
83
102
|
this.#navigateToItem(target);
|
|
84
|
-
if (
|
|
103
|
+
if (hadFocus) {
|
|
85
104
|
LOG.debug("Restoring focus within menu to", target);
|
|
86
105
|
requestAnimationFrame(() => target.focus());
|
|
87
106
|
}
|
|
88
|
-
|
|
89
|
-
this.#activateItem(void 0);
|
|
107
|
+
return;
|
|
90
108
|
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Called by items when they receive focus.
|
|
94
|
-
*
|
|
95
|
-
* This is redundant when using arrow key navigation, but its important if
|
|
96
|
-
* the item receives focus from the outside (e.g. programmatically) to sync it
|
|
97
|
-
* with the menu's state.
|
|
98
|
-
*/
|
|
99
|
-
onItemFocus(value) {
|
|
100
|
-
this.#activateItem(value);
|
|
109
|
+
this.#activateItem(void 0);
|
|
101
110
|
}
|
|
102
111
|
#navigateToItem(target) {
|
|
103
112
|
const value = getItemValue(target);
|
|
@@ -144,6 +153,9 @@ function getNavDirection(event, orientation) {
|
|
|
144
153
|
}
|
|
145
154
|
}
|
|
146
155
|
function getFocusTarget(items, current, direction, wrap = true) {
|
|
156
|
+
if (items.length === 0) {
|
|
157
|
+
return void 0;
|
|
158
|
+
}
|
|
147
159
|
let currentIndex = -1;
|
|
148
160
|
if (typeof current === "number") {
|
|
149
161
|
currentIndex = current;
|
|
@@ -151,10 +163,14 @@ function getFocusTarget(items, current, direction, wrap = true) {
|
|
|
151
163
|
currentIndex = findItemIndex(items, current);
|
|
152
164
|
}
|
|
153
165
|
if (currentIndex === -1 || direction === "home") {
|
|
154
|
-
|
|
166
|
+
const index = items.findIndex((item) => !isDisabled(item));
|
|
167
|
+
const el = items[index];
|
|
168
|
+
return el;
|
|
155
169
|
}
|
|
156
170
|
if (direction === "end") {
|
|
157
|
-
|
|
171
|
+
const index = items.findLastIndex((item) => !isDisabled(item));
|
|
172
|
+
const el = items[index];
|
|
173
|
+
return el;
|
|
158
174
|
}
|
|
159
175
|
if (direction === "forward") {
|
|
160
176
|
for (let nextIndex = currentIndex + 1; ; ) {
|
|
@@ -221,9 +237,6 @@ function getItemValue(item) {
|
|
|
221
237
|
function isDisabled(item) {
|
|
222
238
|
return !!item.disabled || item.ariaDisabled === "true";
|
|
223
239
|
}
|
|
224
|
-
function hasFocus(item) {
|
|
225
|
-
return item.getRootNode().activeElement === item;
|
|
226
|
-
}
|
|
227
240
|
|
|
228
241
|
export { InternalMenuState, MENU_ID_ATTR, MENU_OWNER_ATTR, MENU_VALUE_ATTR, MenuStateContext, getInternalState };
|
|
229
242
|
//# sourceMappingURL=RovingMenuState.js.map
|
|
@@ -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 { RootNode } from \"@open-pioneer/runtime\";\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 #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 current(): string | undefined {\n return this.#current.value;\n }\n\n isActive(value: string): boolean {\n return this.current === 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.current, direction);\n if (!target) {\n LOG.warn(\"Failed to identify focus target for keyboard navigation\");\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.current) {\n this.#activateItem(value);\n }\n }\n\n /**\n * Called by items when they unmount.\n *\n * This will move the active value to another item to ensure that one item is focusable.\n */\n onItemUnmount(value: string): void {\n if (!this.isActive(value)) {\n return;\n }\n\n const items = getMenuItems(this.#menuRef, this.menuId, true, false);\n const disabledIndex = findItemIndex(items, value);\n\n let target;\n let focus;\n if (disabledIndex === -1) {\n target = getFocusTarget(items, -1, \"home\");\n } else {\n // Attempt to move focus to something sensible\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const disabledItem = items[disabledIndex]!;\n\n // NOTE: focus change requires not using 'disabled' but 'aria-disabled' instead\n focus = hasFocus(disabledItem);\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 (focus) {\n LOG.debug(\"Restoring focus within menu to\", target);\n requestAnimationFrame(() => target.focus());\n }\n } else {\n this.#activateItem(undefined);\n }\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 #navigateToItem(target: HTMLElement) {\n const value = getItemValue(target);\n if (!value) {\n LOG.warn(\"Menu item without a value\", value);\n return;\n }\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 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 return items.find((item) => !isDisabled(item));\n }\n if (direction === \"end\") {\n return items.findLast((item) => !isDisabled(item));\n }\n\n if (direction === \"forward\") {\n for (let nextIndex = currentIndex + 1; ; ) {\n if (nextIndex >= items.length) {\n if (!wrap) {\n break;\n }\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\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\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/no-explicit-any\n return !!(item as any).disabled || item.ariaDisabled === \"true\";\n}\n\nfunction hasFocus(item: HTMLElement): boolean {\n return (item.getRootNode() as RootNode).activeElement === item;\n}\n"],"names":[],"mappings":";;;;;AAQA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAG1B,MAAM,YAAA,GAAe;AAGrB,MAAM,eAAA,GAAkB;AAGxB,MAAM,eAAA,GAAkB;AAGxB,MAAM,aAAA,GAAgB,iBAAA;AAOtB,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;AAOO,MAAM,iBAAA,CAAkB;AAAA,EAC3B,QAAA;AAAA,EACA,WAAW,QAAA,EAA6B;AAAA,EAE/B,MAAA;AAAA,EACA,WAAA;AAAA,EAET,WAAA,CACI,MAAA,EACA,WAAA,EACA,OAAA,EACF;AACE,IAAA,IAAA,CAAK,QAAA,GAAW,OAAA;AAChB,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,IAAA,IAAA,CAAK,WAAA,GAAc,WAAA;AAAA,EACvB;AAAA,EAEA,IAAI,OAAA,GAA8B;AAC9B,IAAA,OAAO,KAAK,QAAA,CAAS,KAAA;AAAA,EACzB;AAAA,EAEA,SAAS,KAAA,EAAwB;AAC7B,IAAA,OAAO,KAAK,OAAA,KAAY,KAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,KAAA,EAA4B;AAClC,IAAA,MAAM,SAAA,GAAY,eAAA,CAAgB,KAAA,EAAO,IAAA,CAAK,WAAW,CAAA;AACzD,IAAA,IAAI,CAAC,SAAA,EAAW;AACZ,MAAA;AAAA,IACJ;AAEA,IAAA,MAAM,KAAA,GAAQ,YAAA,CAAa,IAAA,CAAK,QAAA,EAAU,K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,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;AAEA,SAAS,SAAS,IAAA,EAA4B;AAC1C,EAAA,OAAQ,IAAA,CAAK,WAAA,EAAY,CAAe,aAAA,KAAkB,IAAA;AAC9D;;;;"}
|
|
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/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;AAOtB,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;AAOO,MAAM,iBAAA,CAAkB;AAAA,EAC3B,QAAA;AAAA;AAAA,EAGA,WAAW,QAAA,EAA6B;AAAA,EAE/B,MAAA;AAAA,EACA,WAAA;AAAA,EAET,WAAA,CACI,MAAA,EACA,WAAA,EACA,OAAA,EACF;AACE,IAAA,IAAA,CAAK,QAAA,GAAW,OAAA;AAChB,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,IAAA,IAAA,CAAK,WAAA,GAAc,WAAA;AAAA,EACvB;AAAA,EAEA,IAAI,aAAA,GAAoC;AACpC,IAAA,OAAO,KAAK,QAAA,CAAS,KAAA;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,SAAS,KAAA,EAAwB;AAC7B,IAAA,OAAO,KAAK,aAAA,KAAkB,KAAA;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,KAAA,EAA4B;AAClC,IAAA,MAAM,SAAA,GAAY,eAAA,CAAgB,KAAA,EAAO,IAAA,CAAK,WAAW,CAAA;AACzD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,aACL,OAAA,EACA,MAAA,EACA,OAAA,GAAU,KAAA,EACV,kBAAkB,IAAA,EACL;AACb,EAAA,MAAM,QAAQ,OAAA,CAAQ,OAAA;AACtB,EAAA,IAAI,CAAC,KAAA,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,18 +1,20 @@
|
|
|
1
1
|
import { useReactiveSnapshot } from '@open-pioneer/reactivity';
|
|
2
|
-
import {
|
|
2
|
+
import { useRef, useLayoutEffect, useMemo, useContext } from 'react';
|
|
3
3
|
import { MENU_VALUE_ATTR, MENU_OWNER_ATTR, MenuStateContext, getInternalState } from './RovingMenuState.js';
|
|
4
4
|
|
|
5
5
|
function useRovingMenuItem(props) {
|
|
6
6
|
const { value, disabled = false, required = true } = props;
|
|
7
7
|
const state = useMenuState(required);
|
|
8
|
-
|
|
8
|
+
const hasFocus = useRef(false);
|
|
9
|
+
useLayoutEffect(() => {
|
|
9
10
|
if (!state || disabled) {
|
|
10
11
|
return;
|
|
11
12
|
}
|
|
12
13
|
state.onItemMount(value);
|
|
13
|
-
return () => state.onItemUnmount(value);
|
|
14
|
+
return () => state.onItemUnmount(value, hasFocus.current);
|
|
14
15
|
}, [state, value, disabled]);
|
|
15
|
-
const
|
|
16
|
+
const isActiveValue = useReactiveSnapshot(() => state?.isActive(value), [state, value]);
|
|
17
|
+
const isActive = !disabled && isActiveValue;
|
|
16
18
|
const result = useMemo(() => {
|
|
17
19
|
if (!state) {
|
|
18
20
|
return void 0;
|
|
@@ -24,7 +26,11 @@ function useRovingMenuItem(props) {
|
|
|
24
26
|
[MENU_VALUE_ATTR]: value,
|
|
25
27
|
tabIndex: isActive ? 0 : -1,
|
|
26
28
|
onFocus: (_event) => {
|
|
29
|
+
hasFocus.current = true;
|
|
27
30
|
state?.onItemFocus(value);
|
|
31
|
+
},
|
|
32
|
+
onBlur: (_event) => {
|
|
33
|
+
hasFocus.current = false;
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
36
|
};
|
|
@@ -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, useContext,
|
|
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, useContext, useLayoutEffect, useMemo, useRef } from \"react\";\nimport {\n getInternalState,\n InternalMenuState,\n MENU_OWNER_ATTR,\n MENU_VALUE_ATTR,\n MenuStateContext\n} from \"./RovingMenuState\";\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/**\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: {\n [MENU_OWNER_ATTR]: string;\n [MENU_VALUE_ATTR]: string;\n tabIndex: number;\n onFocus: FocusEventHandler;\n onBlur: FocusEventHandler;\n };\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 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 tabIndex: isActive ? 0 : -1,\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]);\n return result;\n}\n\nfunction useMenuState(required: boolean): 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":";;;;AA0EO,SAAS,kBAAkB,KAAA,EAA8D;AAC5F,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,QAAA,GAAW,CAAC,QAAA,IAAY,aAAA;AAC9B,EAAA,MAAM,MAAA,GAAS,QAAQ,MAAwC;AAC3D,IAAA,IAAI,CAAC,KAAA,EAAO;AACR,MAAA,OAAO,MAAA;AAAA,IACX;AACA,IAAA,MAAM,SAAS,KAAA,C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