Skip to content

Commit 238aef0

Browse files
crisbetovivian-hu-zz
authored andcommitted
fix(overlay): wait for panel to detach before removing panelClass (#13199)
Currently we remove the `panelClass` as soon as `detach` is called, however the pane may still be animating. These changes wait for the panel to be done animating and to be removed from the DOM, before removing the class. Fixes #13189.
1 parent d6ba25f commit 238aef0

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
189189
this._config.scrollStrategy.disable();
190190
}
191191

192-
if (this._config.panelClass) {
193-
this._toggleClasses(this._pane, this._config.panelClass, false);
194-
}
195-
196192
const detachmentResult = this._portalOutlet.detach();
197193

198194
// Only emit after everything is detached.
@@ -463,6 +459,10 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
463459
// Needs a couple of checks for the pane and host, because
464460
// they may have been removed by the time the zone stabilizes.
465461
if (!this._pane || !this._host || this._pane.children.length === 0) {
462+
if (this._pane && this._config.panelClass) {
463+
this._toggleClasses(this._pane, this._config.panelClass, false);
464+
}
465+
466466
if (this._host && this._host.parentElement) {
467467
this._previousHostParent = this._host.parentElement;
468468
this._previousHostParent.removeChild(this._host);

src/cdk/overlay/overlay.spec.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,41 @@ describe('Overlay', () => {
758758
viewContainerFixture.detectChanges();
759759

760760
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
761-
expect(pane.classList).toContain('custom-panel-class');
761+
expect(pane.classList).toContain('custom-panel-class', 'Expected class to be added');
762762

763763
overlayRef.detach();
764+
zone.simulateZoneExit();
764765
viewContainerFixture.detectChanges();
765-
expect(pane.classList).not.toContain('custom-panel-class');
766+
expect(pane.classList).not.toContain('custom-panel-class', 'Expected class to be removed');
766767

767768
overlayRef.attach(componentPortal);
768769
viewContainerFixture.detectChanges();
769-
expect(pane.classList).toContain('custom-panel-class');
770+
expect(pane.classList).toContain('custom-panel-class', 'Expected class to be re-added');
771+
});
772+
773+
it('should wait for the overlay to be detached before removing the panelClass', () => {
774+
const config = new OverlayConfig({panelClass: 'custom-panel-class'});
775+
const overlayRef = overlay.create(config);
776+
777+
overlayRef.attach(componentPortal);
778+
viewContainerFixture.detectChanges();
779+
780+
const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
781+
expect(pane.classList).toContain('custom-panel-class', 'Expected class to be added');
782+
783+
overlayRef.detach();
784+
viewContainerFixture.detectChanges();
785+
786+
expect(pane.classList)
787+
.toContain('custom-panel-class', 'Expected class not to be removed immediately');
788+
789+
zone.simulateZoneExit();
790+
791+
expect(pane.classList)
792+
.not.toContain('custom-panel-class', 'Expected class to be removed on stable');
770793
});
771794

795+
772796
});
773797

774798
describe('scroll strategy', () => {

0 commit comments

Comments
 (0)