-
-
Save brunosabot/41f8acd09fee652c24d4575e29dd5e42 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 { useCallback, useEffect } from "react"; | |
export default function useEscape(active: boolean, onClose: () => void) { | |
const onEchap = useCallback( | |
(event) => { | |
if (event.keyCode === 27) { | |
onClose(); | |
} | |
}, | |
[onClose] | |
); | |
useEffect(() => { | |
if (active) { | |
window.addEventListener("keydown", onEchap); | |
return () => { | |
window.removeEventListener("keydown", onEchap); | |
}; | |
} | |
}, [onEchap, active]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment