2022-04-20 12:38:38 +00:00
|
|
|
import { Directive } from 'vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
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-29 08:40:34 +00:00
|
|
|
created (el: HTMLElement, { value }: { value: TAnyFunction }): void {
|
2022-04-15 17:00:08 +00:00
|
|
|
handler = (e: MouseEvent) => el.contains(e.target as Node) || value()
|
2022-04-29 08:40:34 +00:00
|
|
|
document.addEventListener('click', handler, { once: true })
|
|
|
|
document.addEventListener('contextmenu', handler, { once: true })
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|