Skip to content

Commit

Permalink
feat: 🎸 keep keyboard events in useKeyPress hook
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 28, 2019
1 parent 5c95f28 commit 00fecab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/__stories__/useKeyPress.story.tsx
Expand Up @@ -8,7 +8,7 @@ const keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];

const Demo = () => {
const states = [];
for (const key of keys) states.push(useKeyPress(key));
for (const key of keys) states.push(useKeyPress(key)[0]);

return (
<CenterStory>
Expand Down
8 changes: 4 additions & 4 deletions src/useKeyPress.ts
Expand Up @@ -2,10 +2,10 @@ import {useState} from 'react';
import useKey, {KeyFilter} from './useKey';

const useKeyPress = (keyFilter: KeyFilter) => {
const [isDown, set] = useState(false);
useKey(keyFilter, () => set(true), {event: 'keydown'}, [isDown]);
useKey(keyFilter, () => set(false), {event: 'keyup'}, [isDown]);
return isDown;
const [state, set] = useState<[boolean, null | KeyboardEvent]>([false, null]);
useKey(keyFilter, (event) => set([true, event]), {event: 'keydown'}, [state]);
useKey(keyFilter, (event) => set([false, event]), {event: 'keyup'}, [state]);
return state;
};

export default useKeyPress;

0 comments on commit 00fecab

Please sign in to comment.