Skip to content

feat(material/menu): allow updating menu position a la autocomplete #22046

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
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
3 changes: 3 additions & 0 deletions src/material-experimental/mdc-menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,9 @@ describe('MDC-based MatMenu', () => {
.toBe(Math.floor(trigger.getBoundingClientRect().bottom), 'Expected menu to open below');
});

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

describe('fallback positions', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/material/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
}
}

/**
* Updates the position of the menu to ensure that it fits all options within the viewport.
*/
updatePosition(): void {
this._overlayRef?.updatePosition();
}

/** Closes the menu and does the necessary cleanup. */
private _destroyMenu(reason: MenuCloseReason) {
if (!this._overlayRef || !this.menuOpen) {
Expand Down
3 changes: 3 additions & 0 deletions src/material/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,9 @@ describe('MatMenu', () => {
.toBe(Math.floor(trigger.getBoundingClientRect().bottom), 'Expected menu to open below');
});

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

describe('fallback positions', () => {
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/menu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export declare class MatMenuTrigger implements AfterContentInit, OnDestroy {
openMenu(): void;
toggleMenu(): void;
triggersSubmenu(): boolean;
updatePosition(): void;
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>;
static ɵfac: i0.ɵɵFactoryDef<MatMenuTrigger, [null, null, null, null, { optional: true; }, { optional: true; self: true; }, { optional: true; }, null]>;
}
Expand Down