From a5c2cc1835cfdc9ba42cb6a3ff812102f1351a24 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Mon, 1 Oct 2018 17:31:55 +0200 Subject: [PATCH] fix(menu): wait until all menus are ready fixes #15727 --- .../menu-controller/menu-controller.ts | 25 ++++++++++++++----- core/src/components/menu/menu-interface.ts | 2 +- core/src/components/menu/menu.tsx | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/core/src/components/menu-controller/menu-controller.ts b/core/src/components/menu-controller/menu-controller.ts index ef5e3f8d5ff..c1b30bad7a9 100644 --- a/core/src/components/menu-controller/menu-controller.ts +++ b/core/src/components/menu-controller/menu-controller.ts @@ -16,6 +16,7 @@ export class MenuController implements MenuControllerI { private menuAnimations = new Map(); @Prop({ connect: 'ion-animation-controller' }) animationCtrl!: HTMLIonAnimationControllerElement; + @Prop({ context: 'document' }) doc!: Document; constructor() { this.registerAnimation('reveal', menuRevealAnimation); @@ -134,6 +135,8 @@ export class MenuController implements MenuControllerI { return undefined; } } + await this.waitUntilReady(); + if (menuId === 'start' || menuId === 'end') { // there could be more than one menu on the same side // so first try to get the enabled one @@ -166,24 +169,27 @@ export class MenuController implements MenuControllerI { * Returns the instance of the menu already opened, otherwise `null`. */ @Method() - getOpen(): Promise { - return Promise.resolve(this.getOpenSync()); + async getOpen(): Promise { + await this.waitUntilReady(); + return this.getOpenSync(); } /** * Returns an array of all menu instances. */ @Method() - getMenus(): Promise { - return Promise.resolve(this.getMenusSync()); + async getMenus(): Promise { + await this.waitUntilReady(); + return this.getMenusSync(); } /** * Returns true if any menu is currently animating. */ @Method() - isAnimating(): Promise { - return Promise.resolve(this.isAnimatingSync()); + async isAnimating(): Promise { + await this.waitUntilReady(); + return this.isAnimatingSync(); } @Method() @@ -264,4 +270,11 @@ export class MenuController implements MenuControllerI { } return undefined; } + + private waitUntilReady() { + return Promise.all( + Array.from(this.doc.querySelectorAll('ion-menu')) + .map(menu => menu.componentOnReady()) + ); + } } diff --git a/core/src/components/menu/menu-interface.ts b/core/src/components/menu/menu-interface.ts index 919551a2625..d9cb7fafc92 100644 --- a/core/src/components/menu/menu-interface.ts +++ b/core/src/components/menu/menu-interface.ts @@ -31,7 +31,7 @@ export interface MenuControllerI { _unregister(menu: MenuI): void; _setActiveMenu(menu: MenuI): void; - getMenusSync(): HTMLIonMenuElement[]; + getMenus(): Promise; getOpenSync(): HTMLIonMenuElement | undefined; } diff --git a/core/src/components/menu/menu.tsx b/core/src/components/menu/menu.tsx index 15bb7cf2f72..22b27607970 100644 --- a/core/src/components/menu/menu.tsx +++ b/core/src/components/menu/menu.tsx @@ -168,7 +168,7 @@ export class Menu implements ComponentInterface, MenuI { let isEnabled = !this.disabled; if (isEnabled) { - const menus = this.menuCtrl!.getMenusSync(); + const menus = await this.menuCtrl!.getMenus(); isEnabled = !menus.some((m: any) => { return m.side === this.side && !m.disabled; });