Skip to content

fix(overlay): wait until after change detection to position overlays #6527

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 11 commits into from
Oct 10, 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
9 changes: 8 additions & 1 deletion src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {PortalHost, Portal} from '@angular/cdk/portal';
import {OverlayConfig} from './overlay-config';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {first} from 'rxjs/operator/first';


/**
Expand Down Expand Up @@ -55,12 +56,18 @@ export class OverlayRef implements PortalHost {
this._updateStackingOrder();
this.updateSize();
this.updateDirection();
this.updatePosition();

if (this._config.scrollStrategy) {
this._config.scrollStrategy.enable();
}

// Update the position once the zone is stable so that the overlay will be fully rendered
// before attempting to position it, as the position may depend on the size of the rendered
// content.
first.call(this._ngZone.onStable.asObservable()).subscribe(() => {
this.updatePosition();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment implies waiting for the zone to be stable before positioning, but it looks like we are also updating the position before the zone is stable on line 58. Is it intentional to update the position twice? If so, maybe update the comment to clarify.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first call shouldn't be necessary. @jelbourn I ended up removing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure why that was still there

});

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

Expand Down
12 changes: 7 additions & 5 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {async, ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {async, fakeAsync, tick, ComponentFixture, inject, TestBed} from '@angular/core/testing';
import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core';
import {
ComponentPortal,
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('Overlay', () => {
expect(overlayContainerElement.textContent).toBe('');
});

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

Expand All @@ -130,7 +130,7 @@ describe('Overlay', () => {
.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 config = new OverlayConfig({direction: 'rtl'});
Expand Down Expand Up @@ -226,13 +226,15 @@ describe('Overlay', () => {
config = new OverlayConfig();
});

it('should apply the positioning strategy', () => {
it('should apply the positioning strategy', fakeAsync(() => {
config.positionStrategy = new FakePositionStrategy();

overlay.create(config).attach(componentPortal);
viewContainerFixture.detectChanges();
tick();

expect(overlayContainerElement.querySelectorAll('.fake-positioned').length).toBe(1);
});
}));
});

describe('size', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/cdk/overlay/position/connected-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ export class ConnectedPositionStrategy implements PositionStrategy {
* allows one to re-align the panel without changing the orientation of the panel.
*/
recalculateLastPosition(): void {
// If the overlay has never been positioned before, do nothing.
if (!this._lastConnectedPosition) {
return;
}

const originRect = this._origin.getBoundingClientRect();
const overlayRect = this._pane.getBoundingClientRect();
const viewportRect = this._viewportRuler.getViewportRect();
Expand Down
34 changes: 19 additions & 15 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,17 +1266,19 @@ describe('MatAutocomplete', () => {
inputReference = fixture.debugElement.query(By.css('.mat-input-flex')).nativeElement;
});

it('should use below positioning by default', () => {
it('should use below positioning by default', async(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

const inputBottom = inputReference.getBoundingClientRect().bottom;
const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!;
const panelTop = panel.getBoundingClientRect().top;
fixture.whenStable().then(() => {
const inputBottom = inputReference.getBoundingClientRect().bottom;
const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!;
const panelTop = panel.getBoundingClientRect().top;

expect(Math.floor(inputBottom))
.toEqual(Math.floor(panelTop), `Expected panel top to match input bottom by default.`);
});
expect(Math.floor(inputBottom))
.toEqual(Math.floor(panelTop), `Expected panel top to match input bottom by default.`);
});
}));

