@@ -14,6 +14,7 @@ import {
1414 EmbeddedViewRef ,
1515 EnvironmentInjector ,
1616 NgZone ,
17+ Renderer2 ,
1718 afterNextRender ,
1819 afterRender ,
1920 untracked ,
@@ -46,10 +47,8 @@ export class OverlayRef implements PortalOutlet {
4647 private _positionStrategy : PositionStrategy | undefined ;
4748 private _scrollStrategy : ScrollStrategy | undefined ;
4849 private _locationChanges : SubscriptionLike = Subscription . EMPTY ;
49- private _backdropClickHandler = ( event : MouseEvent ) => this . _backdropClick . next ( event ) ;
50- private _backdropTransitionendHandler = ( event : TransitionEvent ) => {
51- this . _disposeBackdrop ( event . target as HTMLElement | null ) ;
52- } ;
50+ private _cleanupBackdropClick : ( ( ) => void ) | undefined ;
51+ private _cleanupBackdropTransitionEnd : ( ( ) => void ) | undefined ;
5352
5453 /**
5554 * Reference to the parent of the `_host` at the time it was detached. Used to restore
@@ -82,6 +81,7 @@ export class OverlayRef implements PortalOutlet {
8281 private _outsideClickDispatcher : OverlayOutsideClickDispatcher ,
8382 private _animationsDisabled = false ,
8483 private _injector : EnvironmentInjector ,
84+ private _renderer : Renderer2 ,
8585 ) {
8686 if ( _config . scrollStrategy ) {
8787 this . _scrollStrategy = _config . scrollStrategy ;
@@ -449,7 +449,12 @@ export class OverlayRef implements PortalOutlet {
449449
450450 // Forward backdrop clicks such that the consumer of the overlay can perform whatever
451451 // action desired when such a click occurs (usually closing the overlay).
452- this . _backdropElement . addEventListener ( 'click' , this . _backdropClickHandler ) ;
452+ this . _cleanupBackdropClick ?.( ) ;
453+ this . _cleanupBackdropClick = this . _renderer . listen (
454+ this . _backdropElement ,
455+ 'click' ,
456+ ( event : MouseEvent ) => this . _backdropClick . next ( event ) ,
457+ ) ;
453458
454459 // Add class to fade-in the backdrop after one frame.
455460 if ( ! this . _animationsDisabled && typeof requestAnimationFrame !== 'undefined' ) {
@@ -494,7 +499,14 @@ export class OverlayRef implements PortalOutlet {
494499 backdropToDetach . classList . remove ( 'cdk-overlay-backdrop-showing' ) ;
495500
496501 this . _ngZone . runOutsideAngular ( ( ) => {
497- backdropToDetach ! . addEventListener ( 'transitionend' , this . _backdropTransitionendHandler ) ;
502+ this . _cleanupBackdropTransitionEnd ?.( ) ;
503+ this . _cleanupBackdropTransitionEnd = this . _renderer . listen (
504+ backdropToDetach ,
505+ 'transitionend' ,
506+ ( event : TransitionEvent ) => {
507+ this . _disposeBackdrop ( event . target as HTMLElement | null ) ;
508+ } ,
509+ ) ;
498510 } ) ;
499511
500512 // If the backdrop doesn't have a transition, the `transitionend` event won't fire.
@@ -565,9 +577,10 @@ export class OverlayRef implements PortalOutlet {
565577
566578 /** Removes a backdrop element from the DOM. */
567579 private _disposeBackdrop ( backdrop : HTMLElement | null ) {
580+ this . _cleanupBackdropClick ?.( ) ;
581+ this . _cleanupBackdropClick ?.( ) ;
582+
568583 if ( backdrop ) {
569- backdrop . removeEventListener ( 'click' , this . _backdropClickHandler ) ;
570- backdrop . removeEventListener ( 'transitionend' , this . _backdropTransitionendHandler ) ;
571584 backdrop . remove ( ) ;
572585
573586 // It is possible that a new portal has been attached to this overlay since we started
0 commit comments