2025-10-03 16:51:44 +02:00
|
|
|
import type { DirectiveBinding } from 'vue';
|
|
|
|
|
|
|
|
|
|
interface ClickOutsideElement extends HTMLElement {
|
|
|
|
|
clickOutsideEvent?: (event: MouseEvent) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 14:18:04 +02:00
|
|
|
export default {
|
2025-10-03 16:51:44 +02:00
|
|
|
beforeMount(el: ClickOutsideElement, binding: DirectiveBinding<(event: MouseEvent) => void>) {
|
|
|
|
|
el.clickOutsideEvent = function (event: MouseEvent) {
|
|
|
|
|
// Check if the clicked element is neither the element
|
|
|
|
|
// to which the directive is applied nor its child
|
|
|
|
|
if (!(el === event.target || el.contains(event.target as Node))) {
|
|
|
|
|
// Invoke the provided method
|
|
|
|
|
binding.value(event);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener('click', el.clickOutsideEvent);
|
|
|
|
|
},
|
|
|
|
|
unmounted(el: ClickOutsideElement) {
|
|
|
|
|
// Remove the event listener when the bound element is unmounted
|
|
|
|
|
if (el.clickOutsideEvent) {
|
|
|
|
|
document.removeEventListener('click', el.clickOutsideEvent);
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-09-23 14:18:04 +02:00
|
|
|
};
|