Skip to content

Commit 6741617

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 c28549d commit 6741617

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -335,38 +335,40 @@ export class OverlayRef implements PortalOutlet {
335335
detachBackdrop(): void {
336336
let backdropToDetach = this._backdropElement;
337337

338-
if (backdropToDetach) {
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-
}
344-
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-
}
351-
};
338+
if (!backdropToDetach) {
339+
return;
340+
}
352341

353-
backdropToDetach.classList.remove('cdk-overlay-backdrop-showing');
342+
let finishDetach = () => {
343+
// It may not be attached to anything in certain cases (e.g. unit tests).
344+
if (backdropToDetach && backdropToDetach.parentNode) {
345+
backdropToDetach.parentNode.removeChild(backdropToDetach);
346+
}
354347

355-
if (this._config.backdropClass) {
356-
backdropToDetach.classList.remove(this._config.backdropClass);
348+
// It is possible that a new portal has been attached to this overlay since we started
349+
// removing the backdrop. If that is the case, only clear the backdrop reference if it
350+
// is still the same instance that we started to remove.
351+
if (this._backdropElement == backdropToDetach) {
352+
this._backdropElement = null;
357353
}
354+
};
358355

359-
backdropToDetach.addEventListener('transitionend', finishDetach);
356+
backdropToDetach.classList.remove('cdk-overlay-backdrop-showing');
360357

361-
// If the backdrop doesn't have a transition, the `transitionend` event won't fire.
362-
// In this case we make it unclickable and we try to remove it after a delay.
363-
backdropToDetach.style.pointerEvents = 'none';
358+
this._ngZone.runOutsideAngular(() => {
359+
backdropToDetach!.addEventListener('transitionend', finishDetach);
360+
});
364361

365-
// Run this outside the Angular zone because there's nothing that Angular cares about.
366-
// If it were to run inside the Angular zone, every test that used Overlay would have to be
367-
// either async or fakeAsync.
368-
this._ngZone.runOutsideAngular(() => setTimeout(finishDetach, 500));
369-
}
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';
365+
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+
this._ngZone.runOutsideAngular(() => {
370+
setTimeout(finishDetach, 500);
371+
});
370372
}
371373
}
372374

0 commit comments

Comments
 (0)