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;
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);
});
</script>