Skip to content

Commit 5e12f9a

Browse files
committed
fix(menu): update position on every open
- menu overlay is only created once and the position of the overlay wasn't calculated again
1 parent 6e865b7 commit 5e12f9a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/lib/menu/menu-trigger.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
308308
* OverlayRef so that it can be attached to the DOM when openMenu is called.
309309
*/
310310
private _createOverlay(): OverlayRef {
311-
if (!this._overlayRef) {
311+
if (this._overlayRef) {
312+
this._overlayRef.getConfig().positionStrategy = this._getPosition();
313+
} else {
312314
this._portal = new TemplatePortal(this.menu.templateRef, this._viewContainerRef);
313315
const config = this._getOverlayConfig();
314316
this._subscribeToPositions(config.positionStrategy as ConnectedPositionStrategy);

src/lib/menu/menu.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,24 @@ describe('MatMenu', () => {
304304
expect(panel.classList).toContain('mat-menu-after');
305305
});
306306

307+
it('should update menu position on every open', fakeAsync(() => {
308+
const {top: beforeTop, left: beforeLeft} = panel.getBoundingClientRect();
309+
310+
fixture.componentInstance.trigger.closeMenu();
311+
fixture.componentInstance.xPosition = 'after';
312+
fixture.componentInstance.yPosition = 'below';
313+
fixture.detectChanges();
314+
tick(500);
315+
316+
fixture.componentInstance.trigger.openMenu();
317+
fixture.detectChanges();
318+
319+
const newPanel = overlayContainerElement.querySelector('.mat-menu-panel') as HTMLElement;
320+
const {top: afterTop, left: afterLeft} = newPanel.getBoundingClientRect();
321+
322+
expect(beforeLeft).toBeLessThan(afterLeft);
323+
expect(beforeTop).toBeLessThan(afterTop);
324+
}));
307325
});
308326

309327
describe('fallback positions', () => {

0 commit comments

Comments
 (0)