File size: 562 Bytes
0ad74ed |
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 |
/** Dispatch event on click outside of node */
namespace svelte.JSX {
interface HTMLProps<T> {
onclick_outside?: (e: CustomEvent) => void;
}
}
export function clickOutside(node: Node) {
const handleClick = (event: MouseEvent) => {
if (
node &&
!node.contains(event.target as Node) &&
!event.defaultPrevented
) {
node.dispatchEvent(new CustomEvent("click_outside", node as any));
}
};
document.addEventListener("click", handleClick, true);
return {
destroy() {
document.removeEventListener("click", handleClick, true);
}
};
}
|