koel/resources/assets/js/directives/clickaway.ts
2022-04-19 21:26:06 +02:00

19 lines
630 B
TypeScript

import { Directive } from '@vue/runtime-core'
let handler: any
/**
* A fork of https://github.com/simplesmiler/vue-clickaway.
* Trigger a function if the user clicks out of the bound element.
*/
export const clickaway: Directive = {
created (el: HTMLElement, { value }: { value: TAnyFunction }): void {
handler = (e: MouseEvent) => el.contains(e.target as Node) || value()
document.addEventListener('mouseup', handler)
document.addEventListener('contextmenu', handler)
},
unmounted: () => {
document.removeEventListener('mouseup', handler)
document.removeEventListener('contextmenu', handler)
}
}