Skip to content

fix(overlay): wait for panel to detach before removing panelClass #13199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ 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.
Expand Down Expand Up @@ -426,6 +422,10 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
// Needs a couple of checks for the pane and host, because
// they may have been removed by the time the zone stabilizes.
if (!this._pane || !this._host || this._pane.children.length === 0) {
if (this._pane && this._config.panelClass) {
this._toggleClasses(this._pane, this._config.panelClass, false);
}

if (this._host && this._host.parentElement) {
this._previousHostParent = this._host.parentElement;
this._previousHostParent.removeChild(this._host);
Expand Down
30 changes: 27 additions & 3 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,17 +674,41 @@ describe('Overlay', () => {
viewContainerFixture.detectChanges();

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

overlayRef.detach();
zone.simulateZoneExit();
viewContainerFixture.detectChanges();
expect(pane.classList).not.toContain('custom-panel-class');
expect(pane.classList).not.toContain('custom-panel-class', 'Expected class to be removed');

overlayRef.attach(componentPortal);
viewContainerFixture.detectChanges();
expect(pane.classList).toContain('custom-panel-class');
expect(pane.classList).toContain('custom-panel-class', 'Expected class to be re-added');
});

it('should wait for the overlay to be detached before removing the panelClass', () => {
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', 'Expected class to be added');

overlayRef.detach();
viewContainerFixture.detectChanges();

expect(pane.classList)
.toContain('custom-panel-class', 'Expected class not to be removed immediately');

zone.simulateZoneExit();

expect(pane.classList)
.not.toContain('custom-panel-class', 'Expected class to be removed on stable');
});


});

describe('scroll strategy', () => {
Expand Down