type mouseEventListener = (event: MouseEvent) => void;
const listenersMap = new Map<HTMLElement, mouseEventListener>();
export const vClickOutside: ObjectDirective<HTMLElement, mouseEventListener> = {
mounted(el, binding){
const listener: mouseEventListener = (event) => {
if(!(el === event.target || el.contains(event.target as HTMLElement))){
binding.value(event);
}
};
listenersMap.set(el, listener);
document.addEventListener('click', listener);
},
unmounted(el){
const listener = listenersMap.get(el);
if(!listener){
throw new Error('не удалось найти связанный с элементом обработчик событий');
}
document.removeEventListener('click', listener);
},
};
document.addEventListener('click', (event) => {
if(!(el === event.target || el.contains(event.target))){
trigger(event);
}
});