From 5b241f0b64fa7a2639211da08be301beadbaa51a Mon Sep 17 00:00:00 2001 From: bo0tzz Date: Sat, 15 Apr 2023 18:51:56 +0200 Subject: [PATCH] feat(web): Allow closing modals with Escape key (#2257) --- web/src/lib/utils/click-outside.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/web/src/lib/utils/click-outside.ts b/web/src/lib/utils/click-outside.ts index dae9848626..e54ee8ef09 100644 --- a/web/src/lib/utils/click-outside.ts +++ b/web/src/lib/utils/click-outside.ts @@ -6,11 +6,19 @@ export function clickOutside(node: Node) { } }; + const handleKey = (event: KeyboardEvent) => { + if (event.key == 'Escape') { + node.dispatchEvent(new CustomEvent('outclick')); + } + }; + document.addEventListener('click', handleClick, true); + document.addEventListener('keydown', handleKey, true); return { destroy() { document.removeEventListener('click', handleClick, true); + document.removeEventListener('keydown', handleKey, true); } }; }