wip bsky client for the web & android
bbell.vt3e.cat
1import { onMounted, onUnmounted, type Ref } from 'vue'
2
3export function useClickOutside(options: {
4 isOpen: Ref<boolean>
5 containerRef: Ref<HTMLElement | null>
6 triggerRef?: Ref<HTMLElement | null>
7 isMobile?: Ref<boolean>
8 onClose: () => void
9}) {
10 const { isOpen, containerRef, triggerRef, isMobile, onClose } = options
11
12 const handleClickOutside = (e: MouseEvent) => {
13 if (!isOpen.value) return
14 if (isMobile?.value) return
15 const target = e.target as Node
16 if (!containerRef.value?.contains(target) && !triggerRef?.value?.contains(target)) {
17 onClose()
18 }
19 }
20
21 onMounted(() => {
22 document.addEventListener('mousedown', handleClickOutside)
23 })
24 onUnmounted(() => {
25 document.removeEventListener('mousedown', handleClickOutside)
26 })
27}