Skip to content

Commit c7ad145

Browse files
crisbetojelbourn
authored andcommitted
fix(overlay): attempting to position overlay if it was detached immediately after being attached (#9507)
Adds a check that ensures that the `OverlayRef` doesn't attempt to position itself if it was detached before the zone managed to stabilize. This manifested itself as an issue in the select where it could end up throwing an error, because the relevant nodes are no longer in the DOM. Fixes #9406.
1 parent 403ebbd commit c7ad145

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ export class OverlayRef implements PortalOutlet {
8383
// before attempting to position it, as the position may depend on the size of the rendered
8484
// content.
8585
this._ngZone.onStable.asObservable().pipe(take(1)).subscribe(() => {
86-
this.updatePosition();
86+
// The overlay could've been detached before the zone has stabilized.
87+
if (this.hasAttached()) {
88+
this.updatePosition();
89+
}
8790
});
8891

8992
// Enable pointer events for the overlay pane element.

src/cdk/overlay/overlay.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,22 @@ describe('Overlay', () => {
265265

266266
expect(overlayContainerElement.querySelectorAll('.fake-positioned').length).toBe(1);
267267
}));
268+
269+
it('should not apply the position if it detaches before the zone stabilizes', fakeAsync(() => {
270+
config.positionStrategy = new FakePositionStrategy();
271+
272+
const overlayRef = overlay.create(config);
273+
274+
spyOn(config.positionStrategy, 'apply');
275+
276+
overlayRef.attach(componentPortal);
277+
overlayRef.detach();
278+
viewContainerFixture.detectChanges();
279+
tick();
280+
281+
expect(config.positionStrategy.apply).not.toHaveBeenCalled();
282+
}));
283+
268284
});
269285

270286
describe('size', () => {

0 commit comments

Comments
 (0)