Skip to content

Instantly share code, notes, and snippets.

@baranovxyz
Created May 7, 2020 19:39
Show Gist options
  • Save baranovxyz/2ad2d7a0f0dce41f202c9d6d74be9feb to your computer and use it in GitHub Desktop.
Save baranovxyz/2ad2d7a0f0dce41f202c9d6d74be9feb to your computer and use it in GitHub Desktop.
const App = () => {
const [showModal, setShowModal] = useState(false);
const handleKeyup = e => e.keyCode === 27 && setShowModal(false);
const toggleModal = () => setShowModal(!showModal);
useEffect(() => {
if (showModal) window.addEventListener('keyup', handleKeyup);
return () => window.removeEventListener('keyup', handleKeyup);
});
return <div onClick={toggleModal} className="App">
App - click window
{showModal && <Modal>
<Component />
</Modal>}
</div>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment