Added some basic shared components, utils, hooks
This commit is contained in:
21
client/src/shared/hooks/onEscapeKeyDown.js
Normal file
21
client/src/shared/hooks/onEscapeKeyDown.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { KeyCodes } from 'shared/constants/keyCodes';
|
||||
|
||||
const useOnEscapeKeyDown = (isListening, onEscapeKeyDown) => {
|
||||
useEffect(() => {
|
||||
const handleKeyDown = event => {
|
||||
if (event.keyCode === KeyCodes.escape) {
|
||||
onEscapeKeyDown();
|
||||
}
|
||||
};
|
||||
if (isListening) {
|
||||
document.addEventListener('keydown', handleKeyDown);
|
||||
}
|
||||
return () => {
|
||||
document.removeEventListener('keydown', handleKeyDown);
|
||||
};
|
||||
}, [isListening, onEscapeKeyDown]);
|
||||
};
|
||||
|
||||
export default useOnEscapeKeyDown;
|
||||
Reference in New Issue
Block a user