Skip to content

fix(menu): panel positions not changing if position is updated after first open #11707

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 1 commit into from
Jun 20, 2018
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
52 changes: 26 additions & 26 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
}

const overlayRef = this._createOverlay();
this._setPosition(overlayRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy);
overlayRef.attach(this._portal);

if (this.menu.lazyContent) {
Expand Down Expand Up @@ -349,7 +350,9 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
*/
private _getOverlayConfig(): OverlayConfig {
return new OverlayConfig({
positionStrategy: this._getPosition(),
positionStrategy: this._overlay.position()
.flexibleConnectedTo(this._element)
.withTransformOriginOn('.mat-menu-panel'),
hasBackdrop: this.menu.hasBackdrop == null ? !this.triggersSubmenu() : this.menu.hasBackdrop,
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
scrollStrategy: this._scrollStrategy(),
Expand All @@ -374,11 +377,11 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
}

/**
* This method builds the position strategy for the overlay, so the menu is properly connected
* to the trigger.
* @returns ConnectedPositionStrategy
* Sets the appropriate positions on a position strategy
* so the overlay connects with the trigger correctly.
* @param positionStrategy Strategy whose position to update.
*/
private _getPosition(): FlexibleConnectedPositionStrategy {
private _setPosition(positionStrategy: FlexibleConnectedPositionStrategy) {
let [originX, originFallbackX]: HorizontalConnectionPos[] =
this.menu.xPosition === 'before' ? ['end', 'start'] : ['start', 'end'];

Expand All @@ -400,27 +403,24 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
originFallbackY = overlayFallbackY === 'top' ? 'bottom' : 'top';
}

return this._overlay.position()
.flexibleConnectedTo(this._element)
.withTransformOriginOn('.mat-menu-panel')
.withPositions([
{originX, originY, overlayX, overlayY, offsetY},
{originX: originFallbackX, originY, overlayX: overlayFallbackX, overlayY, offsetY},
{
originX,
originY: originFallbackY,
overlayX,
overlayY: overlayFallbackY,
offsetY: -offsetY
},
{
originX: originFallbackX,
originY: originFallbackY,
overlayX: overlayFallbackX,
overlayY: overlayFallbackY,
offsetY: -offsetY
}
]);
positionStrategy.withPositions([
{originX, originY, overlayX, overlayY, offsetY},
{originX: originFallbackX, originY, overlayX: overlayFallbackX, overlayY, offsetY},
{
originX,
originY: originFallbackY,
overlayX,
overlayY: overlayFallbackY,
offsetY: -offsetY
},
{
originX: originFallbackX,
originY: originFallbackY,
overlayX: overlayFallbackX,
overlayY: overlayFallbackY,
offsetY: -offsetY
}
]);
}

/** Cleans up the active subscriptions. */
Expand Down
47 changes: 41 additions & 6 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,27 +494,28 @@ describe('MatMenu', () => {

describe('positions', () => {
let fixture: ComponentFixture<PositionedMenu>;
let panel: HTMLElement;
let trigger: HTMLElement;

beforeEach(() => {
fixture = createComponent(PositionedMenu);
fixture.detectChanges();

const trigger = fixture.componentInstance.triggerEl.nativeElement;
trigger = fixture.componentInstance.triggerEl.nativeElement;

// Push trigger to the bottom edge of viewport,so it has space to open "above"
trigger.style.position = 'fixed';
trigger.style.top = '600px';

// Push trigger to the right, so it has space to open "before"
trigger.style.left = '100px';
});

it('should append mat-menu-before if the x position is changed', () => {
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
panel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;
});

it('should append mat-menu-before if the x position is changed', () => {
const panel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;

expect(panel.classList).toContain('mat-menu-before');
expect(panel.classList).not.toContain('mat-menu-after');

Expand All @@ -526,6 +527,11 @@ describe('MatMenu', () => {
});

it('should append mat-menu-above if the y position is changed', () => {
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

const panel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;

expect(panel.classList).toContain('mat-menu-above');
expect(panel.classList).not.toContain('mat-menu-below');

Expand All @@ -546,12 +552,41 @@ describe('MatMenu', () => {
newFixture.detectChanges();
newFixture.componentInstance.trigger.openMenu();
newFixture.detectChanges();
panel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;
const panel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;

expect(panel.classList).toContain('mat-menu-below');
expect(panel.classList).toContain('mat-menu-after');
});

it('should be able to update the position after the first open', () => {
trigger.style.position = 'fixed';
trigger.style.top = '200px';

fixture.componentInstance.yPosition = 'above';
fixture.detectChanges();

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

let panel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;

expect(Math.floor(panel.getBoundingClientRect().bottom))
.toBe(Math.floor(trigger.getBoundingClientRect().bottom), 'Expected menu to open above');

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

fixture.componentInstance.yPosition = 'below';
fixture.detectChanges();

fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
panel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;

expect(Math.floor(panel.getBoundingClientRect().top))
.toBe(Math.floor(trigger.getBoundingClientRect().top), 'Expected menu to open below');
});

});

describe('fallback positions', () => {
Expand Down