Skip to content

Commit 2ce3232

Browse files
committed
fix(overlay): backdrop exit animation not working
Fixes the transition when closing an overlay with an opaque backdrop appearing broken. The issue comes the fact that when we start animating the backdrop out, we also remove the `backdropClass` which makes the backdrop transparent. We don't need to remove the class, because we'll remove the backdrop from the DOM and clear the reference once the transition is done.
1 parent 8dfce58 commit 2ce3232

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -331,43 +331,45 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
331331
detachBackdrop(): void {
332332
let backdropToDetach = this._backdropElement;
333333

334-
if (backdropToDetach) {
335-
let timeoutId: number;
336-
let finishDetach = () => {
337-
// It may not be attached to anything in certain cases (e.g. unit tests).
338-
if (backdropToDetach && backdropToDetach.parentNode) {
339-
backdropToDetach.parentNode.removeChild(backdropToDetach);
340-
}
341-
342-
// It is possible that a new portal has been attached to this overlay since we started
343-
// removing the backdrop. If that is the case, only clear the backdrop reference if it
344-
// is still the same instance that we started to remove.
345-
if (this._backdropElement == backdropToDetach) {
346-
this._backdropElement = null;
347-
}
334+
if (!backdropToDetach) {
335+
return;
336+
}
348337

349-
clearTimeout(timeoutId);
350-
};
338+
let timeoutId: number;
339+
let finishDetach = () => {
340+
// It may not be attached to anything in certain cases (e.g. unit tests).
341+
if (backdropToDetach && backdropToDetach.parentNode) {
342+
backdropToDetach.parentNode.removeChild(backdropToDetach);
343+
}
351344

352-
backdropToDetach.classList.remove('cdk-overlay-backdrop-showing');
345+
// It is possible that a new portal has been attached to this overlay since we started
346+
// removing the backdrop. If that is the case, only clear the backdrop reference if it
347+
// is still the same instance that we started to remove.
348+
if (this._backdropElement == backdropToDetach) {
349+
this._backdropElement = null;
350+
}
353351

354352
if (this._config.backdropClass) {
355-
this._toggleClasses(backdropToDetach, this._config.backdropClass, false);
353+
this._toggleClasses(backdropToDetach!, this._config.backdropClass, false);
356354
}
357355

358-
this._ngZone.runOutsideAngular(() => {
359-
backdropToDetach!.addEventListener('transitionend', finishDetach);
360-
});
356+
clearTimeout(timeoutId);
357+
};
361358

362-
// If the backdrop doesn't have a transition, the `transitionend` event won't fire.
363-
// In this case we make it unclickable and we try to remove it after a delay.
364-
backdropToDetach.style.pointerEvents = 'none';
359+
backdropToDetach.classList.remove('cdk-overlay-backdrop-showing');
365360

366-
// Run this outside the Angular zone because there's nothing that Angular cares about.
367-
// If it were to run inside the Angular zone, every test that used Overlay would have to be
368-
// either async or fakeAsync.
369-
timeoutId = this._ngZone.runOutsideAngular(() => setTimeout(finishDetach, 500));
370-
}
361+
this._ngZone.runOutsideAngular(() => {
362+
backdropToDetach!.addEventListener('transitionend', finishDetach);
363+
});
364+
365+
// If the backdrop doesn't have a transition, the `transitionend` event won't fire.
366+
// In this case we make it unclickable and we try to remove it after a delay.
367+
backdropToDetach.style.pointerEvents = 'none';
368+
369+
// Run this outside the Angular zone because there's nothing that Angular cares about.
370+
// If it were to run inside the Angular zone, every test that used Overlay would have to be
371+
// either async or fakeAsync.
372+
timeoutId = this._ngZone.runOutsideAngular(() => setTimeout(finishDetach, 500));
371373
}
372374

373375
/** Toggles a single CSS class or an array of classes on an element. */

0 commit comments

Comments
 (0)