@open-pioneer/react-utils 4.5.0-dev-roving-menu.20260224142901 → 4.5.0-dev-hook-timing.20260305145815
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 +7 -6
- package/package.json +3 -3
- package/roving-menu/RovingMenuState.d.ts +12 -1
- package/roving-menu/RovingMenuState.js +21 -13
- package/roving-menu/RovingMenuState.js.map +1 -1
- package/roving-menu/useRovingMenu.d.ts +13 -0
- package/roving-menu/useRovingMenu.js +16 -5
- package/roving-menu/useRovingMenu.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-hook-timing.20260305145815
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
@@ -17,17 +17,18 @@
|
|
|
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
|
-
-
|
|
20
|
+
- 8df39a2: Roving Menu - Add support for nested menus
|
|
21
21
|
- d523ade: Implement `mergeChakraProps` helper to merge multiple props objects.
|
|
22
|
-
- 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").
|
|
23
23
|
This roving menu can be used to implement accessible menus with support for keyboard navigation.
|
|
24
24
|
|
|
25
25
|
### Patch Changes
|
|
26
26
|
|
|
27
|
-
- bb3f515:
|
|
27
|
+
- bb3f515: Roving Menu -Move focus correctly when an item unmounts while it is active.
|
|
28
28
|
- Updated dependencies [4e76893]
|
|
29
|
-
|
|
30
|
-
- @open-pioneer/
|
|
29
|
+
- Updated dependencies [4d1b9ed]
|
|
30
|
+
- @open-pioneer/core@4.5.0-dev-hook-timing.20260305145815
|
|
31
|
+
- @open-pioneer/reactivity@4.5.0-dev-hook-timing.20260305145815
|
|
31
32
|
|
|
32
33
|
## 4.4.0
|
|
33
34
|
|
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-hook-timing.20260305145815",
|
|
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-hook-timing.20260305145815",
|
|
22
|
+
"@open-pioneer/reactivity": "4.5.0-dev-hook-timing.20260305145815"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
25
|
"./package.json": "./package.json",
|
|
@@ -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,7 +42,10 @@ 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
|
*
|
|
@@ -17,18 +17,29 @@ function getInternalState(menuState) {
|
|
|
17
17
|
return menuState;
|
|
18
18
|
}
|
|
19
19
|
class InternalMenuState {
|
|
20
|
-
|
|
20
|
+
#active;
|
|
21
|
+
// True if the menu is currently active in its parent menu (used for nesting).
|
|
22
|
+
// Note: reactive function
|
|
21
23
|
#isActiveInParent;
|
|
22
24
|
#menuRef;
|
|
23
25
|
// Value of the currently active item
|
|
24
26
|
#current = reactive();
|
|
25
27
|
menuId;
|
|
26
28
|
orientation;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this
|
|
30
|
-
this.
|
|
31
|
-
this.orientation = orientation;
|
|
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;
|
|
32
43
|
}
|
|
33
44
|
get #currentValue() {
|
|
34
45
|
return this.#current.value;
|
|
@@ -39,7 +50,7 @@ class InternalMenuState {
|
|
|
39
50
|
* The active menu item can receive the focus via keyboard navigation (tab).
|
|
40
51
|
*/
|
|
41
52
|
isActive(value) {
|
|
42
|
-
return this.#isActiveInParent() && this.#currentValue === value;
|
|
53
|
+
return this.active && this.#isActiveInParent() && this.#currentValue === value;
|
|
43
54
|
}
|
|
44
55
|
/**
|
|
45
56
|
* Called when the menu itself is focused.
|
|
@@ -74,11 +85,8 @@ class InternalMenuState {
|
|
|
74
85
|
return;
|
|
75
86
|
}
|
|
76
87
|
const items = getMenuItems(this.#menuRef, this.menuId);
|
|
77
|
-
const target = getFocusTarget(items, this.#currentValue, direction);
|
|
88
|
+
const target = getFocusTarget(items, this.#currentValue, direction, this.wrap);
|
|
78
89
|
if (!target) {
|
|
79
|
-
if (items.length > 1) {
|
|
80
|
-
LOG.warn("Failed to identify focus target for keyboard navigation");
|
|
81
|
-
}
|
|
82
90
|
return;
|
|
83
91
|
}
|
|
84
92
|
event.preventDefault();
|
|
@@ -122,7 +130,7 @@ class InternalMenuState {
|
|
|
122
130
|
const disabledIndex = findItemIndex(items, disabledValue);
|
|
123
131
|
let target;
|
|
124
132
|
if (disabledIndex === -1) {
|
|
125
|
-
target = getFocusTarget(items, -1, "home");
|
|
133
|
+
target = getFocusTarget(items, -1, "home", false);
|
|
126
134
|
} else {
|
|
127
135
|
target = getFocusTarget(items, disabledIndex, "forward", false) ?? getFocusTarget(items, disabledIndex, "backward", false);
|
|
128
136
|
}
|
|
@@ -180,7 +188,7 @@ function getNavDirection(event, orientation) {
|
|
|
180
188
|
return "end";
|
|
181
189
|
}
|
|
182
190
|
}
|
|
183
|
-
function getFocusTarget(items, current, direction, wrap
|
|
191
|
+
function getFocusTarget(items, current, direction, wrap) {
|
|
184
192
|
if (items.length === 0) {
|
|
185
193
|
return void 0;
|
|
186
194
|
}
|
|
@@ -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, 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\n/**\n * Internal model class; created by the menu and accessible by the menu items.\n *\n * @internal\n */\nexport class InternalMenuState {\n // True if the menu is currently active in its parent menu (used for nesting),\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\n constructor(\n menuId: string,\n orientation: \"horizontal\" | \"vertical\",\n menuRef: RefObject<HTMLElement | null>,\n isActiveInParent?: () => boolean\n ) {\n this.#isActiveInParent = isActiveInParent ?? (() => true);\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.#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);\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_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;AAOO,MAAM,iBAAA,CAAkB;AAAA;AAAA,EAE3B,iBAAA;AAAA,EAEA,QAAA;AAAA;AAAA,EAGA,WAAW,QAAA,EAA6B;AAAA,EAE/B,MAAA;AAAA,EACA,WAAA;AAAA,EAET,WAAA,CACI,MAAA,EACA,WAAA,EACA,OAAA,EACA,gBAAA,EACF;AACE,IAAA,IAAA,CAAK,iBAAA,GAAoB,qBAAqB,MAAM,IAAA,CAAA;AACpD,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,IAAA,CAAK,iBAAA,EAAkB,IAAK,IAAA,CAAK,aAAA,KAAkB,KAAA;AAAA,EAC9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I,eAAA,EAAiB;AACjB,IAAA,QAAA,IAAY,qBAAA;AAAA,EAChB;AACA,EAAA,MAAM,QAAA,GAAW,KAAA,CAAM,gBAAA,CAAiB,QAAQ,CAAA;AAChD,EAAA,OAAO,KAAA,CAAM,KAAK,QAAQ,CAAA;AAC9B;AAEA,SAAS,WAAA,CACL,OAAA,EACA,MAAA,EACA,KAAA,EACuB;AACvB,EAAA,MAAM,QAAQ,OAAA,CAAQ,OAAA;AACtB,EAAA,IAAI,CAAC,KAAA,EAAO;AACR,IAAA,OAAO,MAAA;AAAA,EACX;AAEA,EAAA,MAAM,aAAA,GAAgB,GAAA,CAAI,MAAA,CAAO,MAAM,CAAA;AACvC,EAAA,MAAM,YAAA,GAAe,GAAA,CAAI,MAAA,CAAO,KAAK,CAAA;AACrC,EAAA,MAAM,OAAO,KAAA,CAAM,aAAA;AAAA,IACf,CAAA,CAAA,EAAI,eAAe,CAAA,CAAA,EAAI,aAAa,KAAK,eAAe,CAAA,CAAA,EAAI,YAAY,CAAA,CAAA,EAAI,qBAAqB,CAAA;AAAA,GACrG;AACA,EAAA,OAAQ,IAAA,IAAQ,MAAA;AACpB;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,IAA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rE,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;;;;"}
|
|
@@ -13,6 +13,19 @@ export interface RovingMenuProps {
|
|
|
13
13
|
* Horizontal menus can be navigated using left/right, vertical menus use up/down.
|
|
14
14
|
*/
|
|
15
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;
|
|
16
29
|
}
|
|
17
30
|
/** @internal */
|
|
18
31
|
export interface RovingMenuDomProps {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useMemo, useId, useRef } from 'react';
|
|
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
4
|
import { useRovingMenuItemImpl } from './useRovingMenuItem.js';
|
|
@@ -28,14 +28,25 @@ function useNestedRovingMenu(props) {
|
|
|
28
28
|
};
|
|
29
29
|
}, [menuProps, menuState, itemProps]);
|
|
30
30
|
}
|
|
31
|
-
function useRovingMenuImpl(props
|
|
32
|
-
const { orientation = "horizontal" } = props;
|
|
31
|
+
function useRovingMenuImpl(props, isActiveInParent) {
|
|
32
|
+
const { orientation = "horizontal", active = true, wrap = true } = props;
|
|
33
33
|
const menuId = useId();
|
|
34
34
|
const menuRef = useRef(null);
|
|
35
|
+
const initialActive = useRef(active);
|
|
35
36
|
const state = useMemo(
|
|
36
|
-
() => new InternalMenuState(
|
|
37
|
-
|
|
37
|
+
() => new InternalMenuState({
|
|
38
|
+
menuId,
|
|
39
|
+
orientation,
|
|
40
|
+
menuRef,
|
|
41
|
+
wrap,
|
|
42
|
+
active: initialActive.current,
|
|
43
|
+
isActiveInParent
|
|
44
|
+
}),
|
|
45
|
+
[menuRef, orientation, menuId, wrap, isActiveInParent]
|
|
38
46
|
);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
state.active = active;
|
|
49
|
+
}, [state, active]);
|
|
39
50
|
const onKeyDown = useEvent((event) => {
|
|
40
51
|
state.onKeyDown(event);
|
|
41
52
|
});
|
|
@@ -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, 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\";\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/** @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
|
|
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,MAAM;AACjB,IAAA,OAAO;AAAA,MACH,SAAA,EAAW;AAAA,QACP,GAAG,SAAA;AAAA,QACH,GAAG,SAAA;AAAA,QACH,QAAQ,CAAA,EAAe;AAEnB,UAAA,SAAA,CAAU,QAAQ,CAAC,CAAA;AAGnB,UAAA,SAAA,CAAU,QAAQ,CAAC,CA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