Easily add global hotkeys/shortcuts to your React app
With npm:
$ npm install --save react-shortcutOr with Yarn:
$ yarn add react-shortcutThe usage is very simple, there is just a couple of props to pass.
const HotKey = require('react-shortcut');
// ...
render() {
return (
<HotKey
keys={/* Array of hotkeys */}
simultaneous={/* Add this prop if keys should be pressed all together */}
onKeysCoincide={/* Callback when target key combination is pressed */}
/>
);
}You can add react-shortcut anywhere in your component hierarchy, because it adds a global
keyboard events listener and doesn't stops any event bubbling.
For example:
const HotKey = require('react-shortcut');
const React = require('react');
const Menu = require('menu/Menu'); // Just an example
const MenuItem = require('menu/MenuItem'); // Just an example
export default class AnyYourComponent extends React.Component {
static propTypes = {
showOpenFileDialog: React.PropTypes.fn.isRequired
};
render() {
const openFileKeys = ['ctrl', 'o'];
return (
<Menu>
<MenuItem
label="Open File"
onClick={this.onFileOpen}
/>
</Menu>
<HotKey
keys={openFileKeys}
simultaneous
onKeysCoincide={this.onFileOpen}
/>
);
}
onFileOpen(keys, events) {
const {showOpenFileDialog} = this.props;
showOpenFileDialog();
}
}keys– Just array of string representing each button to be pressed;simultaneous– Set this prop if user should press buttons all together;onKeysCoincide– Callback function to be called when user pressed the target buttons.
All alphabetic letters and numbers could be passed as is, i.e. letter "a" is just "a".
If you use simultaneous mode and you have the Shift button in your hotkey combination,
please set the unmodified buttons.
For example, to have a Shift+! hotkey, you should pass keys={["shift", "1"]},
because "Shift" and "1" pressed together produce "!".
- react-easter – Easily add Easter eggs to your React app
Library has ~100% test coverage:
$ npm run test:coverage
> react-shortcut@1.0.0 test:coverage ~/projects/react-shortcut
> NODE_ENV=test jest --coverage --no-cache --config .jestrc
PASS test/Component.js
<HotKey />
✓ Calls componentDidMount (16ms)
✓ Should handle keys sequently (10ms)
✓ Should not react to events without keys (5ms)
✓ Should not react if empty keys passed (506ms)
✓ Should pass keys and events buffers (5ms)
✓ Should remove listener on unmount (6ms)
--------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
--------------|----------|----------|----------|----------|----------------|
All files | 100 | 83.33 | 100 | 100 | |
Component.js | 100 | 83.33 | 100 | 100 |... 35,41,42,66 |
--------------|----------|----------|----------|----------|----------------|
Test Suites: 1 passed, 1 total
Tests: 6 passed, 6 total
Snapshots: 0 total
Time: 2.628s
Ran all test suites.
Library is 100% compatible with airbnb-base for ES5.
Library has the following commands available:
-
Run the tests:
$ npm test -
Run the tests and display test coverage:
$ npm run test:coverage -
Run the linter:
$ npm run lint
No building required, library is implemented with ES5 React syntax for better compatibility and shipped as is.
Library is shipped "as is" under MIT License.
Feel free to contribute but don't forget to test everything properly.
