From 5fed37087b30512af0483fb86902b0fe412ec88d Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 9 Jul 2024 15:35:46 -0600 Subject: [PATCH] Fix Shoelace context menu did not properly hide on Esc / click out --- api/js/egw_action/EgwMenuShoelace.ts | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/api/js/egw_action/EgwMenuShoelace.ts b/api/js/egw_action/EgwMenuShoelace.ts index 4e8f7ed6a0..1861d278cf 100644 --- a/api/js/egw_action/EgwMenuShoelace.ts +++ b/api/js/egw_action/EgwMenuShoelace.ts @@ -41,16 +41,24 @@ export class EgwMenuShoelace extends LitElement { super(); this.structure = _structure; + + this.handleDocumentClick = this.handleDocumentClick.bind(this); + this.handleKeypress = this.handleKeypress.bind(this); } connectedCallback() { super.connectedCallback(); + + document.addEventListener("click", this.handleDocumentClick); + document.addEventListener("keydown", this.handleKeypress); } disconnectedCallback() { super.disconnectedCallback(); + document.removeEventListener("click", this.handleDocumentClick); + document.removeEventListener("keydown", this.handleKeypress); if(this.popup) { this.popup.remove(); @@ -102,7 +110,10 @@ export class EgwMenuShoelace extends LitElement public hide() { - this.popup.active = false; + if(this.popup) + { + this.popup.active = false; + } // egw_menu always creates a new menu this.remove(); @@ -131,6 +142,24 @@ export class EgwMenuShoelace extends LitElement } } + handleDocumentClick(event) + { + if(!event.composedPath().includes(this)) + { + this.hide(); + } + } + + handleKeypress(event : KeyboardEvent) + { + if(event.key == "Escape") + { + event.preventDefault(); + event.stopPropagation(); + this.hide(); + } + } + private itemTemplate(item : egwMenuItem) { if(item.caption == "-")