Skip to content

Commit 4dff505

Browse files
committed
feat(material/menu): allow updating menu position a la autocomplete
1 parent 4316787 commit 4dff505

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

src/material-experimental/mdc-menu/menu.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,9 @@ describe('MDC-based MatMenu', () => {
12601260
.toBe(Math.floor(trigger.getBoundingClientRect().bottom), 'Expected menu to open below');
12611261
});
12621262

1263+
it('should not throw if a menu reposition is requested while the menu is closed', () => {
1264+
expect(fixture.componentInstance.trigger.updatePosition()).not.toThrow();
1265+
});
12631266
});
12641267

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

src/material/menu/menu-trigger.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ 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+
this._overlayRef?.updatePosition();
299+
}
300+
294301
/** Closes the menu and does the necessary cleanup. */
295302
private _destroyMenu(reason: MenuCloseReason) {
296303
if (!this._overlayRef || !this.menuOpen) {

src/material/menu/menu.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,9 @@ 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+
});
12831286
});
12841287

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

tools/public_api_guard/material/menu.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export declare class MatMenuTrigger implements AfterContentInit, OnDestroy {
173173
openMenu(): void;
174174
toggleMenu(): void;
175175
triggersSubmenu(): boolean;
176+
updatePosition(): void;
176177
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MatMenuTrigger, "[mat-menu-trigger-for], [matMenuTriggerFor]", ["matMenuTrigger"], { "_deprecatedMatMenuTriggerFor": "mat-menu-trigger-for"; "menu": "matMenuTriggerFor"; "menuData": "matMenuTriggerData"; "restoreFocus": "matMenuTriggerRestoreFocus"; }, { "menuOpened": "menuOpened"; "onMenuOpen": "onMenuOpen"; "menuClosed": "menuClosed"; "onMenuClose": "onMenuClose"; }, never>;
177178
static ɵfac: i0.ɵɵFactoryDef<MatMenuTrigger, [null, null, null, null, { optional: true; }, { optional: true; self: true; }, { optional: true; }, null]>;
178179
}

0 commit comments

Comments
 (0)