Created
May 11, 2020 22:58
-
-
Save staydecent/d8f9ab4f34347e87f87379da819e928a 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
import { useEffect } from 'react' | |
export function useDocumentClick (ref, cb) { | |
useEffect(() => { | |
const callback = (ev) => { | |
cb && cb(ref.current && !ref.current.contains(ev.target)) | |
} | |
document.addEventListener('mousedown', callback) | |
document.addEventListener('touchstart', callback) | |
return () => { | |
document.removeEventListener('mousedown', callback) | |
document.removeEventListener('touchstart', callback) | |
} | |
}, [ref.current]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment