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

16 lines
510 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 = {
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()
document.addEventListener('click', handler, { once: true })
document.addEventListener('contextmenu', handler, { once: true })
2022-04-15 14:24:30 +00:00
}
}