Created
July 15, 2020 23:01
-
-
Save chamatt/d61eb8cf00c57a96255259aa94e99f2f to your computer and use it in GitHub Desktop.
Close on click outside in react (for custom dropdown components)
This file contains hidden or 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
const CustomDropdown = () => { | |
const [isOpen, setIsOpen] = useState(false); | |
const handleBlur = e => { | |
const currentTarget = e.currentTarget; | |
setTimeout(() => { | |
if (!currentTarget.contains(document.activeElement)) { | |
setIsOpen(false); | |
} | |
}, 0); | |
}; | |
<div tabIndex="0" onBlur={handleBlur}> | |
// Your custom dropdown goes here | |
</div> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment