2022-04-15 14:24:30 +00:00
|
|
|
import { Directive } from '@vue/runtime-core'
|
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
let handler: any
|
|
|
|
|
2022-04-15 14:24:30 +00:00
|
|
|
/**
|
|
|
|
* 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 = {
|
2022-04-15 17:00:08 +00:00
|
|
|
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)
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|