10
10
* governing permissions and limitations under the License.
11
11
*/
12
12
13
- import { getOwnerWindow } from '@react-aria/utils' ;
14
-
15
- const supportsInert = typeof HTMLElement !== 'undefined' && 'inert' in HTMLElement . prototype ;
16
-
17
- interface AriaHideOutsideOptions {
18
- root ?: Element ,
19
- shouldUseInert ?: boolean
20
- }
21
-
22
13
// Keeps a ref count of all hidden elements. Added to when hiding an element, and
23
14
// subtracted from when showing it again. When it reaches zero, aria-hidden is removed.
24
15
let refCountMap = new WeakMap < Element , number > ( ) ;
@@ -38,28 +29,10 @@ let observerStack: Array<ObserverWrapper> = [];
38
29
* @param root - Nothing will be hidden above this element.
39
30
* @returns - A function to restore all hidden elements.
40
31
*/
41
- export function ariaHideOutside ( targets : Element [ ] , options ?: AriaHideOutsideOptions | Element ) {
42
- let windowObj = getOwnerWindow ( targets ?. [ 0 ] ) ;
43
- let opts = options instanceof windowObj . Element ? { root : options } : options ;
44
- let root = opts ?. root ?? document . body ;
45
- let shouldUseInert = opts ?. shouldUseInert && supportsInert ;
32
+ export function ariaHideOutside ( targets : Element [ ] , root = document . body ) {
46
33
let visibleNodes = new Set < Element > ( targets ) ;
47
34
let hiddenNodes = new Set < Element > ( ) ;
48
35
49
- let getHidden = ( element : Element ) => {
50
- return shouldUseInert && element instanceof windowObj . HTMLElement ? element . inert : element . getAttribute ( 'aria-hidden' ) === 'true' ;
51
- } ;
52
-
53
- let setHidden = ( element : Element , hidden : boolean ) => {
54
- if ( shouldUseInert && element instanceof windowObj . HTMLElement ) {
55
- element . inert = hidden ;
56
- } else if ( hidden ) {
57
- element . setAttribute ( 'aria-hidden' , 'true' ) ;
58
- } else {
59
- element . removeAttribute ( 'aria-hidden' ) ;
60
- }
61
- } ;
62
-
63
36
let walk = ( root : Element ) => {
64
37
// Keep live announcer and top layer elements (e.g. toasts) visible.
65
38
for ( let element of root . querySelectorAll ( '[data-live-announcer], [data-react-aria-top-layer]' ) ) {
@@ -114,12 +87,12 @@ export function ariaHideOutside(targets: Element[], options?: AriaHideOutsideOpt
114
87
115
88
// If already aria-hidden, and the ref count is zero, then this element
116
89
// was already hidden and there's nothing for us to do.
117
- if ( getHidden ( node ) && refCount === 0 ) {
90
+ if ( node . getAttribute ( 'aria-hidden' ) === 'true' && refCount === 0 ) {
118
91
return ;
119
92
}
120
93
121
94
if ( refCount === 0 ) {
122
- setHidden ( node , true ) ;
95
+ node . setAttribute ( 'aria-hidden' , ' true' ) ;
123
96
}
124
97
125
98
hiddenNodes . add ( node ) ;
@@ -188,7 +161,7 @@ export function ariaHideOutside(targets: Element[], options?: AriaHideOutsideOpt
188
161
continue ;
189
162
}
190
163
if ( count === 1 ) {
191
- setHidden ( node , false ) ;
164
+ node . removeAttribute ( 'aria-hidden' ) ;
192
165
refCountMap . delete ( node ) ;
193
166
} else {
194
167
refCountMap . set ( node , count - 1 ) ;
0 commit comments