@open-pioneer/react-utils 4.5.0-dev-roving-menu.20260129100701 → 4.5.0-dev-async-chakra-config.20260203104545
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
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# @open-pioneer/react-utils
|
|
2
2
|
|
|
3
|
-
## 4.5.0-dev-
|
|
3
|
+
## 4.5.0-dev-async-chakra-config.20260203104545
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- 604a589: Provide an implementation of the roving tab index pattern for menus.
|
|
8
8
|
This roving menu can be used to implement accessible menus with support for keyboard navigation.
|
|
9
9
|
|
|
10
10
|
### Patch Changes
|
|
11
11
|
|
|
12
|
-
- @open-pioneer/
|
|
13
|
-
- @open-pioneer/
|
|
12
|
+
- @open-pioneer/reactivity@4.5.0-dev-async-chakra-config.20260203104545
|
|
13
|
+
- @open-pioneer/core@4.5.0-dev-async-chakra-config.20260203104545
|
|
14
14
|
|
|
15
15
|
## 4.4.0
|
|
16
16
|
|
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-async-chakra-config.20260203104545",
|
|
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.3",
|
|
21
|
-
"@open-pioneer/core": "4.5.0-dev-
|
|
22
|
-
"@open-pioneer/reactivity": "4.5.0-dev-
|
|
21
|
+
"@open-pioneer/core": "4.5.0-dev-async-chakra-config.20260203104545",
|
|
22
|
+
"@open-pioneer/reactivity": "4.5.0-dev-async-chakra-config.20260203104545"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
25
|
"./package.json": "./package.json",
|
|
@@ -39,7 +39,7 @@ class InternalMenuState {
|
|
|
39
39
|
if (!direction) {
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
|
-
const items =
|
|
42
|
+
const items = getMenuItems(this.#menuRef, this.menuId);
|
|
43
43
|
const target = getFocusTarget(items, this.current, direction);
|
|
44
44
|
if (!target) {
|
|
45
45
|
LOG.warn("Failed to identify focus target for keyboard navigation");
|
|
@@ -65,14 +65,28 @@ class InternalMenuState {
|
|
|
65
65
|
* This will move the active value to another item to ensure that one item is focusable.
|
|
66
66
|
*/
|
|
67
67
|
onItemUnmount(value) {
|
|
68
|
-
if (this.isActive(value)) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
68
|
+
if (!this.isActive(value)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const items = getMenuItems(this.#menuRef, this.menuId, true, false);
|
|
72
|
+
const disabledIndex = findItemIndex(items, value);
|
|
73
|
+
let target;
|
|
74
|
+
let focus;
|
|
75
|
+
if (disabledIndex === -1) {
|
|
76
|
+
target = getFocusTarget(items, -1, "home");
|
|
77
|
+
} else {
|
|
78
|
+
const disabledItem = items[disabledIndex];
|
|
79
|
+
focus = hasFocus(disabledItem);
|
|
80
|
+
target = getFocusTarget(items, disabledIndex, "forward", false) ?? getFocusTarget(items, disabledIndex, "backward", false);
|
|
81
|
+
}
|
|
82
|
+
if (target) {
|
|
83
|
+
this.#navigateToItem(target);
|
|
84
|
+
if (focus) {
|
|
85
|
+
LOG.debug("Restoring focus within menu to", target);
|
|
86
|
+
requestAnimationFrame(() => target.focus());
|
|
75
87
|
}
|
|
88
|
+
} else {
|
|
89
|
+
this.#activateItem(void 0);
|
|
76
90
|
}
|
|
77
91
|
}
|
|
78
92
|
/**
|
|
@@ -129,32 +143,60 @@ function getNavDirection(event, orientation) {
|
|
|
129
143
|
return "end";
|
|
130
144
|
}
|
|
131
145
|
}
|
|
132
|
-
function getFocusTarget(items, current, direction) {
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
146
|
+
function getFocusTarget(items, current, direction, wrap = true) {
|
|
147
|
+
let currentIndex = -1;
|
|
148
|
+
if (typeof current === "number") {
|
|
149
|
+
currentIndex = current;
|
|
150
|
+
} else if (typeof current === "string") {
|
|
151
|
+
currentIndex = findItemIndex(items, current);
|
|
136
152
|
}
|
|
137
|
-
if (direction === "home") {
|
|
138
|
-
return items.
|
|
153
|
+
if (currentIndex === -1 || direction === "home") {
|
|
154
|
+
return items.find((item) => !isDisabled(item));
|
|
139
155
|
}
|
|
140
156
|
if (direction === "end") {
|
|
141
|
-
return items.
|
|
157
|
+
return items.findLast((item) => !isDisabled(item));
|
|
142
158
|
}
|
|
143
|
-
let nextIndex;
|
|
144
159
|
if (direction === "forward") {
|
|
145
|
-
nextIndex = currentIndex + 1;
|
|
146
|
-
|
|
147
|
-
|
|
160
|
+
for (let nextIndex = currentIndex + 1; ; ) {
|
|
161
|
+
if (nextIndex >= items.length) {
|
|
162
|
+
if (!wrap) {
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
nextIndex = 0;
|
|
166
|
+
}
|
|
167
|
+
if (nextIndex === currentIndex) {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
const item = items[nextIndex];
|
|
171
|
+
if (!isDisabled(item)) {
|
|
172
|
+
return item;
|
|
173
|
+
}
|
|
174
|
+
nextIndex += 1;
|
|
148
175
|
}
|
|
149
176
|
} else {
|
|
150
|
-
nextIndex = currentIndex - 1;
|
|
151
|
-
|
|
152
|
-
|
|
177
|
+
for (let nextIndex = currentIndex - 1; ; ) {
|
|
178
|
+
if (nextIndex < 0) {
|
|
179
|
+
if (!wrap) {
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
nextIndex = items.length - 1;
|
|
183
|
+
}
|
|
184
|
+
if (nextIndex === currentIndex) {
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
const item = items[nextIndex];
|
|
188
|
+
if (!isDisabled(item)) {
|
|
189
|
+
return item;
|
|
190
|
+
}
|
|
191
|
+
nextIndex -= 1;
|
|
153
192
|
}
|
|
154
193
|
}
|
|
155
|
-
return
|
|
194
|
+
return void 0;
|
|
195
|
+
}
|
|
196
|
+
function findItemIndex(items, value) {
|
|
197
|
+
return items.findIndex((item) => getItemValue(item) === value);
|
|
156
198
|
}
|
|
157
|
-
function
|
|
199
|
+
function getMenuItems(menuRef, menuId, unmount = false, excludeDisabled = true) {
|
|
158
200
|
const owner = menuRef.current;
|
|
159
201
|
if (!owner) {
|
|
160
202
|
if (unmount) {
|
|
@@ -166,12 +208,22 @@ function getFocusableItems(menuRef, menuId, unmount = false) {
|
|
|
166
208
|
}
|
|
167
209
|
}
|
|
168
210
|
const escapedId = CSS.escape(menuId);
|
|
169
|
-
|
|
211
|
+
let selector = `[${MENU_OWNER_ATTR}=${escapedId}]`;
|
|
212
|
+
if (excludeDisabled) {
|
|
213
|
+
selector += ":not([disabled],[aria-disabled='true'])";
|
|
214
|
+
}
|
|
215
|
+
const children = owner.querySelectorAll(selector);
|
|
170
216
|
return Array.from(children);
|
|
171
217
|
}
|
|
172
218
|
function getItemValue(item) {
|
|
173
219
|
return item?.dataset[MENU_VALUE_JS] ?? void 0;
|
|
174
220
|
}
|
|
221
|
+
function isDisabled(item) {
|
|
222
|
+
return !!item.disabled || item.ariaDisabled === "true";
|
|
223
|
+
}
|
|
224
|
+
function hasFocus(item) {
|
|
225
|
+
return item.getRootNode().activeElement === item;
|
|
226
|
+
}
|
|
175
227
|
|
|
176
228
|
export { InternalMenuState, MENU_ID_ATTR, MENU_OWNER_ATTR, MENU_VALUE_ATTR, MenuStateContext, getInternalState };
|
|
177
229
|
//# 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 { 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 = getFocusableItems(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 const items = getFocusableItems(this.#menuRef, this.menuId, true);\n const target = items[0];\n if (target) {\n this.#navigateToItem(target);\n } else {\n this.#activateItem(undefined);\n }\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 | undefined,\n direction: NavDirection\n): HTMLElement | undefined {\n const currentIndex = items.findIndex((item) => getItemValue(item) === current);\n if (currentIndex === -1) {\n // Some keyboard event from an unknown child?\n return items.at(0);\n }\n\n if (direction === \"home\") {\n return items.at(0);\n }\n if (direction === \"end\") {\n return items.at(-1);\n }\n\n let nextIndex;\n if (direction === \"forward\") {\n nextIndex = currentIndex + 1;\n if (nextIndex >= items.length) {\n nextIndex = 0;\n }\n } else {\n nextIndex = currentIndex - 1;\n if (nextIndex < 0) {\n nextIndex = items.length - 1;\n }\n }\n return items.at(nextIndex);\n}\n\nfunction getFocusableItems(\n menuRef: RefObject<HTMLElement | null>,\n menuId: string,\n unmount = false\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 const children = owner.querySelectorAll(`[${MENU_OWNER_ATTR}=${escapedId}]:not([disabled])`);\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"],"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,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AG;AACf,MAAA,SAAA,GAAY,MAAM,MAAA,GAAS,CAAA;AAAA,IAC/B;AAAA,EACJ;AACA,EAAA,OAAO,KAAA,CAAM,GAAG,SAAS,CAAA;AAC7B;AAEA,SAAS,iBAAA,CACL,OAAA,EACA,MAAA,EACA,OAAA,GAAU,KAAA,EACG;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,MAAM,WAAW,KAAA,CAAM,gBAAA,CAAiB,IAAI,eAAe,CAAA,CAAA,EAAI,SAAS,CAAA,iBAAA,CAAmB,CAAA;AAC3F,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;;;;"}
|
|
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;A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