@open-pioneer/react-utils 4.5.0-dev-roving-menu.20260128080955 → 4.5.0-dev-roving-menu.20260129122847
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,6 +1,6 @@
|
|
|
1
1
|
# @open-pioneer/react-utils
|
|
2
2
|
|
|
3
|
-
## 4.5.0-dev-roving-menu.
|
|
3
|
+
## 4.5.0-dev-roving-menu.20260129122847
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
|
|
10
10
|
### Patch Changes
|
|
11
11
|
|
|
12
|
-
- @open-pioneer/core@4.5.0-dev-roving-menu.
|
|
13
|
-
- @open-pioneer/reactivity@4.5.0-dev-roving-menu.
|
|
12
|
+
- @open-pioneer/core@4.5.0-dev-roving-menu.20260129122847
|
|
13
|
+
- @open-pioneer/reactivity@4.5.0-dev-roving-menu.20260129122847
|
|
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-roving-menu.
|
|
4
|
+
"version": "4.5.0-dev-roving-menu.20260129122847",
|
|
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-roving-menu.
|
|
22
|
-
"@open-pioneer/reactivity": "4.5.0-dev-roving-menu.
|
|
21
|
+
"@open-pioneer/core": "4.5.0-dev-roving-menu.20260129122847",
|
|
22
|
+
"@open-pioneer/reactivity": "4.5.0-dev-roving-menu.20260129122847"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
25
|
"./package.json": "./package.json",
|
|
@@ -53,12 +53,6 @@ export declare class InternalMenuState {
|
|
|
53
53
|
* This will move the active value to another item to ensure that one item is focusable.
|
|
54
54
|
*/
|
|
55
55
|
onItemUnmount(value: string): void;
|
|
56
|
-
/**
|
|
57
|
-
* Called by items when they become disabled.
|
|
58
|
-
*
|
|
59
|
-
* This will move the active value to another item to ensure that one item is focusable.
|
|
60
|
-
*/
|
|
61
|
-
onItemDisabled(value: string): void;
|
|
62
56
|
/**
|
|
63
57
|
* Called by items when they receive focus.
|
|
64
58
|
*
|
|
@@ -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,19 +65,29 @@ 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
|
-
|
|
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());
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
this.#activateItem(void 0);
|
|
72
90
|
}
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Called by items when they become disabled.
|
|
76
|
-
*
|
|
77
|
-
* This will move the active value to another item to ensure that one item is focusable.
|
|
78
|
-
*/
|
|
79
|
-
onItemDisabled(value) {
|
|
80
|
-
this.onItemUnmount(value);
|
|
81
91
|
}
|
|
82
92
|
/**
|
|
83
93
|
* Called by items when they receive focus.
|
|
@@ -133,32 +143,60 @@ function getNavDirection(event, orientation) {
|
|
|
133
143
|
return "end";
|
|
134
144
|
}
|
|
135
145
|
}
|
|
136
|
-
function getFocusTarget(items, current, direction) {
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
|
|
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);
|
|
140
152
|
}
|
|
141
|
-
if (direction === "home") {
|
|
142
|
-
return items.
|
|
153
|
+
if (currentIndex === -1 || direction === "home") {
|
|
154
|
+
return items.find((item) => !isDisabled(item));
|
|
143
155
|
}
|
|
144
156
|
if (direction === "end") {
|
|
145
|
-
return items.
|
|
157
|
+
return items.findLast((item) => !isDisabled(item));
|
|
146
158
|
}
|
|
147
|
-
let nextIndex;
|
|
148
159
|
if (direction === "forward") {
|
|
149
|
-
nextIndex = currentIndex + 1;
|
|
150
|
-
|
|
151
|
-
|
|
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;
|
|
152
175
|
}
|
|
153
176
|
} else {
|
|
154
|
-
nextIndex = currentIndex - 1;
|
|
155
|
-
|
|
156
|
-
|
|
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;
|
|
157
192
|
}
|
|
158
193
|
}
|
|
159
|
-
return
|
|
194
|
+
return void 0;
|
|
195
|
+
}
|
|
196
|
+
function findItemIndex(items, value) {
|
|
197
|
+
return items.findIndex((item) => getItemValue(item) === value);
|
|
160
198
|
}
|
|
161
|
-
function
|
|
199
|
+
function getMenuItems(menuRef, menuId, unmount = false, excludeDisabled = true) {
|
|
162
200
|
const owner = menuRef.current;
|
|
163
201
|
if (!owner) {
|
|
164
202
|
if (unmount) {
|
|
@@ -170,12 +208,22 @@ function getFocusableItems(menuRef, menuId, unmount = false) {
|
|
|
170
208
|
}
|
|
171
209
|
}
|
|
172
210
|
const escapedId = CSS.escape(menuId);
|
|
173
|
-
|
|
211
|
+
let selector = `[${MENU_OWNER_ATTR}=${escapedId}]`;
|
|
212
|
+
if (excludeDisabled) {
|
|
213
|
+
selector += ":not([disabled],[aria-disabled='true'])";
|
|
214
|
+
}
|
|
215
|
+
const children = owner.querySelectorAll(selector);
|
|
174
216
|
return Array.from(children);
|
|
175
217
|
}
|
|
176
218
|
function getItemValue(item) {
|
|
177
219
|
return item?.dataset[MENU_VALUE_JS] ?? void 0;
|
|
178
220
|
}
|
|
221
|
+
function isDisabled(item) {
|
|
222
|
+
return !!item.disabled || item.ariaDisabled === "true";
|
|
223
|
+
}
|
|
224
|
+
function hasFocus(item) {
|
|
225
|
+
return item.getRootNode().activeElement === item;
|
|
226
|
+
}
|
|
179
227
|
|
|
180
228
|
export { InternalMenuState, MENU_ID_ATTR, MENU_OWNER_ATTR, MENU_VALUE_ATTR, MenuStateContext, getInternalState };
|
|
181
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 target && this.#navigateToItem(target);\n }\n }\n\n /**\n * Called by items when they become disabled.\n *\n * This will move the active value to another item to ensure that one item is focusable.\n */\n onItemDisabled(value: string): void {\n // improvement: don't reset to first but to a close item instead\n this.onItemUnmount(value); // happens to do what we want :)\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): 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,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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,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,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AAK,YAAA,KAAiB,MAAA;AAC7D;AAEA,SAAS,SAAS,IAAA,EAA4B;AAC1C,EAAA,OAAQ,IAAA,CAAK,WAAA,EAAY,CAAe,aAAA,KAAkB,IAAA;AAC9D;;;;"}
|
|
@@ -6,14 +6,12 @@ function useRovingMenuItem(props) {
|
|
|
6
6
|
const { value, disabled = false, required = true } = props;
|
|
7
7
|
const state = useMenuState(required);
|
|
8
8
|
useEffect(() => {
|
|
9
|
-
state
|
|
10
|
-
|
|
11
|
-
}, [state, value]);
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
if (disabled) {
|
|
14
|
-
state?.onItemDisabled(value);
|
|
9
|
+
if (!state || disabled) {
|
|
10
|
+
return;
|
|
15
11
|
}
|
|
16
|
-
|
|
12
|
+
state.onItemMount(value);
|
|
13
|
+
return () => state.onItemUnmount(value);
|
|
14
|
+
}, [state, value, disabled]);
|
|
17
15
|
const isActive = useReactiveSnapshot(() => state?.isActive(value), [state, value]);
|
|
18
16
|
const result = useMemo(() => {
|
|
19
17
|
if (!state) {
|
|
@@ -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, useEffect, useMemo } 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 };\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 useEffect(() => {\n
|
|
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, useEffect, useMemo } 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 };\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 useEffect(() => {\n if (!state || disabled) {\n return;\n }\n\n state.onItemMount(value);\n return () => state.onItemUnmount(value);\n }, [state, value, disabled]);\n\n const isActive = useReactiveSnapshot(() => state?.isActive(value), [state, value]);\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 state?.onItemFocus(value);\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":";;;;AAyEO,SAAS,kBAAkB,KAAA,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