web/elements: properly cleanup event handlers

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-08-10 23:22:13 +02:00
parent 9e2f165dd8
commit 92990b4ded
1 changed files with 14 additions and 8 deletions

View File

@ -64,14 +64,20 @@ export class ModalButton extends LitElement {
];
}
constructor() {
super();
window.addEventListener("keyup", (e) => {
if (e.code === "Escape") {
this.resetForms();
this.open = false;
}
});
firstUpdated(): void {
window.addEventListener("keyup", this.keyUpHandler);
}
keyUpHandler = (e: KeyboardEvent) => {
if (e.code === "Escape") {
this.resetForms();
this.open = false;
}
}
disconnectedCallback(): void {
super.disconnectedCallback()
window.removeEventListener("keyup", this.keyUpHandler)
}
resetForms(): void {