From 4ad5c83a5605ce99495fe9bbe2124347d277dab2 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 5 Dec 2021 02:46:27 +0100 Subject: [PATCH] Unbind window listeners on content drawer --- pages/[roadmap]/interactive.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pages/[roadmap]/interactive.tsx b/pages/[roadmap]/interactive.tsx index 3b4f739ec..d1d0d01e2 100644 --- a/pages/[roadmap]/interactive.tsx +++ b/pages/[roadmap]/interactive.tsx @@ -38,13 +38,13 @@ function RoadmapRenderer(props: RoadmapProps) { // Event bindings for the roadmap interactivity useEffect(() => { - window.addEventListener('keydown', (event: KeyboardEvent) => { + function keydownListener(event: KeyboardEvent) { if (event.key.toLowerCase() === 'escape') { setGroupId(''); } - }); + } - window.addEventListener('click', (event: MouseEvent) => { + function clickListener(event: MouseEvent) { const targetGroup = (event?.target as HTMLElement)?.closest('g'); const groupId = targetGroup?.dataset?.groupId; if (!targetGroup || !groupId) { @@ -59,7 +59,15 @@ function RoadmapRenderer(props: RoadmapProps) { // e.g. 100-internet:how-does-the-internet-work // will be translated to `internet:how-does-the-internet-work` setGroupId(groupId.replace(/^\d+-/, '')); - }); + } + + window.addEventListener('keydown', keydownListener); + window.addEventListener('click', clickListener); + + return () => { + window.removeEventListener('keydown', keydownListener); + window.removeEventListener('click', clickListener); + } }, []); useEffect(() => {