File size: 699 Bytes
a03b3ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/**
* Svelte action to handle clicks outside of a DOM node
* @param node DOM node to check the click is outside of
* @param callback callback function to call if click is outside
* @returns svelte action return object with destroy method to remove event listener
*/
export function click_outside(
node: Node,
callback: (arg: MouseEvent) => void
): any {
const handle_click = (event: MouseEvent): void => {
if (
node &&
!node.contains(event.target as Node) &&
!event.defaultPrevented
) {
callback(event);
}
};
document.addEventListener("mousedown", handle_click, true);
return {
destroy() {
document.removeEventListener("mousedown", handle_click, true);
}
};
}
|