mirror of
https://github.com/koel/koel
synced 2024-12-20 09:33:23 +00:00
20 lines
694 B
TypeScript
20 lines
694 B
TypeScript
|
import { DirectiveArguments } from 'vue'
|
||
|
import { Directive } from '@vue/runtime-core'
|
||
|
|
||
|
/**
|
||
|
* 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: any }): void {
|
||
|
if (typeof value !== 'function') {
|
||
|
/* eslint no-console: 0 */
|
||
|
console.warn(`Expect a function, got ${value}`)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
document.addEventListener('click', (e: MouseEvent): void => el.contains(e.target as Node) || value())
|
||
|
document.addEventListener('contextmenu', (e: MouseEvent): void => el.contains(e.target as Node) || value())
|
||
|
}
|
||
|
}
|