-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
92 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# `useKeyboardJs` | ||
|
||
React UI sensor hook that detects complex key combos like detecting when | ||
multiple keys are held down at the same time or requiring them to be held down in a specified order. | ||
|
||
Via [KeyboardJS key combos](https://github.com/RobertWHurst/KeyboardJS). | ||
Check its documentation for further details on how to make combo strings. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import useKeyboardJs from 'react-use/lib/useKeyboardJs'; | ||
|
||
const Demo = () => { | ||
const isPressed = useKeyboardJs('a + b'); | ||
|
||
return ( | ||
<div> | ||
[a + b] pressed: {isPressed ? 'Yes' : 'No'} | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
|
||
## Examples | ||
|
||
```js | ||
const isPressed = useKeyboardJs('a + b'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import {withKnobs, text} from '@storybook/addon-knobs'; | ||
import * as React from 'react'; | ||
import {useKeyboardJs} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
import {CenterStory} from './util/CenterStory'; | ||
|
||
const Demo = ({combo}) => { | ||
const pressed = useKeyboardJs(combo) | ||
|
||
return ( | ||
<CenterStory> | ||
<div style={{textAlign: 'center'}}> | ||
Press <code style={{color: 'red', background: '#f6f6f6', padding: '3px 6px', borderRadius: '3px'}}>{combo}</code> combo | ||
<br /> | ||
<br /> | ||
<div style={{fontSize: '4em'}}> | ||
{pressed ? '💋' : ''} | ||
</div> | ||
</div> | ||
</CenterStory> | ||
); | ||
}; | ||
|
||
storiesOf("Sensors|useKeyboardJs", module) | ||
.addDecorator(withKnobs) | ||
.add("Docs", () => <ShowDocs md={require("../../docs/useKeyPress.md")} />) | ||
.add("Demo", () => { | ||
const combo = text('Combo', 'i + l + u'); | ||
return <Demo combo={combo} />; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {useState, useEffect} from 'react'; | ||
import useMount from './useMount'; | ||
|
||
const useKeyboardJs = (combination: string) => { | ||
const [state, set] = useState(false); | ||
const [keyboardJs, setKeyboardJs] = useState<any>(null); | ||
|
||
useMount(() => { | ||
import('keyboardjs').then(setKeyboardJs); | ||
}); | ||
|
||
useEffect(() => { | ||
if (!keyboardJs) return; | ||
|
||
const down = () => set(true); | ||
const up = () => set(false); | ||
keyboardJs.bind(combination, down, up); | ||
|
||
return () => { | ||
keyboardJs.unbind(combination, down, up); | ||
}; | ||
}, [combination, keyboardJs]); | ||
|
||
return state; | ||
}; | ||
|
||
export default useKeyboardJs; |