Files
veridian/app/composables/useClickOutside.ts
T
2026-01-11 05:04:29 -06:00

18 lines
465 B
TypeScript

import { onMounted, onUnmounted } from 'vue'
import type { Ref } from 'vue'
export const useClickOutside = (target: Ref<HTMLElement | null>, callback: () => void) => {
const onClick = (event: MouseEvent) => {
if (target.value && !target.value.contains(event.target as Node)) {
callback()
}
}
onMounted(() => {
document.addEventListener('click', onClick)
})
onUnmounted(() => {
document.removeEventListener('click', onClick)
})
}