Fix menu leave event
This commit is contained in:
parent
5dc13634e4
commit
3cd7ef0f82
|
@ -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>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue