Skip to content
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

fix(overlay): ensure proper stacking order when attaching #3581

Merged
merged 1 commit into from
Mar 27, 2017
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
22 changes: 18 additions & 4 deletions src/lib/core/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ export class OverlayRef implements PortalHost {
* @returns The portal attachment result.
*/
attach(portal: Portal<any>): any {
if (this._state.hasBackdrop) {
this._attachBackdrop();
}

let attachResult = this._portalHost.attach(portal);

// Update the pane element with the given state configuration.
this._updateStackingOrder();
this.updateSize();
this.updateDirection();
this.updatePosition();

// Enable pointer events for the overlay pane element.
this._togglePointerEvents(true);

if (this._state.hasBackdrop) {
this._attachBackdrop();
}

return attachResult;
}

Expand Down Expand Up @@ -153,6 +154,19 @@ export class OverlayRef implements PortalHost {
});
}

/**
* Updates the stacking order of the element, moving it to the top if necessary.
* This is required in cases where one overlay was detached, while another one,
* that should be behind it, was destroyed. The next time both of them are opened,
* the stacking will be wrong, because the detached element's pane will still be
* in its original DOM position.
*/
private _updateStackingOrder() {
if (this._pane.nextSibling) {
this._pane.parentNode.appendChild(this._pane);
}
}

/** Detaches the backdrop (if any) associated with the overlay. */
detachBackdrop(): void {
let backdropToDetach = this._backdropElement;
Expand Down
25 changes: 25 additions & 0 deletions src/lib/core/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ describe('Overlay', () => {
expect(overlayContainerElement.textContent).toBe('');
});

it('should ensure that the most-recently-attached overlay is on top', () => {
let pizzaOverlayRef = overlay.create();
let cakeOverlayRef = overlay.create();

pizzaOverlayRef.attach(componentPortal);
cakeOverlayRef.attach(templatePortal);

expect(pizzaOverlayRef.overlayElement.nextSibling)
.toBeTruthy('Expected pizza to be on the bottom.');
expect(cakeOverlayRef.overlayElement.nextSibling)
.toBeFalsy('Expected cake to be on top.');

pizzaOverlayRef.dispose();
cakeOverlayRef.detach();

pizzaOverlayRef = overlay.create();
pizzaOverlayRef.attach(componentPortal);
cakeOverlayRef.attach(templatePortal);

expect(pizzaOverlayRef.overlayElement.nextSibling)
.toBeTruthy('Expected pizza to still be on the bottom.');
expect(cakeOverlayRef.overlayElement.nextSibling)
.toBeFalsy('Expected cake to still be on top.');
});

it('should set the direction', () => {
const state = new OverlayState();
state.direction = 'rtl';
Expand Down