From 4207f80ce9816a6d8950618b70fcb0e286abeb7a Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 7 Apr 2024 00:57:23 -0700 Subject: [PATCH] feat: close model on esc --- src/lib/components/common/Modal.svelte | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib/components/common/Modal.svelte b/src/lib/components/common/Modal.svelte index 038fa48d8..776bfaaf9 100644 --- a/src/lib/components/common/Modal.svelte +++ b/src/lib/components/common/Modal.svelte @@ -7,6 +7,7 @@ export let show = true; export let size = 'md'; + let modalElement = null; let mounted = false; const sizeToWidth = (size) => { @@ -19,14 +20,23 @@ } }; + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + console.log('Escape'); + show = false; + } + }; + onMount(() => { mounted = true; }); $: if (mounted) { if (show) { + window.addEventListener('keydown', handleKeyDown); document.body.style.overflow = 'hidden'; } else { + window.removeEventListener('keydown', handleKeyDown); document.body.style.overflow = 'unset'; } } @@ -36,6 +46,7 @@
{