@elliemae/ds-menu-button 3.80.0-next.8 → 3.80.0-next.81
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/dist/cjs/config/useLatestValueReferenciallyStable.js +43 -0
- package/dist/cjs/config/useLatestValueReferenciallyStable.js.map +7 -0
- package/dist/cjs/config/useSideEffectSyntheticEvent.js +44 -0
- package/dist/cjs/config/useSideEffectSyntheticEvent.js.map +7 -0
- package/dist/cjs/config/useSplitInherithedProps.js +19 -2
- package/dist/cjs/config/useSplitInherithedProps.js.map +2 -2
- package/dist/cjs/parts/DSFlyoutMenu/DSFlyoutMenu.js +22 -1
- package/dist/cjs/parts/DSFlyoutMenu/DSFlyoutMenu.js.map +2 -2
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/DSMenuBehaviouralContextProvider.js +4 -3
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/DSMenuBehaviouralContextProvider.js.map +2 -2
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useFocusTracker.js +10 -1
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useFocusTracker.js.map +2 -2
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useGlobalEvents.js +4 -3
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useGlobalEvents.js.map +2 -2
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useMenuItemEventsHandlers.js +59 -24
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useMenuItemEventsHandlers.js.map +2 -2
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useMenuOpenStatus.js +14 -8
- package/dist/cjs/parts/DSMenuBehaviouralContextProvider/config/useMenuOpenStatus.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/ActivableMenuItem.js +6 -3
- package/dist/cjs/parts/DSMenuItemRendererFactory/ActivableMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/ActivableWithSubmenuMenuItem.js +6 -3
- package/dist/cjs/parts/DSMenuItemRendererFactory/ActivableWithSubmenuMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/MultipleSelectMenuItem.js +8 -5
- package/dist/cjs/parts/DSMenuItemRendererFactory/MultipleSelectMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/MultipleSelectWithSubmenuMenuItem.js +8 -5
- package/dist/cjs/parts/DSMenuItemRendererFactory/MultipleSelectWithSubmenuMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/SingleSelectMenuItem.js +6 -3
- package/dist/cjs/parts/DSMenuItemRendererFactory/SingleSelectMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/SingleSelectWithSubmenuMenuItem.js +6 -3
- package/dist/cjs/parts/DSMenuItemRendererFactory/SingleSelectWithSubmenuMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/SkeletonMenuItem.js +9 -1
- package/dist/cjs/parts/DSMenuItemRendererFactory/SkeletonMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSMenuItemRendererFactory/WithSubmenuMenuItem.js +6 -3
- package/dist/cjs/parts/DSMenuItemRendererFactory/WithSubmenuMenuItem.js.map +2 -2
- package/dist/cjs/parts/DSOpinionatedButton/DSOpinionatedButton.js +9 -2
- package/dist/cjs/parts/DSOpinionatedButton/DSOpinionatedButton.js.map +2 -2
- package/dist/cjs/parts/DSOpinionatedButton/config/useTriggerEventsHandlers.js +58 -21
- package/dist/cjs/parts/DSOpinionatedButton/config/useTriggerEventsHandlers.js.map +2 -2
- package/dist/cjs/react-desc-prop-types.js +18 -2
- package/dist/cjs/react-desc-prop-types.js.map +2 -2
- package/dist/cjs/utils/nodesTypeguardsAndGetters.js +9 -0
- package/dist/cjs/utils/nodesTypeguardsAndGetters.js.map +2 -2
- package/dist/esm/config/useLatestValueReferenciallyStable.js +13 -0
- package/dist/esm/config/useLatestValueReferenciallyStable.js.map +7 -0
- package/dist/esm/config/useSideEffectSyntheticEvent.js +14 -0
- package/dist/esm/config/useSideEffectSyntheticEvent.js.map +7 -0
- package/dist/esm/config/useSplitInherithedProps.js +19 -2
- package/dist/esm/config/useSplitInherithedProps.js.map +2 -2
- package/dist/esm/parts/DSFlyoutMenu/DSFlyoutMenu.js +22 -1
- package/dist/esm/parts/DSFlyoutMenu/DSFlyoutMenu.js.map +2 -2
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/DSMenuBehaviouralContextProvider.js +4 -3
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/DSMenuBehaviouralContextProvider.js.map +2 -2
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useFocusTracker.js +10 -1
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useFocusTracker.js.map +2 -2
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useGlobalEvents.js +4 -3
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useGlobalEvents.js.map +2 -2
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useMenuItemEventsHandlers.js +61 -24
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useMenuItemEventsHandlers.js.map +2 -2
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useMenuOpenStatus.js +14 -8
- package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useMenuOpenStatus.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/ActivableMenuItem.js +6 -3
- package/dist/esm/parts/DSMenuItemRendererFactory/ActivableMenuItem.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/ActivableWithSubmenuMenuItem.js +6 -3
- package/dist/esm/parts/DSMenuItemRendererFactory/ActivableWithSubmenuMenuItem.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/MultipleSelectMenuItem.js +8 -5
- package/dist/esm/parts/DSMenuItemRendererFactory/MultipleSelectMenuItem.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/MultipleSelectWithSubmenuMenuItem.js +8 -5
- package/dist/esm/parts/DSMenuItemRendererFactory/MultipleSelectWithSubmenuMenuItem.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/SingleSelectMenuItem.js +6 -3
- package/dist/esm/parts/DSMenuItemRendererFactory/SingleSelectMenuItem.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/SingleSelectWithSubmenuMenuItem.js +6 -3
- package/dist/esm/parts/DSMenuItemRendererFactory/SingleSelectWithSubmenuMenuItem.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/SkeletonMenuItem.js +9 -1
- package/dist/esm/parts/DSMenuItemRendererFactory/SkeletonMenuItem.js.map +2 -2
- package/dist/esm/parts/DSMenuItemRendererFactory/WithSubmenuMenuItem.js +6 -3
- package/dist/esm/parts/DSMenuItemRendererFactory/WithSubmenuMenuItem.js.map +2 -2
- package/dist/esm/parts/DSOpinionatedButton/DSOpinionatedButton.js +9 -2
- package/dist/esm/parts/DSOpinionatedButton/DSOpinionatedButton.js.map +2 -2
- package/dist/esm/parts/DSOpinionatedButton/config/useTriggerEventsHandlers.js +58 -21
- package/dist/esm/parts/DSOpinionatedButton/config/useTriggerEventsHandlers.js.map +2 -2
- package/dist/esm/react-desc-prop-types.js +18 -2
- package/dist/esm/react-desc-prop-types.js.map +2 -2
- package/dist/esm/utils/nodesTypeguardsAndGetters.js +9 -0
- package/dist/esm/utils/nodesTypeguardsAndGetters.js.map +2 -2
- package/dist/types/config/useLatestValueReferenciallyStable.d.ts +4 -0
- package/dist/types/config/useSideEffectSyntheticEvent.d.ts +9 -0
- package/dist/types/config/useSplitInherithedProps.d.ts +7 -3
- package/dist/types/parts/DSMenuBehaviouralContextProvider/config/useFocusTracker.d.ts +4 -1
- package/dist/types/parts/DSMenuBehaviouralContextProvider/config/useMenuItemEventsHandlers.d.ts +1 -0
- package/dist/types/parts/DSMenuBehaviouralContextProvider/config/useMenuOpenStatus.d.ts +12 -4
- package/dist/types/parts/DSMenuItemRendererFactory/focusNodeRacingConditionSolved.d.ts +1 -1
- package/dist/types/parts/DSOpinionatedButton/config/useOpinionatedButton.d.ts +2 -1
- package/dist/types/parts/DSOpinionatedButton/config/useTriggerEventsHandlers.d.ts +1 -0
- package/dist/types/react-desc-prop-types.d.ts +64 -2
- package/dist/types/utils/nodesTypeguardsAndGetters.d.ts +21 -0
- package/package.json +14 -13
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../../../../scripts/build/transpile/react-shim.js", "../../../../../src/parts/DSMenuBehaviouralContextProvider/config/useGlobalEvents.ts"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import React from 'react';\nimport type { DSMenuButtonT } from '../../../react-desc-prop-types.js';\nimport type { useMenuOpenStatus } from './useMenuOpenStatus.js';\nimport type { DSMenuBehaviouralContextProviderT } from '../react-desc-prop-types.js';\n\ntype UseGlobalEventsConfig = {\n menuOpenStatus: ReturnType<typeof useMenuOpenStatus>;\n handleChangeOpenedSubItems: Required<DSMenuButtonT.MenuBehaviouralLayerOptionalProps>['onDisplayedSubmenuChange'];\n propsWithDefault: DSMenuBehaviouralContextProviderT.InternalProps;\n};\n\nexport const useGlobalEvents = ({\n menuOpenStatus,\n handleChangeOpenedSubItems,\n propsWithDefault,\n}: UseGlobalEventsConfig) => {\n const { onOpinionatedClose, isMenuOpen } = menuOpenStatus;\n const { buttonDOMNodeRef } = propsWithDefault;\n const mainMenuRef = React.useRef<HTMLDivElement | null>(null);\n // we are using \"portal\" but then we check the native evenet to check if \"on click outside\" the menu\n // for such reason, we need to track the current HTML NODES of the submenus (topmost parent)\n // this will allow us to check if the click event is outside the menu or any of it's submenus\n const submenusRefs = React.useRef<Record<DSMenuButtonT.WithSubmenuMenuNodes['dsId'], HTMLElement>>({});\n const handleSubmenusRefChange = React.useCallback(\n (htmlNode: HTMLElement | null, dsId: DSMenuButtonT.WithSubmenuMenuNodes['dsId']) => {\n if (htmlNode) {\n submenusRefs.current[dsId] = htmlNode;\n } else {\n delete submenusRefs.current[dsId];\n }\n },\n [],\n );\n React.useEffect(() => {\n // when the window loses focus, close all the menus and submenus\n const closeMenuAndSubmenus = (e: FocusEvent) => {\n handleChangeOpenedSubItems([], { itemNode: null, event: e });\n onOpinionatedClose({ skipFocus: true });\n };\n // when the user clicks \"outside\" the menu and submenus, close all the menus and submenus\n const detectIfClickIsOutsideMenuAndCloseIfSo = (e: MouseEvent | TouchEvent) => {\n // we are running this on literally ALL the clicks that happen on the window\n // we don't want to be consuming resources that are not strictly necessary\n // if the main menu is not open, we don't need to check if the click is outside the menu ofc\n if (!isMenuOpen) return;\n // we discard the fastest to check \"trigger\" click first\n if (buttonDOMNodeRef.current
|
|
5
|
-
"mappings": "AAAA,YAAY,WAAW;ACAvB,OAAOA,YAAW;AAWX,MAAM,kBAAkB,CAAC;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACF,MAA6B;AAC3B,QAAM,EAAE,oBAAoB,WAAW,IAAI;AAC3C,QAAM,EAAE,iBAAiB,IAAI;AAC7B,QAAM,cAAcA,OAAM,OAA8B,IAAI;AAI5D,QAAM,eAAeA,OAAM,OAAwE,CAAC,CAAC;AACrG,QAAM,0BAA0BA,OAAM;AAAA,IACpC,CAAC,UAA8B,SAAqD;AAClF,UAAI,UAAU;AACZ,qBAAa,QAAQ,IAAI,IAAI;AAAA,MAC/B,OAAO;AACL,eAAO,aAAa,QAAQ,IAAI;AAAA,MAClC;AAAA,IACF;AAAA,IACA,CAAC;AAAA,EACH;AACA,EAAAA,OAAM,UAAU,MAAM;AAEpB,UAAM,uBAAuB,CAAC,MAAkB;
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import React from 'react';\nimport type { DSMenuButtonT } from '../../../react-desc-prop-types.js';\nimport type { useMenuOpenStatus } from './useMenuOpenStatus.js';\nimport type { DSMenuBehaviouralContextProviderT } from '../react-desc-prop-types.js';\n\ntype UseGlobalEventsConfig = {\n menuOpenStatus: ReturnType<typeof useMenuOpenStatus>;\n handleChangeOpenedSubItems: Required<DSMenuButtonT.MenuBehaviouralLayerOptionalProps>['onDisplayedSubmenuChange'];\n propsWithDefault: DSMenuBehaviouralContextProviderT.InternalProps;\n};\n\nexport const useGlobalEvents = ({\n menuOpenStatus,\n handleChangeOpenedSubItems,\n propsWithDefault,\n}: UseGlobalEventsConfig) => {\n const { onOpinionatedClose, isMenuOpen } = menuOpenStatus;\n const { buttonDOMNodeRef } = propsWithDefault;\n const mainMenuRef = React.useRef<HTMLDivElement | null>(null);\n // we are using \"portal\" but then we check the native evenet to check if \"on click outside\" the menu\n // for such reason, we need to track the current HTML NODES of the submenus (topmost parent)\n // this will allow us to check if the click event is outside the menu or any of it's submenus\n const submenusRefs = React.useRef<Record<DSMenuButtonT.WithSubmenuMenuNodes['dsId'], HTMLElement>>({});\n const handleSubmenusRefChange = React.useCallback(\n (htmlNode: HTMLElement | null, dsId: DSMenuButtonT.WithSubmenuMenuNodes['dsId']) => {\n if (htmlNode) {\n submenusRefs.current[dsId] = htmlNode;\n } else {\n delete submenusRefs.current[dsId];\n }\n },\n [],\n );\n React.useEffect(() => {\n // when the window loses focus, close all the menus and submenus\n const closeMenuAndSubmenus = (e: FocusEvent) => {\n // same guard as the click-outside handler: every instance listens to window blur,\n // a closed menu has nothing to close (and must not report `onDisplayedSubmenuChange`/`onClose`)\n if (!isMenuOpen) return;\n handleChangeOpenedSubItems([], { itemNode: null, event: e });\n onOpinionatedClose({ skipFocus: true, reason: 'window-blur', event: e });\n };\n // when the user clicks \"outside\" the menu and submenus, close all the menus and submenus\n const detectIfClickIsOutsideMenuAndCloseIfSo = (e: MouseEvent | TouchEvent) => {\n // we are running this on literally ALL the clicks that happen on the window\n // we don't want to be consuming resources that are not strictly necessary\n // if the main menu is not open, we don't need to check if the click is outside the menu ofc\n if (!isMenuOpen) return;\n // we discard the fastest to check \"trigger\" click first \u2014 the trigger CONTAINS its content (icons, text...),\n // a pointer going down on any of it is a trigger interaction (the click that follows toggles the menu),\n // not an outside one: closing here would make the click re-open the menu we just closed\n if (buttonDOMNodeRef.current?.contains(e.target as Node)) return;\n // fastest check is the mainmenu, cuz we don't loop through all the submenus\n const isClickInsideMainMenu = mainMenuRef.current?.contains(e.target as Node);\n if (isClickInsideMainMenu) return;\n const submenuNodesToCheck = Object.values(submenusRefs.current).filter(Boolean);\n // we check if the event.target is a child of any of our known HTML nodes (via native .contains)\n const isClickInsideAnySubmenu = submenuNodesToCheck.some((node) => node.contains(e.target as Node));\n if (isClickInsideAnySubmenu) return;\n handleChangeOpenedSubItems([], { itemNode: null, event: e });\n onOpinionatedClose({ skipFocus: true, reason: 'outside-click', event: e });\n };\n\n window.addEventListener('blur', closeMenuAndSubmenus);\n document.addEventListener('mousedown', detectIfClickIsOutsideMenuAndCloseIfSo);\n document.addEventListener('touchstart', detectIfClickIsOutsideMenuAndCloseIfSo);\n return () => {\n window.removeEventListener('blur', closeMenuAndSubmenus);\n document.removeEventListener('mousedown', detectIfClickIsOutsideMenuAndCloseIfSo);\n document.removeEventListener('touchstart', detectIfClickIsOutsideMenuAndCloseIfSo);\n };\n }, [buttonDOMNodeRef, handleChangeOpenedSubItems, isMenuOpen, onOpinionatedClose]);\n\n return React.useMemo(\n () => ({\n mainMenuRef,\n submenusRefs,\n handleSubmenusRefChange,\n }),\n [handleSubmenusRefChange],\n );\n};\n"],
|
|
5
|
+
"mappings": "AAAA,YAAY,WAAW;ACAvB,OAAOA,YAAW;AAWX,MAAM,kBAAkB,CAAC;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AACF,MAA6B;AAC3B,QAAM,EAAE,oBAAoB,WAAW,IAAI;AAC3C,QAAM,EAAE,iBAAiB,IAAI;AAC7B,QAAM,cAAcA,OAAM,OAA8B,IAAI;AAI5D,QAAM,eAAeA,OAAM,OAAwE,CAAC,CAAC;AACrG,QAAM,0B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A,OAAM;AAAA,IACX,OAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,CAAC,uBAAuB;AAAA,EAC1B;AACF;",
|
|
6
6
|
"names": ["React"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
+
import { isActivationKey } from "@elliemae/ds-props-helpers";
|
|
2
3
|
import React2 from "react";
|
|
3
4
|
import {
|
|
4
5
|
isFocusableNode,
|
|
6
|
+
isItemDisabled,
|
|
7
|
+
isItemLocked,
|
|
5
8
|
isMultipleSelectNode,
|
|
6
9
|
isRootNode,
|
|
7
10
|
isSingleSelectNode,
|
|
@@ -12,13 +15,17 @@ import { FORBIDDEN_BEHAVIOURS, UNEXPECTED_INTERNAL_ERRORS } from "../constants/E
|
|
|
12
15
|
import { getNewSelectionMultipleSelect } from "../utils/multipleSelectionHelpers.js";
|
|
13
16
|
import { getSubMenusPath } from "../utils/nodeGettersByCriterias.js";
|
|
14
17
|
import { getNewSelectionSingleSelect } from "../utils/singleSelectionHelpers.js";
|
|
18
|
+
const invokeItemOnClickOnKeyboardActivation = (pseudoFocusedItemNode, event) => {
|
|
19
|
+
if (event?.type === "keydown" && pseudoFocusedItemNode.plainItem.onClick)
|
|
20
|
+
pseudoFocusedItemNode.plainItem.onClick(event);
|
|
21
|
+
};
|
|
15
22
|
const useMenuItemEventsHandlers = ({
|
|
16
23
|
propsWithDefault,
|
|
17
24
|
focusTrackers,
|
|
18
25
|
menuOpenStatus,
|
|
19
26
|
handleChangeOpenedSubItems
|
|
20
27
|
}) => {
|
|
21
|
-
const { onItemSelected, onActivateItem, selectedNodes } = propsWithDefault;
|
|
28
|
+
const { onItemSelected, onActivateItem, onItemKeyDown, selectedNodes, buttonDOMNodeRef } = propsWithDefault;
|
|
22
29
|
const { onOpinionatedClose } = menuOpenStatus;
|
|
23
30
|
const {
|
|
24
31
|
trackFocusFirstChildItem,
|
|
@@ -28,7 +35,8 @@ const useMenuItemEventsHandlers = ({
|
|
|
28
35
|
trackFocusParentItem,
|
|
29
36
|
trackFocusNode,
|
|
30
37
|
focusedElementItemNode,
|
|
31
|
-
trackFocusTrigger
|
|
38
|
+
trackFocusTrigger,
|
|
39
|
+
trackFocusRegionReset
|
|
32
40
|
} = focusTrackers;
|
|
33
41
|
const openSubmenu = React2.useCallback(
|
|
34
42
|
({
|
|
@@ -90,10 +98,11 @@ const useMenuItemEventsHandlers = ({
|
|
|
90
98
|
} else {
|
|
91
99
|
onActivateItem(pseudoFocusedItemNode, { itemNode: pseudoFocusedItemNode, event });
|
|
92
100
|
}
|
|
101
|
+
invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);
|
|
93
102
|
const metainfo = { itemNode: pseudoFocusedItemNode, event };
|
|
94
103
|
const newOpenedItems = [];
|
|
95
104
|
handleChangeOpenedSubItems(newOpenedItems, metainfo);
|
|
96
|
-
onOpinionatedClose();
|
|
105
|
+
onOpinionatedClose({ reason: "item-activation", event });
|
|
97
106
|
}
|
|
98
107
|
},
|
|
99
108
|
[
|
|
@@ -116,18 +125,21 @@ const useMenuItemEventsHandlers = ({
|
|
|
116
125
|
if (isMultipleSelectNode(pseudoFocusedItemNode)) {
|
|
117
126
|
const newSelection = getNewSelectionMultipleSelect(selectedNodes, pseudoFocusedItemNode);
|
|
118
127
|
onItemSelected(newSelection, { itemNode: pseudoFocusedItemNode, event });
|
|
128
|
+
invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);
|
|
119
129
|
return;
|
|
120
130
|
}
|
|
121
131
|
if (isSingleSelectNode(pseudoFocusedItemNode)) {
|
|
122
132
|
const newSelection = getNewSelectionSingleSelect(selectedNodes, pseudoFocusedItemNode);
|
|
123
133
|
onItemSelected(newSelection, { itemNode: pseudoFocusedItemNode, event });
|
|
134
|
+
invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);
|
|
124
135
|
return;
|
|
125
136
|
}
|
|
126
137
|
onActivateItem?.(pseudoFocusedItemNode, { itemNode: pseudoFocusedItemNode, event });
|
|
138
|
+
invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);
|
|
127
139
|
const metainfo = { itemNode: pseudoFocusedItemNode, event };
|
|
128
140
|
const newOpenedItems = [];
|
|
129
141
|
handleChangeOpenedSubItems(newOpenedItems, metainfo);
|
|
130
|
-
onOpinionatedClose();
|
|
142
|
+
onOpinionatedClose({ reason: "item-activation", event });
|
|
131
143
|
},
|
|
132
144
|
[
|
|
133
145
|
onActivateItem,
|
|
@@ -144,9 +156,7 @@ const useMenuItemEventsHandlers = ({
|
|
|
144
156
|
if (isWithSubmenuNode(pseudoFocusedItemNode)) {
|
|
145
157
|
openSubmenu({ itemNode: pseudoFocusedItemNode, event });
|
|
146
158
|
trackFocusFirstChildItem(pseudoFocusedItemNode);
|
|
147
|
-
return;
|
|
148
159
|
}
|
|
149
|
-
return;
|
|
150
160
|
},
|
|
151
161
|
[openSubmenu, trackFocusFirstChildItem]
|
|
152
162
|
);
|
|
@@ -159,36 +169,62 @@ const useMenuItemEventsHandlers = ({
|
|
|
159
169
|
},
|
|
160
170
|
[closeCurrentSubmenu, trackFocusParentItem]
|
|
161
171
|
);
|
|
172
|
+
const handleMenuTabKeyDownCapture = React2.useCallback(
|
|
173
|
+
(event) => {
|
|
174
|
+
if (event.key !== "Tab") return;
|
|
175
|
+
buttonDOMNodeRef.current?.focus();
|
|
176
|
+
trackFocusRegionReset();
|
|
177
|
+
onOpinionatedClose({ skipFocus: true, reason: "tab", event });
|
|
178
|
+
},
|
|
179
|
+
[buttonDOMNodeRef, onOpinionatedClose, trackFocusRegionReset]
|
|
180
|
+
);
|
|
162
181
|
const handleFocusableMenuItemKeyDown = React2.useCallback(
|
|
163
182
|
(event) => {
|
|
183
|
+
if (event.key === "Tab") return;
|
|
184
|
+
const consumeKey = () => {
|
|
185
|
+
event.preventDefault();
|
|
186
|
+
event.stopPropagation();
|
|
187
|
+
};
|
|
164
188
|
const pseudoFocusedItemNode = focusedElementItemNode.current;
|
|
165
189
|
if (pseudoFocusedItemNode === null) throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_NODE_NOT_SET;
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
190
|
+
const isLockedActivation = isItemLocked(pseudoFocusedItemNode.plainItem) && isActivationKey(event);
|
|
191
|
+
if (pseudoFocusedItemNode.plainItem.onKeyDown && !isLockedActivation)
|
|
192
|
+
pseudoFocusedItemNode.plainItem.onKeyDown(event);
|
|
193
|
+
if (onItemKeyDown) {
|
|
194
|
+
onItemKeyDown({
|
|
195
|
+
itemNode: pseudoFocusedItemNode,
|
|
196
|
+
event,
|
|
197
|
+
nestLevel: getSubMenusPath(pseudoFocusedItemNode).length,
|
|
198
|
+
hasSubmenu: isWithSubmenuNode(pseudoFocusedItemNode)
|
|
199
|
+
});
|
|
171
200
|
}
|
|
172
201
|
if (event.key === "ArrowDown") {
|
|
173
|
-
|
|
202
|
+
consumeKey();
|
|
174
203
|
trackFocusNextItem(pseudoFocusedItemNode);
|
|
175
204
|
return;
|
|
176
205
|
}
|
|
177
206
|
if (event.key === "ArrowUp") {
|
|
178
|
-
|
|
207
|
+
consumeKey();
|
|
179
208
|
trackFocusPreviousItem(pseudoFocusedItemNode);
|
|
180
209
|
return;
|
|
181
210
|
}
|
|
182
211
|
if (event.key === "ArrowLeft") {
|
|
183
|
-
|
|
212
|
+
consumeKey();
|
|
184
213
|
handleMenuItemLeftArrowKeyDown({ pseudoFocusedItemNode, event });
|
|
185
214
|
return;
|
|
186
215
|
}
|
|
216
|
+
if (event.key === "ArrowRight") {
|
|
217
|
+
consumeKey();
|
|
218
|
+
if (!isItemDisabled(pseudoFocusedItemNode.plainItem))
|
|
219
|
+
handleMenuItemRightArrowKeyDown({ pseudoFocusedItemNode, event });
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
187
222
|
if (event.key === "Home") {
|
|
188
223
|
if (!pseudoFocusedItemNode.parent) {
|
|
189
224
|
console.log("handleFocusableMenuItemKeyDown -> Home > focused item node", pseudoFocusedItemNode);
|
|
190
225
|
throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_HAS_NO_PARENT;
|
|
191
226
|
}
|
|
227
|
+
consumeKey();
|
|
192
228
|
trackFocusFirstChildItem(pseudoFocusedItemNode.parent);
|
|
193
229
|
return;
|
|
194
230
|
}
|
|
@@ -197,10 +233,12 @@ const useMenuItemEventsHandlers = ({
|
|
|
197
233
|
console.log("handleFocusableMenuItemKeyDown -> Home > focused item node", pseudoFocusedItemNode);
|
|
198
234
|
throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_HAS_NO_PARENT;
|
|
199
235
|
}
|
|
236
|
+
consumeKey();
|
|
200
237
|
trackFocusLastChildItem(pseudoFocusedItemNode.parent);
|
|
201
238
|
return;
|
|
202
239
|
}
|
|
203
240
|
if (event.key === "Escape") {
|
|
241
|
+
event.stopPropagation();
|
|
204
242
|
if (event.altKey && isWithSubmenuNode(pseudoFocusedItemNode)) {
|
|
205
243
|
closeSubmenusAfterNode({ itemNode: pseudoFocusedItemNode, event });
|
|
206
244
|
return;
|
|
@@ -208,30 +246,27 @@ const useMenuItemEventsHandlers = ({
|
|
|
208
246
|
const { currentSubmenuWasMainMenu } = closeCurrentSubmenu({ itemNode: pseudoFocusedItemNode, event });
|
|
209
247
|
if (currentSubmenuWasMainMenu) {
|
|
210
248
|
trackFocusTrigger({ shouldFocusImmediately: true });
|
|
211
|
-
onOpinionatedClose({ skipFocus: true });
|
|
249
|
+
onOpinionatedClose({ skipFocus: true, reason: "escape", event });
|
|
212
250
|
} else {
|
|
213
251
|
trackFocusParentItem(pseudoFocusedItemNode);
|
|
214
252
|
}
|
|
215
253
|
return;
|
|
216
254
|
}
|
|
217
|
-
if (pseudoFocusedItemNode.plainItem
|
|
255
|
+
if (isItemLocked(pseudoFocusedItemNode.plainItem)) return;
|
|
218
256
|
if (event.key === "Enter") {
|
|
257
|
+
event.stopPropagation();
|
|
219
258
|
handleMenuItemEnterKeyDown({ pseudoFocusedItemNode, event });
|
|
220
259
|
return;
|
|
221
260
|
}
|
|
222
261
|
if (event.key === " ") {
|
|
223
|
-
|
|
262
|
+
consumeKey();
|
|
224
263
|
handleMenuItemSpaceKeyDown({ pseudoFocusedItemNode, event });
|
|
225
264
|
return;
|
|
226
265
|
}
|
|
227
|
-
if (event.key === "ArrowRight") {
|
|
228
|
-
event?.preventDefault();
|
|
229
|
-
handleMenuItemRightArrowKeyDown({ pseudoFocusedItemNode, event });
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
266
|
},
|
|
233
267
|
[
|
|
234
268
|
focusedElementItemNode,
|
|
269
|
+
onItemKeyDown,
|
|
235
270
|
trackFocusTrigger,
|
|
236
271
|
trackFocusNextItem,
|
|
237
272
|
trackFocusPreviousItem,
|
|
@@ -251,7 +286,7 @@ const useMenuItemEventsHandlers = ({
|
|
|
251
286
|
(event) => {
|
|
252
287
|
const pseudoFocusedItemNode = focusedElementItemNode.current;
|
|
253
288
|
if (pseudoFocusedItemNode === null) throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_NODE_NOT_SET;
|
|
254
|
-
if (pseudoFocusedItemNode.plainItem
|
|
289
|
+
if (isItemLocked(pseudoFocusedItemNode.plainItem)) return;
|
|
255
290
|
const parentMenuNode = pseudoFocusedItemNode.parent;
|
|
256
291
|
if (isRootNode(parentMenuNode)) {
|
|
257
292
|
const metainfo = { itemNode: pseudoFocusedItemNode, event };
|
|
@@ -289,7 +324,7 @@ const useMenuItemEventsHandlers = ({
|
|
|
289
324
|
handleChangeOpenedSubItems(newOpenedItems, metainfo);
|
|
290
325
|
return;
|
|
291
326
|
}
|
|
292
|
-
if (itemNode.plainItem
|
|
327
|
+
if (isItemDisabled(itemNode.plainItem)) return;
|
|
293
328
|
openSubmenu({ itemNode, event });
|
|
294
329
|
},
|
|
295
330
|
[handleChangeOpenedSubItems, openSubmenu, trackFocusNode]
|
|
@@ -312,12 +347,14 @@ const useMenuItemEventsHandlers = ({
|
|
|
312
347
|
);
|
|
313
348
|
return React2.useMemo(
|
|
314
349
|
() => ({
|
|
350
|
+
handleMenuTabKeyDownCapture,
|
|
315
351
|
handleFocusableMenuItemKeyDown,
|
|
316
352
|
handleFocusableMenuItemClick,
|
|
317
353
|
handleFocusableMenuItemOnMouseEnter,
|
|
318
354
|
handleFocusableMenuItemNativeFocusEvent
|
|
319
355
|
}),
|
|
320
356
|
[
|
|
357
|
+
handleMenuTabKeyDownCapture,
|
|
321
358
|
handleFocusableMenuItemClick,
|
|
322
359
|
handleFocusableMenuItemKeyDown,
|
|
323
360
|
handleFocusableMenuItemNativeFocusEvent,
|
package/dist/esm/parts/DSMenuBehaviouralContextProvider/config/useMenuItemEventsHandlers.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../../../../scripts/build/transpile/react-shim.js", "../../../../../src/parts/DSMenuBehaviouralContextProvider/config/useMenuItemEventsHandlers.ts"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable no-console */\n/* eslint-disable max-lines */\n// in this files the returns are not useless, they are used to clearly demark the intended behavior shortcircuit for the next developer that reads the code\n/* eslint-disable no-useless-return */\n/* eslint-disable complexity */\n/* eslint-disable max-statements */\nimport React from 'react';\nimport { type DSMenuButtonT } from '../../../react-desc-prop-types.js';\nimport {\n isFocusableNode,\n isMultipleSelectNode,\n isRootNode,\n isSingleSelectNode,\n isWithSubmenuNode,\n isWithSubmenuOnlyNode,\n} from '../../../utils/nodesTypeguardsAndGetters.js';\nimport { FORBIDDEN_BEHAVIOURS, UNEXPECTED_INTERNAL_ERRORS } from '../constants/Errors.js';\nimport { type DSMenuBehaviouralContextProviderT } from '../react-desc-prop-types.js';\nimport { getNewSelectionMultipleSelect } from '../utils/multipleSelectionHelpers.js';\nimport { getSubMenusPath } from '../utils/nodeGettersByCriterias.js';\nimport { getNewSelectionSingleSelect } from '../utils/singleSelectionHelpers.js';\nimport type { useFocusTracker } from './useFocusTracker.js';\nimport type { useMenuOpenStatus } from './useMenuOpenStatus.js';\n\ntype ItemKeydownHelpersArgs = {\n pseudoFocusedItemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent;\n};\ntype SubmenuChangeMetainfo = {\n itemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.SyntheticEvent;\n};\n\ntype UseMenuItemEventsHandlersConfig = {\n propsWithDefault: DSMenuBehaviouralContextProviderT.InternalProps;\n focusTrackers: ReturnType<typeof useFocusTracker>;\n menuOpenStatus: ReturnType<typeof useMenuOpenStatus>;\n handleChangeOpenedSubItems: Required<DSMenuButtonT.MenuBehaviouralLayerOptionalProps>['onDisplayedSubmenuChange'];\n};\n\nexport const useMenuItemEventsHandlers = ({\n propsWithDefault,\n focusTrackers,\n menuOpenStatus,\n handleChangeOpenedSubItems,\n}: UseMenuItemEventsHandlersConfig) => {\n const { onItemSelected, onActivateItem, selectedNodes } = propsWithDefault;\n const { onOpinionatedClose } = menuOpenStatus;\n const {\n trackFocusFirstChildItem,\n trackFocusLastChildItem,\n trackFocusNextItem,\n trackFocusPreviousItem,\n trackFocusParentItem,\n trackFocusNode,\n focusedElementItemNode,\n trackFocusTrigger,\n } = focusTrackers;\n\n const openSubmenu = React.useCallback(\n ({\n itemNode,\n event,\n }: {\n itemNode: DSMenuButtonT.WithSubmenuMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent | React.FocusEvent;\n }) => {\n if (!isWithSubmenuNode(itemNode)) {\n console.log('openSubmenu -> itemNode:', itemNode);\n throw FORBIDDEN_BEHAVIOURS.TRYING_TO_OPEN_SUBMENU_OF_NODE_WITHOUT_SUBMENU;\n }\n const subMenuNodesPath = getSubMenusPath(itemNode);\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...subMenuNodesPath, itemNode];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n },\n [handleChangeOpenedSubItems],\n );\n const closeSubmenusAfterNode = React.useCallback(\n ({\n itemNode,\n event,\n }: {\n itemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent | React.FocusEvent;\n }) => {\n const subMenuNodesPath = getSubMenusPath(itemNode);\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...subMenuNodesPath];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n },\n [handleChangeOpenedSubItems],\n );\n const closeCurrentSubmenu = React.useCallback(\n ({\n itemNode,\n event,\n }: {\n itemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent;\n }) => {\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const subMenuNodesPath = getSubMenusPath(itemNode);\n // if length is 0, it means the parent is the root node, so we close all the submenus\n if (subMenuNodesPath.length === 0) {\n handleChangeOpenedSubItems([], metainfo);\n return { currentSubmenuWasMainMenu: true };\n }\n\n // we want to close current submenu, so we pop the last element\n // getSubMenusPath guarantees the order by walkParents + reverse at the end\n subMenuNodesPath.pop();\n handleChangeOpenedSubItems(subMenuNodesPath, metainfo);\n return { currentSubmenuWasMainMenu: false };\n },\n [handleChangeOpenedSubItems],\n );\n\n /*\n * 13th August 2024:\n * https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/\n * Additional roles, states, and properties needed for the menu element are described in the Menu and Menubar Pattern.\n * https://www.w3.org/WAI/ARIA/apg/patterns/menubar/\n * Because menubar and menu elements are composite widgets as described in the practice for Keyboard Navigation Inside Components,\n * Tab and Shift + Tab do not move focus among the items in the menu.\n * Instead, the keyboard commands described in this section enable users to move focus among the elements in a menubar or menu.\n * Tab and Shift + Tab:\n * - Move focus into a menubar <- Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n * - When focus is on a menuitem in a menu or menubar, move focus out of the menu or menubar, and close all menus and submenus.\n * ************************************************************************************************************************************\n * this will be implemented by not setting document.activeElement to the menuitem but keeping it on the trigger button\n * why? because there is no javascript API to trigger a \"tab\"/\"shift+tab\" navigation and any algorithm trying to do so is fragile\n * also, this is enforced by w3.org too as per:\n * ************************************************************************************************************************************\n * Note that Tab and Shift + Tab do not move focus into a menu.\n * Unlike a menubar, a menu is not visually persistent,\n * and authors are responsible for ensuring focus moves to an item inside of a menu when the menu opens.\n *\n */\n const handleMenuItemEnterKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Enter:\n // - When focus is on a menuitem that has a submenu, opens the submenu and places focus on its first item.\n // ...\n if (isWithSubmenuOnlyNode(pseudoFocusedItemNode)) {\n openSubmenu({ itemNode: pseudoFocusedItemNode, event });\n trackFocusFirstChildItem(pseudoFocusedItemNode);\n } else {\n // - Otherwise, activates the item...\n\n // ----------------------------------\n // 4. Although it is recommended that authors avoid doing so,\n // some implementations of navigation menubars may have menuitem elements that both perform a function and open a submenu.\n // In such implementations, Enter and Space perform a navigation function,\n // e.g., load new content, while Down Arrow, in a horizontal menubar, opens the submenu associated with that same menuitem.\n // ---------------------------------\n // Based on an investigation, the above statement is not specific to \"navigation\" nor to ONLY \"menubars\", it really is about \"menuitems\" in general\n // as long as the role=\"menuitem\" & aria-haspopup=\"${id-of-the-submenu}\" & aria-expanded=\"${true/false}\" (& aria-orientation=\"vertical/horizontal\" if non-default)\n // are set, the Screen Readers will provide the same announcement for the menuitem regardless of the parent role\n\n // \"activation\" is a concept,\n // WCAG here implies that for menuitemscheckbox and menuitemsradio this triggers the selection\n // for menuitems (that doesn't fit selection nor submenus) it triggers the action\n // this also triggers enter -> single-select-item-with-submenu as expected\n if (isMultipleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionMultipleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n } else if (isSingleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionSingleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n } else {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n onActivateItem!(pseudoFocusedItemNode, { itemNode: pseudoFocusedItemNode, event });\n }\n // ... and closes the menu.\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n onOpinionatedClose();\n }\n },\n [\n trackFocusFirstChildItem,\n openSubmenu,\n handleChangeOpenedSubItems,\n onOpinionatedClose,\n selectedNodes,\n onItemSelected,\n onActivateItem,\n ],\n );\n const handleMenuItemSpaceKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Space:\n // [X] (Optional): When focus is on a menuitem that has a submenu, opens the submenu and places focus on its first item.\n // ----------------------------------\n // 4. Although it is recommended that authors avoid doing so,\n // some implementations of navigation menubars may have menuitem elements that both perform a function and open a submenu.\n // In such implementations, Enter and Space perform a navigation function,\n // e.g., load new content, while Down Arrow, in a horizontal menubar, opens the submenu associated with that same menuitem.\n // ---------------------------------\n // Based on an investigation, the above statement is not specific to \"navigation\" nor to ONLY \"menubars\", it really is about \"menuitems\" in general\n // as long as the role=\"menuitem\" & aria-haspopup=\"${id-of-the-submenu}\" & aria-expanded=\"${true/false}\" (& aria-orientation=\"vertical/horizontal\" if non-default)\n // are set, the Screen Readers will provide the same announcement for the menuitem regardless of the parent role\n if (isWithSubmenuOnlyNode(pseudoFocusedItemNode)) {\n openSubmenu({ itemNode: pseudoFocusedItemNode, event });\n trackFocusFirstChildItem(pseudoFocusedItemNode);\n return;\n }\n // [X] (Optional): When focus is on a menuitemcheckbox, changes the state without closing the menu.\n if (isMultipleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionMultipleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n return;\n }\n // [X] (Optional): When focus is on a menuitemradio that is not checked, without closing the menu,\n // checks the focused menuitemradio and unchecks any other checked menuitemradio element in the same group.\n if (isSingleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionSingleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n return;\n }\n // [X] (Optional): When focus is on a menuitem that does not have a submenu, activates the menuitem and closes the menu.\n onActivateItem?.(pseudoFocusedItemNode, { itemNode: pseudoFocusedItemNode, event });\n\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n onOpinionatedClose();\n },\n [\n onActivateItem,\n handleChangeOpenedSubItems,\n onOpinionatedClose,\n trackFocusFirstChildItem,\n openSubmenu,\n selectedNodes,\n onItemSelected,\n ],\n );\n const handleMenuItemRightArrowKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Right Arrow:\n // [N/A] When focus is in a menubar, moves focus to the next item, optionally wrapping from the last to the first.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n\n // [X] When focus is in a menu and on a menuitem that has a submenu, opens the submenu and places focus on its first item.\n // [N/A] When focus is in a menu and on an item that does not have a submenu, performs the following 3 actions:\n // [N/A] Closes the submenu and any parent menus.\n // [N/A] Moves focus to the next item in the menubar.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n\n // [X] If focus is now on a menuitem with a submenu, either:\n // [X] (Recommended) opens the submenu of that menuitem without moving focus into the submenu,\n // /\\ we choose the recommended option\n // OR\n // [N/A] opens the submenu of that menuitem and places focus on the first item in the submenu.\n // ***********************************************************\n // Dimsum implements IoC, if APP side for whatever reason wants to open the new sub sub menu,\n // it's up to them to listen to the pseudo focus event and open the sub sub menu\n // ***********************************************************\n if (isWithSubmenuNode(pseudoFocusedItemNode)) {\n openSubmenu({ itemNode: pseudoFocusedItemNode, event });\n trackFocusFirstChildItem(pseudoFocusedItemNode);\n return;\n }\n // Note that if the menubar were not present, e.g., the menus were opened from a menubutton,\n // Right Arrow would not do anything when focus is on an item that does not have a submenu.\n return;\n },\n [openSubmenu, trackFocusFirstChildItem],\n );\n\n const handleMenuItemLeftArrowKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Left Arrow:\n // [N/A] When focus is in a menubar, moves focus to the previous item, optionally wrapping from the first to the last.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n\n // [X] When focus is in a submenu of an item in a menu, closes the submenu and returns focus to the parent menuitem.\n const { currentSubmenuWasMainMenu } = closeCurrentSubmenu({ itemNode: pseudoFocusedItemNode, event });\n if (!currentSubmenuWasMainMenu) {\n trackFocusParentItem(pseudoFocusedItemNode);\n }\n\n // [N/A] When focus is in a submenu of an item in a menubar, performs the following 3 actions:\n // [N/A] Closes the submenu.\n // [N/A] Moves focus to the previous item in the menubar.\n // [N/A] If focus is now on a menuitem with a submenu, either:\n // (Recommended) opens the submenu of that menuitem without moving focus into the submenu,\n // or\n // opens the submenu of that menuitem and places focus on the first item in the submenu.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n },\n [closeCurrentSubmenu, trackFocusParentItem],\n );\n\n const handleFocusableMenuItemKeyDown = React.useCallback<React.KeyboardEventHandler<HTMLDivElement>>(\n (event) => {\n const pseudoFocusedItemNode = focusedElementItemNode.current;\n // if there is no focused item, we can't handle the logics\n if (pseudoFocusedItemNode === null) throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_NODE_NOT_SET;\n // if the item contains an `onKeyDown` prop, we trigger it\n if (pseudoFocusedItemNode.plainItem.onKeyDown) pseudoFocusedItemNode.plainItem.onKeyDown(event);\n // TAB:\n // When focus is on a menuitem in a menu or menubar, move focus to the trigger so tab flows occurs from there.\n // Then close all menus and submenus.\n if (event.key === 'Tab') {\n trackFocusTrigger({ shouldFocusImmediately: true });\n onOpinionatedClose();\n return;\n }\n // Down Arrow:\n // [N/A] When focus is on a menuitem in a menubar, and that menuitem has a submenu, opens the submenu and places focus on the first item in the submenu.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n // [X] When focus is in a menu, moves focus to the next item,\n // [X] optionally wrapping from the last to the first.\n if (event.key === 'ArrowDown') {\n event?.preventDefault(); // if scrollbar are present, this keydown event should not interact with the scrollbar\n trackFocusNextItem(pseudoFocusedItemNode);\n return;\n }\n // Up Arrow:\n // [X] When focus is in a menu, moves focus to the previous item,\n // [X] optionally wrapping from the first to the last.\n // [N/A] (Optional): When focus is on a menuitem in a menubar, and that menuitem has a submenu, opens the submenu and places focus on the last item in the submenu.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n if (event.key === 'ArrowUp') {\n event?.preventDefault(); // if scrollbar are present, this keydown event should not interact with the scrollbar\n trackFocusPreviousItem(pseudoFocusedItemNode);\n return;\n }\n // Left Arrow:\n if (event.key === 'ArrowLeft') {\n event?.preventDefault(); // if scrollbar are present, this keydown event should not interact with the scrollbar\n handleMenuItemLeftArrowKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n // Home: If arrow key wrapping is not supported, moves focus to the first item in the current menu or menubar.\n if (event.key === 'Home') {\n if (!pseudoFocusedItemNode.parent) {\n console.log('handleFocusableMenuItemKeyDown -> Home > focused item node', pseudoFocusedItemNode);\n // should be impossible, at most the .parent is the root node, which is not able to trigger this event\n throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_HAS_NO_PARENT;\n }\n trackFocusFirstChildItem(pseudoFocusedItemNode.parent);\n return;\n }\n // End: If arrow key wrapping is not supported, moves focus to the last item in the current menu or menubar.\n if (event.key === 'End') {\n if (!pseudoFocusedItemNode.parent) {\n console.log('handleFocusableMenuItemKeyDown -> Home > focused item node', pseudoFocusedItemNode);\n // should be impossible, at most the .parent is the root node, which is not able to trigger this event\n throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_HAS_NO_PARENT;\n }\n trackFocusLastChildItem(pseudoFocusedItemNode.parent);\n return;\n }\n if (event.key === 'Escape') {\n // if the item has no submenu, the alt key is transparent, we behave as if it's not even pressed\n if (event.altKey && isWithSubmenuNode(pseudoFocusedItemNode)) {\n // PUI-14639 - Implement submenu dismissal mechanism\n // Description: we need to add a mechanism to close submenus that automatically expand when a menu item is focused with the keyboard.\n // we have decided to implement a special shortcut (Alt/Option + Esc) as the dismissal mechanism.\n // This change is necessary to comply with WCAG success criterion 1.4.13.\n // https://www.w3.org/WAI/WCAG21/Understanding/content-on-hover-or-focus.html\n // if the item has a submenu, we close it\n closeSubmenusAfterNode({ itemNode: pseudoFocusedItemNode, event });\n return;\n }\n // Escape: Close the menu that contains focus and return focus to the element or context, e.g., menu button or parent menuitem, from which the menu was opened\n const { currentSubmenuWasMainMenu } = closeCurrentSubmenu({ itemNode: pseudoFocusedItemNode, event });\n if (currentSubmenuWasMainMenu) {\n // if the menu we are trying to close is the main menu, we additionally close the main menu itself\n // we focus the trigger immediately (shouldFocusImmediately: true) so the focus return is synchronous\n // and not deferred to requestAnimationFrame \u2014 Escape on the trigger is safe since it only calls\n // onOpinionatedClose() which is already a no-op when the menu is closed\n trackFocusTrigger({ shouldFocusImmediately: true });\n onOpinionatedClose({ skipFocus: true });\n } else {\n trackFocusParentItem(pseudoFocusedItemNode);\n }\n return;\n }\n\n /* ************************************************************************************\n * ******* ACTIVATION & SUBMENU keys doesn't apply if the item is \"disabled\" **********\n * ************************************************************************************ */\n if (pseudoFocusedItemNode.plainItem.disabled) return;\n // Enter:\n if (event.key === 'Enter') {\n handleMenuItemEnterKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n // Space:\n if (event.key === ' ') {\n event?.preventDefault(); // if scrollbar are present, this keydown event should not interact with the scrollbar\n handleMenuItemSpaceKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n // Right Arrow:\n if (event.key === 'ArrowRight') {\n event?.preventDefault(); // if scrollbar are present, this keydown event should not interact with the scrollbar\n handleMenuItemRightArrowKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n },\n [\n focusedElementItemNode,\n trackFocusTrigger,\n trackFocusNextItem,\n trackFocusPreviousItem,\n handleMenuItemLeftArrowKeyDown,\n trackFocusFirstChildItem,\n trackFocusLastChildItem,\n closeCurrentSubmenu,\n trackFocusParentItem,\n closeSubmenusAfterNode,\n onOpinionatedClose,\n handleMenuItemEnterKeyDown,\n handleMenuItemSpaceKeyDown,\n handleMenuItemRightArrowKeyDown,\n ],\n );\n const handleFocusableMenuItemClick = React.useCallback(\n (event: React.MouseEvent<HTMLDivElement>) => {\n const pseudoFocusedItemNode = focusedElementItemNode.current;\n if (pseudoFocusedItemNode === null) throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_NODE_NOT_SET;\n // if the item is disabled, we don't do anything\n if (pseudoFocusedItemNode.plainItem.disabled) return;\n // we need to handle which submenu needs to close automagically based on the click\n // starting from the itemNode, we check:\n // - the parent of the itemNode is root?\n // - yes -> we close all the submenus\n const parentMenuNode = pseudoFocusedItemNode.parent;\n if (isRootNode(parentMenuNode)) {\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n } else {\n // - no -> we close all the submenus that are not in the path to the root of the itemNode\n // this is necessary on click because the user can still interact with other (visual) depths of the menu\n // during keyboard navigation, the user is limited to the current (visual) depth\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...getSubMenusPath(pseudoFocusedItemNode)];\n // - if the clicked node has a submenu, keep it open\n if (isWithSubmenuNode(pseudoFocusedItemNode)) {\n newOpenedItems.push(pseudoFocusedItemNode);\n }\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n }\n\n // to all effects and purposes, the click event is the same as the space key\n // the main difference between enter and space is that\n // - enter ALWAYS closes the menu excepts for submenus\n // - space closes the menu if \"activable\", selections doesn't, submenus doesn't\n handleMenuItemSpaceKeyDown({ pseudoFocusedItemNode, event });\n // if the item contains an `onClick` prop, we trigger it\n if (pseudoFocusedItemNode.plainItem.onClick) pseudoFocusedItemNode.plainItem.onClick(event);\n },\n [focusedElementItemNode, handleMenuItemSpaceKeyDown, handleChangeOpenedSubItems],\n );\n\n // the \"menu-button\" widget is an opinionated widget with a specific behaviour to follow when a focusable item is \"focused\"\n // this function achieve the opinionated \"focus an item\" opinionated behaviour (regardless of why the item is focused)\n const opinionatedFocusBehaviour = React.useCallback(\n // we currently know only one \"event\" that can trigger this opinionated behaviour\n // - focus => React.FocusEvent <-- triggered by ATs \"focus mode\" or \"virtual cursor\" navigation\n // NOTE:\n // mouseEnter => React.MouseEvent\n // still ends up calling this function, but as a side effect of mouse-enter focusing the item, not as the main event\n (itemNode: DSMenuButtonT.PseudoFocusableMenuNodes, event: React.FocusEvent) => {\n if (!isFocusableNode(itemNode)) {\n console.log('handleFocusableMenuItemOnMouseEnter > itemNode:', itemNode);\n throw FORBIDDEN_BEHAVIOURS.TRYING_TO_FOCUS_NON_FOCUSABLE_NODE;\n }\n // on hover -> we track the hovered item as the focused one on top of which events should work on\n trackFocusNode(itemNode);\n if (!isWithSubmenuNode(itemNode)) {\n // if the item has nosubmenu we close all the siblings submenus\n const subMenuNodesPath = getSubMenusPath(itemNode);\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...subMenuNodesPath];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n return;\n }\n // if the item is disabled, we don't open the submenu, and there is nothing else to do\n if (itemNode.plainItem.disabled) return;\n // if not disabled and it has a submenu, we open it, and there is nothing else to do\n openSubmenu({ itemNode, event });\n },\n [handleChangeOpenedSubItems, openSubmenu, trackFocusNode],\n );\n\n // when an item is hovered, we focus it\n // this effectively achieves:\n // - the item is focused (it trigger the handleFocusableMenuItemNativeFocusEvent function because render runs node.focus())\n // - if the user presses a keyboard key, the interaction will work starting from the (hovered hence focused) item\n const handleFocusableMenuItemOnMouseEnter = React.useCallback(\n (itemNode: DSMenuButtonT.PseudoFocusableMenuNodes) => {\n if (!isFocusableNode(itemNode)) {\n console.log('handleFocusableMenuItemOnMouseEnter > itemNode:', itemNode);\n throw FORBIDDEN_BEHAVIOURS.TRYING_TO_FOCUS_NON_FOCUSABLE_NODE;\n }\n // on hover -> we track the hovered item as the focused one on top of which events should work on\n trackFocusNode(itemNode);\n },\n [trackFocusNode],\n );\n\n const handleFocusableMenuItemNativeFocusEvent = React.useCallback(\n (itemNode: DSMenuButtonT.PseudoFocusableMenuNodes, event: React.FocusEvent) => {\n opinionatedFocusBehaviour(itemNode, event);\n },\n [opinionatedFocusBehaviour],\n );\n\n return React.useMemo(\n () => ({\n handleFocusableMenuItemKeyDown,\n handleFocusableMenuItemClick,\n handleFocusableMenuItemOnMouseEnter,\n handleFocusableMenuItemNativeFocusEvent,\n }),\n [\n handleFocusableMenuItemClick,\n handleFocusableMenuItemKeyDown,\n handleFocusableMenuItemNativeFocusEvent,\n handleFocusableMenuItemOnMouseEnter,\n ],\n );\n};\n"],
|
|
5
|
-
"mappings": "AAAA,YAAY,WAAW;ACMvB,OAAOA,YAAW;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,sBAAsB,kCAAkC;AAEjE,SAAS,qCAAqC;AAC9C,SAAS,uBAAuB;AAChC,SAAS,mCAAmC;
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "/* eslint-disable no-console */\n/* eslint-disable max-lines */\n// in this files the returns are not useless, they are used to clearly demark the intended behavior shortcircuit for the next developer that reads the code\n/* eslint-disable no-useless-return */\n/* eslint-disable complexity */\n/* eslint-disable max-statements */\nimport { isActivationKey } from '@elliemae/ds-props-helpers';\nimport React from 'react';\nimport { type DSMenuButtonT } from '../../../react-desc-prop-types.js';\nimport {\n isFocusableNode,\n isItemDisabled,\n isItemLocked,\n isMultipleSelectNode,\n isRootNode,\n isSingleSelectNode,\n isWithSubmenuNode,\n isWithSubmenuOnlyNode,\n} from '../../../utils/nodesTypeguardsAndGetters.js';\nimport { FORBIDDEN_BEHAVIOURS, UNEXPECTED_INTERNAL_ERRORS } from '../constants/Errors.js';\nimport { type DSMenuBehaviouralContextProviderT } from '../react-desc-prop-types.js';\nimport { getNewSelectionMultipleSelect } from '../utils/multipleSelectionHelpers.js';\nimport { getSubMenusPath } from '../utils/nodeGettersByCriterias.js';\nimport { getNewSelectionSingleSelect } from '../utils/singleSelectionHelpers.js';\nimport type { useFocusTracker } from './useFocusTracker.js';\nimport type { useMenuOpenStatus } from './useMenuOpenStatus.js';\n\ntype ItemKeydownHelpersArgs = {\n pseudoFocusedItemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent;\n};\ntype SubmenuChangeMetainfo = {\n itemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.SyntheticEvent;\n};\n\ntype UseMenuItemEventsHandlersConfig = {\n propsWithDefault: DSMenuBehaviouralContextProviderT.InternalProps;\n focusTrackers: ReturnType<typeof useFocusTracker>;\n menuOpenStatus: ReturnType<typeof useMenuOpenStatus>;\n handleChangeOpenedSubItems: Required<DSMenuButtonT.MenuBehaviouralLayerOptionalProps>['onDisplayedSubmenuChange'];\n};\n\n// the item's own `onClick` is part of the item contract for ANY activation (its type accepts keyboard events):\n// the click path already invokes it, so the keyboard activation (Enter/Space) must invoke it too.\n// The click path routes through the Space handler and then calls `onClick` itself, hence the `keydown` check to never fire it twice.\nconst invokeItemOnClickOnKeyboardActivation = (\n pseudoFocusedItemNode: DSMenuButtonT.PseudoFocusableMenuNodes,\n event?: React.KeyboardEvent | React.MouseEvent,\n) => {\n if (event?.type === 'keydown' && pseudoFocusedItemNode.plainItem.onClick)\n pseudoFocusedItemNode.plainItem.onClick(event as React.KeyboardEvent);\n};\n\nexport const useMenuItemEventsHandlers = ({\n propsWithDefault,\n focusTrackers,\n menuOpenStatus,\n handleChangeOpenedSubItems,\n}: UseMenuItemEventsHandlersConfig) => {\n const { onItemSelected, onActivateItem, onItemKeyDown, selectedNodes, buttonDOMNodeRef } = propsWithDefault;\n const { onOpinionatedClose } = menuOpenStatus;\n const {\n trackFocusFirstChildItem,\n trackFocusLastChildItem,\n trackFocusNextItem,\n trackFocusPreviousItem,\n trackFocusParentItem,\n trackFocusNode,\n focusedElementItemNode,\n trackFocusTrigger,\n trackFocusRegionReset,\n } = focusTrackers;\n\n const openSubmenu = React.useCallback(\n ({\n itemNode,\n event,\n }: {\n itemNode: DSMenuButtonT.WithSubmenuMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent | React.FocusEvent;\n }) => {\n if (!isWithSubmenuNode(itemNode)) {\n console.log('openSubmenu -> itemNode:', itemNode);\n throw FORBIDDEN_BEHAVIOURS.TRYING_TO_OPEN_SUBMENU_OF_NODE_WITHOUT_SUBMENU;\n }\n const subMenuNodesPath = getSubMenusPath(itemNode);\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...subMenuNodesPath, itemNode];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n },\n [handleChangeOpenedSubItems],\n );\n const closeSubmenusAfterNode = React.useCallback(\n ({\n itemNode,\n event,\n }: {\n itemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent | React.FocusEvent;\n }) => {\n const subMenuNodesPath = getSubMenusPath(itemNode);\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...subMenuNodesPath];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n },\n [handleChangeOpenedSubItems],\n );\n const closeCurrentSubmenu = React.useCallback(\n ({\n itemNode,\n event,\n }: {\n itemNode: DSMenuButtonT.PseudoFocusableMenuNodes;\n event?: React.KeyboardEvent | React.MouseEvent;\n }) => {\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const subMenuNodesPath = getSubMenusPath(itemNode);\n // if length is 0, it means the parent is the root node, so we close all the submenus\n if (subMenuNodesPath.length === 0) {\n handleChangeOpenedSubItems([], metainfo);\n return { currentSubmenuWasMainMenu: true };\n }\n\n // we want to close current submenu, so we pop the last element\n // getSubMenusPath guarantees the order by walkParents + reverse at the end\n subMenuNodesPath.pop();\n handleChangeOpenedSubItems(subMenuNodesPath, metainfo);\n return { currentSubmenuWasMainMenu: false };\n },\n [handleChangeOpenedSubItems],\n );\n\n /*\n * 13th August 2024:\n * https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/\n * Additional roles, states, and properties needed for the menu element are described in the Menu and Menubar Pattern.\n * https://www.w3.org/WAI/ARIA/apg/patterns/menubar/\n * Because menubar and menu elements are composite widgets as described in the practice for Keyboard Navigation Inside Components,\n * Tab and Shift + Tab do not move focus among the items in the menu.\n * Instead, the keyboard commands described in this section enable users to move focus among the elements in a menubar or menu.\n * Tab and Shift + Tab:\n * - Move focus into a menubar <- Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n * - When focus is on a menuitem in a menu or menubar, move focus out of the menu or menubar, and close all menus and submenus.\n * ************************************************************************************************************************************\n * this will be implemented by not setting document.activeElement to the menuitem but keeping it on the trigger button\n * why? because there is no javascript API to trigger a \"tab\"/\"shift+tab\" navigation and any algorithm trying to do so is fragile\n * also, this is enforced by w3.org too as per:\n * ************************************************************************************************************************************\n * Note that Tab and Shift + Tab do not move focus into a menu.\n * Unlike a menubar, a menu is not visually persistent,\n * and authors are responsible for ensuring focus moves to an item inside of a menu when the menu opens.\n *\n */\n const handleMenuItemEnterKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Enter:\n // - When focus is on a menuitem that has a submenu, opens the submenu and places focus on its first item.\n // ...\n if (isWithSubmenuOnlyNode(pseudoFocusedItemNode)) {\n openSubmenu({ itemNode: pseudoFocusedItemNode, event });\n trackFocusFirstChildItem(pseudoFocusedItemNode);\n } else {\n // - Otherwise, activates the item...\n\n // ----------------------------------\n // 4. Although it is recommended that authors avoid doing so,\n // some implementations of navigation menubars may have menuitem elements that both perform a function and open a submenu.\n // In such implementations, Enter and Space perform a navigation function,\n // e.g., load new content, while Down Arrow, in a horizontal menubar, opens the submenu associated with that same menuitem.\n // ---------------------------------\n // Based on an investigation, the above statement is not specific to \"navigation\" nor to ONLY \"menubars\", it really is about \"menuitems\" in general\n // as long as the role=\"menuitem\" & aria-haspopup=\"${id-of-the-submenu}\" & aria-expanded=\"${true/false}\" (& aria-orientation=\"vertical/horizontal\" if non-default)\n // are set, the Screen Readers will provide the same announcement for the menuitem regardless of the parent role\n\n // \"activation\" is a concept,\n // WCAG here implies that for menuitemscheckbox and menuitemsradio this triggers the selection\n // for menuitems (that doesn't fit selection nor submenus) it triggers the action\n // this also triggers enter -> single-select-item-with-submenu as expected\n if (isMultipleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionMultipleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n } else if (isSingleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionSingleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n } else {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n onActivateItem!(pseudoFocusedItemNode, { itemNode: pseudoFocusedItemNode, event });\n }\n invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);\n // ... and closes the menu.\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n onOpinionatedClose({ reason: 'item-activation', event });\n }\n },\n [\n trackFocusFirstChildItem,\n openSubmenu,\n handleChangeOpenedSubItems,\n onOpinionatedClose,\n selectedNodes,\n onItemSelected,\n onActivateItem,\n ],\n );\n const handleMenuItemSpaceKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Space:\n // [X] (Optional): When focus is on a menuitem that has a submenu, opens the submenu and places focus on its first item.\n // ----------------------------------\n // 4. Although it is recommended that authors avoid doing so,\n // some implementations of navigation menubars may have menuitem elements that both perform a function and open a submenu.\n // In such implementations, Enter and Space perform a navigation function,\n // e.g., load new content, while Down Arrow, in a horizontal menubar, opens the submenu associated with that same menuitem.\n // ---------------------------------\n // Based on an investigation, the above statement is not specific to \"navigation\" nor to ONLY \"menubars\", it really is about \"menuitems\" in general\n // as long as the role=\"menuitem\" & aria-haspopup=\"${id-of-the-submenu}\" & aria-expanded=\"${true/false}\" (& aria-orientation=\"vertical/horizontal\" if non-default)\n // are set, the Screen Readers will provide the same announcement for the menuitem regardless of the parent role\n if (isWithSubmenuOnlyNode(pseudoFocusedItemNode)) {\n openSubmenu({ itemNode: pseudoFocusedItemNode, event });\n trackFocusFirstChildItem(pseudoFocusedItemNode);\n return;\n }\n // [X] (Optional): When focus is on a menuitemcheckbox, changes the state without closing the menu.\n if (isMultipleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionMultipleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);\n return;\n }\n // [X] (Optional): When focus is on a menuitemradio that is not checked, without closing the menu,\n // checks the focused menuitemradio and unchecks any other checked menuitemradio element in the same group.\n if (isSingleSelectNode(pseudoFocusedItemNode)) {\n // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator\n // useAdvancedValidation guarantees the non-null in this \"if\" branch\n const newSelection = getNewSelectionSingleSelect(selectedNodes!, pseudoFocusedItemNode);\n onItemSelected!(newSelection, { itemNode: pseudoFocusedItemNode, event });\n invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);\n return;\n }\n // [X] (Optional): When focus is on a menuitem that does not have a submenu, activates the menuitem and closes the menu.\n onActivateItem?.(pseudoFocusedItemNode, { itemNode: pseudoFocusedItemNode, event });\n invokeItemOnClickOnKeyboardActivation(pseudoFocusedItemNode, event);\n\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n onOpinionatedClose({ reason: 'item-activation', event });\n },\n [\n onActivateItem,\n handleChangeOpenedSubItems,\n onOpinionatedClose,\n trackFocusFirstChildItem,\n openSubmenu,\n selectedNodes,\n onItemSelected,\n ],\n );\n const handleMenuItemRightArrowKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Right Arrow:\n // [N/A] When focus is in a menubar, moves focus to the next item, optionally wrapping from the last to the first.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n\n // [X] When focus is in a menu and on a menuitem that has a submenu, opens the submenu and places focus on its first item.\n // [N/A] When focus is in a menu and on an item that does not have a submenu, performs the following 3 actions:\n // [N/A] Closes the submenu and any parent menus.\n // [N/A] Moves focus to the next item in the menubar.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n\n // [X] If focus is now on a menuitem with a submenu, either:\n // [X] (Recommended) opens the submenu of that menuitem without moving focus into the submenu,\n // /\\ we choose the recommended option\n // OR\n // [N/A] opens the submenu of that menuitem and places focus on the first item in the submenu.\n // ***********************************************************\n // Dimsum implements IoC, if APP side for whatever reason wants to open the new sub sub menu,\n // it's up to them to listen to the pseudo focus event and open the sub sub menu\n // ***********************************************************\n if (isWithSubmenuNode(pseudoFocusedItemNode)) {\n openSubmenu({ itemNode: pseudoFocusedItemNode, event });\n trackFocusFirstChildItem(pseudoFocusedItemNode);\n }\n // Note that if the menubar were not present, e.g., the menus were opened from a menubutton,\n // Right Arrow would not do anything when focus is on an item that does not have a submenu.\n // \"Does nothing\" is still a demand on whoever is around the menu: the key is consumed on every item,\n // see handleFocusableMenuItemKeyDown.\n },\n [openSubmenu, trackFocusFirstChildItem],\n );\n\n const handleMenuItemLeftArrowKeyDown = React.useCallback(\n ({ pseudoFocusedItemNode, event }: ItemKeydownHelpersArgs) => {\n // Left Arrow:\n // [N/A] When focus is in a menubar, moves focus to the previous item, optionally wrapping from the first to the last.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n\n // [X] When focus is in a submenu of an item in a menu, closes the submenu and returns focus to the parent menuitem.\n // on the main menu there is no submenu to leave and nothing else happens; the key is still consumed, see\n // handleFocusableMenuItemKeyDown\n const { currentSubmenuWasMainMenu } = closeCurrentSubmenu({ itemNode: pseudoFocusedItemNode, event });\n if (!currentSubmenuWasMainMenu) {\n trackFocusParentItem(pseudoFocusedItemNode);\n }\n\n // [N/A] When focus is in a submenu of an item in a menubar, performs the following 3 actions:\n // [N/A] Closes the submenu.\n // [N/A] Moves focus to the previous item in the menubar.\n // [N/A] If focus is now on a menuitem with a submenu, either:\n // (Recommended) opens the submenu of that menuitem without moving focus into the submenu,\n // or\n // opens the submenu of that menuitem and places focus on the first item in the submenu.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n },\n [closeCurrentSubmenu, trackFocusParentItem],\n );\n\n /*\n * TAB (APG): \"When focus is on a menuitem in a menu or menubar, move focus out of the menu or menubar, and close\n * all menus and submenus.\"\n *\n * Tab is an event of the TRIGGER, not of the items. The menu is the trigger's contextual child (it only lives in a\n * portal to solve z-index/visual issues): real browser focus sits on the items \u2014 instead of a simulated\n * pseudo-focus \u2014 so assistive technologies announce them, but to the browser the Tab must behave as if focus never\n * left the trigger. So the items do not take part in it at all (their own `onKeyDown` is not invoked for Tab) and\n * it cannot be altered from them: it is handled in the CAPTURE phase of the root menu, before anything else can\n * stop or alter the event.\n *\n * The ONE place in this widget where focus is moved from a handler instead of the focus tracker's ref callbacks \u2014\n * on purpose (see KNOWN_INTENTIONAL_DEVIATIONS.md, \"Tab moves focus from the keydown handler\"). Tab/Shift+Tab have\n * a browser DEFAULT ACTION that runs right after the keydown handlers: it moves focus on from wherever focus is at\n * that moment. Anything that moves focus later (tracker write -> render -> ref callback, throttled or deferred) is\n * too late: the default has already moved on from the (about to unmount) item and focus ends on the body (Tab) or\n * after the menu (Shift+Tab). So: focus the trigger ONCE, synchronously, and let the browser take over from there\n * (next tabbable for Tab, previous for Shift+Tab).\n */\n const handleMenuTabKeyDownCapture = React.useCallback<React.KeyboardEventHandler<HTMLElement>>(\n (event) => {\n if (event.key !== 'Tab') return;\n buttonDOMNodeRef.current?.focus();\n // focus is about to leave the whole button navigation for another element of the page: neutralize the focus\n // tracker (\"nothing to focus\") so nothing in it can ever steal the focus back\n trackFocusRegionReset();\n onOpinionatedClose({ skipFocus: true, reason: 'tab', event });\n },\n [buttonDOMNodeRef, onOpinionatedClose, trackFocusRegionReset],\n );\n\n const handleFocusableMenuItemKeyDown = React.useCallback<React.KeyboardEventHandler<HTMLDivElement>>(\n (event) => {\n // TAB is handled in the CAPTURE phase of the root menu (`handleMenuTabKeyDownCapture`, above): by the time this\n // handler runs, the trigger already holds focus and the tracker/menu state already moved on\n if (event.key === 'Tab') return;\n // A key the menu USES is consumed: preventDefault + stopPropagation. React bubbles synthetic events through the\n // portal, so without this an ancestor that owns the same key (a toolbar moving focus with Home/End/Left/Right, a\n // dialog closing on Escape...) would act on it too and pull focus out of the open menu. The old\n // DSDropdownMenuV2 listbox stopped every key it handled for the same reason.\n // A key that does nothing for the focused item is NOT consumed (activation keys on a locked item, any key the\n // menu has no use for): it keeps reaching the container.\n // EXCEPTION: Left and Right Arrow are consumed on EVERY item, also where the menu has nothing to do with them\n // (Right on an item without a submenu, Left on the main menu, locked items). APG, toolbar example, keyboard\n // support table (https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/examples/toolbar/): for both Right Arrow and\n // Left Arrow it says \"If an item in the popup menu has focus, does nothing.\" A menu button in a toolbar is\n // exactly that case, and the pattern itself demands the toolbar does not act on the key: for that to be true\n // the key must not bubble out of the popup (React bubbles synthetic events through the portal) nor trigger\n // its default.\n const consumeKey = () => {\n event.preventDefault(); // if scrollbar are present, a consumed key should not interact with the scrollbar\n event.stopPropagation();\n };\n const pseudoFocusedItemNode = focusedElementItemNode.current;\n // if there is no focused item, we can't handle the logics\n if (pseudoFocusedItemNode === null) throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_NODE_NOT_SET;\n // if the item contains an `onKeyDown` prop, we trigger it, but a locked item (disabled or applyAriaDisabled)\n // does not get Enter/Space\n const isLockedActivation = isItemLocked(pseudoFocusedItemNode.plainItem) && isActivationKey(event);\n if (pseudoFocusedItemNode.plainItem.onKeyDown && !isLockedActivation)\n pseudoFocusedItemNode.plainItem.onKeyDown(event);\n // then the menu-level counterpart (`onItemKeyDown`), still before the menu handles or consumes the key: it is how a\n // host that wants menubar behaviour reads the Left/Right that this menu consumes (they never bubble out of it).\n // `nestLevel` counts the submenus above the item (groups do not open a menu), 0 is the main menu\n if (onItemKeyDown) {\n onItemKeyDown({\n itemNode: pseudoFocusedItemNode,\n event,\n nestLevel: getSubMenusPath(pseudoFocusedItemNode).length,\n hasSubmenu: isWithSubmenuNode(pseudoFocusedItemNode),\n });\n }\n // Down Arrow:\n // [N/A] When focus is on a menuitem in a menubar, and that menuitem has a submenu, opens the submenu and places focus on the first item in the submenu.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n // [X] When focus is in a menu, moves focus to the next item,\n // [X] optionally wrapping from the last to the first.\n if (event.key === 'ArrowDown') {\n consumeKey();\n trackFocusNextItem(pseudoFocusedItemNode);\n return;\n }\n // Up Arrow:\n // [X] When focus is in a menu, moves focus to the previous item,\n // [X] optionally wrapping from the first to the last.\n // [N/A] (Optional): When focus is on a menuitem in a menubar, and that menuitem has a submenu, opens the submenu and places focus on the last item in the submenu.\n // /\\ Not relevant for this component, this is ONLY FOR MENU, not for MENUBARS\n if (event.key === 'ArrowUp') {\n consumeKey();\n trackFocusPreviousItem(pseudoFocusedItemNode);\n return;\n }\n // Left Arrow: consumed on every item (see the APG note above), also on a locked one\n if (event.key === 'ArrowLeft') {\n consumeKey();\n handleMenuItemLeftArrowKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n // Right Arrow: consumed on every item (see the APG note above); a disabled item does not open its submenu\n // (PUI-16695), a soft-locked one (applyAriaDisabled) still does\n if (event.key === 'ArrowRight') {\n consumeKey();\n if (!isItemDisabled(pseudoFocusedItemNode.plainItem))\n handleMenuItemRightArrowKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n // Home: If arrow key wrapping is not supported, moves focus to the first item in the current menu or menubar.\n if (event.key === 'Home') {\n if (!pseudoFocusedItemNode.parent) {\n console.log('handleFocusableMenuItemKeyDown -> Home > focused item node', pseudoFocusedItemNode);\n // should be impossible, at most the .parent is the root node, which is not able to trigger this event\n throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_HAS_NO_PARENT;\n }\n consumeKey();\n trackFocusFirstChildItem(pseudoFocusedItemNode.parent);\n return;\n }\n // End: If arrow key wrapping is not supported, moves focus to the last item in the current menu or menubar.\n if (event.key === 'End') {\n if (!pseudoFocusedItemNode.parent) {\n console.log('handleFocusableMenuItemKeyDown -> Home > focused item node', pseudoFocusedItemNode);\n // should be impossible, at most the .parent is the root node, which is not able to trigger this event\n throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_HAS_NO_PARENT;\n }\n consumeKey();\n trackFocusLastChildItem(pseudoFocusedItemNode.parent);\n return;\n }\n if (event.key === 'Escape') {\n // an item only receives keys while the menu is open: the menu consumes this Escape, so it must not also reach\n // an ancestor that closes on Escape (DSDialog, DSModalSlide... React bubbles through portals) \u2014 the old\n // DSDropdownMenuV2 listbox stopped every key here for the same reason\n event.stopPropagation();\n // if the item has no submenu, the alt key is transparent, we behave as if it's not even pressed\n if (event.altKey && isWithSubmenuNode(pseudoFocusedItemNode)) {\n // PUI-14639 - Implement submenu dismissal mechanism\n // Description: we need to add a mechanism to close submenus that automatically expand when a menu item is focused with the keyboard.\n // we have decided to implement a special shortcut (Alt/Option + Esc) as the dismissal mechanism.\n // This change is necessary to comply with WCAG success criterion 1.4.13.\n // https://www.w3.org/WAI/WCAG21/Understanding/content-on-hover-or-focus.html\n // if the item has a submenu, we close it\n closeSubmenusAfterNode({ itemNode: pseudoFocusedItemNode, event });\n return;\n }\n // Escape: Close the menu that contains focus and return focus to the element or context, e.g., menu button or parent menuitem, from which the menu was opened\n const { currentSubmenuWasMainMenu } = closeCurrentSubmenu({ itemNode: pseudoFocusedItemNode, event });\n if (currentSubmenuWasMainMenu) {\n // if the menu we are trying to close is the main menu, we additionally close the main menu itself\n // we focus the trigger immediately (shouldFocusImmediately: true) so the focus return is synchronous\n // and not deferred to requestAnimationFrame \u2014 Escape on the trigger is safe since it only calls\n // onOpinionatedClose() which is already a no-op when the menu is closed\n trackFocusTrigger({ shouldFocusImmediately: true });\n onOpinionatedClose({ skipFocus: true, reason: 'escape', event });\n } else {\n trackFocusParentItem(pseudoFocusedItemNode);\n }\n return;\n }\n\n /* ************************************************************************************\n * ******* ACTIVATION & SUBMENU keys doesn't apply if the item is \"locked\" **********\n * ************************************************************************************ */\n if (isItemLocked(pseudoFocusedItemNode.plainItem)) return;\n // Enter:\n if (event.key === 'Enter') {\n event.stopPropagation(); // activation is ours; the browser default of Enter is left alone (custom item renderers may rely on it)\n handleMenuItemEnterKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n // Space:\n if (event.key === ' ') {\n consumeKey();\n handleMenuItemSpaceKeyDown({ pseudoFocusedItemNode, event });\n return;\n }\n },\n [\n focusedElementItemNode,\n onItemKeyDown,\n trackFocusTrigger,\n trackFocusNextItem,\n trackFocusPreviousItem,\n handleMenuItemLeftArrowKeyDown,\n trackFocusFirstChildItem,\n trackFocusLastChildItem,\n closeCurrentSubmenu,\n trackFocusParentItem,\n closeSubmenusAfterNode,\n onOpinionatedClose,\n handleMenuItemEnterKeyDown,\n handleMenuItemSpaceKeyDown,\n handleMenuItemRightArrowKeyDown,\n ],\n );\n const handleFocusableMenuItemClick = React.useCallback(\n (event: React.MouseEvent<HTMLDivElement>) => {\n const pseudoFocusedItemNode = focusedElementItemNode.current;\n if (pseudoFocusedItemNode === null) throw UNEXPECTED_INTERNAL_ERRORS.FOCUSED_ITEM_NODE_NOT_SET;\n // if the item is locked (disabled or applyAriaDisabled), we don't do anything\n if (isItemLocked(pseudoFocusedItemNode.plainItem)) return;\n // we need to handle which submenu needs to close automagically based on the click\n // starting from the itemNode, we check:\n // - the parent of the itemNode is root?\n // - yes -> we close all the submenus\n const parentMenuNode = pseudoFocusedItemNode.parent;\n if (isRootNode(parentMenuNode)) {\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n } else {\n // - no -> we close all the submenus that are not in the path to the root of the itemNode\n // this is necessary on click because the user can still interact with other (visual) depths of the menu\n // during keyboard navigation, the user is limited to the current (visual) depth\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...getSubMenusPath(pseudoFocusedItemNode)];\n // - if the clicked node has a submenu, keep it open\n if (isWithSubmenuNode(pseudoFocusedItemNode)) {\n newOpenedItems.push(pseudoFocusedItemNode);\n }\n const metainfo: SubmenuChangeMetainfo = { itemNode: pseudoFocusedItemNode, event };\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n }\n\n // to all effects and purposes, the click event is the same as the space key\n // the main difference between enter and space is that\n // - enter ALWAYS closes the menu excepts for submenus\n // - space closes the menu if \"activable\", selections doesn't, submenus doesn't\n handleMenuItemSpaceKeyDown({ pseudoFocusedItemNode, event });\n // if the item contains an `onClick` prop, we trigger it\n if (pseudoFocusedItemNode.plainItem.onClick) pseudoFocusedItemNode.plainItem.onClick(event);\n },\n [focusedElementItemNode, handleMenuItemSpaceKeyDown, handleChangeOpenedSubItems],\n );\n\n // the \"menu-button\" widget is an opinionated widget with a specific behaviour to follow when a focusable item is \"focused\"\n // this function achieve the opinionated \"focus an item\" opinionated behaviour (regardless of why the item is focused)\n const opinionatedFocusBehaviour = React.useCallback(\n // we currently know only one \"event\" that can trigger this opinionated behaviour\n // - focus => React.FocusEvent <-- triggered by ATs \"focus mode\" or \"virtual cursor\" navigation\n // NOTE:\n // mouseEnter => React.MouseEvent\n // still ends up calling this function, but as a side effect of mouse-enter focusing the item, not as the main event\n (itemNode: DSMenuButtonT.PseudoFocusableMenuNodes, event: React.FocusEvent) => {\n if (!isFocusableNode(itemNode)) {\n console.log('handleFocusableMenuItemOnMouseEnter > itemNode:', itemNode);\n throw FORBIDDEN_BEHAVIOURS.TRYING_TO_FOCUS_NON_FOCUSABLE_NODE;\n }\n // on hover -> we track the hovered item as the focused one on top of which events should work on\n trackFocusNode(itemNode);\n if (!isWithSubmenuNode(itemNode)) {\n // if the item has nosubmenu we close all the siblings submenus\n const subMenuNodesPath = getSubMenusPath(itemNode);\n const metainfo: SubmenuChangeMetainfo = { itemNode, event };\n const newOpenedItems: DSMenuButtonT.WithSubmenuMenuNodes[] = [...subMenuNodesPath];\n handleChangeOpenedSubItems(newOpenedItems, metainfo);\n return;\n }\n // if the item is disabled (hard lock, PUI-16695), we don't open the submenu, and there is nothing else to do.\n // a soft-locked item (applyAriaDisabled) still opens it: the submenu is a view-only popup\n if (isItemDisabled(itemNode.plainItem)) return;\n // if not disabled and it has a submenu, we open it, and there is nothing else to do\n openSubmenu({ itemNode, event });\n },\n [handleChangeOpenedSubItems, openSubmenu, trackFocusNode],\n );\n\n // when an item is hovered, we focus it\n // this effectively achieves:\n // - the item is focused (it trigger the handleFocusableMenuItemNativeFocusEvent function because render runs node.focus())\n // - if the user presses a keyboard key, the interaction will work starting from the (hovered hence focused) item\n const handleFocusableMenuItemOnMouseEnter = React.useCallback(\n (itemNode: DSMenuButtonT.PseudoFocusableMenuNodes) => {\n if (!isFocusableNode(itemNode)) {\n console.log('handleFocusableMenuItemOnMouseEnter > itemNode:', itemNode);\n throw FORBIDDEN_BEHAVIOURS.TRYING_TO_FOCUS_NON_FOCUSABLE_NODE;\n }\n // on hover -> we track the hovered item as the focused one on top of which events should work on\n trackFocusNode(itemNode);\n },\n [trackFocusNode],\n );\n\n const handleFocusableMenuItemNativeFocusEvent = React.useCallback(\n (itemNode: DSMenuButtonT.PseudoFocusableMenuNodes, event: React.FocusEvent) => {\n opinionatedFocusBehaviour(itemNode, event);\n },\n [opinionatedFocusBehaviour],\n );\n\n return React.useMemo(\n () => ({\n handleMenuTabKeyDownCapture,\n handleFocusableMenuItemKeyDown,\n handleFocusableMenuItemClick,\n handleFocusableMenuItemOnMouseEnter,\n handleFocusableMenuItemNativeFocusEvent,\n }),\n [\n handleMenuTabKeyDownCapture,\n handleFocusableMenuItemClick,\n handleFocusableMenuItemKeyDown,\n handleFocusableMenuItemNativeFocusEvent,\n handleFocusableMenuItemOnMouseEnter,\n ],\n );\n};\n"],
|
|
5
|
+
"mappings": "AAAA,YAAY,WAAW;ACMvB,SAAS,uBAAuB;AAChC,OAAOA,YAAW;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,sBAAsB,kCAAkC;AAEjE,SAAS,qCAAqC;AAC9C,SAAS,uBAAuB;AAChC,SAAS,mCAAmC;AAuB5C,MAAM,wCAAwC,CAC5C,uBACA,UACG;AACH,MAAI,OAAO,SAAS,aAAa,sBAAsB,UAAU;AAC/D,0BAAsB,UAAU,QAAQ,KAA4B;AACxE;AAEO,MAAM,4BAA4B,CAAC;AAAA,EACxC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAAuC;AACrC,QAAM,EAAE,gBAAgB,gBAAgB,eAAe,eAAe,iBAAiB,IAAI;AAC3F,QAAM,EAAE,mBAAmB,IAAI;AAC/B,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,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,IACA,CAAC,cAAc;AAAA,EACjB;AAEA,QAAM,0CAA0CA,OAAM;AAAA,IACpD,CAAC,UAAkD,UAA4B;AAC7E,gCAA0B,UAAU,KAAK;AAAA,IAC3C;AAAA,IACA,CAAC,yBAAyB;AAAA,EAC5B;AAEA,SAAOA,OAAM;AAAA,IACX,OAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["React"]
|
|
7
7
|
}
|
|
@@ -2,22 +2,28 @@ import * as React from "react";
|
|
|
2
2
|
import React2 from "react";
|
|
3
3
|
const useMenuOpenStatus = ({ propsWithDefault, focusTrackers }) => {
|
|
4
4
|
const { trackFocusTrigger, trackFocusFirstChildItem, trackFocusLastChildItem, preventBlurReset } = focusTrackers;
|
|
5
|
-
const { onClose, onOpen, optionsTree } = propsWithDefault;
|
|
6
|
-
const [
|
|
5
|
+
const { onClose, onOpen, optionsTree, a11yDangerouslyConsumerOwnedMenuOpenState } = propsWithDefault;
|
|
6
|
+
const [internalIsMenuOpen, setIsMenuOpen] = React2.useState(false);
|
|
7
|
+
const isMenuOpen = a11yDangerouslyConsumerOwnedMenuOpenState ?? internalIsMenuOpen;
|
|
8
|
+
const isMenuOpenRef = React2.useRef(isMenuOpen);
|
|
9
|
+
isMenuOpenRef.current = isMenuOpen;
|
|
10
|
+
const openIntentPendingRef = React2.useRef(false);
|
|
7
11
|
const onOpinionatedClose = React2.useCallback(
|
|
8
|
-
({ skipFocus
|
|
12
|
+
({ skipFocus = false, reason, event }) => {
|
|
13
|
+
if (!isMenuOpenRef.current) return;
|
|
14
|
+
openIntentPendingRef.current = false;
|
|
9
15
|
setIsMenuOpen(false);
|
|
10
|
-
if (onClose) onClose();
|
|
16
|
+
if (onClose) onClose({ reason, event });
|
|
11
17
|
if (!skipFocus) trackFocusTrigger();
|
|
12
18
|
preventBlurReset.current = false;
|
|
13
19
|
},
|
|
14
20
|
[onClose, preventBlurReset, trackFocusTrigger]
|
|
15
21
|
);
|
|
16
22
|
const onOpinionatedOpen = React2.useCallback(
|
|
17
|
-
(
|
|
18
|
-
|
|
23
|
+
({ focusLastChild = false, reason, event }) => {
|
|
24
|
+
if (onOpen && !isMenuOpenRef.current) onOpen({ reason, event });
|
|
19
25
|
setIsMenuOpen(true);
|
|
20
|
-
|
|
26
|
+
openIntentPendingRef.current = true;
|
|
21
27
|
const typeCastedOptionsTree = optionsTree;
|
|
22
28
|
if (focusLastChild) trackFocusLastChildItem(typeCastedOptionsTree);
|
|
23
29
|
else trackFocusFirstChildItem(typeCastedOptionsTree);
|
|
@@ -26,7 +32,7 @@ const useMenuOpenStatus = ({ propsWithDefault, focusTrackers }) => {
|
|
|
26
32
|
[onOpen, optionsTree, preventBlurReset, trackFocusFirstChildItem, trackFocusLastChildItem]
|
|
27
33
|
);
|
|
28
34
|
return React2.useMemo(
|
|
29
|
-
() => ({ isMenuOpen, onOpinionatedClose, onOpinionatedOpen }),
|
|
35
|
+
() => ({ isMenuOpen, onOpinionatedClose, onOpinionatedOpen, openIntentPendingRef }),
|
|
30
36
|
[isMenuOpen, onOpinionatedClose, onOpinionatedOpen]
|
|
31
37
|
);
|
|
32
38
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../../../../scripts/build/transpile/react-shim.js", "../../../../../src/parts/DSMenuBehaviouralContextProvider/config/useMenuOpenStatus.ts"],
|
|
4
|
-
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import React from 'react';\nimport { type DSMenuButtonT } from '../../../react-desc-prop-types.js';\nimport { type DSMenuBehaviouralContextProviderT } from '../react-desc-prop-types.js';\nimport type { useFocusTracker } from './useFocusTracker.js';\n\ntype UseMenuOpenStatusConfig = {\n propsWithDefault: DSMenuBehaviouralContextProviderT.InternalProps;\n focusTrackers: ReturnType<typeof useFocusTracker>;\n};\n\ntype OnOpinionatedOpenConfig = {\n focusLastChild?: boolean;\n};\n\nexport const useMenuOpenStatus = ({ propsWithDefault, focusTrackers }: UseMenuOpenStatusConfig) => {\n // when the menu closes the focus must be returned to the trigger\n const { trackFocusTrigger, trackFocusFirstChildItem, trackFocusLastChildItem, preventBlurReset } = focusTrackers;\n const { onClose, onOpen, optionsTree } = propsWithDefault;\n const [
|
|
5
|
-
"mappings": "AAAA,YAAY,WAAW;ACAvB,OAAOA,YAAW;
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nexport { React };\n", "import React from 'react';\nimport { type DSMenuButtonT } from '../../../react-desc-prop-types.js';\nimport { type DSMenuBehaviouralContextProviderT } from '../react-desc-prop-types.js';\nimport type { useFocusTracker } from './useFocusTracker.js';\n\ntype UseMenuOpenStatusConfig = {\n propsWithDefault: DSMenuBehaviouralContextProviderT.InternalProps;\n focusTrackers: ReturnType<typeof useFocusTracker>;\n};\n\ntype OnOpinionatedOpenConfig = {\n focusLastChild?: boolean;\n reason: DSMenuButtonT.MenuOpenReason;\n event?: DSMenuButtonT.MenuOpenMetainfo['event'];\n};\n\ntype OnOpinionatedCloseConfig = {\n skipFocus?: boolean;\n reason: DSMenuButtonT.MenuCloseReason;\n event?: DSMenuButtonT.MenuCloseMetainfo['event'];\n};\n\nexport const useMenuOpenStatus = ({ propsWithDefault, focusTrackers }: UseMenuOpenStatusConfig) => {\n // when the menu closes the focus must be returned to the trigger\n const { trackFocusTrigger, trackFocusFirstChildItem, trackFocusLastChildItem, preventBlurReset } = focusTrackers;\n const { onClose, onOpen, optionsTree, a11yDangerouslyConsumerOwnedMenuOpenState } = propsWithDefault;\n const [internalIsMenuOpen, setIsMenuOpen] = React.useState(false);\n /*\n * Escape hatch (a11yDangerouslyConsumerOwnedMenuOpenState): when the consumer provides a boolean\n * they own the effective open state \u2014 this resolved value is the single source of truth read by\n * the flyout visibility, the floating context's `externallyControlledIsOpen` and the trigger's\n * open/close toggle check, so consumer-ownership propagates through the already-controlled paths.\n * The intents below still fire `onOpen`/`onClose` (+ focus tracking) so the consumer can sync;\n * if they don't, the documented trade-off applies: focus may leave a panel that stays open.\n */\n const isMenuOpen = a11yDangerouslyConsumerOwnedMenuOpenState ?? internalIsMenuOpen;\n // read through a ref to keep `onOpinionatedClose` referentially stable (it is a dependency of many handlers/effects)\n const isMenuOpenRef = React.useRef(isMenuOpen);\n isMenuOpenRef.current = isMenuOpen;\n /*\n * An open INTENT that already armed the focus tracker (trigger click / ArrowDown / ArrowUp) and whose menu has not\n * mounted yet. The tracker throttles its calls (50ms, last one wins), so the armed region may not be visible yet\n * when the menu mounts: this synchronous flag tells the root flyout \"the focus target is already decided, do not\n * decide a second one\" (otherwise ArrowUp -> last item would be overridden by the default \"first item\").\n */\n const openIntentPendingRef = React.useRef(false);\n\n const onOpinionatedClose = React.useCallback(\n ({ skipFocus = false, reason, event }: OnOpinionatedCloseConfig) => {\n // closing a menu that is not open is a no-op: no `onClose`, no focus change.\n // without this guard every closed instance reports a \"close intent\" on window blur / Escape on the trigger\n if (!isMenuOpenRef.current) return;\n openIntentPendingRef.current = false;\n setIsMenuOpen(false);\n if (onClose) onClose({ reason, event });\n if (!skipFocus) trackFocusTrigger();\n preventBlurReset.current = false;\n },\n [onClose, preventBlurReset, trackFocusTrigger],\n );\n\n const onOpinionatedOpen = React.useCallback(\n ({ focusLastChild = false, reason, event }: OnOpinionatedOpenConfig) => {\n // `onOpen` reports a closed -> open intent only; re-arming the focus while already open (ArrowDown/Up) is still allowed\n if (onOpen && !isMenuOpenRef.current) onOpen({ reason, event });\n setIsMenuOpen(true);\n openIntentPendingRef.current = true;\n // Focus on the first item\n // this conversion is a unsafe but that's because we are handling the treeroot node\n // and DSTree doesn't have direct support for typescript tree-root interfaces being different from the rest of the tree\n // the function trackFocusLastChildItem takes any MenuNode and generates the list from subitems autonomously\n // except for the \"trigger\" scenario, we always expect to invoke this function with a PseudoFocusableMenuNodes\n // so we typecast here instead of losing the type safety in the rest of the code\n const typeCastedOptionsTree = optionsTree as DSMenuButtonT.PseudoFocusableMenuNodes;\n if (focusLastChild) trackFocusLastChildItem(typeCastedOptionsTree);\n else trackFocusFirstChildItem(typeCastedOptionsTree);\n preventBlurReset.current = true;\n },\n [onOpen, optionsTree, preventBlurReset, trackFocusFirstChildItem, trackFocusLastChildItem],\n );\n\n return React.useMemo(\n () => ({ isMenuOpen, onOpinionatedClose, onOpinionatedOpen, openIntentPendingRef }),\n [isMenuOpen, onOpinionatedClose, onOpinionatedOpen],\n );\n};\n"],
|
|
5
|
+
"mappings": "AAAA,YAAY,WAAW;ACAvB,OAAOA,YAAW;AAsBX,MAAM,oBAAoB,CAAC,EAAE,kBAAkB,cAAc,MAA+B;AAEjG,QAAM,EAAE,mBAAmB,0BAA0B,yBAAyB,iBAAiB,IAAI;AACnG,QAAM,EAAE,SAAS,QAAQ,aAAa,0CAA0C,IAAI;AACpF,QAAM,CAAC,oBAAoB,aAAa,IAAIA,OAAM,SAAS,KAAK;AAShE,QAAM,aAAa,6CAA6C;AAEhE,QAAM,gBAAgBA,OAAM,OAAO,UAAU;AAC7C,gBAAc,UAAU;AAOxB,QAAM,uBAAuBA,OAAM,OAAO,KAAK;AAE/C,QAAM,qBAAqBA,OAAM;AAAA,IAC/B,CAAC,EAAE,YAAY,OAAO,QAAQ,MAAM,MAAgC;AAGlE,UAAI,CAAC,cAAc,QAAS;AAC5B,2BAAqB,UAAU;AAC/B,oBAAc,KAAK;AACnB,UAAI,QAAS,SAAQ,EAAE,QAAQ,MAAM,CAAC;AACtC,UAAI,CAAC,UAAW,mBAAkB;AAClC,uBAAiB,UAAU;AAAA,IAC7B;AAAA,IACA,CAAC,SAAS,kBAAkB,iBAAiB;AAAA,EAC/C;AAEA,QAAM,oBAAoBA,OAAM;AAAA,IAC9B,CAAC,EAAE,iBAAiB,OAAO,QAAQ,MAAM,MAA+B;AAEtE,UAAI,UAAU,CAAC,cAAc,QAAS,QAAO,EAAE,QAAQ,MAAM,CAAC;AAC9D,oBAAc,IAAI;AAClB,2BAAqB,UAAU;AAO/B,YAAM,wBAAwB;AAC9B,UAAI,eAAgB,yBAAwB,qBAAqB;AAAA,UAC5D,0BAAyB,qBAAqB;AACnD,uBAAiB,UAAU;AAAA,IAC7B;AAAA,IACA,CAAC,QAAQ,aAAa,kBAAkB,0BAA0B,uBAAuB;AAAA,EAC3F;AAEA,SAAOA,OAAM;AAAA,IACX,OAAO,EAAE,YAAY,oBAAoB,mBAAmB,qBAAqB;AAAA,IACjF,CAAC,YAAY,oBAAoB,iBAAiB;AAAA,EACpD;AACF;",
|
|
6
6
|
"names": ["React"]
|
|
7
7
|
}
|
|
@@ -11,9 +11,11 @@ import { MenuBehaviouralContextProviderContext } from "../DSMenuBehaviouralConte
|
|
|
11
11
|
import { MENU_FOCUS_REGIONS } from "../DSMenuBehaviouralContextProvider/constants/index.js";
|
|
12
12
|
import { focusNodeRacingConditionSolved } from "./focusNodeRacingConditionSolved.js";
|
|
13
13
|
import { MenuButtonContextProvider } from "../../MenuButtonContextProviderCTX.js";
|
|
14
|
+
import { isItemLocked } from "../../utils/nodesTypeguardsAndGetters.js";
|
|
14
15
|
const ActivableMenuItem = ({ itemNode, ariaPosInSet, ariaSetSize }) => {
|
|
15
16
|
const { dsId, plainItem } = itemNode;
|
|
16
17
|
const { label, secondaryLabel, leftDecoration: LeftDecComponent, minWidth, disabled } = plainItem;
|
|
18
|
+
const locked = isItemLocked(plainItem);
|
|
17
19
|
const {
|
|
18
20
|
ownerProps: { getOwnerProps }
|
|
19
21
|
} = React2.useContext(MenuButtonContextProvider);
|
|
@@ -69,10 +71,11 @@ const ActivableMenuItem = ({ itemNode, ariaPosInSet, ariaSetSize }) => {
|
|
|
69
71
|
id: `${spacelessDsIdForDom}`,
|
|
70
72
|
role: "menuitem",
|
|
71
73
|
tabIndex: isFocused ? 0 : -1,
|
|
72
|
-
"aria-disabled":
|
|
74
|
+
"aria-disabled": locked ? true : void 0,
|
|
73
75
|
"aria-posinset": ariaPosInSet,
|
|
74
76
|
"aria-setsize": ariaSetSize,
|
|
75
|
-
applyAriaDisabled:
|
|
77
|
+
applyAriaDisabled: locked,
|
|
78
|
+
$disabled: disabled,
|
|
76
79
|
getOwnerProps,
|
|
77
80
|
getOwnerPropsArguments,
|
|
78
81
|
children: /* @__PURE__ */ jsxs(
|
|
@@ -102,7 +105,7 @@ const ActivableMenuItem = ({ itemNode, ariaPosInSet, ariaSetSize }) => {
|
|
|
102
105
|
{
|
|
103
106
|
getOwnerProps,
|
|
104
107
|
getOwnerPropsArguments,
|
|
105
|
-
disabled,
|
|
108
|
+
$disabled: locked,
|
|
106
109
|
children: secondaryLabel
|
|
107
110
|
}
|
|
108
111
|
)
|