import { useEffect , useRef } from 'react' ; export default function useClickOutside < T extends HTMLElement = any > ( handler , eventName = 'click' ) { const ref = useRef < T > ( ) ; useEffect ( ( ) => { const listener = ( e ) => { const targetElement = ref . current ; if ( ! targetElement || targetElement . contains ( e . target ) ) { return ; } if ( handler ) { handler ( e ) ; } } ; document . addEventListener ( eventName , listener ) ; return ( ) => { document . removeEventListener ( eventName , listener ) ; } ; } , [ eventName , handler ] ) ; return ref ; }