From e95cac6fab2af568ad25dd88d0390c86ca86724f Mon Sep 17 00:00:00 2001 From: "Terence D. Honles" Date: Fri, 26 Feb 2021 10:57:14 -0800 Subject: [PATCH] feat(material/menu): allow updating menu position a la autocomplete --- src/material-experimental/mdc-menu/menu.spec.ts | 3 +++ src/material/menu/menu-trigger.ts | 7 +++++++ src/material/menu/menu.spec.ts | 3 +++ tools/public_api_guard/material/menu.d.ts | 1 + 4 files changed, 14 insertions(+) diff --git a/src/material-experimental/mdc-menu/menu.spec.ts b/src/material-experimental/mdc-menu/menu.spec.ts index c9bd6844d6b7..6705bfbc63b9 100644 --- a/src/material-experimental/mdc-menu/menu.spec.ts +++ b/src/material-experimental/mdc-menu/menu.spec.ts @@ -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', () => { diff --git a/src/material/menu/menu-trigger.ts b/src/material/menu/menu-trigger.ts index f17c10f4c373..fc88cd9a2958 100644 --- a/src/material/menu/menu-trigger.ts +++ b/src/material/menu/menu-trigger.ts @@ -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) { diff --git a/src/material/menu/menu.spec.ts b/src/material/menu/menu.spec.ts index 62abc42518bf..72eb5c62abcc 100644 --- a/src/material/menu/menu.spec.ts +++ b/src/material/menu/menu.spec.ts @@ -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', () => { diff --git a/tools/public_api_guard/material/menu.d.ts b/tools/public_api_guard/material/menu.d.ts index 69ca9dd9b348..c53ffb5d92d3 100644 --- a/tools/public_api_guard/material/menu.d.ts +++ b/tools/public_api_guard/material/menu.d.ts @@ -173,6 +173,7 @@ export declare class MatMenuTrigger implements AfterContentInit, OnDestroy { openMenu(): void; toggleMenu(): void; triggersSubmenu(): boolean; + updatePosition(): void; static ɵdir: i0.ɵɵDirectiveDefWithMeta; static ɵfac: i0.ɵɵFactoryDef; }