Skip to content

Commit 04abccb

Browse files
committed
Vendor keyboard event shim
1 parent a6632d5 commit 04abccb

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ React hook for creating simple keyboard shortcuts.
1414
npm install @reecelucas/react-use-hotkeys
1515
```
1616

17-
This package has a single dependency, a tiny shim called [`shim-keyboard-event-key`](https://www.npmjs.com/package/shim-keyboard-event-key) that normalises the non-standard `KeyBoardEvent.key` [values](https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8860571/) implemented in Edge and IE.
18-
1917
## Example Usage
2018

2119
All hotkey combinations must use valid `KeyBoardEvent` `"key"` values. A full list can be found on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and Wes Bos has created a great [interactive lookup](https://keycode.info/).

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@
6666
"tslint": "^5.15.0",
6767
"tslint-config-prettier": "^1.18.0",
6868
"tslint-react": "^4.0.0",
69-
"typescript": "^3.4.1"
70-
},
71-
"dependencies": {
72-
"shim-keyboard-event-key": "^1.0.3"
69+
"typescript": "^4.8.2"
7370
},
7471
"husky": {
7572
"hooks": {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import modifierKeyPressed from './helpers/modifierKeyPressed';
88
import tail from './helpers/tail';
99
import takeUntilLast from './helpers/takeUntilLast';
1010

11-
import 'shim-keyboard-event-key';
11+
import './vendor/shim-keyboard-event-key';
1212

1313
interface SequenceTimers {
1414
[key: number]: number;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Vendored version of https://github.com/shvaikalesh/shim-keyboard-event-key.
2+
// Necessary to fix an issue with SSR.
3+
(function() {
4+
'use strict';
5+
6+
if (typeof self === 'undefined' || !self.document) return;
7+
8+
var event = KeyboardEvent.prototype;
9+
var desc = Object.getOwnPropertyDescriptor(event, 'key');
10+
if (!desc) return;
11+
12+
var keys = {
13+
Win: 'Meta',
14+
Scroll: 'ScrollLock',
15+
Spacebar: ' ',
16+
17+
Down: 'ArrowDown',
18+
Left: 'ArrowLeft',
19+
Right: 'ArrowRight',
20+
Up: 'ArrowUp',
21+
22+
Del: 'Delete',
23+
Apps: 'ContextMenu',
24+
Esc: 'Escape',
25+
26+
Multiply: '*',
27+
Add: '+',
28+
Subtract: '-',
29+
Decimal: '.',
30+
Divide: '/'
31+
};
32+
33+
Object.defineProperty(event, 'key', {
34+
get: function() {
35+
var key = desc.get.call(this);
36+
37+
return keys.hasOwnProperty(key) ? keys[key] : key;
38+
}
39+
});
40+
})();

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"pretty": true,
99
"rootDir": "src",
1010
"strict": true,
11+
"allowJs": true,
1112
"noUnusedLocals": true,
1213
"noImplicitReturns": true,
1314
"outDir": "lib",

yarn.lock

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8664,11 +8664,6 @@ shellwords@^0.1.1:
86648664
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
86658665
integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==
86668666

8667-
shim-keyboard-event-key@^1.0.3:
8668-
version "1.0.3"
8669-
resolved "https://registry.yarnpkg.com/shim-keyboard-event-key/-/shim-keyboard-event-key-1.0.3.tgz#9a9441d6b7d1fb25819d86182caf554e821a515c"
8670-
integrity sha512-PTNRkOxDlZ2+Xz4CbKJJsh/pe1DJdaC+b4HHV02A1aEWNmwh1g9am0ZiU/ktu3uVfQrY3yDHTOVhst3xpLhw2A==
8671-
86728667
sigmund@^1.0.1:
86738668
version "1.0.1"
86748669
resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"
@@ -9432,10 +9427,10 @@ typedarray@^0.0.6:
94329427
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
94339428
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
94349429

9435-
typescript@^3.4.1:
9436-
version "3.4.1"
9437-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.1.tgz#b6691be11a881ffa9a05765a205cb7383f3b63c6"
9438-
integrity sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q==
9430+
typescript@^4.8.2:
9431+
version "4.8.2"
9432+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.2.tgz#e3b33d5ccfb5914e4eeab6699cf208adee3fd790"
9433+
integrity sha512-C0I1UsrrDHo2fYI5oaCGbSejwX4ch+9Y5jTQELvovfmFkK3HHSZJB8MSJcWLmCUBzQBchCrZ9rMRV6GuNrvGtw==
94399434

94409435
ua-parser-js@^0.7.18:
94419436
version "0.7.28"

0 commit comments

Comments
 (0)