Skip to content

Commit

Permalink
feat: 🎸 add useKeyboardJs hook
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 28, 2019
1 parent 772f046 commit 3516aa6
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- [`useGeolocation`](./docs/useGeolocation.md) — tracks geo location state of user's device. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usegeolocation--demo)
- [`useHover` and `useHoverDirty`](./docs/useHover.md) — tracks mouse hover state of some element. [![][img-demo]](https://codesandbox.io/s/zpn583rvx)
- [`useIdle`](./docs/useIdle.md) — tracks whether user is being inactive.
- [`useKey`](./docs/useKey.md), [`useKeyPress`](./docs/useKeyPress.md), and [`useKeyPressEvent`](./docs/useKeyPressEvent.md) — track keys. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usekeypressevent--demo)
- [`useKey`](./docs/useKey.md), [`useKeyPress`](./docs/useKeyPress.md), [`useKeyPressEvent`](./docs/useKeyPressEvent.md), and [`useKeyboardJs`](./docs/useKeyboardJs.md) — track keys. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usekeypressevent--demo)
- [`useLocation`](./docs/useLocation.md) — tracks page navigation bar location state.
- [`useMedia`](./docs/useMedia.md) — tracks state of a CSS media query. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usemedia--demo)
- [`useMediaDevices`](./docs/useMediaDevices.md) — tracks state of connected hardware devices.
Expand Down
5 changes: 0 additions & 5 deletions docs/useKeyPress.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
React UI sensor hook that detects when the user is pressing a specific
key on their keyboard.

Complex bindings like detecting when multiple keys are held down at the same
time or requiring them to be held down in a specified order are also available
via [KeyboardJS key combos](https://github.com/RobertWHurst/KeyboardJS).
Check its documentation for further details on how to make combo strings.


## Usage

Expand Down
31 changes: 31 additions & 0 deletions docs/useKeyboardJs.md
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');
```
31 changes: 31 additions & 0 deletions src/__stories__/useKeyboardJs.story.tsx
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} />;
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import useIdle from './useIdle';
import useKey from './useKey';
import useKeyPress from './useKeyPress';
import useKeyPressEvent from './useKeyPressEvent';
import useKeyboardJs from './useKeyboardJs';
import useLifecycles from './useLifecycles';
import useList from './useList';
import useLocalStorage from './useLocalStorage';
Expand Down Expand Up @@ -86,6 +87,7 @@ export {
useKey,
useKeyPress,
useKeyPressEvent,
useKeyboardJs,
useLifecycles,
useList,
useLocalStorage,
Expand Down
27 changes: 27 additions & 0 deletions src/useKeyboardJs.ts
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;

0 comments on commit 3516aa6

Please sign in to comment.