From 92990b4ded51850b59c246b6c16bab75fd290def Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 10 Aug 2021 23:22:13 +0200 Subject: [PATCH] web/elements: properly cleanup event handlers Signed-off-by: Jens Langhammer --- web/src/elements/buttons/ModalButton.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/web/src/elements/buttons/ModalButton.ts b/web/src/elements/buttons/ModalButton.ts index bd69b8778..2b4130475 100644 --- a/web/src/elements/buttons/ModalButton.ts +++ b/web/src/elements/buttons/ModalButton.ts @@ -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 {