Fix menu leave event

This commit is contained in:
Brieuc Dubois 2024-01-15 16:08:03 +01:00
parent 5dc13634e4
commit 3cd7ef0f82
1 changed files with 9 additions and 4 deletions

View File

@ -5,19 +5,24 @@
let menuElement: HTMLElement; let menuElement: HTMLElement;
export let onLeave: () => void = () => {}; export let onLeave: () => void = () => {};
function handleFocus(event: FocusEvent) { function clickOutside(event: MouseEvent) {
if (isOpen && !menuElement.contains(event.target as Node) && menuElement !== event.target) { if (
isOpen &&
menuElement &&
!menuElement.contains(event.target as Node) &&
menuElement !== event.target
) {
isOpen = false; isOpen = false;
onLeave(); onLeave();
} }
} }
onMount(() => { onMount(() => {
window.addEventListener('focus', handleFocus, true); window.addEventListener('click', clickOutside, true);
}); });
onDestroy(() => { onDestroy(() => {
window.removeEventListener('focus', handleFocus, true); window.removeEventListener('click', clickOutside, true);
}); });
</script> </script>