koel/resources/assets/js/directives/clickaway.ts

16 lines
478 B
TypeScript
Raw Normal View History

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 = {
mounted (el: HTMLElement, { value }: { value: TAnyFunction }): void {
2022-04-15 17:00:08 +00:00
handler = (e: MouseEvent) => el.contains(e.target as Node) || value()
document.addEventListener('click', handler)
2022-04-15 17:00:08 +00:00
document.addEventListener('contextmenu', handler)
2022-04-15 14:24:30 +00:00
}
}