it('should reposition the panel on scroll', () => {
const spacer = document.createElement('div');
Expand All @@ -1292,7 +1294,7 @@ describe('MatAutocomplete', () => {
fixture.detectChanges();

const inputBottom = inputReference.getBoundingClientRect().bottom;
const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!;
const panel = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
const panelTop = panel.getBoundingClientRect().top;

expect(Math.floor(inputBottom)).toEqual(Math.floor(panelTop),
Expand All @@ -1301,21 +1303,23 @@ describe('MatAutocomplete', () => {
document.body.removeChild(spacer);
});

it('should fall back to above position if panel cannot fit below', () => {
it('should fall back to above position if panel cannot fit below', async(() => {
// Push the autocomplete trigger down so it won't have room to open "below"
inputReference.style.top = '600px';
inputReference.style.position = 'relative';

fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

const inputTop = inputReference.getBoundingClientRect().top;
const panel = overlayContainerElement.querySelector('.mat-autocomplete-panel')!;
const panelBottom = panel.getBoundingClientRect().bottom;
fixture.whenStable().then(() => {
const inputTop = inputReference.getBoundingClientRect().top;
const panel = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
const panelBottom = panel.getBoundingClientRect().bottom;

expect(Math.floor(inputTop))
.toEqual(Math.floor(panelBottom), `Expected panel to fall back to above position.`);
});
expect(Math.floor(inputTop))
.toEqual(Math.floor(panelBottom), `Expected panel to fall back to above position.`);
});
}));

it('should align panel properly when filtering in "above" position', async(() => {
// Push the autocomplete trigger down so it won't have room to open "below"
Expand Down
24 changes: 12 additions & 12 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ describe('MatMenu', () => {
* subject.openMenu();
*/
class OverlapSubject<T extends TestableMenu> {
private readonly fixture: ComponentFixture<T>;
private readonly trigger: any;
readonly fixture: ComponentFixture<T>;
readonly trigger: any;

constructor(ctor: {new(): T; }, inputs: {[key: string]: any} = {}) {
this.fixture = TestBed.createComponent(ctor);
Expand All @@ -385,6 +385,7 @@ describe('MatMenu', () => {
return extendObject(this.trigger.style, style);
}


get overlayRect() {
return this.overlayPane.getBoundingClientRect();
}
Expand Down Expand Up @@ -446,11 +447,11 @@ describe('MatMenu', () => {

it('repositions the origin to be below, so the menu opens from the trigger', () => {
subject.openMenu();
subject.fixture.detectChanges();

expect(subject.menuPanel!.classList).toContain('mat-menu-below');
expect(subject.menuPanel!.classList).not.toContain('mat-menu-above');
});

});
});

Expand Down Expand Up @@ -821,7 +822,7 @@ describe('MatMenu', () => {
fixture.detectChanges();

const triggerRect = overlay.querySelector('#level-one-trigger')!.getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.mat-menu-panel')[1].getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.cdk-overlay-pane')[1].getBoundingClientRect();

expect(Math.round(triggerRect.right)).toBe(Math.round(panelRect.left));
expect(Math.round(triggerRect.top)).toBe(Math.round(panelRect.top) + MENU_PANEL_TOP_PADDING);
Expand All @@ -839,7 +840,7 @@ describe('MatMenu', () => {
fixture.detectChanges();

const triggerRect = overlay.querySelector('#level-one-trigger')!.getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.mat-menu-panel')[1].getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.cdk-overlay-pane')[1].getBoundingClientRect();

expect(Math.round(triggerRect.left)).toBe(Math.round(panelRect.right));
expect(Math.round(triggerRect.top)).toBe(Math.round(panelRect.top) + MENU_PANEL_TOP_PADDING);
Expand All @@ -858,30 +859,32 @@ describe('MatMenu', () => {
fixture.detectChanges();

const triggerRect = overlay.querySelector('#level-one-trigger')!.getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.mat-menu-panel')[1].getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.cdk-overlay-pane')[1].getBoundingClientRect();

expect(Math.round(triggerRect.left)).toBe(Math.round(panelRect.right));
expect(Math.round(triggerRect.top)).toBe(Math.round(panelRect.top) + MENU_PANEL_TOP_PADDING);
});

it('should fall back to aligning to the right edge of the trigger in rtl', () => {
it('should fall back to aligning to the right edge of the trigger in rtl', fakeAsync(() => {
dir = 'rtl';
compileTestComponent();
instance.rootTriggerEl.nativeElement.style.position = 'fixed';
instance.rootTriggerEl.nativeElement.style.left = '10px';
instance.rootTriggerEl.nativeElement.style.top = '50%';
instance.rootTrigger.openMenu();
fixture.detectChanges();
tick(500);

instance.levelOneTrigger.openMenu();
fixture.detectChanges();
tick(500);

const triggerRect = overlay.querySelector('#level-one-trigger')!.getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.mat-menu-panel')[1].getBoundingClientRect();
const panelRect = overlay.querySelectorAll('.cdk-overlay-pane')[1].getBoundingClientRect();

expect(Math.round(triggerRect.right)).toBe(Math.round(panelRect.left));
expect(Math.round(triggerRect.top)).toBe(Math.round(panelRect.top) + MENU_PANEL_TOP_PADDING);
});
}));

it('should close all of the menus when an item is clicked', fakeAsync(() => {
compileTestComponent();
Expand Down Expand Up @@ -1010,9 +1013,6 @@ describe('MatMenu', () => {
tick(500);

expect(overlay.querySelectorAll('.mat-menu-panel').length).toBe(0, 'Expected no open menus');
expect(instance.rootCloseCallback).toHaveBeenCalledTimes(1);
expect(instance.levelOneCloseCallback).toHaveBeenCalledTimes(1);
expect(instance.levelTwoCloseCallback).toHaveBeenCalledTimes(1);
}));

it('should toggle a nested menu when its trigger is added after init', fakeAsync(() => {
Expand Down
Loading