Skip to content

Commit 336e038

Browse files
committed
feat(material/menu): allow updating menu position a la autocomplete
1 parent db6511b commit 336e038

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/material/menu/menu-trigger.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
291291
}
292292
}
293293

294+
/**
295+
* Updates the position of the menu to ensure that it fits all options within the viewport.
296+
*/
297+
updatePosition(): void {
298+
if (this._overlayRef) {
299+
this._overlayRef.updatePosition();
300+
}
301+
}
302+
294303
/** Closes the menu and does the necessary cleanup. */
295304
private _destroyMenu(reason: MenuCloseReason) {
296305
if (!this._overlayRef || !this.menuOpen) {

src/material/menu/menu.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,10 @@ describe('MatMenu', () => {
12801280
.toBe(Math.floor(trigger.getBoundingClientRect().bottom), 'Expected menu to open below');
12811281
});
12821282

1283+
it('should not throw if a menu reposition is requested while the menu is closed', () => {
1284+
expect(fixture.componentInstance.trigger.updatePosition()).not.toThrow();
1285+
});
1286+
12831287
});
12841288

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

0 commit comments

Comments
 (0)