diff --git a/src/cdk/overlay/overlay-ref.ts b/src/cdk/overlay/overlay-ref.ts index f55a36abcfb7..3ff749565ed8 100644 --- a/src/cdk/overlay/overlay-ref.ts +++ b/src/cdk/overlay/overlay-ref.ts @@ -164,12 +164,16 @@ export class OverlayRef implements PortalOutlet, OverlayReference { this._config.scrollStrategy.disable(); } + if (this._config.panelClass) { + this._toggleClasses(this._pane, this._config.panelClass, false); + } + const detachmentResult = this._portalOutlet.detach(); // Only emit after everything is detached. this._detachments.next(); - // Remove this overlay from keyboard dispatcher tracking + // Remove this overlay from keyboard dispatcher tracking. this._keyboardDispatcher.remove(this); return detachmentResult; diff --git a/src/cdk/overlay/overlay.spec.ts b/src/cdk/overlay/overlay.spec.ts index dbcfc6b51d6a..bde5039aaf3e 100644 --- a/src/cdk/overlay/overlay.spec.ts +++ b/src/cdk/overlay/overlay.spec.ts @@ -628,6 +628,26 @@ describe('Overlay', () => { expect(pane.classList).toContain('custom-class-one'); expect(pane.classList).toContain('custom-class-two'); }); + + it('should remove the custom panel class when the overlay is detached', () => { + const config = new OverlayConfig({panelClass: 'custom-panel-class'}); + const overlayRef = overlay.create(config); + + overlayRef.attach(componentPortal); + viewContainerFixture.detectChanges(); + + const pane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement; + expect(pane.classList).toContain('custom-panel-class'); + + overlayRef.detach(); + viewContainerFixture.detectChanges(); + expect(pane.classList).not.toContain('custom-panel-class'); + + overlayRef.attach(componentPortal); + viewContainerFixture.detectChanges(); + expect(pane.classList).toContain('custom-panel-class'); + }); + }); describe('scroll strategy', () => {