@@ -23,6 +23,7 @@ import {of as observableOf, Observable, Subject, defer} from 'rxjs';
2323import { DialogRef } from './dialog-ref' ;
2424import { DialogConfig } from './dialog-config' ;
2525import { Directionality } from '@angular/cdk/bidi' ;
26+ import { _supportsInert } from '@angular/cdk/platform' ;
2627import {
2728 ComponentType ,
2829 Overlay ,
@@ -44,7 +45,7 @@ export class Dialog implements OnDestroy {
4445 private _openDialogsAtThisLevel : DialogRef < any , any > [ ] = [ ] ;
4546 private readonly _afterAllClosedAtThisLevel = new Subject < void > ( ) ;
4647 private readonly _afterOpenedAtThisLevel = new Subject < DialogRef > ( ) ;
47- private _ariaHiddenElements = new Map < Element , string | null > ( ) ;
48+ private _inertElements = new Map < Element , string | null > ( ) ;
4849 private _scrollStrategy : ( ) => ScrollStrategy ;
4950
5051 /** Keeps track of the currently-open dialogs. */
@@ -162,7 +163,7 @@ export class Dialog implements OnDestroy {
162163 ngOnDestroy ( ) {
163164 // Make one pass over all the dialogs that need to be untracked, but should not be closed. We
164165 // want to stop tracking the open dialog even if it hasn't been closed, because the tracking
165- // determines when `aria-hidden ` is removed from elements outside the dialog.
166+ // determines when `inert ` is removed from elements outside the dialog.
166167 reverseForEach ( this . _openDialogsAtThisLevel , dialog => {
167168 // Check for `false` specifically since we want `undefined` to be interpreted as `true`.
168169 if ( dialog . config . closeOnDestroy === false ) {
@@ -350,18 +351,13 @@ export class Dialog implements OnDestroy {
350351 if ( index > - 1 ) {
351352 ( this . openDialogs as DialogRef < R , C > [ ] ) . splice ( index , 1 ) ;
352353
353- // If all the dialogs were closed, remove/restore the `aria-hidden`
354+ // If all the dialogs were closed, remove/restore the inert attribute
354355 // to a the siblings and emit to the `afterAllClosed` stream.
355356 if ( ! this . openDialogs . length ) {
356- this . _ariaHiddenElements . forEach ( ( previousValue , element ) => {
357- if ( previousValue ) {
358- element . setAttribute ( 'aria-hidden' , previousValue ) ;
359- } else {
360- element . removeAttribute ( 'aria-hidden' ) ;
361- }
362- } ) ;
363-
364- this . _ariaHiddenElements . clear ( ) ;
357+ this . _inertElements . forEach ( ( previousValue , element ) =>
358+ toggleInertAttribute ( element , previousValue ) ,
359+ ) ;
360+ this . _inertElements . clear ( ) ;
365361
366362 if ( emitEvent ) {
367363 this . _getAfterAllClosed ( ) . next ( ) ;
@@ -387,8 +383,11 @@ export class Dialog implements OnDestroy {
387383 sibling . nodeName !== 'STYLE' &&
388384 ! sibling . hasAttribute ( 'aria-live' )
389385 ) {
390- this . _ariaHiddenElements . set ( sibling , sibling . getAttribute ( 'aria-hidden' ) ) ;
391- sibling . setAttribute ( 'aria-hidden' , 'true' ) ;
386+ this . _inertElements . set (
387+ sibling ,
388+ sibling . getAttribute ( _supportsInert ( ) ? 'inert' : 'aria-hidden' ) ,
389+ ) ;
390+ toggleInertAttribute ( sibling , 'true' ) ;
392391 }
393392 }
394393 }
@@ -400,6 +399,22 @@ export class Dialog implements OnDestroy {
400399 }
401400}
402401
402+ /** Toggles the `inert`/`aria-hidden` attribute on an element. */
403+ function toggleInertAttribute ( element : Element , value : string | null ) : void {
404+ const supportsInert = _supportsInert ( ) ;
405+
406+ // Note: this logic is somewhat repetitive, but it's written this way so that we pass static
407+ // strings into `setAttribute`. There's some internal security checks that disallow variables
408+ // to be passed due to the potential of XSS attacks.
409+ if ( ! value ) {
410+ element . removeAttribute ( supportsInert ? 'inert' : 'aria-hidden' ) ;
411+ } else if ( supportsInert ) {
412+ element . setAttribute ( 'inert' , value ) ;
413+ } else {
414+ element . setAttribute ( 'aria-hidden' , value ) ;
415+ }
416+ }
417+
403418/**
404419 * Executes a callback against all elements in an array while iterating in reverse.
405420 * Useful if the array is being modified as it is being iterated.
0 commit comments