@react-aria/overlays 3.31.1 → 3.32.0
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/import.mjs +12 -12
- package/dist/main.js +28 -28
- package/dist/main.js.map +1 -1
- package/dist/module.js +12 -12
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +19 -0
- package/package.json +15 -21
- package/src/index.ts +19 -22
- package/dist/DismissButton.main.js +0 -51
- package/dist/DismissButton.main.js.map +0 -1
- package/dist/DismissButton.mjs +0 -46
- package/dist/DismissButton.module.js +0 -46
- package/dist/DismissButton.module.js.map +0 -1
- package/dist/Overlay.main.js +0 -73
- package/dist/Overlay.main.js.map +0 -1
- package/dist/Overlay.mjs +0 -63
- package/dist/Overlay.module.js +0 -63
- package/dist/Overlay.module.js.map +0 -1
- package/dist/PortalProvider.main.js +0 -41
- package/dist/PortalProvider.main.js.map +0 -1
- package/dist/PortalProvider.mjs +0 -31
- package/dist/PortalProvider.module.js +0 -31
- package/dist/PortalProvider.module.js.map +0 -1
- package/dist/ar-AE.main.js +0 -6
- package/dist/ar-AE.main.js.map +0 -1
- package/dist/ar-AE.mjs +0 -8
- package/dist/ar-AE.module.js +0 -8
- package/dist/ar-AE.module.js.map +0 -1
- package/dist/ariaHideOutside.main.js +0 -152
- package/dist/ariaHideOutside.main.js.map +0 -1
- package/dist/ariaHideOutside.mjs +0 -146
- package/dist/ariaHideOutside.module.js +0 -146
- package/dist/ariaHideOutside.module.js.map +0 -1
- package/dist/bg-BG.main.js +0 -6
- package/dist/bg-BG.main.js.map +0 -1
- package/dist/bg-BG.mjs +0 -8
- package/dist/bg-BG.module.js +0 -8
- package/dist/bg-BG.module.js.map +0 -1
- package/dist/calculatePosition.main.js +0 -414
- package/dist/calculatePosition.main.js.map +0 -1
- package/dist/calculatePosition.mjs +0 -408
- package/dist/calculatePosition.module.js +0 -408
- package/dist/calculatePosition.module.js.map +0 -1
- package/dist/cs-CZ.main.js +0 -6
- package/dist/cs-CZ.main.js.map +0 -1
- package/dist/cs-CZ.mjs +0 -8
- package/dist/cs-CZ.module.js +0 -8
- package/dist/cs-CZ.module.js.map +0 -1
- package/dist/da-DK.main.js +0 -6
- package/dist/da-DK.main.js.map +0 -1
- package/dist/da-DK.mjs +0 -8
- package/dist/da-DK.module.js +0 -8
- package/dist/da-DK.module.js.map +0 -1
- package/dist/de-DE.main.js +0 -6
- package/dist/de-DE.main.js.map +0 -1
- package/dist/de-DE.mjs +0 -8
- package/dist/de-DE.module.js +0 -8
- package/dist/de-DE.module.js.map +0 -1
- package/dist/el-GR.main.js +0 -6
- package/dist/el-GR.main.js.map +0 -1
- package/dist/el-GR.mjs +0 -8
- package/dist/el-GR.module.js +0 -8
- package/dist/el-GR.module.js.map +0 -1
- package/dist/en-US.main.js +0 -6
- package/dist/en-US.main.js.map +0 -1
- package/dist/en-US.mjs +0 -8
- package/dist/en-US.module.js +0 -8
- package/dist/en-US.module.js.map +0 -1
- package/dist/es-ES.main.js +0 -6
- package/dist/es-ES.main.js.map +0 -1
- package/dist/es-ES.mjs +0 -8
- package/dist/es-ES.module.js +0 -8
- package/dist/es-ES.module.js.map +0 -1
- package/dist/et-EE.main.js +0 -6
- package/dist/et-EE.main.js.map +0 -1
- package/dist/et-EE.mjs +0 -8
- package/dist/et-EE.module.js +0 -8
- package/dist/et-EE.module.js.map +0 -1
- package/dist/fi-FI.main.js +0 -6
- package/dist/fi-FI.main.js.map +0 -1
- package/dist/fi-FI.mjs +0 -8
- package/dist/fi-FI.module.js +0 -8
- package/dist/fi-FI.module.js.map +0 -1
- package/dist/fr-FR.main.js +0 -6
- package/dist/fr-FR.main.js.map +0 -1
- package/dist/fr-FR.mjs +0 -8
- package/dist/fr-FR.module.js +0 -8
- package/dist/fr-FR.module.js.map +0 -1
- package/dist/he-IL.main.js +0 -6
- package/dist/he-IL.main.js.map +0 -1
- package/dist/he-IL.mjs +0 -8
- package/dist/he-IL.module.js +0 -8
- package/dist/he-IL.module.js.map +0 -1
- package/dist/hr-HR.main.js +0 -6
- package/dist/hr-HR.main.js.map +0 -1
- package/dist/hr-HR.mjs +0 -8
- package/dist/hr-HR.module.js +0 -8
- package/dist/hr-HR.module.js.map +0 -1
- package/dist/hu-HU.main.js +0 -6
- package/dist/hu-HU.main.js.map +0 -1
- package/dist/hu-HU.mjs +0 -8
- package/dist/hu-HU.module.js +0 -8
- package/dist/hu-HU.module.js.map +0 -1
- package/dist/intlStrings.main.js +0 -108
- package/dist/intlStrings.main.js.map +0 -1
- package/dist/intlStrings.mjs +0 -110
- package/dist/intlStrings.module.js +0 -110
- package/dist/intlStrings.module.js.map +0 -1
- package/dist/it-IT.main.js +0 -6
- package/dist/it-IT.main.js.map +0 -1
- package/dist/it-IT.mjs +0 -8
- package/dist/it-IT.module.js +0 -8
- package/dist/it-IT.module.js.map +0 -1
- package/dist/ja-JP.main.js +0 -6
- package/dist/ja-JP.main.js.map +0 -1
- package/dist/ja-JP.mjs +0 -8
- package/dist/ja-JP.module.js +0 -8
- package/dist/ja-JP.module.js.map +0 -1
- package/dist/ko-KR.main.js +0 -6
- package/dist/ko-KR.main.js.map +0 -1
- package/dist/ko-KR.mjs +0 -8
- package/dist/ko-KR.module.js +0 -8
- package/dist/ko-KR.module.js.map +0 -1
- package/dist/lt-LT.main.js +0 -6
- package/dist/lt-LT.main.js.map +0 -1
- package/dist/lt-LT.mjs +0 -8
- package/dist/lt-LT.module.js +0 -8
- package/dist/lt-LT.module.js.map +0 -1
- package/dist/lv-LV.main.js +0 -6
- package/dist/lv-LV.main.js.map +0 -1
- package/dist/lv-LV.mjs +0 -8
- package/dist/lv-LV.module.js +0 -8
- package/dist/lv-LV.module.js.map +0 -1
- package/dist/nb-NO.main.js +0 -6
- package/dist/nb-NO.main.js.map +0 -1
- package/dist/nb-NO.mjs +0 -8
- package/dist/nb-NO.module.js +0 -8
- package/dist/nb-NO.module.js.map +0 -1
- package/dist/nl-NL.main.js +0 -6
- package/dist/nl-NL.main.js.map +0 -1
- package/dist/nl-NL.mjs +0 -8
- package/dist/nl-NL.module.js +0 -8
- package/dist/nl-NL.module.js.map +0 -1
- package/dist/pl-PL.main.js +0 -6
- package/dist/pl-PL.main.js.map +0 -1
- package/dist/pl-PL.mjs +0 -8
- package/dist/pl-PL.module.js +0 -8
- package/dist/pl-PL.module.js.map +0 -1
- package/dist/pt-BR.main.js +0 -6
- package/dist/pt-BR.main.js.map +0 -1
- package/dist/pt-BR.mjs +0 -8
- package/dist/pt-BR.module.js +0 -8
- package/dist/pt-BR.module.js.map +0 -1
- package/dist/pt-PT.main.js +0 -6
- package/dist/pt-PT.main.js.map +0 -1
- package/dist/pt-PT.mjs +0 -8
- package/dist/pt-PT.module.js +0 -8
- package/dist/pt-PT.module.js.map +0 -1
- package/dist/ro-RO.main.js +0 -6
- package/dist/ro-RO.main.js.map +0 -1
- package/dist/ro-RO.mjs +0 -8
- package/dist/ro-RO.module.js +0 -8
- package/dist/ro-RO.module.js.map +0 -1
- package/dist/ru-RU.main.js +0 -6
- package/dist/ru-RU.main.js.map +0 -1
- package/dist/ru-RU.mjs +0 -8
- package/dist/ru-RU.module.js +0 -8
- package/dist/ru-RU.module.js.map +0 -1
- package/dist/sk-SK.main.js +0 -6
- package/dist/sk-SK.main.js.map +0 -1
- package/dist/sk-SK.mjs +0 -8
- package/dist/sk-SK.module.js +0 -8
- package/dist/sk-SK.module.js.map +0 -1
- package/dist/sl-SI.main.js +0 -6
- package/dist/sl-SI.main.js.map +0 -1
- package/dist/sl-SI.mjs +0 -8
- package/dist/sl-SI.module.js +0 -8
- package/dist/sl-SI.module.js.map +0 -1
- package/dist/sr-SP.main.js +0 -6
- package/dist/sr-SP.main.js.map +0 -1
- package/dist/sr-SP.mjs +0 -8
- package/dist/sr-SP.module.js +0 -8
- package/dist/sr-SP.module.js.map +0 -1
- package/dist/sv-SE.main.js +0 -6
- package/dist/sv-SE.main.js.map +0 -1
- package/dist/sv-SE.mjs +0 -8
- package/dist/sv-SE.module.js +0 -8
- package/dist/sv-SE.module.js.map +0 -1
- package/dist/tr-TR.main.js +0 -6
- package/dist/tr-TR.main.js.map +0 -1
- package/dist/tr-TR.mjs +0 -8
- package/dist/tr-TR.module.js +0 -8
- package/dist/tr-TR.module.js.map +0 -1
- package/dist/types.d.ts +0 -355
- package/dist/types.d.ts.map +0 -1
- package/dist/uk-UA.main.js +0 -6
- package/dist/uk-UA.main.js.map +0 -1
- package/dist/uk-UA.mjs +0 -8
- package/dist/uk-UA.module.js +0 -8
- package/dist/uk-UA.module.js.map +0 -1
- package/dist/useCloseOnScroll.main.js +0 -52
- package/dist/useCloseOnScroll.main.js.map +0 -1
- package/dist/useCloseOnScroll.mjs +0 -46
- package/dist/useCloseOnScroll.module.js +0 -46
- package/dist/useCloseOnScroll.module.js.map +0 -1
- package/dist/useModal.main.js +0 -118
- package/dist/useModal.main.js.map +0 -1
- package/dist/useModal.mjs +0 -105
- package/dist/useModal.module.js +0 -105
- package/dist/useModal.module.js.map +0 -1
- package/dist/useModalOverlay.main.js +0 -57
- package/dist/useModalOverlay.main.js.map +0 -1
- package/dist/useModalOverlay.mjs +0 -52
- package/dist/useModalOverlay.module.js +0 -52
- package/dist/useModalOverlay.module.js.map +0 -1
- package/dist/useOverlay.main.js +0 -111
- package/dist/useOverlay.main.js.map +0 -1
- package/dist/useOverlay.mjs +0 -106
- package/dist/useOverlay.module.js +0 -106
- package/dist/useOverlay.module.js.map +0 -1
- package/dist/useOverlayPosition.main.js +0 -225
- package/dist/useOverlayPosition.main.js.map +0 -1
- package/dist/useOverlayPosition.mjs +0 -220
- package/dist/useOverlayPosition.module.js +0 -220
- package/dist/useOverlayPosition.module.js.map +0 -1
- package/dist/useOverlayTrigger.main.js +0 -54
- package/dist/useOverlayTrigger.main.js.map +0 -1
- package/dist/useOverlayTrigger.mjs +0 -49
- package/dist/useOverlayTrigger.module.js +0 -49
- package/dist/useOverlayTrigger.module.js.map +0 -1
- package/dist/usePopover.main.js +0 -77
- package/dist/usePopover.main.js.map +0 -1
- package/dist/usePopover.mjs +0 -72
- package/dist/usePopover.module.js +0 -72
- package/dist/usePopover.module.js.map +0 -1
- package/dist/usePreventScroll.main.js +0 -215
- package/dist/usePreventScroll.main.js.map +0 -1
- package/dist/usePreventScroll.mjs +0 -210
- package/dist/usePreventScroll.module.js +0 -210
- package/dist/usePreventScroll.module.js.map +0 -1
- package/dist/zh-CN.main.js +0 -6
- package/dist/zh-CN.main.js.map +0 -1
- package/dist/zh-CN.mjs +0 -8
- package/dist/zh-CN.module.js +0 -8
- package/dist/zh-CN.module.js.map +0 -1
- package/dist/zh-TW.main.js +0 -6
- package/dist/zh-TW.main.js.map +0 -1
- package/dist/zh-TW.mjs +0 -8
- package/dist/zh-TW.module.js +0 -8
- package/dist/zh-TW.module.js.map +0 -1
- package/src/DismissButton.tsx +0 -52
- package/src/Overlay.tsx +0 -95
- package/src/PortalProvider.tsx +0 -41
- package/src/ariaHideOutside.ts +0 -221
- package/src/calculatePosition.ts +0 -680
- package/src/useCloseOnScroll.ts +0 -65
- package/src/useModal.tsx +0 -197
- package/src/useModalOverlay.ts +0 -69
- package/src/useOverlay.ts +0 -169
- package/src/useOverlayPosition.ts +0 -327
- package/src/useOverlayTrigger.ts +0 -72
- package/src/usePopover.ts +0 -138
- package/src/usePreventScroll.ts +0 -273
package/src/useCloseOnScroll.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {nodeContains} from '@react-aria/utils';
|
|
14
|
-
import {RefObject} from '@react-types/shared';
|
|
15
|
-
import {useEffect} from 'react';
|
|
16
|
-
|
|
17
|
-
// This behavior moved from useOverlayTrigger to useOverlayPosition.
|
|
18
|
-
// For backward compatibility, where useOverlayTrigger handled hiding the popover on close,
|
|
19
|
-
// it sets a close function here mapped from the trigger element. This way we can avoid
|
|
20
|
-
// forcing users to pass an onClose function to useOverlayPosition which could be considered
|
|
21
|
-
// a breaking change.
|
|
22
|
-
export const onCloseMap: WeakMap<Element, () => void> = new WeakMap();
|
|
23
|
-
|
|
24
|
-
interface CloseOnScrollOptions {
|
|
25
|
-
triggerRef: RefObject<Element | null>,
|
|
26
|
-
isOpen?: boolean,
|
|
27
|
-
onClose?: (() => void) | null
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** @private */
|
|
31
|
-
export function useCloseOnScroll(opts: CloseOnScrollOptions): void {
|
|
32
|
-
let {triggerRef, isOpen, onClose} = opts;
|
|
33
|
-
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
if (!isOpen || onClose === null) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let onScroll = (e: Event) => {
|
|
40
|
-
// Ignore if scrolling an scrollable region outside the trigger's tree.
|
|
41
|
-
let target = e.target;
|
|
42
|
-
// window is not a Node and doesn't have contain, but window contains everything
|
|
43
|
-
if (!triggerRef.current || ((target instanceof Node) && !nodeContains(target, triggerRef.current))) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Ignore scroll events on any input or textarea as the cursor position can cause it to scroll
|
|
48
|
-
// such as in a combobox. Clicking the dropdown button places focus on the input, and if the
|
|
49
|
-
// text inside the input extends beyond the 'end', then it will scroll so the cursor is visible at the end.
|
|
50
|
-
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let onCloseHandler = onClose || onCloseMap.get(triggerRef.current);
|
|
55
|
-
if (onCloseHandler) {
|
|
56
|
-
onCloseHandler();
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
window.addEventListener('scroll', onScroll, true);
|
|
61
|
-
return () => {
|
|
62
|
-
window.removeEventListener('scroll', onScroll, true);
|
|
63
|
-
};
|
|
64
|
-
}, [isOpen, onClose, triggerRef]);
|
|
65
|
-
}
|
package/src/useModal.tsx
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {DOMAttributes} from '@react-types/shared';
|
|
14
|
-
import React, {AriaAttributes, JSX, ReactNode, useContext, useEffect, useMemo, useState} from 'react';
|
|
15
|
-
import ReactDOM from 'react-dom';
|
|
16
|
-
import {useIsSSR} from '@react-aria/ssr';
|
|
17
|
-
import {useUNSAFE_PortalContext} from './PortalProvider';
|
|
18
|
-
|
|
19
|
-
export interface ModalProviderProps extends DOMAttributes {
|
|
20
|
-
children: ReactNode
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface ModalContext {
|
|
24
|
-
parent: ModalContext | null,
|
|
25
|
-
modalCount: number,
|
|
26
|
-
addModal: () => void,
|
|
27
|
-
removeModal: () => void
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const Context = React.createContext<ModalContext | null>(null);
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Each ModalProvider tracks how many modals are open in its subtree. On mount, the modals
|
|
34
|
-
* trigger `addModal` to increment the count, and trigger `removeModal` on unmount to decrement it.
|
|
35
|
-
* This is done recursively so that all parent providers are incremented and decremented.
|
|
36
|
-
* If the modal count is greater than zero, we add `aria-hidden` to this provider to hide its
|
|
37
|
-
* subtree from screen readers. This is done using React context in order to account for things
|
|
38
|
-
* like portals, which can cause the React tree and the DOM tree to differ significantly in structure.
|
|
39
|
-
*/
|
|
40
|
-
export function ModalProvider(props: ModalProviderProps): JSX.Element {
|
|
41
|
-
let {children} = props;
|
|
42
|
-
let parent = useContext(Context);
|
|
43
|
-
let [modalCount, setModalCount] = useState(0);
|
|
44
|
-
let context = useMemo(() => ({
|
|
45
|
-
parent,
|
|
46
|
-
modalCount,
|
|
47
|
-
addModal() {
|
|
48
|
-
setModalCount(count => count + 1);
|
|
49
|
-
if (parent) {
|
|
50
|
-
parent.addModal();
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
removeModal() {
|
|
54
|
-
setModalCount(count => count - 1);
|
|
55
|
-
if (parent) {
|
|
56
|
-
parent.removeModal();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}), [parent, modalCount]);
|
|
60
|
-
|
|
61
|
-
return (
|
|
62
|
-
<Context.Provider value={context}>
|
|
63
|
-
{children}
|
|
64
|
-
</Context.Provider>
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface ModalProviderAria {
|
|
69
|
-
/**
|
|
70
|
-
* Props to be spread on the container element.
|
|
71
|
-
*/
|
|
72
|
-
modalProviderProps: AriaAttributes
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Used to determine if the tree should be aria-hidden based on how many
|
|
77
|
-
* modals are open.
|
|
78
|
-
*/
|
|
79
|
-
export function useModalProvider(): ModalProviderAria {
|
|
80
|
-
let context = useContext(Context);
|
|
81
|
-
return {
|
|
82
|
-
modalProviderProps: {
|
|
83
|
-
'aria-hidden': context && context.modalCount > 0 ? true : undefined
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Creates a root node that will be aria-hidden if there are other modals open.
|
|
90
|
-
*/
|
|
91
|
-
function OverlayContainerDOM(props: ModalProviderProps) {
|
|
92
|
-
let {modalProviderProps} = useModalProvider();
|
|
93
|
-
return <div data-overlay-container {...props} {...modalProviderProps} />;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* An OverlayProvider acts as a container for the top-level application.
|
|
98
|
-
* Any application that uses modal dialogs or other overlays should
|
|
99
|
-
* be wrapped in a `<OverlayProvider>`. This is used to ensure that
|
|
100
|
-
* the main content of the application is hidden from screen readers
|
|
101
|
-
* if a modal or other overlay is opened. Only the top-most modal or
|
|
102
|
-
* overlay should be accessible at once.
|
|
103
|
-
*/
|
|
104
|
-
export function OverlayProvider(props: ModalProviderProps): JSX.Element {
|
|
105
|
-
return (
|
|
106
|
-
<ModalProvider>
|
|
107
|
-
<OverlayContainerDOM {...props} />
|
|
108
|
-
</ModalProvider>
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface OverlayContainerProps extends ModalProviderProps {
|
|
113
|
-
/**
|
|
114
|
-
* The container element in which the overlay portal will be placed.
|
|
115
|
-
* @default document.body
|
|
116
|
-
* @deprecated - Use a parent UNSAFE_PortalProvider to set your portal container instead.
|
|
117
|
-
*/
|
|
118
|
-
portalContainer?: Element
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* A container for overlays like modals and popovers. Renders the overlay
|
|
123
|
-
* into a Portal which is placed at the end of the document body.
|
|
124
|
-
* Also ensures that the overlay is hidden from screen readers if a
|
|
125
|
-
* nested modal is opened. Only the top-most modal or overlay should
|
|
126
|
-
* be accessible at once.
|
|
127
|
-
*/
|
|
128
|
-
export function OverlayContainer(props: OverlayContainerProps): React.ReactPortal | null {
|
|
129
|
-
let isSSR = useIsSSR();
|
|
130
|
-
let {portalContainer = isSSR ? null : document.body, ...rest} = props;
|
|
131
|
-
let {getContainer} = useUNSAFE_PortalContext();
|
|
132
|
-
if (!props.portalContainer && getContainer) {
|
|
133
|
-
portalContainer = getContainer();
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
React.useEffect(() => {
|
|
137
|
-
if (portalContainer?.closest('[data-overlay-container]')) {
|
|
138
|
-
throw new Error('An OverlayContainer must not be inside another container. Please change the portalContainer prop.');
|
|
139
|
-
}
|
|
140
|
-
}, [portalContainer]);
|
|
141
|
-
|
|
142
|
-
if (!portalContainer) {
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
let contents = <OverlayProvider {...rest} />;
|
|
147
|
-
return ReactDOM.createPortal(contents, portalContainer);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
interface ModalAriaProps extends DOMAttributes {
|
|
151
|
-
/** Data attribute marks the dom node as a modal for the aria-modal-polyfill. */
|
|
152
|
-
'data-ismodal': boolean
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export interface AriaModalOptions {
|
|
156
|
-
isDisabled?: boolean
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export interface ModalAria {
|
|
160
|
-
/** Props for the modal content element. */
|
|
161
|
-
modalProps: ModalAriaProps
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Hides content outside the current `<OverlayContainer>` from screen readers
|
|
166
|
-
* on mount and restores it on unmount. Typically used by modal dialogs and
|
|
167
|
-
* other types of overlays to ensure that only the top-most modal is
|
|
168
|
-
* accessible at once.
|
|
169
|
-
*/
|
|
170
|
-
export function useModal(options?: AriaModalOptions): ModalAria {
|
|
171
|
-
// Add aria-hidden to all parent providers on mount, and restore on unmount.
|
|
172
|
-
let context = useContext(Context);
|
|
173
|
-
if (!context) {
|
|
174
|
-
throw new Error('Modal is not contained within a provider');
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
useEffect(() => {
|
|
178
|
-
if (options?.isDisabled || !context || !context.parent) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// The immediate context is from the provider containing this modal, so we only
|
|
183
|
-
// want to trigger aria-hidden on its parents not on the modal provider itself.
|
|
184
|
-
context.parent.addModal();
|
|
185
|
-
return () => {
|
|
186
|
-
if (context && context.parent) {
|
|
187
|
-
context.parent.removeModal();
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
}, [context, context.parent, options?.isDisabled]);
|
|
191
|
-
|
|
192
|
-
return {
|
|
193
|
-
modalProps: {
|
|
194
|
-
'data-ismodal': !options?.isDisabled
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
}
|
package/src/useModalOverlay.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2022 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 {ariaHideOutside} from './ariaHideOutside';
|
|
14
|
-
import {AriaOverlayProps, useOverlay} from './useOverlay';
|
|
15
|
-
import {DOMAttributes, RefObject} from '@react-types/shared';
|
|
16
|
-
import {mergeProps} from '@react-aria/utils';
|
|
17
|
-
import {OverlayTriggerState} from '@react-stately/overlays';
|
|
18
|
-
import {useEffect} from 'react';
|
|
19
|
-
import {useOverlayFocusContain} from './Overlay';
|
|
20
|
-
import {usePreventScroll} from './usePreventScroll';
|
|
21
|
-
|
|
22
|
-
export interface AriaModalOverlayProps extends Pick<AriaOverlayProps, 'shouldCloseOnInteractOutside'> {
|
|
23
|
-
/**
|
|
24
|
-
* Whether to close the modal when the user interacts outside it.
|
|
25
|
-
* @default false
|
|
26
|
-
*/
|
|
27
|
-
isDismissable?: boolean,
|
|
28
|
-
/**
|
|
29
|
-
* Whether pressing the escape key to close the modal should be disabled.
|
|
30
|
-
* @default false
|
|
31
|
-
*/
|
|
32
|
-
isKeyboardDismissDisabled?: boolean
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface ModalOverlayAria {
|
|
36
|
-
/** Props for the modal element. */
|
|
37
|
-
modalProps: DOMAttributes,
|
|
38
|
-
/** Props for the underlay element. */
|
|
39
|
-
underlayProps: DOMAttributes
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Provides the behavior and accessibility implementation for a modal component.
|
|
44
|
-
* A modal is an overlay element which blocks interaction with elements outside it.
|
|
45
|
-
*/
|
|
46
|
-
export function useModalOverlay(props: AriaModalOverlayProps, state: OverlayTriggerState, ref: RefObject<HTMLElement | null>): ModalOverlayAria {
|
|
47
|
-
let {overlayProps, underlayProps} = useOverlay({
|
|
48
|
-
...props,
|
|
49
|
-
isOpen: state.isOpen,
|
|
50
|
-
onClose: state.close
|
|
51
|
-
}, ref);
|
|
52
|
-
|
|
53
|
-
usePreventScroll({
|
|
54
|
-
isDisabled: !state.isOpen
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
useOverlayFocusContain();
|
|
58
|
-
|
|
59
|
-
useEffect(() => {
|
|
60
|
-
if (state.isOpen && ref.current) {
|
|
61
|
-
return ariaHideOutside([ref.current], {shouldUseInert: true});
|
|
62
|
-
}
|
|
63
|
-
}, [state.isOpen, ref]);
|
|
64
|
-
|
|
65
|
-
return {
|
|
66
|
-
modalProps: mergeProps(overlayProps),
|
|
67
|
-
underlayProps
|
|
68
|
-
};
|
|
69
|
-
}
|
package/src/useOverlay.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 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 {DOMAttributes, RefObject} from '@react-types/shared';
|
|
14
|
-
import {isElementInChildOfActiveScope} from '@react-aria/focus';
|
|
15
|
-
import {useEffect, useRef} from 'react';
|
|
16
|
-
import {useFocusWithin, useInteractOutside} from '@react-aria/interactions';
|
|
17
|
-
|
|
18
|
-
export interface AriaOverlayProps {
|
|
19
|
-
/** Whether the overlay is currently open. */
|
|
20
|
-
isOpen?: boolean,
|
|
21
|
-
|
|
22
|
-
/** Handler that is called when the overlay should close. */
|
|
23
|
-
onClose?: () => void,
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Whether to close the overlay when the user interacts outside it.
|
|
27
|
-
* @default false
|
|
28
|
-
*/
|
|
29
|
-
isDismissable?: boolean,
|
|
30
|
-
|
|
31
|
-
/** Whether the overlay should close when focus is lost or moves outside it. */
|
|
32
|
-
shouldCloseOnBlur?: boolean,
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Whether pressing the escape key to close the overlay should be disabled.
|
|
36
|
-
* @default false
|
|
37
|
-
*/
|
|
38
|
-
isKeyboardDismissDisabled?: boolean,
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* When user interacts with the argument element outside of the overlay ref,
|
|
42
|
-
* return true if onClose should be called. This gives you a chance to filter
|
|
43
|
-
* out interaction with elements that should not dismiss the overlay.
|
|
44
|
-
* By default, onClose will always be called on interaction outside the overlay ref.
|
|
45
|
-
*/
|
|
46
|
-
shouldCloseOnInteractOutside?: (element: Element) => boolean
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface OverlayAria {
|
|
50
|
-
/** Props to apply to the overlay container element. */
|
|
51
|
-
overlayProps: DOMAttributes,
|
|
52
|
-
/** Props to apply to the underlay element, if any. */
|
|
53
|
-
underlayProps: DOMAttributes
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const visibleOverlays: RefObject<Element | null>[] = [];
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Provides the behavior for overlays such as dialogs, popovers, and menus.
|
|
60
|
-
* Hides the overlay when the user interacts outside it, when the Escape key is pressed,
|
|
61
|
-
* or optionally, on blur. Only the top-most overlay will close at once.
|
|
62
|
-
*/
|
|
63
|
-
export function useOverlay(props: AriaOverlayProps, ref: RefObject<Element | null>): OverlayAria {
|
|
64
|
-
let {
|
|
65
|
-
onClose,
|
|
66
|
-
shouldCloseOnBlur,
|
|
67
|
-
isOpen,
|
|
68
|
-
isDismissable = false,
|
|
69
|
-
isKeyboardDismissDisabled = false,
|
|
70
|
-
shouldCloseOnInteractOutside
|
|
71
|
-
} = props;
|
|
72
|
-
|
|
73
|
-
let lastVisibleOverlay = useRef<RefObject<Element | null>>(undefined);
|
|
74
|
-
|
|
75
|
-
// Add the overlay ref to the stack of visible overlays on mount, and remove on unmount.
|
|
76
|
-
useEffect(() => {
|
|
77
|
-
if (isOpen && !visibleOverlays.includes(ref)) {
|
|
78
|
-
visibleOverlays.push(ref);
|
|
79
|
-
return () => {
|
|
80
|
-
let index = visibleOverlays.indexOf(ref);
|
|
81
|
-
if (index >= 0) {
|
|
82
|
-
visibleOverlays.splice(index, 1);
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
}, [isOpen, ref]);
|
|
87
|
-
|
|
88
|
-
// Only hide the overlay when it is the topmost visible overlay in the stack
|
|
89
|
-
let onHide = () => {
|
|
90
|
-
if (visibleOverlays[visibleOverlays.length - 1] === ref && onClose) {
|
|
91
|
-
onClose();
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
let onInteractOutsideStart = (e: PointerEvent) => {
|
|
96
|
-
const topMostOverlay = visibleOverlays[visibleOverlays.length - 1];
|
|
97
|
-
lastVisibleOverlay.current = topMostOverlay;
|
|
98
|
-
if (!shouldCloseOnInteractOutside || shouldCloseOnInteractOutside(e.target as Element)) {
|
|
99
|
-
if (topMostOverlay === ref) {
|
|
100
|
-
e.stopPropagation();
|
|
101
|
-
e.preventDefault();
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
let onInteractOutside = (e: PointerEvent) => {
|
|
107
|
-
if (!shouldCloseOnInteractOutside || shouldCloseOnInteractOutside(e.target as Element)) {
|
|
108
|
-
if (visibleOverlays[visibleOverlays.length - 1] === ref) {
|
|
109
|
-
e.stopPropagation();
|
|
110
|
-
e.preventDefault();
|
|
111
|
-
}
|
|
112
|
-
if (lastVisibleOverlay.current === ref) {
|
|
113
|
-
onHide();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
lastVisibleOverlay.current = undefined;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
// Handle the escape key
|
|
120
|
-
let onKeyDown = (e) => {
|
|
121
|
-
if (e.key === 'Escape' && !isKeyboardDismissDisabled && !e.nativeEvent.isComposing) {
|
|
122
|
-
e.stopPropagation();
|
|
123
|
-
e.preventDefault();
|
|
124
|
-
onHide();
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
// Handle clicking outside the overlay to close it
|
|
129
|
-
useInteractOutside({ref, onInteractOutside: isDismissable && isOpen ? onInteractOutside : undefined, onInteractOutsideStart});
|
|
130
|
-
|
|
131
|
-
let {focusWithinProps} = useFocusWithin({
|
|
132
|
-
isDisabled: !shouldCloseOnBlur,
|
|
133
|
-
onBlurWithin: (e) => {
|
|
134
|
-
// Do not close if relatedTarget is null, which means focus is lost to the body.
|
|
135
|
-
// That can happen when switching tabs, or due to a VoiceOver/Chrome bug with Control+Option+Arrow navigation.
|
|
136
|
-
// Clicking on the body to close the overlay should already be handled by useInteractOutside.
|
|
137
|
-
// https://github.com/adobe/react-spectrum/issues/4130
|
|
138
|
-
// https://github.com/adobe/react-spectrum/issues/4922
|
|
139
|
-
//
|
|
140
|
-
// If focus is moving into a child focus scope (e.g. menu inside a dialog),
|
|
141
|
-
// do not close the outer overlay. At this point, the active scope should
|
|
142
|
-
// still be the outer overlay, since blur events run before focus.
|
|
143
|
-
if (!e.relatedTarget || isElementInChildOfActiveScope(e.relatedTarget)) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (!shouldCloseOnInteractOutside || shouldCloseOnInteractOutside(e.relatedTarget as Element)) {
|
|
148
|
-
onClose?.();
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
let onPointerDownUnderlay = e => {
|
|
154
|
-
// fixes a firefox issue that starts text selection https://bugzilla.mozilla.org/show_bug.cgi?id=1675846
|
|
155
|
-
if (e.target === e.currentTarget) {
|
|
156
|
-
e.preventDefault();
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
return {
|
|
161
|
-
overlayProps: {
|
|
162
|
-
onKeyDown,
|
|
163
|
-
...focusWithinProps
|
|
164
|
-
},
|
|
165
|
-
underlayProps: {
|
|
166
|
-
onPointerDown: onPointerDownUnderlay
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
}
|