import type { RefObject } from 'react'; import { useEventListener } from '../useEventListener/useEventListener.ts'; type Handler = (event: MouseEvent) => void; export function useOnClickOutside( ref: RefObject, handler: Handler, mouseEvent: 'mousedown' | 'mouseup' = 'mousedown' ): void { useEventListener(mouseEvent, (event) => { const el = ref.current; // Do nothing if clicking ref's element or descendent elements if (!el || el.contains(event.target as Node)) { return; } handler(event); }); }