From 25ef7476f22de56df88766c788fb0f027c0229b2 Mon Sep 17 00:00:00 2001 From: Dirk Luijk Date: Mon, 2 Dec 2019 15:14:47 +0100 Subject: [PATCH] fix(overlay): restore previous host element before attaching --- src/cdk/overlay/overlay-ref.ts | 8 ++++---- src/cdk/overlay/overlay.spec.ts | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/cdk/overlay/overlay-ref.ts b/src/cdk/overlay/overlay-ref.ts index d28b619fd637..d7065bd7f39a 100644 --- a/src/cdk/overlay/overlay-ref.ts +++ b/src/cdk/overlay/overlay-ref.ts @@ -114,15 +114,15 @@ export class OverlayRef implements PortalOutlet, OverlayReference { attach(portal: Portal): any { let attachResult = this._portalOutlet.attach(portal); - if (this._positionStrategy) { - this._positionStrategy.attach(this); - } - // Update the pane element with the given configuration. if (!this._host.parentElement && this._previousHostParent) { this._previousHostParent.appendChild(this._host); } + if (this._positionStrategy) { + this._positionStrategy.attach(this); + } + this._updateStackingOrder(); this._updateElementSize(); this._updateElementDirection(); diff --git a/src/cdk/overlay/overlay.spec.ts b/src/cdk/overlay/overlay.spec.ts index 7e9e3598fa99..f262dc151ff0 100644 --- a/src/cdk/overlay/overlay.spec.ts +++ b/src/cdk/overlay/overlay.spec.ts @@ -444,6 +444,29 @@ describe('Overlay', () => { expect(overlayContainerElement.querySelectorAll('.fake-positioned').length).toBe(1); })); + it('should have the overlay in the DOM in position strategy when reattaching', fakeAsync(() => { + let overlayPresentInDom = false; + + config.positionStrategy = { + attach: (ref: OverlayRef) => overlayPresentInDom = !!ref.hostElement.parentElement, + apply: () => {}, + dispose: () => {} + }; + + const overlayRef = overlay.create(config); + + overlayRef.attach(componentPortal); + expect(overlayPresentInDom).toBeTruthy('Expected host element to be attached to the DOM.'); + + overlayRef.detach(); + zone.simulateZoneExit(); + tick(); + + overlayRef.attach(componentPortal); + + expect(overlayPresentInDom).toBeTruthy('Expected host element to be attached to the DOM.'); + })); + it('should not apply the position if it detaches before the zone stabilizes', fakeAsync(() => { config.positionStrategy = new FakePositionStrategy();