@@ -44,6 +44,8 @@ export class OverlayRef implements PortalOutlet {
44
44
private _scrollStrategy : ScrollStrategy | undefined ;
45
45
private _locationChanges : SubscriptionLike = Subscription . EMPTY ;
46
46
private _backdropRef : BackdropRef | null = null ;
47
+ private _detachContentMutationObserver : MutationObserver | undefined ;
48
+ private _detachContentAfterRenderRef : AfterRenderRef | undefined ;
47
49
48
50
/**
49
51
* Reference to the parent of the `_host` at the time it was detached. Used to restore
@@ -163,7 +165,7 @@ export class OverlayRef implements PortalOutlet {
163
165
164
166
// Only emit the `attachments` event once all other setup is done.
165
167
this . _attachments . next ( ) ;
166
- this . _detachWhenEmptyMutationObserver ?. disconnect ( ) ;
168
+ this . _completeDetachContent ( ) ;
167
169
168
170
// Track this overlay by the keyboard dispatcher
169
171
this . _keyboardDispatcher . add ( this ) ;
@@ -224,7 +226,7 @@ export class OverlayRef implements PortalOutlet {
224
226
225
227
// Only emit after everything is detached.
226
228
this . _detachments . next ( ) ;
227
- this . _detachWhenEmptyMutationObserver ?. disconnect ( ) ;
229
+ this . _completeDetachContent ( ) ;
228
230
229
231
// Remove this overlay from keyboard dispatcher tracking.
230
232
this . _keyboardDispatcher . remove ( this ) ;
@@ -264,7 +266,7 @@ export class OverlayRef implements PortalOutlet {
264
266
}
265
267
266
268
this . _detachments . complete ( ) ;
267
- this . _detachWhenEmptyMutationObserver ?. disconnect ( ) ;
269
+ this . _completeDetachContent ( ) ;
268
270
}
269
271
270
272
/** Whether the overlay has attached content. */
@@ -470,7 +472,20 @@ export class OverlayRef implements PortalOutlet {
470
472
}
471
473
}
472
474
473
- private _detachWhenEmptyMutationObserver : MutationObserver | null = null ;
475
+ /** Detaches the overlay once the content finishes animating and is removed from the DOM. */
476
+ private _detachContentWhenEmpty ( ) {
477
+ // Attempt to detach on the next render.
478
+ this . _detachContentAfterRenderRef = afterNextRender ( ( ) => this . _detachContent ( ) , {
479
+ injector : this . _injector ,
480
+ } ) ;
481
+ // Otherwise wait until the content finishes animating out and detach.
482
+ if ( globalThis . MutationObserver && this . _pane ) {
483
+ this . _detachContentMutationObserver ||= new globalThis . MutationObserver ( ( ) => {
484
+ this . _detachContent ( ) ;
485
+ } ) ;
486
+ this . _detachContentMutationObserver . observe ( this . _pane , { childList : true } ) ;
487
+ }
488
+ }
474
489
475
490
private _detachContent ( ) {
476
491
// Needs a couple of checks for the pane and host, because
@@ -485,19 +500,14 @@ export class OverlayRef implements PortalOutlet {
485
500
this . _host . remove ( ) ;
486
501
}
487
502
488
- this . _detachWhenEmptyMutationObserver ?. disconnect ( ) ;
503
+ this . _completeDetachContent ( ) ;
489
504
}
490
505
}
491
506
492
- /** Detaches the overlay content next time the zone stabilizes. */
493
- private _detachContentWhenEmpty ( ) {
494
- if ( globalThis . MutationObserver && this . _pane ) {
495
- this . _detachWhenEmptyMutationObserver ||= new globalThis . MutationObserver ( ( ) => {
496
- this . _detachContent ( ) ;
497
- } ) ;
498
- this . _detachWhenEmptyMutationObserver . observe ( this . _pane , { childList : true } ) ;
499
- }
500
- this . _detachContent ( ) ;
507
+ private _completeDetachContent ( ) {
508
+ this . _detachContentAfterRenderRef ?. destroy ( ) ;
509
+ this . _detachContentAfterRenderRef = undefined ;
510
+ this . _detachContentMutationObserver ?. disconnect ( ) ;
501
511
}
502
512
503
513
/** Disposes of a scroll strategy. */
0 commit comments