Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
16 lines (14 sloc) 395 Bytes
import { useEffect } from 'react';
export default function useEscapeToClose(onClose: () => void) {
useEffect(() => {
function handleKeyDown(e: KeyboardEvent) {
if (e.keyCode === 27) {
onClose();
}
}
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [onClose]);
}
You can’t perform that action at this time.