@equinor/eds-utils
Version:
Utility functions and hooks for the Equinor Design System
23 lines (20 loc) • 522 B
JavaScript
import { useEffect } from 'react';
const useGlobalKeyPress = (targetKey, callback) => {
useEffect(() => {
const handleGlobalKeyPress = e => {
const {
key
} = e;
switch (key) {
case targetKey:
callback(e);
break;
}
};
document.addEventListener('keydown', handleGlobalKeyPress, true);
return () => {
document.removeEventListener('keydown', handleGlobalKeyPress, true);
};
}, [targetKey, callback]);
};
export { useGlobalKeyPress };