@react-aria/menu 3.13.2-nightly.4555 → 3.13.2-nightly.4560
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/ar-AE.mjs +1 -1
- package/dist/bg-BG.mjs +1 -1
- package/dist/cs-CZ.mjs +1 -1
- package/dist/da-DK.mjs +1 -1
- package/dist/de-DE.mjs +1 -1
- package/dist/el-GR.mjs +1 -1
- package/dist/en-US.mjs +1 -1
- package/dist/es-ES.mjs +1 -1
- package/dist/et-EE.mjs +1 -1
- package/dist/fi-FI.mjs +1 -1
- package/dist/fr-FR.mjs +1 -1
- package/dist/he-IL.mjs +1 -1
- package/dist/hr-HR.mjs +1 -1
- package/dist/hu-HU.mjs +1 -1
- package/dist/import.mjs +5 -721
- 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.mjs +1 -1
- package/dist/ja-JP.mjs +1 -1
- package/dist/ko-KR.mjs +1 -1
- package/dist/lt-LT.mjs +1 -1
- package/dist/lv-LV.mjs +1 -1
- package/dist/main.js +10 -726
- package/dist/main.js.map +1 -1
- package/dist/module.js +5 -721
- package/dist/module.js.map +1 -1
- package/dist/nb-NO.mjs +1 -1
- package/dist/nl-NL.mjs +1 -1
- package/dist/pl-PL.mjs +1 -1
- package/dist/pt-BR.mjs +1 -1
- package/dist/pt-PT.mjs +1 -1
- package/dist/ro-RO.mjs +1 -1
- package/dist/ru-RU.mjs +1 -1
- package/dist/sk-SK.mjs +1 -1
- package/dist/sl-SI.mjs +1 -1
- package/dist/sr-SP.mjs +1 -1
- package/dist/sv-SE.mjs +1 -1
- package/dist/tr-TR.mjs +1 -1
- package/dist/uk-UA.mjs +1 -1
- 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 +175 -0
- package/dist/useMenuItem.main.js.map +1 -0
- package/dist/useMenuItem.mjs +170 -0
- package/dist/useMenuItem.module.js +170 -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.mjs +1 -1
- package/dist/zh-TW.mjs +1 -1
- package/package.json +14 -14
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import $czs6v$intlStringsmodulejs from "./intlStrings.mjs";
|
|
2
|
+
import {useId as $czs6v$useId} from "@react-aria/utils";
|
|
3
|
+
import {useLocalizedStringFormatter as $czs6v$useLocalizedStringFormatter} from "@react-aria/i18n";
|
|
4
|
+
import {useLongPress as $czs6v$useLongPress} from "@react-aria/interactions";
|
|
5
|
+
import {useOverlayTrigger as $czs6v$useOverlayTrigger} from "@react-aria/overlays";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function $parcel$interopDefault(a) {
|
|
9
|
+
return a && a.__esModule ? a.default : a;
|
|
10
|
+
}
|
|
11
|
+
/*
|
|
12
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
13
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
+
* governing permissions and limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
function $168583247155ddda$export$dc9c12ed27dd1b49(props, state, ref) {
|
|
27
|
+
let { type: type = "menu", isDisabled: isDisabled, trigger: trigger = "press" } = props;
|
|
28
|
+
let menuTriggerId = (0, $czs6v$useId)();
|
|
29
|
+
let { triggerProps: triggerProps, overlayProps: overlayProps } = (0, $czs6v$useOverlayTrigger)({
|
|
30
|
+
type: type
|
|
31
|
+
}, state, ref);
|
|
32
|
+
let onKeyDown = (e)=>{
|
|
33
|
+
if (isDisabled) return;
|
|
34
|
+
if (trigger === "longPress" && !e.altKey) return;
|
|
35
|
+
if (ref && ref.current) switch(e.key){
|
|
36
|
+
case "Enter":
|
|
37
|
+
case " ":
|
|
38
|
+
if (trigger === "longPress") return;
|
|
39
|
+
// fallthrough
|
|
40
|
+
case "ArrowDown":
|
|
41
|
+
// Stop propagation, unless it would already be handled by useKeyboard.
|
|
42
|
+
if (!("continuePropagation" in e)) e.stopPropagation();
|
|
43
|
+
e.preventDefault();
|
|
44
|
+
state.toggle("first");
|
|
45
|
+
break;
|
|
46
|
+
case "ArrowUp":
|
|
47
|
+
if (!("continuePropagation" in e)) e.stopPropagation();
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
state.toggle("last");
|
|
50
|
+
break;
|
|
51
|
+
default:
|
|
52
|
+
// Allow other keys.
|
|
53
|
+
if ("continuePropagation" in e) e.continuePropagation();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
let stringFormatter = (0, $czs6v$useLocalizedStringFormatter)((0, ($parcel$interopDefault($czs6v$intlStringsmodulejs))), "@react-aria/menu");
|
|
57
|
+
let { longPressProps: longPressProps } = (0, $czs6v$useLongPress)({
|
|
58
|
+
isDisabled: isDisabled || trigger !== "longPress",
|
|
59
|
+
accessibilityDescription: stringFormatter.format("longPressMessage"),
|
|
60
|
+
onLongPressStart () {
|
|
61
|
+
state.close();
|
|
62
|
+
},
|
|
63
|
+
onLongPress () {
|
|
64
|
+
state.open("first");
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
let pressProps = {
|
|
68
|
+
onPressStart (e) {
|
|
69
|
+
// For consistency with native, open the menu on mouse/key down, but touch up.
|
|
70
|
+
if (e.pointerType !== "touch" && e.pointerType !== "keyboard" && !isDisabled) // If opened with a screen reader, auto focus the first item.
|
|
71
|
+
// Otherwise, the menu itself will be focused.
|
|
72
|
+
state.open(e.pointerType === "virtual" ? "first" : null);
|
|
73
|
+
},
|
|
74
|
+
onPress (e) {
|
|
75
|
+
if (e.pointerType === "touch" && !isDisabled) state.toggle();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
// omit onPress from triggerProps since we override it above.
|
|
79
|
+
delete triggerProps.onPress;
|
|
80
|
+
return {
|
|
81
|
+
menuTriggerProps: {
|
|
82
|
+
...triggerProps,
|
|
83
|
+
...trigger === "press" ? pressProps : longPressProps,
|
|
84
|
+
id: menuTriggerId,
|
|
85
|
+
onKeyDown: onKeyDown
|
|
86
|
+
},
|
|
87
|
+
menuProps: {
|
|
88
|
+
...overlayProps,
|
|
89
|
+
"aria-labelledby": menuTriggerId,
|
|
90
|
+
autoFocus: state.focusStrategy || true,
|
|
91
|
+
onClose: state.close
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
export {$168583247155ddda$export$dc9c12ed27dd1b49 as useMenuTrigger};
|
|
98
|
+
//# sourceMappingURL=useMenuTrigger.mjs.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import $czs6v$intlStringsmodulejs from "./intlStrings.module.js";
|
|
2
|
+
import {useId as $czs6v$useId} from "@react-aria/utils";
|
|
3
|
+
import {useLocalizedStringFormatter as $czs6v$useLocalizedStringFormatter} from "@react-aria/i18n";
|
|
4
|
+
import {useLongPress as $czs6v$useLongPress} from "@react-aria/interactions";
|
|
5
|
+
import {useOverlayTrigger as $czs6v$useOverlayTrigger} from "@react-aria/overlays";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function $parcel$interopDefault(a) {
|
|
9
|
+
return a && a.__esModule ? a.default : a;
|
|
10
|
+
}
|
|
11
|
+
/*
|
|
12
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
13
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
+
* governing permissions and limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
function $168583247155ddda$export$dc9c12ed27dd1b49(props, state, ref) {
|
|
27
|
+
let { type: type = "menu", isDisabled: isDisabled, trigger: trigger = "press" } = props;
|
|
28
|
+
let menuTriggerId = (0, $czs6v$useId)();
|
|
29
|
+
let { triggerProps: triggerProps, overlayProps: overlayProps } = (0, $czs6v$useOverlayTrigger)({
|
|
30
|
+
type: type
|
|
31
|
+
}, state, ref);
|
|
32
|
+
let onKeyDown = (e)=>{
|
|
33
|
+
if (isDisabled) return;
|
|
34
|
+
if (trigger === "longPress" && !e.altKey) return;
|
|
35
|
+
if (ref && ref.current) switch(e.key){
|
|
36
|
+
case "Enter":
|
|
37
|
+
case " ":
|
|
38
|
+
if (trigger === "longPress") return;
|
|
39
|
+
// fallthrough
|
|
40
|
+
case "ArrowDown":
|
|
41
|
+
// Stop propagation, unless it would already be handled by useKeyboard.
|
|
42
|
+
if (!("continuePropagation" in e)) e.stopPropagation();
|
|
43
|
+
e.preventDefault();
|
|
44
|
+
state.toggle("first");
|
|
45
|
+
break;
|
|
46
|
+
case "ArrowUp":
|
|
47
|
+
if (!("continuePropagation" in e)) e.stopPropagation();
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
state.toggle("last");
|
|
50
|
+
break;
|
|
51
|
+
default:
|
|
52
|
+
// Allow other keys.
|
|
53
|
+
if ("continuePropagation" in e) e.continuePropagation();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
let stringFormatter = (0, $czs6v$useLocalizedStringFormatter)((0, ($parcel$interopDefault($czs6v$intlStringsmodulejs))), "@react-aria/menu");
|
|
57
|
+
let { longPressProps: longPressProps } = (0, $czs6v$useLongPress)({
|
|
58
|
+
isDisabled: isDisabled || trigger !== "longPress",
|
|
59
|
+
accessibilityDescription: stringFormatter.format("longPressMessage"),
|
|
60
|
+
onLongPressStart () {
|
|
61
|
+
state.close();
|
|
62
|
+
},
|
|
63
|
+
onLongPress () {
|
|
64
|
+
state.open("first");
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
let pressProps = {
|
|
68
|
+
onPressStart (e) {
|
|
69
|
+
// For consistency with native, open the menu on mouse/key down, but touch up.
|
|
70
|
+
if (e.pointerType !== "touch" && e.pointerType !== "keyboard" && !isDisabled) // If opened with a screen reader, auto focus the first item.
|
|
71
|
+
// Otherwise, the menu itself will be focused.
|
|
72
|
+
state.open(e.pointerType === "virtual" ? "first" : null);
|
|
73
|
+
},
|
|
74
|
+
onPress (e) {
|
|
75
|
+
if (e.pointerType === "touch" && !isDisabled) state.toggle();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
// omit onPress from triggerProps since we override it above.
|
|
79
|
+
delete triggerProps.onPress;
|
|
80
|
+
return {
|
|
81
|
+
menuTriggerProps: {
|
|
82
|
+
...triggerProps,
|
|
83
|
+
...trigger === "press" ? pressProps : longPressProps,
|
|
84
|
+
id: menuTriggerId,
|
|
85
|
+
onKeyDown: onKeyDown
|
|
86
|
+
},
|
|
87
|
+
menuProps: {
|
|
88
|
+
...overlayProps,
|
|
89
|
+
"aria-labelledby": menuTriggerId,
|
|
90
|
+
autoFocus: state.focusStrategy || true,
|
|
91
|
+
onClose: state.close
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
export {$168583247155ddda$export$dc9c12ed27dd1b49 as useMenuTrigger};
|
|
98
|
+
//# sourceMappingURL=useMenuTrigger.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;AAqCM,SAAS,0CAAkB,KAA2B,EAAE,KAAuB,EAAE,GAAuB;IAC7G,IAAI,QACF,OAAO,oBACP,UAAU,WACV,UAAU,SACX,GAAG;IAEJ,IAAI,gBAAgB,CAAA,GAAA,YAAI;IACxB,IAAI,gBAAC,YAAY,gBAAE,YAAY,EAAC,GAAG,CAAA,GAAA,wBAAgB,EAAE;cAAC;IAAI,GAAG,OAAO;IAEpE,IAAI,YAAY,CAAC;QACf,IAAI,YACF;QAGF,IAAI,YAAY,eAAe,CAAC,EAAE,MAAM,EACtC;QAGF,IAAI,OAAO,IAAI,OAAO,EACpB,OAAQ,EAAE,GAAG;YACX,KAAK;YACL,KAAK;gBACH,IAAI,YAAY,aACd;YAEF,cAAc;YAChB,KAAK;gBACH,uEAAuE;gBACvE,IAAI,CAAE,CAAA,yBAAyB,CAAA,GAC7B,EAAE,eAAe;gBAEnB,EAAE,cAAc;gBAChB,MAAM,MAAM,CAAC;gBACb;YACF,KAAK;gBACH,IAAI,CAAE,CAAA,yBAAyB,CAAA,GAC7B,EAAE,eAAe;gBAEnB,EAAE,cAAc;gBAChB,MAAM,MAAM,CAAC;gBACb;YACF;gBACE,oBAAoB;gBACpB,IAAI,yBAAyB,GAC3B,EAAE,mBAAmB;QAE3B;IAEJ;IAEA,IAAI,kBAAkB,CAAA,GAAA,kCAA0B,EAAE,CAAA,GAAA,oDAAW,GAAG;IAChE,IAAI,kBAAC,cAAc,EAAC,GAAG,CAAA,GAAA,mBAAW,EAAE;QAClC,YAAY,cAAc,YAAY;QACtC,0BAA0B,gBAAgB,MAAM,CAAC;QACjD;YACE,MAAM,KAAK;QACb;QACA;YACE,MAAM,IAAI,CAAC;QACb;IACF;IAEA,IAAI,aAAc;QAChB,cAAa,CAAC;YACZ,8EAA8E;YAC9E,IAAI,EAAE,WAAW,KAAK,WAAW,EAAE,WAAW,KAAK,cAAc,CAAC,YAChE,6DAA6D;YAC7D,8CAA8C;YAC9C,MAAM,IAAI,CAAC,EAAE,WAAW,KAAK,YAAY,UAAU;QAEvD;QACA,SAAQ,CAAC;YACP,IAAI,EAAE,WAAW,KAAK,WAAW,CAAC,YAChC,MAAM,MAAM;QAEhB;IACF;IAEA,6DAA6D;IAC7D,OAAO,aAAa,OAAO;IAE3B,OAAO;QACL,kBAAkB;YAChB,GAAG,YAAY;YACf,GAAI,YAAY,UAAU,aAAa,cAAc;YACrD,IAAI;uBACJ;QACF;QACA,WAAW;YACT,GAAG,YAAY;YACf,mBAAmB;YACnB,WAAW,MAAM,aAAa,IAAI;YAClC,SAAS,MAAM,KAAK;QACtB;IACF;AACF","sources":["packages/@react-aria/menu/src/useMenuTrigger.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {AriaButtonProps} from '@react-types/button';\nimport {AriaMenuOptions} from './useMenu';\n// @ts-ignore\nimport intlMessages from '../intl/*.json';\nimport {MenuTriggerState} from '@react-stately/menu';\nimport {MenuTriggerType} from '@react-types/menu';\nimport {RefObject} from 'react';\nimport {useId} from '@react-aria/utils';\nimport {useLocalizedStringFormatter} from '@react-aria/i18n';\nimport {useLongPress} from '@react-aria/interactions';\nimport {useOverlayTrigger} from '@react-aria/overlays';\n\nexport interface AriaMenuTriggerProps {\n /** The type of menu that the menu trigger opens. */\n type?: 'menu' | 'listbox',\n /** Whether menu trigger is disabled. */\n isDisabled?: boolean,\n /** How menu is triggered. */\n trigger?: MenuTriggerType\n}\n\nexport interface MenuTriggerAria<T> {\n /** Props for the menu trigger element. */\n menuTriggerProps: AriaButtonProps,\n\n /** Props for the menu. */\n menuProps: AriaMenuOptions<T>\n}\n\n/**\n * Provides the behavior and accessibility implementation for a menu trigger.\n * @param props - Props for the menu trigger.\n * @param state - State for the menu trigger.\n * @param ref - Ref to the HTML element trigger for the menu.\n */\nexport function useMenuTrigger<T>(props: AriaMenuTriggerProps, state: MenuTriggerState, ref: RefObject<Element>): MenuTriggerAria<T> {\n let {\n type = 'menu' as AriaMenuTriggerProps['type'],\n isDisabled,\n trigger = 'press'\n } = props;\n\n let menuTriggerId = useId();\n let {triggerProps, overlayProps} = useOverlayTrigger({type}, state, ref);\n\n let onKeyDown = (e) => {\n if (isDisabled) {\n return;\n }\n\n if (trigger === 'longPress' && !e.altKey) {\n return;\n }\n\n if (ref && ref.current) {\n switch (e.key) {\n case 'Enter':\n case ' ':\n if (trigger === 'longPress') {\n return;\n }\n // fallthrough\n case 'ArrowDown':\n // Stop propagation, unless it would already be handled by useKeyboard.\n if (!('continuePropagation' in e)) {\n e.stopPropagation();\n }\n e.preventDefault();\n state.toggle('first');\n break;\n case 'ArrowUp':\n if (!('continuePropagation' in e)) {\n e.stopPropagation();\n }\n e.preventDefault();\n state.toggle('last');\n break;\n default:\n // Allow other keys.\n if ('continuePropagation' in e) {\n e.continuePropagation();\n }\n }\n }\n };\n\n let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/menu');\n let {longPressProps} = useLongPress({\n isDisabled: isDisabled || trigger !== 'longPress',\n accessibilityDescription: stringFormatter.format('longPressMessage'),\n onLongPressStart() {\n state.close();\n },\n onLongPress() {\n state.open('first');\n }\n });\n\n let pressProps = {\n onPressStart(e) {\n // For consistency with native, open the menu on mouse/key down, but touch up.\n if (e.pointerType !== 'touch' && e.pointerType !== 'keyboard' && !isDisabled) {\n // If opened with a screen reader, auto focus the first item.\n // Otherwise, the menu itself will be focused.\n state.open(e.pointerType === 'virtual' ? 'first' : null);\n }\n },\n onPress(e) {\n if (e.pointerType === 'touch' && !isDisabled) {\n state.toggle();\n }\n }\n };\n\n // omit onPress from triggerProps since we override it above.\n delete triggerProps.onPress;\n\n return {\n menuTriggerProps: {\n ...triggerProps,\n ...(trigger === 'press' ? pressProps : longPressProps),\n id: menuTriggerId,\n onKeyDown\n },\n menuProps: {\n ...overlayProps,\n 'aria-labelledby': menuTriggerId,\n autoFocus: state.focusStrategy || true,\n onClose: state.close\n }\n };\n}\n"],"names":[],"version":3,"file":"useMenuTrigger.module.js.map"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
var $g3RPq$react = require("react");
|
|
2
|
+
var $g3RPq$reactariainteractions = require("@react-aria/interactions");
|
|
3
|
+
var $g3RPq$reactariautils = require("@react-aria/utils");
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
function $parcel$export(e, n, v, s) {
|
|
7
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
$parcel$export(module.exports, "useSafelyMouseToSubmenu", () => $62347d8c4183e713$export$85ec83e04c95f50a);
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const $62347d8c4183e713$var$ALLOWED_INVALID_MOVEMENTS = 2;
|
|
15
|
+
const $62347d8c4183e713$var$THROTTLE_TIME = 50;
|
|
16
|
+
const $62347d8c4183e713$var$TIMEOUT_TIME = 1000;
|
|
17
|
+
const $62347d8c4183e713$var$ANGLE_PADDING = Math.PI / 12; // 15°
|
|
18
|
+
function $62347d8c4183e713$export$85ec83e04c95f50a(options) {
|
|
19
|
+
let { menuRef: menuRef, submenuRef: submenuRef, isOpen: isOpen, isDisabled: isDisabled } = options;
|
|
20
|
+
let prevPointerPos = (0, $g3RPq$react.useRef)();
|
|
21
|
+
let submenuRect = (0, $g3RPq$react.useRef)();
|
|
22
|
+
let lastProcessedTime = (0, $g3RPq$react.useRef)(0);
|
|
23
|
+
let timeout = (0, $g3RPq$react.useRef)();
|
|
24
|
+
let autoCloseTimeout = (0, $g3RPq$react.useRef)();
|
|
25
|
+
let submenuSide = (0, $g3RPq$react.useRef)();
|
|
26
|
+
let movementsTowardsSubmenuCount = (0, $g3RPq$react.useRef)(2);
|
|
27
|
+
let [preventPointerEvents, setPreventPointerEvents] = (0, $g3RPq$react.useState)(false);
|
|
28
|
+
let updateSubmenuRect = ()=>{
|
|
29
|
+
if (submenuRef.current) {
|
|
30
|
+
submenuRect.current = submenuRef.current.getBoundingClientRect();
|
|
31
|
+
submenuSide.current = undefined;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
(0, $g3RPq$reactariautils.useResizeObserver)({
|
|
35
|
+
ref: submenuRef,
|
|
36
|
+
onResize: updateSubmenuRect
|
|
37
|
+
});
|
|
38
|
+
let reset = ()=>{
|
|
39
|
+
setPreventPointerEvents(false);
|
|
40
|
+
movementsTowardsSubmenuCount.current = $62347d8c4183e713$var$ALLOWED_INVALID_MOVEMENTS;
|
|
41
|
+
prevPointerPos.current = undefined;
|
|
42
|
+
};
|
|
43
|
+
let modality = (0, $g3RPq$reactariainteractions.useInteractionModality)();
|
|
44
|
+
(0, $g3RPq$react.useEffect)(()=>{
|
|
45
|
+
if (preventPointerEvents && menuRef.current) menuRef.current.style.pointerEvents = "none";
|
|
46
|
+
else menuRef.current.style.pointerEvents = "";
|
|
47
|
+
}, [
|
|
48
|
+
menuRef,
|
|
49
|
+
preventPointerEvents
|
|
50
|
+
]);
|
|
51
|
+
(0, $g3RPq$react.useEffect)(()=>{
|
|
52
|
+
let submenu = submenuRef.current;
|
|
53
|
+
let menu = menuRef.current;
|
|
54
|
+
if (isDisabled || !submenu || !isOpen || modality !== "pointer") {
|
|
55
|
+
reset();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
submenuRect.current = submenu.getBoundingClientRect();
|
|
59
|
+
let onPointerMove = (e)=>{
|
|
60
|
+
if (e.pointerType === "touch" || e.pointerType === "pen") return;
|
|
61
|
+
let currentTime = Date.now();
|
|
62
|
+
// Throttle
|
|
63
|
+
if (currentTime - lastProcessedTime.current < $62347d8c4183e713$var$THROTTLE_TIME) return;
|
|
64
|
+
clearTimeout(timeout.current);
|
|
65
|
+
clearTimeout(autoCloseTimeout.current);
|
|
66
|
+
let { clientX: mouseX, clientY: mouseY } = e;
|
|
67
|
+
if (!prevPointerPos.current) {
|
|
68
|
+
prevPointerPos.current = {
|
|
69
|
+
x: mouseX,
|
|
70
|
+
y: mouseY
|
|
71
|
+
};
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (!submenuRect.current) return;
|
|
75
|
+
if (!submenuSide.current) submenuSide.current = mouseX > submenuRect.current.right ? "left" : "right";
|
|
76
|
+
// Pointer is outside of parent menu
|
|
77
|
+
if (mouseX < menu.getBoundingClientRect().left || mouseX > menu.getBoundingClientRect().right || mouseY < menu.getBoundingClientRect().top || mouseY > menu.getBoundingClientRect().bottom) {
|
|
78
|
+
reset();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
/* Check if pointer is moving towards submenu.
|
|
82
|
+
Uses the 2-argument arctangent (https://en.wikipedia.org/wiki/Atan2) to calculate:
|
|
83
|
+
- angle between previous pointer and top of submenu
|
|
84
|
+
- angle between previous pointer and bottom of submenu
|
|
85
|
+
- angle between previous pointer and current pointer (delta)
|
|
86
|
+
If the pointer delta angle value is between the top and bottom angle values, we know the pointer is moving towards the submenu.
|
|
87
|
+
*/ let prevMouseX = prevPointerPos.current.x;
|
|
88
|
+
let prevMouseY = prevPointerPos.current.y;
|
|
89
|
+
let toSubmenuX = submenuSide.current === "right" ? submenuRect.current.left - prevMouseX : prevMouseX - submenuRect.current.right;
|
|
90
|
+
let angleTop = Math.atan2(prevMouseY - submenuRect.current.top, toSubmenuX) + $62347d8c4183e713$var$ANGLE_PADDING;
|
|
91
|
+
let angleBottom = Math.atan2(prevMouseY - submenuRect.current.bottom, toSubmenuX) - $62347d8c4183e713$var$ANGLE_PADDING;
|
|
92
|
+
let anglePointer = Math.atan2(prevMouseY - mouseY, submenuSide.current === "left" ? -(mouseX - prevMouseX) : mouseX - prevMouseX);
|
|
93
|
+
let isMovingTowardsSubmenu = anglePointer < angleTop && anglePointer > angleBottom;
|
|
94
|
+
movementsTowardsSubmenuCount.current = isMovingTowardsSubmenu ? Math.min(movementsTowardsSubmenuCount.current + 1, $62347d8c4183e713$var$ALLOWED_INVALID_MOVEMENTS) : Math.max(movementsTowardsSubmenuCount.current - 1, 0);
|
|
95
|
+
if (movementsTowardsSubmenuCount.current >= $62347d8c4183e713$var$ALLOWED_INVALID_MOVEMENTS) setPreventPointerEvents(true);
|
|
96
|
+
else setPreventPointerEvents(false);
|
|
97
|
+
lastProcessedTime.current = currentTime;
|
|
98
|
+
prevPointerPos.current = {
|
|
99
|
+
x: mouseX,
|
|
100
|
+
y: mouseY
|
|
101
|
+
};
|
|
102
|
+
// If the pointer is moving towards the submenu, start a timeout to close if no other movements are made after 500ms.
|
|
103
|
+
if (isMovingTowardsSubmenu) timeout.current = setTimeout(()=>{
|
|
104
|
+
reset();
|
|
105
|
+
autoCloseTimeout.current = setTimeout(()=>{
|
|
106
|
+
// Fire a pointerover event to trigger the menu to close.
|
|
107
|
+
// Wait until pointer-events:none is no longer applied
|
|
108
|
+
let target = document.elementFromPoint(mouseX, mouseY);
|
|
109
|
+
if (target && menu.contains(target)) target.dispatchEvent(new PointerEvent("pointerover", {
|
|
110
|
+
bubbles: true,
|
|
111
|
+
cancelable: true
|
|
112
|
+
}));
|
|
113
|
+
}, 100);
|
|
114
|
+
}, $62347d8c4183e713$var$TIMEOUT_TIME);
|
|
115
|
+
};
|
|
116
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
117
|
+
return ()=>{
|
|
118
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
119
|
+
clearTimeout(timeout.current);
|
|
120
|
+
clearTimeout(autoCloseTimeout.current);
|
|
121
|
+
movementsTowardsSubmenuCount.current = $62347d8c4183e713$var$ALLOWED_INVALID_MOVEMENTS;
|
|
122
|
+
};
|
|
123
|
+
}, [
|
|
124
|
+
isDisabled,
|
|
125
|
+
isOpen,
|
|
126
|
+
menuRef,
|
|
127
|
+
modality,
|
|
128
|
+
setPreventPointerEvents,
|
|
129
|
+
submenuRef
|
|
130
|
+
]);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
//# sourceMappingURL=useSafelyMouseToSubmenu.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;AAeA,MAAM,kDAA4B;AAClC,MAAM,sCAAgB;AACtB,MAAM,qCAAe;AACrB,MAAM,sCAAgB,KAAK,EAAE,GAAG,IAAI,MAAM;AAMnC,SAAS,0CAAwB,OAAoC;IAC1E,IAAI,WAAC,OAAO,cAAE,UAAU,UAAE,MAAM,cAAE,UAAU,EAAC,GAAG;IA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GAAG,GAAG;YAErD,IAAI,6BAA6B,OAAO,IAAI,iDAC1C,wBAAwB;iBAExB,wBAAwB;YAG1B,kBAAkB,OAAO,GAAG;YAC5B,eAAe,OAAO,GAAG;gBAAC,GAAG;gBAAQ,GAAG;YAAM;YAE9C,qHAAqH;YACrH,IAAI,wBACF,QAAQ,OAAO,GAAG,WAAW;gBAC3B;gBACA,iBAAiB,OAAO,GAAG,WAAW;oBACpC,yDAAyD;oBACzD,sDAAsD;oBACtD,IAAI,SAAS,SAAS,gBAAgB,CAAC,QAAQ;oBAC/C,IAAI,UAAU,KAAK,QAAQ,CAAC,SAC1B,OAAO,aAAa,CAAC,IAAI,aAAa,eAAe;wBAAC,SAAS;wBAAM,YAAY;oBAAI;gBAEzF,GAAG;YACL,GAAG;QAEP;QAEA,OAAO,gBAAgB,CAAC,eAAe;QAEvC,OAAO;YACL,OAAO,mBAAmB,CAAC,eAAe;YAC1C,aAAa,QAAQ,OAAO;YAC5B,aAAa,iBAAiB,OAAO;YACrC,6BAA6B,OAAO,GAAG;QACzC;IAEF,GAAG;QAAC;QAAY;QAAQ;QAAS;QAAU;QAAyB;KAAW;AACjF","sources":["packages/@react-aria/menu/src/useSafelyMouseToSubmenu.ts"],"sourcesContent":["import {RefObject, useEffect, useRef, useState} from 'react';\nimport {useInteractionModality} from '@react-aria/interactions';\nimport {useResizeObserver} from '@react-aria/utils';\n\ninterface SafelyMouseToSubmenuOptions {\n /** Ref for the parent menu. */\n menuRef: RefObject<Element>,\n /** Ref for the submenu. */\n submenuRef: RefObject<Element>,\n /** Whether the submenu is open. */\n isOpen: boolean,\n /** Whether this feature is disabled. */\n isDisabled?: boolean\n}\n\nconst ALLOWED_INVALID_MOVEMENTS = 2;\nconst THROTTLE_TIME = 50;\nconst TIMEOUT_TIME = 1000;\nconst ANGLE_PADDING = Math.PI / 12; // 15°\n\n/**\n * Allows the user to move their pointer to the submenu without it closing when their mouse leaves the trigger element.\n * Prevents pointer events from going to the underlying menu if the user is moving their pointer towards the sub-menu.\n */\nexport function useSafelyMouseToSubmenu(options: SafelyMouseToSubmenuOptions) {\n let {menuRef, submenuRef, isOpen, isDisabled} = options;\n let prevPointerPos = useRef<{x: number, y: number} | undefined>();\n let submenuRect = useRef<DOMRect | undefined>();\n let lastProcessedTime = useRef<number>(0);\n let timeout = useRef<ReturnType<typeof setTimeout> | undefined>();\n let autoCloseTimeout = useRef<ReturnType<typeof setTimeout> | undefined>();\n let submenuSide = useRef<'left' | 'right' | undefined>();\n let movementsTowardsSubmenuCount = useRef<number>(2);\n let [preventPointerEvents, setPreventPointerEvents] = useState(false);\n\n let updateSubmenuRect = () => {\n if (submenuRef.current) {\n submenuRect.current = submenuRef.current.getBoundingClientRect();\n submenuSide.current = undefined;\n }\n };\n useResizeObserver({ref: submenuRef, onResize: updateSubmenuRect});\n\n let reset = () => {\n setPreventPointerEvents(false);\n movementsTowardsSubmenuCount.current = ALLOWED_INVALID_MOVEMENTS;\n prevPointerPos.current = undefined;\n };\n\n let modality = useInteractionModality();\n\n useEffect(() => {\n if (preventPointerEvents && menuRef.current) {\n (menuRef.current as HTMLElement).style.pointerEvents = 'none';\n } else {\n (menuRef.current as HTMLElement).style.pointerEvents = '';\n }\n }, [menuRef, preventPointerEvents]);\n\n useEffect(() => {\n let submenu = submenuRef.current;\n let menu = menuRef.current;\n\n if (isDisabled || !submenu || !isOpen || modality !== 'pointer') {\n reset();\n return;\n }\n submenuRect.current = submenu.getBoundingClientRect();\n\n let onPointerMove = (e: PointerEvent) => {\n if (e.pointerType === 'touch' || e.pointerType === 'pen') {\n return;\n }\n\n let currentTime = Date.now();\n\n // Throttle\n if (currentTime - lastProcessedTime.current < THROTTLE_TIME) {\n return;\n }\n clearTimeout(timeout.current);\n clearTimeout(autoCloseTimeout.current);\n\n let {clientX: mouseX, clientY: mouseY} = e;\n\n if (!prevPointerPos.current) {\n prevPointerPos.current = {x: mouseX, y: mouseY};\n return;\n }\n\n if (!submenuRect.current) {\n return;\n }\n\n if (!submenuSide.current) {\n submenuSide.current = mouseX > submenuRect.current.right ? 'left' : 'right';\n }\n\n // Pointer is outside of parent menu\n if (mouseX < menu.getBoundingClientRect().left || mouseX > menu.getBoundingClientRect().right || mouseY < menu.getBoundingClientRect().top || mouseY > menu.getBoundingClientRect().bottom) {\n reset();\n return;\n }\n\n /* Check if pointer is moving towards submenu.\n Uses the 2-argument arctangent (https://en.wikipedia.org/wiki/Atan2) to calculate:\n - angle between previous pointer and top of submenu\n - angle between previous pointer and bottom of submenu\n - angle between previous pointer and current pointer (delta)\n If the pointer delta angle value is between the top and bottom angle values, we know the pointer is moving towards the submenu.\n */\n let prevMouseX = prevPointerPos.current.x;\n let prevMouseY = prevPointerPos.current.y;\n let toSubmenuX = submenuSide.current === 'right' ? submenuRect.current.left - prevMouseX : prevMouseX - submenuRect.current.right;\n let angleTop = Math.atan2(prevMouseY - submenuRect.current.top, toSubmenuX) + ANGLE_PADDING;\n let angleBottom = Math.atan2(prevMouseY - submenuRect.current.bottom, toSubmenuX) - ANGLE_PADDING;\n let anglePointer = Math.atan2(prevMouseY - mouseY, (submenuSide.current === 'left' ? -(mouseX - prevMouseX) : mouseX - prevMouseX));\n let isMovingTowardsSubmenu = anglePointer < angleTop && anglePointer > angleBottom;\n\n movementsTowardsSubmenuCount.current = isMovingTowardsSubmenu ?\n Math.min(movementsTowardsSubmenuCount.current + 1, ALLOWED_INVALID_MOVEMENTS) :\n Math.max(movementsTowardsSubmenuCount.current - 1, 0);\n\n if (movementsTowardsSubmenuCount.current >= ALLOWED_INVALID_MOVEMENTS) {\n setPreventPointerEvents(true);\n } else {\n setPreventPointerEvents(false);\n }\n\n lastProcessedTime.current = currentTime;\n prevPointerPos.current = {x: mouseX, y: mouseY};\n\n // If the pointer is moving towards the submenu, start a timeout to close if no other movements are made after 500ms.\n if (isMovingTowardsSubmenu) {\n timeout.current = setTimeout(() => {\n reset();\n autoCloseTimeout.current = setTimeout(() => {\n // Fire a pointerover event to trigger the menu to close.\n // Wait until pointer-events:none is no longer applied\n let target = document.elementFromPoint(mouseX, mouseY);\n if (target && menu.contains(target)) {\n target.dispatchEvent(new PointerEvent('pointerover', {bubbles: true, cancelable: true}));\n }\n }, 100);\n }, TIMEOUT_TIME);\n }\n };\n\n window.addEventListener('pointermove', onPointerMove);\n\n return () => {\n window.removeEventListener('pointermove', onPointerMove);\n clearTimeout(timeout.current);\n clearTimeout(autoCloseTimeout.current);\n movementsTowardsSubmenuCount.current = ALLOWED_INVALID_MOVEMENTS;\n };\n\n }, [isDisabled, isOpen, menuRef, modality, setPreventPointerEvents, submenuRef]);\n}\n"],"names":[],"version":3,"file":"useSafelyMouseToSubmenu.main.js.map"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {useRef as $fUfeP$useRef, useState as $fUfeP$useState, useEffect as $fUfeP$useEffect} from "react";
|
|
2
|
+
import {useInteractionModality as $fUfeP$useInteractionModality} from "@react-aria/interactions";
|
|
3
|
+
import {useResizeObserver as $fUfeP$useResizeObserver} from "@react-aria/utils";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS = 2;
|
|
9
|
+
const $d275435c250248f8$var$THROTTLE_TIME = 50;
|
|
10
|
+
const $d275435c250248f8$var$TIMEOUT_TIME = 1000;
|
|
11
|
+
const $d275435c250248f8$var$ANGLE_PADDING = Math.PI / 12; // 15°
|
|
12
|
+
function $d275435c250248f8$export$85ec83e04c95f50a(options) {
|
|
13
|
+
let { menuRef: menuRef, submenuRef: submenuRef, isOpen: isOpen, isDisabled: isDisabled } = options;
|
|
14
|
+
let prevPointerPos = (0, $fUfeP$useRef)();
|
|
15
|
+
let submenuRect = (0, $fUfeP$useRef)();
|
|
16
|
+
let lastProcessedTime = (0, $fUfeP$useRef)(0);
|
|
17
|
+
let timeout = (0, $fUfeP$useRef)();
|
|
18
|
+
let autoCloseTimeout = (0, $fUfeP$useRef)();
|
|
19
|
+
let submenuSide = (0, $fUfeP$useRef)();
|
|
20
|
+
let movementsTowardsSubmenuCount = (0, $fUfeP$useRef)(2);
|
|
21
|
+
let [preventPointerEvents, setPreventPointerEvents] = (0, $fUfeP$useState)(false);
|
|
22
|
+
let updateSubmenuRect = ()=>{
|
|
23
|
+
if (submenuRef.current) {
|
|
24
|
+
submenuRect.current = submenuRef.current.getBoundingClientRect();
|
|
25
|
+
submenuSide.current = undefined;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
(0, $fUfeP$useResizeObserver)({
|
|
29
|
+
ref: submenuRef,
|
|
30
|
+
onResize: updateSubmenuRect
|
|
31
|
+
});
|
|
32
|
+
let reset = ()=>{
|
|
33
|
+
setPreventPointerEvents(false);
|
|
34
|
+
movementsTowardsSubmenuCount.current = $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS;
|
|
35
|
+
prevPointerPos.current = undefined;
|
|
36
|
+
};
|
|
37
|
+
let modality = (0, $fUfeP$useInteractionModality)();
|
|
38
|
+
(0, $fUfeP$useEffect)(()=>{
|
|
39
|
+
if (preventPointerEvents && menuRef.current) menuRef.current.style.pointerEvents = "none";
|
|
40
|
+
else menuRef.current.style.pointerEvents = "";
|
|
41
|
+
}, [
|
|
42
|
+
menuRef,
|
|
43
|
+
preventPointerEvents
|
|
44
|
+
]);
|
|
45
|
+
(0, $fUfeP$useEffect)(()=>{
|
|
46
|
+
let submenu = submenuRef.current;
|
|
47
|
+
let menu = menuRef.current;
|
|
48
|
+
if (isDisabled || !submenu || !isOpen || modality !== "pointer") {
|
|
49
|
+
reset();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
submenuRect.current = submenu.getBoundingClientRect();
|
|
53
|
+
let onPointerMove = (e)=>{
|
|
54
|
+
if (e.pointerType === "touch" || e.pointerType === "pen") return;
|
|
55
|
+
let currentTime = Date.now();
|
|
56
|
+
// Throttle
|
|
57
|
+
if (currentTime - lastProcessedTime.current < $d275435c250248f8$var$THROTTLE_TIME) return;
|
|
58
|
+
clearTimeout(timeout.current);
|
|
59
|
+
clearTimeout(autoCloseTimeout.current);
|
|
60
|
+
let { clientX: mouseX, clientY: mouseY } = e;
|
|
61
|
+
if (!prevPointerPos.current) {
|
|
62
|
+
prevPointerPos.current = {
|
|
63
|
+
x: mouseX,
|
|
64
|
+
y: mouseY
|
|
65
|
+
};
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!submenuRect.current) return;
|
|
69
|
+
if (!submenuSide.current) submenuSide.current = mouseX > submenuRect.current.right ? "left" : "right";
|
|
70
|
+
// Pointer is outside of parent menu
|
|
71
|
+
if (mouseX < menu.getBoundingClientRect().left || mouseX > menu.getBoundingClientRect().right || mouseY < menu.getBoundingClientRect().top || mouseY > menu.getBoundingClientRect().bottom) {
|
|
72
|
+
reset();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
/* Check if pointer is moving towards submenu.
|
|
76
|
+
Uses the 2-argument arctangent (https://en.wikipedia.org/wiki/Atan2) to calculate:
|
|
77
|
+
- angle between previous pointer and top of submenu
|
|
78
|
+
- angle between previous pointer and bottom of submenu
|
|
79
|
+
- angle between previous pointer and current pointer (delta)
|
|
80
|
+
If the pointer delta angle value is between the top and bottom angle values, we know the pointer is moving towards the submenu.
|
|
81
|
+
*/ let prevMouseX = prevPointerPos.current.x;
|
|
82
|
+
let prevMouseY = prevPointerPos.current.y;
|
|
83
|
+
let toSubmenuX = submenuSide.current === "right" ? submenuRect.current.left - prevMouseX : prevMouseX - submenuRect.current.right;
|
|
84
|
+
let angleTop = Math.atan2(prevMouseY - submenuRect.current.top, toSubmenuX) + $d275435c250248f8$var$ANGLE_PADDING;
|
|
85
|
+
let angleBottom = Math.atan2(prevMouseY - submenuRect.current.bottom, toSubmenuX) - $d275435c250248f8$var$ANGLE_PADDING;
|
|
86
|
+
let anglePointer = Math.atan2(prevMouseY - mouseY, submenuSide.current === "left" ? -(mouseX - prevMouseX) : mouseX - prevMouseX);
|
|
87
|
+
let isMovingTowardsSubmenu = anglePointer < angleTop && anglePointer > angleBottom;
|
|
88
|
+
movementsTowardsSubmenuCount.current = isMovingTowardsSubmenu ? Math.min(movementsTowardsSubmenuCount.current + 1, $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS) : Math.max(movementsTowardsSubmenuCount.current - 1, 0);
|
|
89
|
+
if (movementsTowardsSubmenuCount.current >= $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS) setPreventPointerEvents(true);
|
|
90
|
+
else setPreventPointerEvents(false);
|
|
91
|
+
lastProcessedTime.current = currentTime;
|
|
92
|
+
prevPointerPos.current = {
|
|
93
|
+
x: mouseX,
|
|
94
|
+
y: mouseY
|
|
95
|
+
};
|
|
96
|
+
// If the pointer is moving towards the submenu, start a timeout to close if no other movements are made after 500ms.
|
|
97
|
+
if (isMovingTowardsSubmenu) timeout.current = setTimeout(()=>{
|
|
98
|
+
reset();
|
|
99
|
+
autoCloseTimeout.current = setTimeout(()=>{
|
|
100
|
+
// Fire a pointerover event to trigger the menu to close.
|
|
101
|
+
// Wait until pointer-events:none is no longer applied
|
|
102
|
+
let target = document.elementFromPoint(mouseX, mouseY);
|
|
103
|
+
if (target && menu.contains(target)) target.dispatchEvent(new PointerEvent("pointerover", {
|
|
104
|
+
bubbles: true,
|
|
105
|
+
cancelable: true
|
|
106
|
+
}));
|
|
107
|
+
}, 100);
|
|
108
|
+
}, $d275435c250248f8$var$TIMEOUT_TIME);
|
|
109
|
+
};
|
|
110
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
111
|
+
return ()=>{
|
|
112
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
113
|
+
clearTimeout(timeout.current);
|
|
114
|
+
clearTimeout(autoCloseTimeout.current);
|
|
115
|
+
movementsTowardsSubmenuCount.current = $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS;
|
|
116
|
+
};
|
|
117
|
+
}, [
|
|
118
|
+
isDisabled,
|
|
119
|
+
isOpen,
|
|
120
|
+
menuRef,
|
|
121
|
+
modality,
|
|
122
|
+
setPreventPointerEvents,
|
|
123
|
+
submenuRef
|
|
124
|
+
]);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
export {$d275435c250248f8$export$85ec83e04c95f50a as useSafelyMouseToSubmenu};
|
|
129
|
+
//# sourceMappingURL=useSafelyMouseToSubmenu.mjs.map
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {useRef as $fUfeP$useRef, useState as $fUfeP$useState, useEffect as $fUfeP$useEffect} from "react";
|
|
2
|
+
import {useInteractionModality as $fUfeP$useInteractionModality} from "@react-aria/interactions";
|
|
3
|
+
import {useResizeObserver as $fUfeP$useResizeObserver} from "@react-aria/utils";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
const $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS = 2;
|
|
9
|
+
const $d275435c250248f8$var$THROTTLE_TIME = 50;
|
|
10
|
+
const $d275435c250248f8$var$TIMEOUT_TIME = 1000;
|
|
11
|
+
const $d275435c250248f8$var$ANGLE_PADDING = Math.PI / 12; // 15°
|
|
12
|
+
function $d275435c250248f8$export$85ec83e04c95f50a(options) {
|
|
13
|
+
let { menuRef: menuRef, submenuRef: submenuRef, isOpen: isOpen, isDisabled: isDisabled } = options;
|
|
14
|
+
let prevPointerPos = (0, $fUfeP$useRef)();
|
|
15
|
+
let submenuRect = (0, $fUfeP$useRef)();
|
|
16
|
+
let lastProcessedTime = (0, $fUfeP$useRef)(0);
|
|
17
|
+
let timeout = (0, $fUfeP$useRef)();
|
|
18
|
+
let autoCloseTimeout = (0, $fUfeP$useRef)();
|
|
19
|
+
let submenuSide = (0, $fUfeP$useRef)();
|
|
20
|
+
let movementsTowardsSubmenuCount = (0, $fUfeP$useRef)(2);
|
|
21
|
+
let [preventPointerEvents, setPreventPointerEvents] = (0, $fUfeP$useState)(false);
|
|
22
|
+
let updateSubmenuRect = ()=>{
|
|
23
|
+
if (submenuRef.current) {
|
|
24
|
+
submenuRect.current = submenuRef.current.getBoundingClientRect();
|
|
25
|
+
submenuSide.current = undefined;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
(0, $fUfeP$useResizeObserver)({
|
|
29
|
+
ref: submenuRef,
|
|
30
|
+
onResize: updateSubmenuRect
|
|
31
|
+
});
|
|
32
|
+
let reset = ()=>{
|
|
33
|
+
setPreventPointerEvents(false);
|
|
34
|
+
movementsTowardsSubmenuCount.current = $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS;
|
|
35
|
+
prevPointerPos.current = undefined;
|
|
36
|
+
};
|
|
37
|
+
let modality = (0, $fUfeP$useInteractionModality)();
|
|
38
|
+
(0, $fUfeP$useEffect)(()=>{
|
|
39
|
+
if (preventPointerEvents && menuRef.current) menuRef.current.style.pointerEvents = "none";
|
|
40
|
+
else menuRef.current.style.pointerEvents = "";
|
|
41
|
+
}, [
|
|
42
|
+
menuRef,
|
|
43
|
+
preventPointerEvents
|
|
44
|
+
]);
|
|
45
|
+
(0, $fUfeP$useEffect)(()=>{
|
|
46
|
+
let submenu = submenuRef.current;
|
|
47
|
+
let menu = menuRef.current;
|
|
48
|
+
if (isDisabled || !submenu || !isOpen || modality !== "pointer") {
|
|
49
|
+
reset();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
submenuRect.current = submenu.getBoundingClientRect();
|
|
53
|
+
let onPointerMove = (e)=>{
|
|
54
|
+
if (e.pointerType === "touch" || e.pointerType === "pen") return;
|
|
55
|
+
let currentTime = Date.now();
|
|
56
|
+
// Throttle
|
|
57
|
+
if (currentTime - lastProcessedTime.current < $d275435c250248f8$var$THROTTLE_TIME) return;
|
|
58
|
+
clearTimeout(timeout.current);
|
|
59
|
+
clearTimeout(autoCloseTimeout.current);
|
|
60
|
+
let { clientX: mouseX, clientY: mouseY } = e;
|
|
61
|
+
if (!prevPointerPos.current) {
|
|
62
|
+
prevPointerPos.current = {
|
|
63
|
+
x: mouseX,
|
|
64
|
+
y: mouseY
|
|
65
|
+
};
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!submenuRect.current) return;
|
|
69
|
+
if (!submenuSide.current) submenuSide.current = mouseX > submenuRect.current.right ? "left" : "right";
|
|
70
|
+
// Pointer is outside of parent menu
|
|
71
|
+
if (mouseX < menu.getBoundingClientRect().left || mouseX > menu.getBoundingClientRect().right || mouseY < menu.getBoundingClientRect().top || mouseY > menu.getBoundingClientRect().bottom) {
|
|
72
|
+
reset();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
/* Check if pointer is moving towards submenu.
|
|
76
|
+
Uses the 2-argument arctangent (https://en.wikipedia.org/wiki/Atan2) to calculate:
|
|
77
|
+
- angle between previous pointer and top of submenu
|
|
78
|
+
- angle between previous pointer and bottom of submenu
|
|
79
|
+
- angle between previous pointer and current pointer (delta)
|
|
80
|
+
If the pointer delta angle value is between the top and bottom angle values, we know the pointer is moving towards the submenu.
|
|
81
|
+
*/ let prevMouseX = prevPointerPos.current.x;
|
|
82
|
+
let prevMouseY = prevPointerPos.current.y;
|
|
83
|
+
let toSubmenuX = submenuSide.current === "right" ? submenuRect.current.left - prevMouseX : prevMouseX - submenuRect.current.right;
|
|
84
|
+
let angleTop = Math.atan2(prevMouseY - submenuRect.current.top, toSubmenuX) + $d275435c250248f8$var$ANGLE_PADDING;
|
|
85
|
+
let angleBottom = Math.atan2(prevMouseY - submenuRect.current.bottom, toSubmenuX) - $d275435c250248f8$var$ANGLE_PADDING;
|
|
86
|
+
let anglePointer = Math.atan2(prevMouseY - mouseY, submenuSide.current === "left" ? -(mouseX - prevMouseX) : mouseX - prevMouseX);
|
|
87
|
+
let isMovingTowardsSubmenu = anglePointer < angleTop && anglePointer > angleBottom;
|
|
88
|
+
movementsTowardsSubmenuCount.current = isMovingTowardsSubmenu ? Math.min(movementsTowardsSubmenuCount.current + 1, $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS) : Math.max(movementsTowardsSubmenuCount.current - 1, 0);
|
|
89
|
+
if (movementsTowardsSubmenuCount.current >= $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS) setPreventPointerEvents(true);
|
|
90
|
+
else setPreventPointerEvents(false);
|
|
91
|
+
lastProcessedTime.current = currentTime;
|
|
92
|
+
prevPointerPos.current = {
|
|
93
|
+
x: mouseX,
|
|
94
|
+
y: mouseY
|
|
95
|
+
};
|
|
96
|
+
// If the pointer is moving towards the submenu, start a timeout to close if no other movements are made after 500ms.
|
|
97
|
+
if (isMovingTowardsSubmenu) timeout.current = setTimeout(()=>{
|
|
98
|
+
reset();
|
|
99
|
+
autoCloseTimeout.current = setTimeout(()=>{
|
|
100
|
+
// Fire a pointerover event to trigger the menu to close.
|
|
101
|
+
// Wait until pointer-events:none is no longer applied
|
|
102
|
+
let target = document.elementFromPoint(mouseX, mouseY);
|
|
103
|
+
if (target && menu.contains(target)) target.dispatchEvent(new PointerEvent("pointerover", {
|
|
104
|
+
bubbles: true,
|
|
105
|
+
cancelable: true
|
|
106
|
+
}));
|
|
107
|
+
}, 100);
|
|
108
|
+
}, $d275435c250248f8$var$TIMEOUT_TIME);
|
|
109
|
+
};
|
|
110
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
111
|
+
return ()=>{
|
|
112
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
113
|
+
clearTimeout(timeout.current);
|
|
114
|
+
clearTimeout(autoCloseTimeout.current);
|
|
115
|
+
movementsTowardsSubmenuCount.current = $d275435c250248f8$var$ALLOWED_INVALID_MOVEMENTS;
|
|
116
|
+
};
|
|
117
|
+
}, [
|
|
118
|
+
isDisabled,
|
|
119
|
+
isOpen,
|
|
120
|
+
menuRef,
|
|
121
|
+
modality,
|
|
122
|
+
setPreventPointerEvents,
|
|
123
|
+
submenuRef
|
|
124
|
+
]);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
export {$d275435c250248f8$export$85ec83e04c95f50a as useSafelyMouseToSubm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AAC,aAAa,YAAY,OAAO,CAAC,MAAM,EAAE,cAAc;YACpF,IAAI,eAAe,KAAK,KAAK,CAAC,aAAa,QAAS,YAAY,OAAO,KAAK,SAAS,CAAE,CAAA,SAAS,UAAS,IAAK,SAAS;YACvH,IAAI,yBAAyB,eAAe,YAAY,eAAe;YAEvE,6BAA6B,OAAO,GAAG,yBACrC,KAAK,GAAG,CAAC,6BAA6B,OAAO,GAAG,GAAG,mDACnD,KAAK,GAAG,CAAC,6BAA6B,OAAO,GAAG,GAAG;YAErD,IAAI,6BAA6B,OAAO,IAAI,iDAC1C,wBAAwB;iBAExB,wBAAwB;YAG1B,kBAAkB,OAAO,GAAG;YAC5B,eAAe,OAAO,GAAG;gBAAC,GAAG;gBAAQ,GAAG;YAAM;YAE9C,qHAAqH;YACrH,IAAI,wBACF,QAAQ,OAAO,GAAG,WAAW;gBAC3B;gBACA,iBAAiB,OAAO,GAAG,WAAW;oBACpC,yDAAyD;oBACzD,sDAAsD;oBACtD,IAAI,SAAS,SAAS,gBAAgB,CAAC,QAAQ;oBAC/C,IAAI,UAAU,KAAK,QAAQ,CAAC,SAC1B,OAAO,aAAa,CAAC,IAAI,aAAa,eAAe;wBAAC,SAAS;wBAAM,YAAY;oBAAI;gBAEzF,GAAG;YACL,GAAG;QAEP;QAEA,OAAO,gBAAgB,CAAC,eAAe;QAEvC,OAAO;YACL,OAAO,mBAAmB,CAAC,eAAe;YAC1C,aAAa,QAAQ,OAAO;YAC5B,aAAa,iBAAiB,OAAO;YACrC,6BAA6B,OAAO,GAAG;QACzC;IAEF,GAAG;QAAC;QAAY;QAAQ;QAAS;QAAU;QAAyB;KAAW;AACjF","sources":["packages/@react-aria/menu/src/useSafelyMouseToSubmenu.ts"],"sourcesContent":["import {RefObject, useEffect, useRef, useState} from 'react';\nimport {useInteractionModality} from '@react-aria/interactions';\nimport {useResizeObserver} from '@react-aria/utils';\n\ninterface SafelyMouseToSubmenuOptions {\n /** Ref for the parent menu. */\n menuRef: RefObject<Element>,\n /** Ref for the submenu. */\n submenuRef: RefObject<Element>,\n /** Whether the submenu is open. */\n isOpen: boolean,\n /** Whether this feature is disabled. */\n isDisabled?: boolean\n}\n\nconst ALLOWED_INVALID_MOVEMENTS = 2;\nconst THROTTLE_TIME = 50;\nconst TIMEOUT_TIME = 1000;\nconst ANGLE_PADDING = Math.PI / 12; // 15°\n\n/**\n * Allows the user to move their pointer to the submenu without it closing when their mouse leaves the trigger element.\n * Prevents pointer events from going to the underlying menu if the user is moving their pointer towards the sub-menu.\n */\nexport function useSafelyMouseToSubmenu(options: SafelyMouseToSubmenuOptions) {\n let {menuRef, submenuRef, isOpen, isDisabled} = options;\n let prevPointerPos = useRef<{x: number, y: number} | undefined>();\n let submenuRect = useRef<DOMRect | undefined>();\n let lastProcessedTime = useRef<number>(0);\n let timeout = useRef<ReturnType<typeof setTimeout> | undefined>();\n let autoCloseTimeout = useRef<ReturnType<typeof setTimeout> | undefined>();\n let submenuSide = useRef<'left' | 'right' | undefined>();\n let movementsTowardsSubmenuCount = useRef<number>(2);\n let [preventPointerEvents, setPreventPointerEvents] = useState(false);\n\n let updateSubmenuRect = () => {\n if (submenuRef.current) {\n submenuRect.current = submenuRef.current.getBoundingClientRect();\n submenuSide.current = undefined;\n }\n };\n useResizeObserver({ref: submenuRef, onResize: updateSubmenuRect});\n\n let reset = () => {\n setPreventPointerEvents(false);\n movementsTowardsSubmenuCount.current = ALLOWED_INVALID_MOVEMENTS;\n prevPointerPos.current = undefined;\n };\n\n let modality = useInteractionModality();\n\n useEffect(() => {\n if (preventPointerEvents && menuRef.current) {\n (menuRef.current as HTMLElement).style.pointerEvents = 'none';\n } else {\n (menuRef.current as HTMLElement).style.pointerEvents = '';\n }\n }, [menuRef, preventPointerEvents]);\n\n useEffect(() => {\n let submenu = submenuRef.current;\n let menu = menuRef.current;\n\n if (isDisabled || !submenu || !isOpen || modality !== 'pointer') {\n reset();\n return;\n }\n submenuRect.current = submenu.getBoundingClientRect();\n\n let onPointerMove = (e: PointerEvent) => {\n if (e.pointerType === 'touch' || e.pointerType === 'pen') {\n return;\n }\n\n let currentTime = Date.now();\n\n // Throttle\n if (currentTime - lastProcessedTime.current < THROTTLE_TIME) {\n return;\n }\n clearTimeout(timeout.current);\n clearTimeout(autoCloseTimeout.current);\n\n let {clientX: mouseX, clientY: mouseY} = e;\n\n if (!prevPointerPos.current) {\n prevPointerPos.current = {x: mouseX, y: mouseY};\n return;\n }\n\n if (!submenuRect.current) {\n return;\n }\n\n if (!submenuSide.current) {\n submenuSide.current = mouseX > submenuRect.current.right ? 'left' : 'right';\n }\n\n // Pointer is outside of parent menu\n if (mouseX < menu.getBoundingClientRect().left || mouseX > menu.getBoundingClientRect().right || mouseY < menu.getBoundingClientRect().top || mouseY > menu.getBoundingClientRect().bottom) {\n reset();\n return;\n }\n\n /* Check if pointer is moving towards submenu.\n Uses the 2-argument arctangent (https://en.wikipedia.org/wiki/Atan2) to calculate:\n - angle between previous pointer and top of submenu\n - angle between previous pointer and bottom of submenu\n - angle between previous pointer and current pointer (delta)\n If the pointer delta angle value is between the top and bottom angle values, we know the pointer is moving towards the submenu.\n */\n let prevMouseX = prevPointerPos.current.x;\n let prevMouseY = prevPointerPos.current.y;\n let toSubmenuX = submenuSide.current === 'right' ? submenuRect.current.left - prevMouseX : prevMouseX - submenuRect.current.right;\n let angleTop = Math.atan2(prevMouseY - submenuRect.current.top, toSubmenuX) + ANGLE_PADDING;\n let angleBottom = Math.atan2(prevMouseY - submenuRect.current.bottom, toSubmenuX) - ANGLE_PADDING;\n let anglePointer = Math.atan2(prevMouseY - mouseY, (submenuSide.current === 'left' ? -(mouseX - prevMouseX) : mouseX - prevMouseX));\n let isMovingTowardsSubmenu = anglePointer < angleTop && anglePointer > angleBottom;\n\n movementsTowardsSubmenuCount.current = isMovingTowardsSubmenu ?\n Math.min(movementsTowardsSubmenuCount.current + 1, ALLOWED_INVALID_MOVEMENTS) :\n Math.max(movementsTowardsSubmenuCount.current - 1, 0);\n\n if (movementsTowardsSubmenuCount.current >= ALLOWED_INVALID_MOVEMENTS) {\n setPreventPointerEvents(true);\n } else {\n setPreventPointerEvents(false);\n }\n\n lastProcessedTime.current = currentTime;\n prevPointerPos.current = {x: mouseX, y: mouseY};\n\n // If the pointer is moving towards the submenu, start a timeout to close if no other movements are made after 500ms.\n if (isMovingTowardsSubmenu) {\n timeout.current = setTimeout(() => {\n reset();\n autoCloseTimeout.current = setTimeout(() => {\n // Fire a pointerover event to trigger the menu to close.\n // Wait until pointer-events:none is no longer applied\n let target = document.elementFromPoint(mouseX, mouseY);\n if (target && menu.contains(target)) {\n target.dispatchEvent(new PointerEvent('pointerover', {bubbles: true, cancelable: true}));\n }\n }, 100);\n }, TIMEOUT_TIME);\n }\n };\n\n window.addEventListener('pointermove', onPointerMove);\n\n return () => {\n window.removeEventListener('pointermove', onPointerMove);\n clearTimeout(timeout.current);\n clearTimeout(autoCloseTimeout.current);\n movementsTowardsSubmenuCount.current = ALLOWED_INVALID_MOVEMENTS;\n };\n\n }, [isDisabled, isOpen, menuRef, modality, setPreventPointerEvents, submenuRef]);\n}\n"],"names":[],"version":3,"file":"useSafelyMouseToSubmenu.module.js.map"}
|