Merge pull request #233 from acoard/feat/close-on-esc

Close the profile password modal via 'Esc' key
This commit is contained in:
Mike Cao 2020-09-29 21:26:25 -07:00 committed by GitHub
commit ce2ca57aba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

19
hooks/useEscapeKey.js Normal file
View file

@ -0,0 +1,19 @@
import { useEffect, useCallback } from 'react';
export default function useEscapeKey(handler) {
const escFunction = useCallback(event => {
if (event.keyCode === 27) {
handler(event);
}
}, []);
useEffect(() => {
document.addEventListener('keydown', escFunction, false);
return () => {
document.removeEventListener('keydown', escFunction, false);
};
}, [escFunction]);
return null;
}