@react-aria/menu 3.0.0-alpha.1 → 3.0.0-nightly-641446f65-240905
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/README.md +1 -1
- package/dist/ar-AE.main.js +6 -0
- package/dist/ar-AE.main.js.map +1 -0
- package/dist/ar-AE.mjs +8 -0
- package/dist/ar-AE.module.js +8 -0
- package/dist/ar-AE.module.js.map +1 -0
- package/dist/bg-BG.main.js +6 -0
- package/dist/bg-BG.main.js.map +1 -0
- package/dist/bg-BG.mjs +8 -0
- package/dist/bg-BG.module.js +8 -0
- package/dist/bg-BG.module.js.map +1 -0
- package/dist/cs-CZ.main.js +6 -0
- package/dist/cs-CZ.main.js.map +1 -0
- package/dist/cs-CZ.mjs +8 -0
- package/dist/cs-CZ.module.js +8 -0
- package/dist/cs-CZ.module.js.map +1 -0
- package/dist/da-DK.main.js +6 -0
- package/dist/da-DK.main.js.map +1 -0
- package/dist/da-DK.mjs +8 -0
- package/dist/da-DK.module.js +8 -0
- package/dist/da-DK.module.js.map +1 -0
- package/dist/de-DE.main.js +6 -0
- package/dist/de-DE.main.js.map +1 -0
- package/dist/de-DE.mjs +8 -0
- package/dist/de-DE.module.js +8 -0
- package/dist/de-DE.module.js.map +1 -0
- package/dist/el-GR.main.js +6 -0
- package/dist/el-GR.main.js.map +1 -0
- package/dist/el-GR.mjs +8 -0
- package/dist/el-GR.module.js +8 -0
- package/dist/el-GR.module.js.map +1 -0
- package/dist/en-US.main.js +6 -0
- package/dist/en-US.main.js.map +1 -0
- package/dist/en-US.mjs +8 -0
- package/dist/en-US.module.js +8 -0
- package/dist/en-US.module.js.map +1 -0
- package/dist/es-ES.main.js +6 -0
- package/dist/es-ES.main.js.map +1 -0
- package/dist/es-ES.mjs +8 -0
- package/dist/es-ES.module.js +8 -0
- package/dist/es-ES.module.js.map +1 -0
- package/dist/et-EE.main.js +6 -0
- package/dist/et-EE.main.js.map +1 -0
- package/dist/et-EE.mjs +8 -0
- package/dist/et-EE.module.js +8 -0
- package/dist/et-EE.module.js.map +1 -0
- package/dist/fi-FI.main.js +6 -0
- package/dist/fi-FI.main.js.map +1 -0
- package/dist/fi-FI.mjs +8 -0
- package/dist/fi-FI.module.js +8 -0
- package/dist/fi-FI.module.js.map +1 -0
- package/dist/fr-FR.main.js +6 -0
- package/dist/fr-FR.main.js.map +1 -0
- package/dist/fr-FR.mjs +8 -0
- package/dist/fr-FR.module.js +8 -0
- package/dist/fr-FR.module.js.map +1 -0
- package/dist/he-IL.main.js +6 -0
- package/dist/he-IL.main.js.map +1 -0
- package/dist/he-IL.mjs +8 -0
- package/dist/he-IL.module.js +8 -0
- package/dist/he-IL.module.js.map +1 -0
- package/dist/hr-HR.main.js +6 -0
- package/dist/hr-HR.main.js.map +1 -0
- package/dist/hr-HR.mjs +8 -0
- package/dist/hr-HR.module.js +8 -0
- package/dist/hr-HR.module.js.map +1 -0
- package/dist/hu-HU.main.js +6 -0
- package/dist/hu-HU.main.js.map +1 -0
- package/dist/hu-HU.mjs +8 -0
- package/dist/hu-HU.module.js +8 -0
- package/dist/hu-HU.module.js.map +1 -0
- package/dist/import.mjs +25 -0
- package/dist/intlStrings.main.js +108 -0
- package/dist/intlStrings.main.js.map +1 -0
- package/dist/intlStrings.mjs +110 -0
- package/dist/intlStrings.module.js +110 -0
- package/dist/intlStrings.module.js.map +1 -0
- package/dist/it-IT.main.js +6 -0
- package/dist/it-IT.main.js.map +1 -0
- package/dist/it-IT.mjs +8 -0
- package/dist/it-IT.module.js +8 -0
- package/dist/it-IT.module.js.map +1 -0
- package/dist/ja-JP.main.js +6 -0
- package/dist/ja-JP.main.js.map +1 -0
- package/dist/ja-JP.mjs +8 -0
- package/dist/ja-JP.module.js +8 -0
- package/dist/ja-JP.module.js.map +1 -0
- package/dist/ko-KR.main.js +6 -0
- package/dist/ko-KR.main.js.map +1 -0
- package/dist/ko-KR.mjs +8 -0
- package/dist/ko-KR.module.js +8 -0
- package/dist/ko-KR.module.js.map +1 -0
- package/dist/lt-LT.main.js +6 -0
- package/dist/lt-LT.main.js.map +1 -0
- package/dist/lt-LT.mjs +8 -0
- package/dist/lt-LT.module.js +8 -0
- package/dist/lt-LT.module.js.map +1 -0
- package/dist/lv-LV.main.js +6 -0
- package/dist/lv-LV.main.js.map +1 -0
- package/dist/lv-LV.mjs +8 -0
- package/dist/lv-LV.module.js +8 -0
- package/dist/lv-LV.module.js.map +1 -0
- package/dist/main.js +24 -251
- package/dist/main.js.map +1 -0
- package/dist/module.js +18 -232
- package/dist/module.js.map +1 -0
- package/dist/nb-NO.main.js +6 -0
- package/dist/nb-NO.main.js.map +1 -0
- package/dist/nb-NO.mjs +8 -0
- package/dist/nb-NO.module.js +8 -0
- package/dist/nb-NO.module.js.map +1 -0
- package/dist/nl-NL.main.js +6 -0
- package/dist/nl-NL.main.js.map +1 -0
- package/dist/nl-NL.mjs +8 -0
- package/dist/nl-NL.module.js +8 -0
- package/dist/nl-NL.module.js.map +1 -0
- package/dist/pl-PL.main.js +6 -0
- package/dist/pl-PL.main.js.map +1 -0
- package/dist/pl-PL.mjs +8 -0
- package/dist/pl-PL.module.js +8 -0
- package/dist/pl-PL.module.js.map +1 -0
- package/dist/pt-BR.main.js +6 -0
- package/dist/pt-BR.main.js.map +1 -0
- package/dist/pt-BR.mjs +8 -0
- package/dist/pt-BR.module.js +8 -0
- package/dist/pt-BR.module.js.map +1 -0
- package/dist/pt-PT.main.js +6 -0
- package/dist/pt-PT.main.js.map +1 -0
- package/dist/pt-PT.mjs +8 -0
- package/dist/pt-PT.module.js +8 -0
- package/dist/pt-PT.module.js.map +1 -0
- package/dist/ro-RO.main.js +6 -0
- package/dist/ro-RO.main.js.map +1 -0
- package/dist/ro-RO.mjs +8 -0
- package/dist/ro-RO.module.js +8 -0
- package/dist/ro-RO.module.js.map +1 -0
- package/dist/ru-RU.main.js +6 -0
- package/dist/ru-RU.main.js.map +1 -0
- package/dist/ru-RU.mjs +8 -0
- package/dist/ru-RU.module.js +8 -0
- package/dist/ru-RU.module.js.map +1 -0
- package/dist/sk-SK.main.js +6 -0
- package/dist/sk-SK.main.js.map +1 -0
- package/dist/sk-SK.mjs +8 -0
- package/dist/sk-SK.module.js +8 -0
- package/dist/sk-SK.module.js.map +1 -0
- package/dist/sl-SI.main.js +6 -0
- package/dist/sl-SI.main.js.map +1 -0
- package/dist/sl-SI.mjs +8 -0
- package/dist/sl-SI.module.js +8 -0
- package/dist/sl-SI.module.js.map +1 -0
- package/dist/sr-SP.main.js +6 -0
- package/dist/sr-SP.main.js.map +1 -0
- package/dist/sr-SP.mjs +8 -0
- package/dist/sr-SP.module.js +8 -0
- package/dist/sr-SP.module.js.map +1 -0
- package/dist/sv-SE.main.js +6 -0
- package/dist/sv-SE.main.js.map +1 -0
- package/dist/sv-SE.mjs +8 -0
- package/dist/sv-SE.module.js +8 -0
- package/dist/sv-SE.module.js.map +1 -0
- package/dist/tr-TR.main.js +6 -0
- package/dist/tr-TR.main.js.map +1 -0
- package/dist/tr-TR.mjs +8 -0
- package/dist/tr-TR.module.js +8 -0
- package/dist/tr-TR.module.js.map +1 -0
- package/dist/types.d.ts +156 -36
- package/dist/types.d.ts.map +1 -1
- package/dist/uk-UA.main.js +6 -0
- package/dist/uk-UA.main.js.map +1 -0
- package/dist/uk-UA.mjs +8 -0
- package/dist/uk-UA.module.js +8 -0
- package/dist/uk-UA.module.js.map +1 -0
- package/dist/useMenu.main.js +59 -0
- package/dist/useMenu.main.js.map +1 -0
- package/dist/useMenu.mjs +53 -0
- package/dist/useMenu.module.js +53 -0
- package/dist/useMenu.module.js.map +1 -0
- package/dist/useMenuItem.main.js +181 -0
- package/dist/useMenuItem.main.js.map +1 -0
- package/dist/useMenuItem.mjs +176 -0
- package/dist/useMenuItem.module.js +176 -0
- package/dist/useMenuItem.module.js.map +1 -0
- package/dist/useMenuSection.main.js +43 -0
- package/dist/useMenuSection.main.js.map +1 -0
- package/dist/useMenuSection.mjs +38 -0
- package/dist/useMenuSection.module.js +38 -0
- package/dist/useMenuSection.module.js.map +1 -0
- package/dist/useMenuTrigger.main.js +103 -0
- package/dist/useMenuTrigger.main.js.map +1 -0
- package/dist/useMenuTrigger.mjs +98 -0
- package/dist/useMenuTrigger.module.js +98 -0
- package/dist/useMenuTrigger.module.js.map +1 -0
- package/dist/useSafelyMouseToSubmenu.main.js +134 -0
- package/dist/useSafelyMouseToSubmenu.main.js.map +1 -0
- package/dist/useSafelyMouseToSubmenu.mjs +129 -0
- package/dist/useSafelyMouseToSubmenu.module.js +129 -0
- package/dist/useSafelyMouseToSubmenu.module.js.map +1 -0
- package/dist/useSubmenuTrigger.main.js +169 -0
- package/dist/useSubmenuTrigger.main.js.map +1 -0
- package/dist/useSubmenuTrigger.mjs +164 -0
- package/dist/useSubmenuTrigger.module.js +164 -0
- package/dist/useSubmenuTrigger.module.js.map +1 -0
- package/dist/zh-CN.main.js +6 -0
- package/dist/zh-CN.main.js.map +1 -0
- package/dist/zh-CN.mjs +8 -0
- package/dist/zh-CN.module.js +8 -0
- package/dist/zh-CN.module.js.map +1 -0
- package/dist/zh-TW.main.js +6 -0
- package/dist/zh-TW.main.js.map +1 -0
- package/dist/zh-TW.mjs +8 -0
- package/dist/zh-TW.module.js +8 -0
- package/dist/zh-TW.module.js.map +1 -0
- package/package.json +26 -16
- package/src/index.ts +24 -0
- package/src/useMenu.ts +88 -0
- package/src/useMenuItem.ts +299 -0
- package/src/useMenuSection.ts +61 -0
- package/src/useMenuTrigger.ts +144 -0
- package/src/useSafelyMouseToSubmenu.ts +161 -0
- package/src/useSubmenuTrigger.ts +243 -0
- package/LICENSE +0 -201
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
|
|
2
|
+
import {RefObject} from '@react-types/shared';
|
|
3
|
+
import {useEffect, useRef, useState} from 'react';
|
|
4
|
+
import {useInteractionModality} from '@react-aria/interactions';
|
|
5
|
+
import {useResizeObserver} from '@react-aria/utils';
|
|
6
|
+
|
|
7
|
+
interface SafelyMouseToSubmenuOptions {
|
|
8
|
+
/** Ref for the parent menu. */
|
|
9
|
+
menuRef: RefObject<Element | null>,
|
|
10
|
+
/** Ref for the submenu. */
|
|
11
|
+
submenuRef: RefObject<Element | null>,
|
|
12
|
+
/** Whether the submenu is open. */
|
|
13
|
+
isOpen: boolean,
|
|
14
|
+
/** Whether this feature is disabled. */
|
|
15
|
+
isDisabled?: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const ALLOWED_INVALID_MOVEMENTS = 2;
|
|
19
|
+
const THROTTLE_TIME = 50;
|
|
20
|
+
const TIMEOUT_TIME = 1000;
|
|
21
|
+
const ANGLE_PADDING = Math.PI / 12; // 15°
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Allows the user to move their pointer to the submenu without it closing when their mouse leaves the trigger element.
|
|
25
|
+
* Prevents pointer events from going to the underlying menu if the user is moving their pointer towards the sub-menu.
|
|
26
|
+
*/
|
|
27
|
+
export function useSafelyMouseToSubmenu(options: SafelyMouseToSubmenuOptions) {
|
|
28
|
+
let {menuRef, submenuRef, isOpen, isDisabled} = options;
|
|
29
|
+
let prevPointerPos = useRef<{x: number, y: number} | undefined>(undefined);
|
|
30
|
+
let submenuRect = useRef<DOMRect | undefined>(undefined);
|
|
31
|
+
let lastProcessedTime = useRef<number>(0);
|
|
32
|
+
let timeout = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
33
|
+
let autoCloseTimeout = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
34
|
+
let submenuSide = useRef<'left' | 'right' | undefined>(undefined);
|
|
35
|
+
let movementsTowardsSubmenuCount = useRef<number>(2);
|
|
36
|
+
let [preventPointerEvents, setPreventPointerEvents] = useState(false);
|
|
37
|
+
|
|
38
|
+
let updateSubmenuRect = () => {
|
|
39
|
+
if (submenuRef.current) {
|
|
40
|
+
submenuRect.current = submenuRef.current.getBoundingClientRect();
|
|
41
|
+
submenuSide.current = undefined;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
useResizeObserver({ref: submenuRef, onResize: updateSubmenuRect});
|
|
45
|
+
|
|
46
|
+
let reset = () => {
|
|
47
|
+
setPreventPointerEvents(false);
|
|
48
|
+
movementsTowardsSubmenuCount.current = ALLOWED_INVALID_MOVEMENTS;
|
|
49
|
+
prevPointerPos.current = undefined;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
let modality = useInteractionModality();
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (preventPointerEvents && menuRef.current) {
|
|
56
|
+
(menuRef.current as HTMLElement).style.pointerEvents = 'none';
|
|
57
|
+
} else {
|
|
58
|
+
(menuRef.current as HTMLElement).style.pointerEvents = '';
|
|
59
|
+
}
|
|
60
|
+
}, [menuRef, preventPointerEvents]);
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
let submenu = submenuRef.current;
|
|
64
|
+
let menu = menuRef.current;
|
|
65
|
+
|
|
66
|
+
if (isDisabled || !submenu || !isOpen || modality !== 'pointer') {
|
|
67
|
+
reset();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
submenuRect.current = submenu.getBoundingClientRect();
|
|
71
|
+
|
|
72
|
+
let onPointerMove = (e: PointerEvent) => {
|
|
73
|
+
if (e.pointerType === 'touch' || e.pointerType === 'pen') {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let currentTime = Date.now();
|
|
78
|
+
|
|
79
|
+
// Throttle
|
|
80
|
+
if (currentTime - lastProcessedTime.current < THROTTLE_TIME) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
clearTimeout(timeout.current);
|
|
84
|
+
clearTimeout(autoCloseTimeout.current);
|
|
85
|
+
|
|
86
|
+
let {clientX: mouseX, clientY: mouseY} = e;
|
|
87
|
+
|
|
88
|
+
if (!prevPointerPos.current) {
|
|
89
|
+
prevPointerPos.current = {x: mouseX, y: mouseY};
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!submenuRect.current) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!submenuSide.current) {
|
|
98
|
+
submenuSide.current = mouseX > submenuRect.current.right ? 'left' : 'right';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Pointer is outside of parent menu
|
|
102
|
+
if (mouseX < menu.getBoundingClientRect().left || mouseX > menu.getBoundingClientRect().right || mouseY < menu.getBoundingClientRect().top || mouseY > menu.getBoundingClientRect().bottom) {
|
|
103
|
+
reset();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* Check if pointer is moving towards submenu.
|
|
108
|
+
Uses the 2-argument arctangent (https://en.wikipedia.org/wiki/Atan2) to calculate:
|
|
109
|
+
- angle between previous pointer and top of submenu
|
|
110
|
+
- angle between previous pointer and bottom of submenu
|
|
111
|
+
- angle between previous pointer and current pointer (delta)
|
|
112
|
+
If the pointer delta angle value is between the top and bottom angle values, we know the pointer is moving towards the submenu.
|
|
113
|
+
*/
|
|
114
|
+
let prevMouseX = prevPointerPos.current.x;
|
|
115
|
+
let prevMouseY = prevPointerPos.current.y;
|
|
116
|
+
let toSubmenuX = submenuSide.current === 'right' ? submenuRect.current.left - prevMouseX : prevMouseX - submenuRect.current.right;
|
|
117
|
+
let angleTop = Math.atan2(prevMouseY - submenuRect.current.top, toSubmenuX) + ANGLE_PADDING;
|
|
118
|
+
let angleBottom = Math.atan2(prevMouseY - submenuRect.current.bottom, toSubmenuX) - ANGLE_PADDING;
|
|
119
|
+
let anglePointer = Math.atan2(prevMouseY - mouseY, (submenuSide.current === 'left' ? -(mouseX - prevMouseX) : mouseX - prevMouseX));
|
|
120
|
+
let isMovingTowardsSubmenu = anglePointer < angleTop && anglePointer > angleBottom;
|
|
121
|
+
|
|
122
|
+
movementsTowardsSubmenuCount.current = isMovingTowardsSubmenu ?
|
|
123
|
+
Math.min(movementsTowardsSubmenuCount.current + 1, ALLOWED_INVALID_MOVEMENTS) :
|
|
124
|
+
Math.max(movementsTowardsSubmenuCount.current - 1, 0);
|
|
125
|
+
|
|
126
|
+
if (movementsTowardsSubmenuCount.current >= ALLOWED_INVALID_MOVEMENTS) {
|
|
127
|
+
setPreventPointerEvents(true);
|
|
128
|
+
} else {
|
|
129
|
+
setPreventPointerEvents(false);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
lastProcessedTime.current = currentTime;
|
|
133
|
+
prevPointerPos.current = {x: mouseX, y: mouseY};
|
|
134
|
+
|
|
135
|
+
// If the pointer is moving towards the submenu, start a timeout to close if no other movements are made after 500ms.
|
|
136
|
+
if (isMovingTowardsSubmenu) {
|
|
137
|
+
timeout.current = setTimeout(() => {
|
|
138
|
+
reset();
|
|
139
|
+
autoCloseTimeout.current = setTimeout(() => {
|
|
140
|
+
// Fire a pointerover event to trigger the menu to close.
|
|
141
|
+
// Wait until pointer-events:none is no longer applied
|
|
142
|
+
let target = document.elementFromPoint(mouseX, mouseY);
|
|
143
|
+
if (target && menu.contains(target)) {
|
|
144
|
+
target.dispatchEvent(new PointerEvent('pointerover', {bubbles: true, cancelable: true}));
|
|
145
|
+
}
|
|
146
|
+
}, 100);
|
|
147
|
+
}, TIMEOUT_TIME);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
window.addEventListener('pointermove', onPointerMove);
|
|
152
|
+
|
|
153
|
+
return () => {
|
|
154
|
+
window.removeEventListener('pointermove', onPointerMove);
|
|
155
|
+
clearTimeout(timeout.current);
|
|
156
|
+
clearTimeout(autoCloseTimeout.current);
|
|
157
|
+
movementsTowardsSubmenuCount.current = ALLOWED_INVALID_MOVEMENTS;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
}, [isDisabled, isOpen, menuRef, modality, setPreventPointerEvents, submenuRef]);
|
|
161
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {AriaMenuItemProps} from './useMenuItem';
|
|
14
|
+
import {AriaMenuOptions} from './useMenu';
|
|
15
|
+
import type {AriaPopoverProps, OverlayProps} from '@react-aria/overlays';
|
|
16
|
+
import {FocusableElement, FocusStrategy, KeyboardEvent, Node, PressEvent, RefObject} from '@react-types/shared';
|
|
17
|
+
import type {SubmenuTriggerState} from '@react-stately/menu';
|
|
18
|
+
import {useCallback, useRef} from 'react';
|
|
19
|
+
import {useEffectEvent, useId, useLayoutEffect} from '@react-aria/utils';
|
|
20
|
+
import {useLocale} from '@react-aria/i18n';
|
|
21
|
+
import {useSafelyMouseToSubmenu} from './useSafelyMouseToSubmenu';
|
|
22
|
+
|
|
23
|
+
export interface AriaSubmenuTriggerProps {
|
|
24
|
+
/**
|
|
25
|
+
* An object representing the submenu trigger menu item. Contains all the relevant information that makes up the menu item.
|
|
26
|
+
* @deprecated
|
|
27
|
+
*/
|
|
28
|
+
node?: Node<unknown>,
|
|
29
|
+
/** Whether the submenu trigger is disabled. */
|
|
30
|
+
isDisabled?: boolean,
|
|
31
|
+
/** The type of the contents that the submenu trigger opens. */
|
|
32
|
+
type?: 'dialog' | 'menu',
|
|
33
|
+
/** Ref of the menu that contains the submenu trigger. */
|
|
34
|
+
parentMenuRef: RefObject<HTMLElement | null>,
|
|
35
|
+
/** Ref of the submenu opened by the submenu trigger. */
|
|
36
|
+
submenuRef: RefObject<HTMLElement | null>,
|
|
37
|
+
/**
|
|
38
|
+
* The delay time in milliseconds for the submenu to appear after hovering over the trigger.
|
|
39
|
+
* @default 200
|
|
40
|
+
*/
|
|
41
|
+
delay?: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface SubmenuTriggerProps extends AriaMenuItemProps {
|
|
45
|
+
/** Whether the submenu trigger is in an expanded state. */
|
|
46
|
+
isOpen: boolean
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface SubmenuProps<T> extends AriaMenuOptions<T> {
|
|
50
|
+
/** The level of the submenu. */
|
|
51
|
+
submenuLevel: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SubmenuTriggerAria<T> {
|
|
55
|
+
/** Props for the submenu trigger menu item. */
|
|
56
|
+
submenuTriggerProps: SubmenuTriggerProps,
|
|
57
|
+
/** Props for the submenu controlled by the submenu trigger menu item. */
|
|
58
|
+
submenuProps: SubmenuProps<T>,
|
|
59
|
+
/** Props for the submenu's popover container. */
|
|
60
|
+
popoverProps: Pick<AriaPopoverProps, 'isNonModal' | 'shouldCloseOnInteractOutside'> & Pick<OverlayProps, 'disableFocusManagement'>
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Provides the behavior and accessibility implementation for a submenu trigger and its associated submenu.
|
|
65
|
+
* @param props - Props for the submenu trigger and refs attach to its submenu and parent menu.
|
|
66
|
+
* @param state - State for the submenu trigger.
|
|
67
|
+
* @param ref - Ref to the submenu trigger element.
|
|
68
|
+
*/
|
|
69
|
+
export function useSubmenuTrigger<T>(props: AriaSubmenuTriggerProps, state: SubmenuTriggerState, ref: RefObject<FocusableElement | null>): SubmenuTriggerAria<T> {
|
|
70
|
+
let {parentMenuRef, submenuRef, type = 'menu', isDisabled, delay = 200} = props;
|
|
71
|
+
let submenuTriggerId = useId();
|
|
72
|
+
let overlayId = useId();
|
|
73
|
+
let {direction} = useLocale();
|
|
74
|
+
let openTimeout = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
75
|
+
let cancelOpenTimeout = useCallback(() => {
|
|
76
|
+
if (openTimeout.current) {
|
|
77
|
+
clearTimeout(openTimeout.current);
|
|
78
|
+
openTimeout.current = undefined;
|
|
79
|
+
}
|
|
80
|
+
}, [openTimeout]);
|
|
81
|
+
|
|
82
|
+
let onSubmenuOpen = useEffectEvent((focusStrategy?: FocusStrategy) => {
|
|
83
|
+
cancelOpenTimeout();
|
|
84
|
+
state.open(focusStrategy);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
let onSubmenuClose = useEffectEvent(() => {
|
|
88
|
+
cancelOpenTimeout();
|
|
89
|
+
state.close();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
useLayoutEffect(() => {
|
|
93
|
+
return () => {
|
|
94
|
+
cancelOpenTimeout();
|
|
95
|
+
};
|
|
96
|
+
}, [cancelOpenTimeout]);
|
|
97
|
+
|
|
98
|
+
let submenuKeyDown = (e: KeyboardEvent) => {
|
|
99
|
+
switch (e.key) {
|
|
100
|
+
case 'ArrowLeft':
|
|
101
|
+
if (direction === 'ltr' && e.currentTarget.contains(e.target as Element)) {
|
|
102
|
+
e.stopPropagation();
|
|
103
|
+
onSubmenuClose();
|
|
104
|
+
ref.current.focus();
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
case 'ArrowRight':
|
|
108
|
+
if (direction === 'rtl' && e.currentTarget.contains(e.target as Element)) {
|
|
109
|
+
e.stopPropagation();
|
|
110
|
+
onSubmenuClose();
|
|
111
|
+
ref.current.focus();
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
case 'Escape':
|
|
115
|
+
e.stopPropagation();
|
|
116
|
+
state.closeAll();
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
let submenuProps = {
|
|
122
|
+
id: overlayId,
|
|
123
|
+
'aria-labelledby': submenuTriggerId,
|
|
124
|
+
submenuLevel: state.submenuLevel,
|
|
125
|
+
...(type === 'menu' && {
|
|
126
|
+
onClose: state.closeAll,
|
|
127
|
+
autoFocus: state.focusStrategy,
|
|
128
|
+
onKeyDown: submenuKeyDown
|
|
129
|
+
})
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
let submenuTriggerKeyDown = (e: KeyboardEvent) => {
|
|
133
|
+
switch (e.key) {
|
|
134
|
+
case 'ArrowRight':
|
|
135
|
+
if (!isDisabled) {
|
|
136
|
+
if (direction === 'ltr') {
|
|
137
|
+
if (!state.isOpen) {
|
|
138
|
+
onSubmenuOpen('first');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (type === 'menu' && !!submenuRef?.current && document.activeElement === ref?.current) {
|
|
142
|
+
submenuRef.current.focus();
|
|
143
|
+
}
|
|
144
|
+
} else if (state.isOpen) {
|
|
145
|
+
onSubmenuClose();
|
|
146
|
+
} else {
|
|
147
|
+
e.continuePropagation();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
break;
|
|
152
|
+
case 'ArrowLeft':
|
|
153
|
+
if (!isDisabled) {
|
|
154
|
+
if (direction === 'rtl') {
|
|
155
|
+
if (!state.isOpen) {
|
|
156
|
+
onSubmenuOpen('first');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (type === 'menu' && !!submenuRef?.current && document.activeElement === ref?.current) {
|
|
160
|
+
submenuRef.current.focus();
|
|
161
|
+
}
|
|
162
|
+
} else if (state.isOpen) {
|
|
163
|
+
onSubmenuClose();
|
|
164
|
+
} else {
|
|
165
|
+
e.continuePropagation();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
break;
|
|
169
|
+
case 'Escape':
|
|
170
|
+
state.closeAll();
|
|
171
|
+
break;
|
|
172
|
+
default:
|
|
173
|
+
e.continuePropagation();
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
let onPressStart = (e: PressEvent) => {
|
|
179
|
+
if (!isDisabled && (e.pointerType === 'virtual' || e.pointerType === 'keyboard')) {
|
|
180
|
+
// If opened with a screen reader or keyboard, auto focus the first submenu item.
|
|
181
|
+
onSubmenuOpen('first');
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
let onPress = (e: PressEvent) => {
|
|
186
|
+
if (!isDisabled && (e.pointerType === 'touch' || e.pointerType === 'mouse')) {
|
|
187
|
+
// For touch or on a desktop device with a small screen open on press up to possible problems with
|
|
188
|
+
// press up happening on the newly opened tray items
|
|
189
|
+
onSubmenuOpen();
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
let onHoverChange = (isHovered) => {
|
|
194
|
+
if (!isDisabled) {
|
|
195
|
+
if (isHovered && !state.isOpen) {
|
|
196
|
+
if (!openTimeout.current) {
|
|
197
|
+
openTimeout.current = setTimeout(() => {
|
|
198
|
+
onSubmenuOpen();
|
|
199
|
+
}, delay);
|
|
200
|
+
}
|
|
201
|
+
} else if (!isHovered) {
|
|
202
|
+
cancelOpenTimeout();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
let onBlur = (e) => {
|
|
208
|
+
if (state.isOpen && parentMenuRef.current.contains(e.relatedTarget)) {
|
|
209
|
+
onSubmenuClose();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
let shouldCloseOnInteractOutside = (target) => {
|
|
214
|
+
if (target !== ref.current) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return false;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
useSafelyMouseToSubmenu({menuRef: parentMenuRef, submenuRef, isOpen: state.isOpen, isDisabled: isDisabled});
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
submenuTriggerProps: {
|
|
225
|
+
id: submenuTriggerId,
|
|
226
|
+
'aria-controls': state.isOpen ? overlayId : undefined,
|
|
227
|
+
'aria-haspopup': !isDisabled ? type : undefined,
|
|
228
|
+
'aria-expanded': state.isOpen ? 'true' : 'false',
|
|
229
|
+
onPressStart,
|
|
230
|
+
onPress,
|
|
231
|
+
onHoverChange,
|
|
232
|
+
onKeyDown: submenuTriggerKeyDown,
|
|
233
|
+
onBlur,
|
|
234
|
+
isOpen: state.isOpen
|
|
235
|
+
},
|
|
236
|
+
submenuProps,
|
|
237
|
+
popoverProps: {
|
|
238
|
+
isNonModal: true,
|
|
239
|
+
disableFocusManagement: true,
|
|
240
|
+
shouldCloseOnInteractOutside
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
Apache License
|
|
2
|
-
Version 2.0, January 2004
|
|
3
|
-
http://www.apache.org/licenses/
|
|
4
|
-
|
|
5
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
-
|
|
7
|
-
1. Definitions.
|
|
8
|
-
|
|
9
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
-
|
|
12
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
-
the copyright owner that is granting the License.
|
|
14
|
-
|
|
15
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
-
other entities that control, are controlled by, or are under common
|
|
17
|
-
control with that entity. For the purposes of this definition,
|
|
18
|
-
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
-
direction or management of such entity, whether by contract or
|
|
20
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
-
|
|
23
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
-
exercising permissions granted by this License.
|
|
25
|
-
|
|
26
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
-
including but not limited to software source code, documentation
|
|
28
|
-
source, and configuration files.
|
|
29
|
-
|
|
30
|
-
"Object" form shall mean any form resulting from mechanical
|
|
31
|
-
transformation or translation of a Source form, including but
|
|
32
|
-
not limited to compiled object code, generated documentation,
|
|
33
|
-
and conversions to other media types.
|
|
34
|
-
|
|
35
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
-
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
-
(an example is provided in the Appendix below).
|
|
39
|
-
|
|
40
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
-
form, that is based on (or derived from) the Work and for which the
|
|
42
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
-
of this License, Derivative Works shall not include works that remain
|
|
45
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
-
the Work and Derivative Works thereof.
|
|
47
|
-
|
|
48
|
-
"Contribution" shall mean any work of authorship, including
|
|
49
|
-
the original version of the Work and any modifications or additions
|
|
50
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
-
means any form of electronic, verbal, or written communication sent
|
|
55
|
-
to the Licensor or its representatives, including but not limited to
|
|
56
|
-
communication on electronic mailing lists, source code control systems,
|
|
57
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
-
excluding communication that is conspicuously marked or otherwise
|
|
60
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
-
|
|
62
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
-
subsequently incorporated within the Work.
|
|
65
|
-
|
|
66
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
-
Work and such Derivative Works in Source or Object form.
|
|
72
|
-
|
|
73
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
-
(except as stated in this section) patent license to make, have made,
|
|
77
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
-
where such license applies only to those patent claims licensable
|
|
79
|
-
by such Contributor that are necessarily infringed by their
|
|
80
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
-
institute patent litigation against any entity (including a
|
|
83
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
-
or contributory patent infringement, then any patent licenses
|
|
86
|
-
granted to You under this License for that Work shall terminate
|
|
87
|
-
as of the date such litigation is filed.
|
|
88
|
-
|
|
89
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
-
modifications, and in Source or Object form, provided that You
|
|
92
|
-
meet the following conditions:
|
|
93
|
-
|
|
94
|
-
(a) You must give any other recipients of the Work or
|
|
95
|
-
Derivative Works a copy of this License; and
|
|
96
|
-
|
|
97
|
-
(b) You must cause any modified files to carry prominent notices
|
|
98
|
-
stating that You changed the files; and
|
|
99
|
-
|
|
100
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
-
that You distribute, all copyright, patent, trademark, and
|
|
102
|
-
attribution notices from the Source form of the Work,
|
|
103
|
-
excluding those notices that do not pertain to any part of
|
|
104
|
-
the Derivative Works; and
|
|
105
|
-
|
|
106
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
-
distribution, then any Derivative Works that You distribute must
|
|
108
|
-
include a readable copy of the attribution notices contained
|
|
109
|
-
within such NOTICE file, excluding those notices that do not
|
|
110
|
-
pertain to any part of the Derivative Works, in at least one
|
|
111
|
-
of the following places: within a NOTICE text file distributed
|
|
112
|
-
as part of the Derivative Works; within the Source form or
|
|
113
|
-
documentation, if provided along with the Derivative Works; or,
|
|
114
|
-
within a display generated by the Derivative Works, if and
|
|
115
|
-
wherever such third-party notices normally appear. The contents
|
|
116
|
-
of the NOTICE file are for informational purposes only and
|
|
117
|
-
do not modify the License. You may add Your own attribution
|
|
118
|
-
notices within Derivative Works that You distribute, alongside
|
|
119
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
-
that such additional attribution notices cannot be construed
|
|
121
|
-
as modifying the License.
|
|
122
|
-
|
|
123
|
-
You may add Your own copyright statement to Your modifications and
|
|
124
|
-
may provide additional or different license terms and conditions
|
|
125
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
-
the conditions stated in this License.
|
|
129
|
-
|
|
130
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
-
this License, without any additional terms or conditions.
|
|
134
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
-
the terms of any separate license agreement you may have executed
|
|
136
|
-
with Licensor regarding such Contributions.
|
|
137
|
-
|
|
138
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
-
except as required for reasonable and customary use in describing the
|
|
141
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
-
|
|
143
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
-
implied, including, without limitation, any warranties or conditions
|
|
148
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
-
appropriateness of using or redistributing the Work and assume any
|
|
151
|
-
risks associated with Your exercise of permissions under this License.
|
|
152
|
-
|
|
153
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
-
unless required by applicable law (such as deliberate and grossly
|
|
156
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
-
liable to You for damages, including any direct, indirect, special,
|
|
158
|
-
incidental, or consequential damages of any character arising as a
|
|
159
|
-
result of this License or out of the use or inability to use the
|
|
160
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
-
other commercial damages or losses), even if such Contributor
|
|
163
|
-
has been advised of the possibility of such damages.
|
|
164
|
-
|
|
165
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
-
or other liability obligations and/or rights consistent with this
|
|
169
|
-
License. However, in accepting such obligations, You may act only
|
|
170
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
-
defend, and hold each Contributor harmless for any liability
|
|
173
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
-
of your accepting any such warranty or additional liability.
|
|
175
|
-
|
|
176
|
-
END OF TERMS AND CONDITIONS
|
|
177
|
-
|
|
178
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
-
|
|
180
|
-
To apply the Apache License to your work, attach the following
|
|
181
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
-
replaced with your own identifying information. (Don't include
|
|
183
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
-
comment syntax for the file format. We also recommend that a
|
|
185
|
-
file or class name and description of purpose be included on the
|
|
186
|
-
same "printed page" as the copyright notice for easier
|
|
187
|
-
identification within third-party archives.
|
|
188
|
-
|
|
189
|
-
Copyright 2019 Adobe
|
|
190
|
-
|
|
191
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
-
you may not use this file except in compliance with the License.
|
|
193
|
-
You may obtain a copy of the License at
|
|
194
|
-
|
|
195
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
-
|
|
197
|
-
Unless required by applicable law or agreed to in writing, software
|
|
198
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
-
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|