1010 * governing permissions and limitations under the License.
1111 */
1212
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-
2213// Keeps a ref count of all hidden elements. Added to when hiding an element, and
2314// subtracted from when showing it again. When it reaches zero, aria-hidden is removed.
2415let refCountMap = new WeakMap < Element , number > ( ) ;
@@ -38,28 +29,10 @@ let observerStack: Array<ObserverWrapper> = [];
3829 * @param root - Nothing will be hidden above this element.
3930 * @returns - A function to restore all hidden elements.
4031 */
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 ) {
4633 let visibleNodes = new Set < Element > ( targets ) ;
4734 let hiddenNodes = new Set < Element > ( ) ;
4835
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-
6336 let walk = ( root : Element ) => {
6437 // Keep live announcer and top layer elements (e.g. toasts) visible.
6538 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
11487
11588 // If already aria-hidden, and the ref count is zero, then this element
11689 // 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 ) {
11891 return ;
11992 }
12093
12194 if ( refCount === 0 ) {
122- setHidden ( node , true ) ;
95+ node . setAttribute ( 'aria-hidden' , ' true' ) ;
12396 }
12497
12598 hiddenNodes . add ( node ) ;
@@ -188,7 +161,7 @@ export function ariaHideOutside(targets: Element[], options?: AriaHideOutsideOpt
188161 continue ;
189162 }
190163 if ( count === 1 ) {
191- setHidden ( node , false ) ;
164+ node . removeAttribute ( 'aria-hidden' ) ;
192165 refCountMap . delete ( node ) ;
193166 } else {
194167 refCountMap . set ( node , count - 1 ) ;
0 commit comments