Skip to content

Commit

Permalink
fix modal close with esc key and body overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
basher committed Apr 21, 2024
1 parent 112e174 commit 24fe08c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ui/src/javascript/web-components/webui-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export default class WebUIModal extends HTMLElement {
}
}

private handleGlobalKeyup(e: KeyboardEvent): void {
if (e.code === 'Escape') {
document.body.classList.remove(this.classNameModalOpen);
}
}

// Handle constructor() event listeners.
public handleEvent(e: MouseEvent) {
const target = e.currentTarget as HTMLButtonElement;
Expand All @@ -73,12 +79,16 @@ export default class WebUIModal extends HTMLElement {

// Handle (global) event listeners which are not part of this web component.
public connectedCallback() {
document.addEventListener('keyup', (e: KeyboardEvent) =>
this.handleGlobalKeyup(e),
);
document.addEventListener('click', (e: MouseEvent) =>
this.handleGlobalClick(e),
);
}

public disconnectedCallback() {
document.removeEventListener('keyup', this.handleGlobalKeyup);
document.removeEventListener('click', this.handleGlobalClick);
}
}

0 comments on commit 24fe08c

Please sign in to comment.