huggi / src /lib /actions /clickOutside.ts
nsarrazin's picture
nsarrazin HF staff
Reworked settings menu (#591)
2cb745f unverified
raw
history blame
443 Bytes
export function clickOutside(element: HTMLDialogElement, callbackFunction: () => void) {
function onClick(event: MouseEvent) {
if (!element.contains(event.target as Node)) {
callbackFunction();
}
}
document.body.addEventListener("click", onClick);
return {
update(newCallbackFunction: () => void) {
callbackFunction = newCallbackFunction;
},
destroy() {
document.body.removeEventListener("click", onClick);
},
};
}