Created
January 28, 2019 14:02
-
-
Save theoperatore/e640077a5a2f235c26bb6b4097981c33 to your computer and use it in GitHub Desktop.
two custom hooks; 1 for appending a DOM node to the document for use in a Portal. Another for conditionally adding an event listener to handle outside of flyout menu clicks (so it can close)
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 useDOMDiv = (changeToUpdate: number = 1) => { | |
const nodeRef = React.useRef(document.createElement('div')); | |
React.useEffect(() => { | |
document.body.appendChild(nodeRef.current); | |
return () => document.body.removeChild(nodeRef.current); | |
}, [changeToUpdate]); | |
return nodeRef.current; | |
}; | |
const useOutsideClick = ( | |
node: HTMLDivElement, | |
isOpen: boolean, | |
onOutsideClick: () => void, | |
) => { | |
React.useEffect(() => { | |
const handleOutsideClick = (ev: MouseEvent) => { | |
if (!node.contains(ev.target as Element)) { | |
onOutsideClick(); | |
} | |
}; | |
if (isOpen) { | |
document.addEventListener('click', handleOutsideClick); | |
} | |
return () => document.removeEventListener('click', handleOutsideClick); | |
}, [isOpen]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you'd use them like this: