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