Skip to content

Commit ee96e05

Browse files
dirkluijkjelbourn
authored andcommitted
fix(overlay): restore previous host element before attaching (#17855)
1 parent 4667cd4 commit ee96e05

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
114114
attach(portal: Portal<any>): any {
115115
let attachResult = this._portalOutlet.attach(portal);
116116

117-
if (this._positionStrategy) {
118-
this._positionStrategy.attach(this);
119-
}
120-
121117
// Update the pane element with the given configuration.
122118
if (!this._host.parentElement && this._previousHostParent) {
123119
this._previousHostParent.appendChild(this._host);
124120
}
125121

122+
if (this._positionStrategy) {
123+
this._positionStrategy.attach(this);
124+
}
125+
126126
this._updateStackingOrder();
127127
this._updateElementSize();
128128
this._updateElementDirection();

src/cdk/overlay/overlay.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,29 @@ describe('Overlay', () => {
444444
expect(overlayContainerElement.querySelectorAll('.fake-positioned').length).toBe(1);
445445
}));
446446

447+
it('should have the overlay in the DOM in position strategy when reattaching', fakeAsync(() => {
448+
let overlayPresentInDom = false;
449+
450+
config.positionStrategy = {
451+
attach: (ref: OverlayRef) => overlayPresentInDom = !!ref.hostElement.parentElement,
452+
apply: () => {},
453+
dispose: () => {}
454+
};
455+
456+
const overlayRef = overlay.create(config);
457+
458+
overlayRef.attach(componentPortal);
459+
expect(overlayPresentInDom).toBeTruthy('Expected host element to be attached to the DOM.');
460+
461+
overlayRef.detach();
462+
zone.simulateZoneExit();
463+
tick();
464+
465+
overlayRef.attach(componentPortal);
466+
467+
expect(overlayPresentInDom).toBeTruthy('Expected host element to be attached to the DOM.');
468+
}));
469+
447470
it('should not apply the position if it detaches before the zone stabilizes', fakeAsync(() => {
448471
config.positionStrategy = new FakePositionStrategy();
449472

0 commit comments

Comments
 (0)