From e389d1186375dc546b5045e40dd95180441a0d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Roucheray?= Date: Fri, 8 Mar 2024 17:22:30 +0100 Subject: [PATCH] fix: Prevent several instances of a sub menu when opening with the mouse 2nd try --- .../context-menu-content.component.ts | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts index e4db2e7..c0c1985 100644 --- a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts +++ b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts @@ -242,11 +242,11 @@ export class ContextMenuContentComponent subMenu: ContextMenuComponent | undefined, event: MouseEvent | KeyboardEvent ): void { - if (!subMenu || subMenu.isOpen) { + if (!subMenu) { return; } - if (this.focusKeyManager?.activeItemIndex === null || !subMenu) { + if (this.focusKeyManager?.activeItemIndex === null) { return; } @@ -265,21 +265,31 @@ export class ContextMenuContentComponent value: this.value, parentContextMenu: this, }); - } else if (event.currentTarget) { + + return; + } + + if (subMenu.isOpen) { + return; + } + + if (event.currentTarget) { subMenu.show({ anchoredTo: 'element', anchorElement: event.currentTarget, value: this.value, parentContextMenu: this, }); - } else { - subMenu.show({ - anchoredTo: 'position', - x: (event as MouseEvent).clientX, - y: (event as MouseEvent).clientY, - value: this.value, - }); + + return; } + + subMenu.show({ + anchoredTo: 'position', + x: (event as MouseEvent).clientX, + y: (event as MouseEvent).clientY, + value: this.value, + }); } /** @@ -342,7 +352,8 @@ export class ContextMenuContentComponent } private closeActiveItemSubMenu(event: KeyboardEvent) { - if (this.focusKeyManager?.activeItemIndex === null) { + this.hideSubMenus(); + if (!this.focusKeyManager?.activeItemIndex) { return; }