Skip to content

Commit 5881da1

Browse files
Added more options
Swapped deps and options param. Updated docz.
1 parent 66914ef commit 5881da1

File tree

4 files changed

+59
-54
lines changed

4 files changed

+59
-54
lines changed

docs/useHotkeys.mdx

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ import { useHotkeys } from '../src';
1010

1111
The `useHotkeys` hook follows the hotkeys call signature.
1212

13-
The callback function takes the exact parameters as the callback function in the hotkeys package.
14-
See hotkeys documentation for more info or look into the typings file.
15-
1613
When the component gets mounted into the DOM, it will listen for the keystroke. When the component will unmount, it
1714
stops listening for the keystroke.
1815

1916
```ts
20-
useHotkeys(keys: string, callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, deps: any[] = [])
17+
useHotkeys(keys: string, callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, options: Options = {}, deps: any[] = [])
2118
```
2219

2320
## Parameters
@@ -27,13 +24,16 @@ section on the hotkeys documentation for more info.
2724
* `callback: (event: KeyboardEvent, handler: HotkeysEvent) => void`: Gets executed when the defined keystroke
2825
gets hit by the user. **Important:** Since version 1.5.0 this callback gets memoised inside the hook. So you don't have
2926
to do this anymore by yourself.
27+
* `options: Options = {}` Pass options directly to the hotkeys package. See [Options](#options) section for more details.
3028
* `deps: any[] = []`: The dependency array that gets appended to the memoisation of the callback. Here you define the inner
3129
dependencies of your callback. If for example your callback actions depend on a referentially unstable value or a value
3230
that will change over time, you should add this value to your deps array. Since most of the time your callback won't
3331
depend on any unstable callbacks or changing values over time you can leave this value alone since it will be set to an
3432
empty array by default. See the [Memoisation](#memoisation) section to
3533
learn more and see an example where you have to set this array.
3634

35+
**Breaking Change in 2.0**: Since version 2.0 the `options` and `deps` parameter switched positions.
36+
3737
## Example
3838

3939
This will listen to the `ctrl+k` keystroke. If you press it, the counter increments by one. If `ctrl+l` gets pressed,
@@ -43,27 +43,12 @@ the counter will decrement by one.
4343
Due to the nature of how `useEffect` works and to prevent resetting the hotkeys handler during every render, before 1.5.0
4444
you had to memoise your callback yourself. Since this is tedious work and dependency arrays are a common pattern with
4545
React hooks, I decided to bring the memoisation inside the hook, so you don't have to deal with it. Please read the
46-
**Memoisation** section for more info on this.
46+
[**Memoisation**](#memoisation) section for more info on this.
4747

4848
```js
4949
import { useHotkeys } from 'react-hotkeys-hook';
5050
```
5151

52-
```jsx
53-
const AddToBankComponent = () => {
54-
const [amount, setAmount] = useState(0);
55-
56-
useHotkeys('ctrl+a', () => setAmount(prevAmount => prevAmount + 100));
57-
useHotkeys('ctrl+d', () => setAmount(prevAmount => prevAmount - 100));
58-
59-
return (
60-
<div>
61-
{amount >= 0 ? 'Add' : 'Remove'} {Math.abs(amount)} dollars {amount >= 0 ? 'from' : 'to'} my bank account.
62-
</div>
63-
);
64-
};
65-
```
66-
6752
<Playground>
6853
{() => {
6954
const [amount, setAmount] = useState(0);
@@ -77,24 +62,21 @@ const AddToBankComponent = () => {
7762
}}
7863
</Playground>
7964

80-
## Memoisation
65+
## Options
8166

82-
Let's check out a slightly different example to see how memoisation effects your application.
67+
The options parameter is used to pass options directly to the hotkeys package. Check out the [hotkeys docs](https://github.com/jaywcjlove/hotkeys/#option)
68+
for an overview.
8369

84-
```jsx
85-
const AddToBankComponent = () => {
86-
const [amount, setAmount] = useState(0);
70+
### Parameters
71+
* `filter: (event: KeyboardEvent): boolean` is used to enable hotkeys inside input elements. Check out [hotkeys docs](https://github.com/jaywcjlove/hotkeys/#filter) for usage
72+
* `splitKey: string` is used to change the splitting character inside the keys argument. Default is `+`, but if you want
73+
to listen to the `+` character, you can set `splitKey` to i.e. `-` and listen for `ctrl-+`
74+
* `keyup: boolean` Determine if you want to listen on the keyup event
75+
* `keydown: boolean` Determine if want to listen on the keydown event
8776

88-
// Don't use this in production, won't work as expected.
89-
useHotkeys('n', () => setAmount(amount + 100), []);
77+
## Memoisation
9078

91-
return (
92-
<div>
93-
Add {Math.abs(amount)} dollars to my bank account.
94-
</div>
95-
);
96-
};
97-
```
79+
Let's check out a slightly different example to see how memoisation effects your application.
9880

9981
<Playground>
10082
{() => {
@@ -120,24 +102,10 @@ So our logic flow got stuck, tis not what we wanted.
120102
To fix this there are two approaches when using values that are retrieved by `useState`. You could define a dependency array as
121103
the third parameter like so:
122104

123-
```jsx
124-
const AddToBankComponent = () => {
125-
const [amount, setAmount] = useState(0);
126-
127-
// Works, but has performance and binding issues.
128-
useHotkeys('m', () => setAmount(amount + 100), [amount]);
129-
130-
return (
131-
<div>
132-
Add {Math.abs(amount)} dollars to my bank account.
133-
</div>
134-
);
135-
};
136-
```
137-
138105
<Playground>
139106
{() => {
140107
const [amount, setAmount] = useState(0);
108+
// Works but has binding issues
141109
useHotkeys('m', () => setAmount(amount + 100), [amount]);
142110
return (
143111
<div>
@@ -165,4 +133,4 @@ const AddToBankComponent = () => {
165133
</div>
166134
);
167135
};
168-
```
136+
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@types/react-dom": "16.9.6",
6565
"docz": "2.3.0",
6666
"emotion-theming": "10.0.27",
67+
"gatsby-plugin-emotion": "4.2.1",
6768
"react": "16.13.1",
6869
"react-dom": "16.13.1",
6970
"typescript": "3.8.3"

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import hotkeys, { KeyHandler } from "hotkeys-js";
2-
import { useCallback, useEffect } from "react";
1+
import hotkeys, {KeyHandler} from "hotkeys-js";
2+
import {useCallback, useEffect} from "react";
33

44
type Options = {
55
filter?: typeof hotkeys.filter;
6+
splitKey?: string;
7+
scope?: string;
8+
keyUp?: boolean;
9+
keyDown?: boolean;
610
};
711

812
export function useHotkeys(
913
keys: string,
1014
callback: KeyHandler,
15+
options: Options = {},
1116
deps: any[] = [],
12-
options: Options = {}
1317
) {
1418
const memoisedCallback = useCallback(callback, deps);
1519

1620
useEffect(() => {
1721
if (options.filter) hotkeys.filter = options.filter;
1822

19-
hotkeys(keys, memoisedCallback);
23+
hotkeys(keys, options, memoisedCallback);
2024

2125
return () => hotkeys.unbind(keys, memoisedCallback);
2226
}, [memoisedCallback, options]);

yarn.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,13 @@
23592359
dependencies:
23602360
regenerator-runtime "^0.13.4"
23612361

2362+
"@babel/runtime@^7.8.7":
2363+
version "7.9.2"
2364+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.9.2.tgz#d90df0583a3a252f09aaa619665367bae518db06"
2365+
integrity sha512-NE2DtOdufG7R5vnfQUTehdTfNycfUANEtCa9PssN9O/xmTzP4E08UI797ixaei6hBEVL9BI/PsdJS5x7mWoB9Q==
2366+
dependencies:
2367+
regenerator-runtime "^0.13.4"
2368+
23622369
"@babel/standalone@^7.4.5":
23632370
version "7.7.4"
23642371
resolved "https://registry.yarnpkg.com/@babel/standalone/-/standalone-7.7.4.tgz#9adcda4b7c33627c65eacf87f5c1f950987294c2"
@@ -2551,6 +2558,13 @@
25512558
dependencies:
25522559
"@babel/plugin-syntax-jsx" "^7.2.0"
25532560

2561+
"@emotion/babel-plugin-jsx-pragmatic@^0.1.5":
2562+
version "0.1.5"
2563+
resolved "https://registry.yarnpkg.com/@emotion/babel-plugin-jsx-pragmatic/-/babel-plugin-jsx-pragmatic-0.1.5.tgz#27debfe9c27c4d83574d509787ae553bf8a34d7e"
2564+
integrity sha512-y+3AJ0SItMDaAgGPVkQBC/S/BaqaPACkQ6MyCI2CUlrjTxKttTVfD3TMtcs7vLEcLxqzZ1xiG0vzwCXjhopawQ==
2565+
dependencies:
2566+
"@babel/plugin-syntax-jsx" "^7.2.0"
2567+
25542568
"@emotion/babel-preset-css-prop@^10.0.23":
25552569
version "10.0.23"
25562570
resolved "https://registry.yarnpkg.com/@emotion/babel-preset-css-prop/-/babel-preset-css-prop-10.0.23.tgz#7c21a36c97c3ce9e96f5896b56f68b9bbac800bd"
@@ -2561,6 +2575,16 @@
25612575
"@emotion/babel-plugin-jsx-pragmatic" "^0.1.4"
25622576
babel-plugin-emotion "^10.0.23"
25632577

2578+
"@emotion/babel-preset-css-prop@^10.0.27":
2579+
version "10.0.27"
2580+
resolved "https://registry.yarnpkg.com/@emotion/babel-preset-css-prop/-/babel-preset-css-prop-10.0.27.tgz#58868d9a6afee0eeaeb0fa9dc5ccb1b12d4f786b"
2581+
integrity sha512-rducrjTpLGDholp0l2l4pXqpzAqYYGMg/x4IteO0db2smf6zegn6RRZdDnbaoMSs63tfPWgo2WukT1/F1gX/AA==
2582+
dependencies:
2583+
"@babel/plugin-transform-react-jsx" "^7.3.0"
2584+
"@babel/runtime" "^7.5.5"
2585+
"@emotion/babel-plugin-jsx-pragmatic" "^0.1.5"
2586+
babel-plugin-emotion "^10.0.27"
2587+
25642588
"@emotion/cache@^10.0.17":
25652589
version "10.0.17"
25662590
resolved "https://registry.yarnpkg.com/@emotion/cache/-/cache-10.0.17.tgz#3491a035f62f276620d586677bfc3d4fad0b8472"
@@ -7487,6 +7511,14 @@ gatsby-plugin-compile-es6-packages@^2.0.0:
74877511
"@babel/runtime" "^7.0.0"
74887512
regex-escape "^3.4.8"
74897513

7514+
7515+
version "4.2.1"
7516+
resolved "https://registry.yarnpkg.com/gatsby-plugin-emotion/-/gatsby-plugin-emotion-4.2.1.tgz#d4244460e23d1bb5cca76266ba8990f534234e32"
7517+
integrity sha512-ygXxkpnWJdDOAgb1XA9TbVCRLkaAYTFLTsqVQXMBhnrknb5iPNO+MP0fZ5LRqWgBALyJ629nxs0efUpnT/RSWw==
7518+
dependencies:
7519+
"@babel/runtime" "^7.8.7"
7520+
"@emotion/babel-preset-css-prop" "^10.0.27"
7521+
74907522
gatsby-plugin-emotion@^4.1.2:
74917523
version "4.1.16"
74927524
resolved "https://registry.yarnpkg.com/gatsby-plugin-emotion/-/gatsby-plugin-emotion-4.1.16.tgz#7ac7f03f8b2a1c93ff0ecd2378a14c97ef9380b1"

0 commit comments

Comments
 (0)