mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
28 lines
685 B
TypeScript
28 lines
685 B
TypeScript
import {useState, useEffect} from 'react';
|
|
import useMount from './useMount';
|
|
|
|
const useKeyboardJs = (combination: string) => {
|
|
const [state, set] = useState<[boolean, null | KeyboardEvent]>([false, null]);
|
|
const [keyboardJs, setKeyboardJs] = useState<any>(null);
|
|
|
|
useMount(() => {
|
|
import('keyboardjs').then(setKeyboardJs);
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!keyboardJs) return;
|
|
|
|
const down = event => set([true, event]);
|
|
const up = event => set([false, event]);
|
|
keyboardJs.bind(combination, down, up);
|
|
|
|
return () => {
|
|
keyboardJs.unbind(combination, down, up);
|
|
};
|
|
}, [combination, keyboardJs]);
|
|
|
|
return state;
|
|
};
|
|
|
|
export default useKeyboardJs;
|