Skip to content

Commit c0658f6

Browse files
committed
feat: 🎸 refactor useKeyPressEvent hook
Changes its interface, fixes bug of calling callback on initial mount, useKeyPress hook is injected as dependency and can be overwirtten to useKeyboardJs. BREAKING CHANGE: 🧨 useKeyPressEvent hook modified for dependency injection and providing event objects to user
1 parent 00fecab commit c0658f6

File tree

4 files changed

+31
-55
lines changed

4 files changed

+31
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
- [`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)
3737
- [`useHover` and `useHoverDirty`](./docs/useHover.md) — tracks mouse hover state of some element. [![][img-demo]](https://codesandbox.io/s/zpn583rvx)
3838
- [`useIdle`](./docs/useIdle.md) — tracks whether user is being inactive.
39-
- [`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)
39+
- [`useKey`](./docs/useKey.md), [`useKeyPress`](./docs/useKeyPress.md), [`useKeyboardJs`](./docs/useKeyPressEvent.md), and [`useKeyPressEvent`](./docs/useKeyPressEvent.md) — track keys. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usekeypressevent--demo)
4040
- [`useLocation`](./docs/useLocation.md) — tracks page navigation bar location state.
4141
- [`useMedia`](./docs/useMedia.md) — tracks state of a CSS media query. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usemedia--demo)
4242
- [`useMediaDevices`](./docs/useMediaDevices.md) — tracks state of connected hardware devices.

docs/useKeyPressEvent.md

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
# `useKeyPressEvent`
22

3-
React UI sensor hook that detects when the user is pressing a specific
4-
key on their keyboard and fires a specified keyup and/or keydown effect. If
5-
you only need to retrieve the state, see [useKeyPress](useKeyPress.md).
6-
7-
Complex bindings like detecting when multiple keys are held down at the same
8-
time or requiring them to be held down in a specified order are also available
9-
via [KeyboardJS key combos](https://github.com/RobertWHurst/KeyboardJS).
10-
Check its documentation for further details on how to make combo strings.
11-
12-
The first argument is the key(s) to watch. If only a second argument
13-
(a function) is passed, it will be used in the keydown event. On the other hand,
14-
if a second and third argument are passed, the second will be used in the keyup
15-
event and the third in the keydown event. Essentially, keydown takes precedence.
16-
17-
Requires `keyboardjs`:
18-
19-
```bash
20-
npm add keyboardjs
21-
# or
22-
yarn add keyboardjs
23-
```
3+
This hook fires `keydown` and `keyup` calllbacks, similar to how [`useKey`](./useKey.md)
4+
hook does, but it only triggers each callback once per press cycle. For example,
5+
if you press and hold a key, it will fire `keydown` callback only once.
6+
247

258
## Usage
269

2710
```jsx
2811
import React, { useState } from React;
29-
import { useKeyPressEvent } from "react-use";
12+
import useKeyPressEvent from 'react-use/lib/useKeyPressEvent';
3013

3114
const Demo = () => {
3215
const [count, setCount] = useState(0);
@@ -50,9 +33,11 @@ const Demo = () => {
5033
};
5134
```
5235

36+
5337
## Reference
5438

5539
```js
56-
useKeyPressEvent('<key>', onKeydown);
57-
useKeyPressEvent('<key>', onKeyup, onKeydown);
40+
useKeyPressEvent('<key>', keydown);
41+
useKeyPressEvent('<key>', keydown, keyup);
42+
useKeyPressEvent('<key>', keydown, keyup, useKeyPress);
5843
```

src/__stories__/useKeyPressEvent.story.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import ShowDocs from "../util/ShowDocs";
66
const Demo = () => {
77
const [count, setCount] = React.useState(0);
88

9-
const increment = () => setCount(count => ++count);
10-
const decrement = () => setCount(count => --count);
11-
const reset = () => setCount(count => 0);
9+
const increment = () => {
10+
console.log('INCREMENT');
11+
setCount(count => ++count);
12+
};
13+
const decrement = () => {
14+
console.log('DECREMENT');
15+
setCount(count => --count);
16+
};
17+
const reset = () => setCount(() => 0);
1218

1319
useKeyPressEvent(']', increment, increment);
1420
useKeyPressEvent('[', decrement, decrement);

src/useKeyPressEvent.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1-
import * as React from 'react';
2-
const { useEffect } = React;
3-
import useKeyPress from './useKeyPress';
4-
5-
type KeyPressCallback = ((targetKey: string) => void) | undefined | null;
1+
import {KeyFilter, Handler} from './useKey';
2+
import useKeyPressDefault from './useKeyPress';
3+
import useUpdateEffect from './useUpdateEffect';
64

75
const useKeyPressEvent = (
8-
targetKey: string,
9-
onKeyup: KeyPressCallback = undefined,
10-
onKeydown: KeyPressCallback = undefined
6+
key: string | KeyFilter,
7+
keydown?: Handler | null | undefined,
8+
keyup?: Handler | null | undefined,
9+
useKeyPress = useKeyPressDefault,
1110
) => {
12-
const useKeyboardJS: boolean = targetKey.length > 1;
13-
const pressedKeys: boolean = useKeyPress(targetKey);
14-
15-
if (onKeydown === undefined) {
16-
onKeydown = onKeyup;
17-
onKeyup = null;
18-
}
19-
20-
useEffect(
21-
() => {
22-
if (!pressedKeys) {
23-
if (onKeyup) onKeyup(targetKey);
24-
return;
25-
}
26-
27-
if (onKeydown) onKeydown(targetKey);
28-
},
29-
[pressedKeys]
30-
);
11+
const [pressed, event] = useKeyPress(key);
12+
useUpdateEffect(() => {
13+
if (!pressed && keyup) keyup(event!);
14+
else if (keydown) keydown(event!);
15+
}, [pressed]);
3116
};
3217

3318
export default useKeyPressEvent;

0 commit comments

Comments
 (0)