From 3cd7ef0f8216e79b0f703c8a51a082a32fdbccab Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Mon, 15 Jan 2024 16:08:03 +0100 Subject: [PATCH] Fix menu leave event --- frontend/src/lib/components/menu/Menu.svelte | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/frontend/src/lib/components/menu/Menu.svelte b/frontend/src/lib/components/menu/Menu.svelte index 5739cdc..ee387a0 100644 --- a/frontend/src/lib/components/menu/Menu.svelte +++ b/frontend/src/lib/components/menu/Menu.svelte @@ -5,19 +5,24 @@ let menuElement: HTMLElement; export let onLeave: () => void = () => {}; - function handleFocus(event: FocusEvent) { - if (isOpen && !menuElement.contains(event.target as Node) && menuElement !== event.target) { + function clickOutside(event: MouseEvent) { + if ( + isOpen && + menuElement && + !menuElement.contains(event.target as Node) && + menuElement !== event.target + ) { isOpen = false; onLeave(); } } onMount(() => { - window.addEventListener('focus', handleFocus, true); + window.addEventListener('click', clickOutside, true); }); onDestroy(() => { - window.removeEventListener('focus', handleFocus, true); + window.removeEventListener('click', clickOutside, true); });