Fix Shoelace context menu did not properly hide on Esc / click out

This commit is contained in:
nathan 2024-07-09 15:35:46 -06:00
parent f809a5a7a6
commit 5fed37087b

View File

@ -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();
@ -101,8 +109,11 @@ export class EgwMenuShoelace extends LitElement
}
public hide()
{
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 == "-")