Created
May 20, 2019 20:25
-
-
Save leofab86/5d91087fbeaca3963b9624170a4da1c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function useClickOutside ({container, handler, dependencies, addListenerConditional = true}) { | |
useEffect(() => { | |
if(addListenerConditional) { | |
document.addEventListener('mousedown', handleClickOutside); | |
} | |
return () => document.removeEventListener('mousedown', handleClickOutside) | |
}, dependencies); | |
function handleClickOutside (e) { | |
if(!container.current.contains(e.target)) { | |
handler() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment