Gathering detailed insights and metrics for @mkljczk/react-hotkeys
Gathering detailed insights and metrics for @mkljczk/react-hotkeys
Gathering detailed insights and metrics for @mkljczk/react-hotkeys
Gathering detailed insights and metrics for @mkljczk/react-hotkeys
react-hotkeys-hook
React hook for handling keyboard shortcuts
react-hotkeys
A declarative library for handling hotkeys and focus within a React application
hotkeys-js
A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
react-hot-keys
React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts.
Declarative hotkey and focus area management for React
npm install @mkljczk/react-hotkeys
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
141 Commits
1 Branches
1 Contributors
Updated on 26 Feb 2024
Minified
Minified + Gzipped
JavaScript (80.22%)
TypeScript (19.78%)
Cumulative downloads
Total Downloads
Last day
-57.1%
3
Compared to previous day
Last week
-61.9%
59
Compared to previous week
Last month
-0.9%
534
Compared to previous month
Last year
0%
2,771
Compared to previous year
A declarative library for handling hotkeys and focus areas in React applications.
1import { HotKeys } from '@mkljczk/react-hotkeys'; 2 3// Simple "name:key sequence/s" to create a hotkey map 4 5const map = { 6 'snapLeft': 'command+left', 7 'deleteNode': ['del', 'backspace'], 8}; 9 10// Component with a key map 11 12const App = React.createClass({ 13 render() { 14 return ( 15 <HotKeys keyMap={map}> 16 <div> 17 <MyNode></MyNode> 18 <MyNode></MyNode> 19 </div> 20 </HotKeys> 21 ); 22 } 23});
1import { HotKeys } from '@mkljczk/react-hotkeys'; 2 3/** 4 * Component with hotkey handlers, which are only called when the component 5 * is within the application's 'focus tree' and prevents cascading hotkeys from 6 * being called 7 */ 8 9const MyNode = React.createClass({ 10 render() { 11 const handlers = { 12 'deleteNode': this.deleteNode, 13 }; 14 15 return ( 16 <HotKeys handlers={handlers}> 17 Node contents 18 </HotKeys> 19 ); 20 } 21});
@mkljczk/react-hotkeys
is available as a ES6 Modules through npm or yarn. It uses NODE_ENV
to determine whether to export the development or production build in your library or application.
npm install react-hotkeys --save
yarn add react-hotkeys
react-hotkeys
uses key maps to separate defining keyboard shortcuts from the actions that they trigger. This allows adding or changing hot keys in the future, without having to also update the actions in many places across your application.
Hotkey maps are simple JavaScript objects, where the keys are the names of the actions triggered and the values are a Mousetrap-supported key sequence that must be activated in order to trigger the action.
1const keyMap = { 2 'deleteNode': 'del', 3 'moveUp': 'up', 4};
You can specify multiple keys that will trigger the same action using arrays:
1const keyMap = { 2 'deleteNode': ['del', 'backspace'], 3 'moveUp': ['up', 'w'], 4};
// Single key sequence
'4'
// Special single key sequence (ie. shift is handled automagically)
'?'
// Combination sequence
'command+shift+k'
// GMail style sequences
'up down left right'
Modifier keys: shift
, ctrl
, alt
/option
, command
/meta
Special keys: backspace
, tab
, enter
, return
, capslock
, esc
, escape
, space
, pageup
, pagedown
, end
, home
, left
, up
, right
, down
, ins
, del
, and plus
Refer to Mousetrap's documentation for an exhaustive list of supported shortcuts and sequences.
react-hotkeys
tries to automatically determine the best key event (usually keypress
) to monitor based on the key sequence provided.
The object syntax and action
attribute lets you explicitly set which key event you wish to bind to:
1const keyMap = { 2 'contract': 'alt+down', 3 'commandDown': {sequence: 'command', action: 'keydown'}, 4};
The full list of valid key events is: keypress
, keydown
, and keyup
.
Key maps trigger named actions when matching keys are pressed - but do not define any behaviour. Handlers are the functions called to handle when a matching action is triggered and define how your application should respond.
Handlers may be defined in the same <HotKeys />
component as the key map:
1import { HotKeys } from '@mkljczk/react-hotkeys'; 2 3const keyMap = { 4 moveUp: 'up', 5} 6 7const handlers = { 8 'moveUp': (event) => console.log('Move up hotkey called!'), 9}; 10 11<HotKeys keyMap={keyMap} handlers={handlers}> 12 <input /> 13</HotKeys>
Or in any descendant of the <HotKeys />
component that defines the key map:
1import { HotKeys } from '@mkljczk/react-hotkeys'; 2 3const keyMap = { 4 moveUp: 'up', 5} 6 7const handlers = { 8 'moveUp': (event) => console.log('Move up hotkey called!') 9}; 10 11<HotKeys keyMap={keyMap}> 12 <div> 13 <HotKeys handlers={handlers}> 14 <input /> 15 </HotKeys> 16 </div> 17 18 <div> 19 <input /> 20 </div> 21</HotKeys>
You can also explicitly define sequences as handlers in case you want a hard-override.
1// If no named hotkey 'up' exists we assume it is a key sequence 2const handlers = { 3 'up': (event) => console.log('up key called'), 4};
Key handlers are only called under the following conditions (all must be true):
<HotKeys />
component that defines handlers
is currently in focus (or the focused prop is true)<HotKeys />
component, or one of its ancestors that is a <HotKeys />
component, defines a keyMap
that has a sequence that matches the keys being pressed<HotKeys />
component that defines handlers
has a handler that matches the action being triggered<HotKeys />
component's handler has not already been calledA more exhaustive enumration of react-hotkeys
behaviour can be found by reviewing the test suite.
In order for a hot key to be triggered, an element that is a descendent of the <HotKey />
component that defines handlers
must be in focus. It is not enough to have a descendent element of a <HotKey />
that defines a keyMap
in focus - it must be one that defines handlers
. See Managing focus in the browser for more details.
You can cause a <HotKey />
with a handlers
prop to behave as if one of its descendents is currently focused (and call any matching handlers) using the focused
prop:
1<HotKeys keyMap={this.keyMap} handlers={this.handlers} focused> 2 <input /> 3</HotKeys>
Actions start at the <HotKeys />
component that is the the closest ancestor to the element in focus and only propagate until they are handled the first time: handlers in parent <HotKeys />
components will not be called if a child has already handled it.
If you wish to support HTML4 you are limited to the following focusable elements:
<a>
<area>
<button>
<input>
<object>
<select>
<textarea>
HTML5 allows any element with a tabindex
attribute to receive focus.
If no elements have a tabindex
in a HTML document, the browser will tab between focusable elements in the order that they appear in the DOM.
If there are elements with tabindex
values greater than zero, they are iterated over first, according their tabindex
value (from smallest to largest). Then the browser tabs over the focusable elements with a 0
or unspecified tabindex
in the order that they appear in the DOM.
If any element is given a negative tabindex
, it will be skipped when a user tabs through the document. However, a user may still click or touch on that element and it can be focused programmatically (see below). By default, <Shortcuts>
elements are given a tabindex
of -1
.
To programmatically focus a DOM element, it must meet two requirements:
You can get a reference to an element using React's ref
property:
1class MyComponent extends Component { 2 componentDidUpdate(prevProps) { 3 if(!prevProps.isFocused && this.props.isFocused) { 4 this._container.focus(); 5 } 6 } 7 8 render() { 9 return ( 10 <div ref={(c) => this._container = c}> 11 My focusable content 12 </div> 13 ) 14 } 15}
You can retrieve the element that is currently focused using the following:
1document.activeElement
react-hotkeys
adds a <div />
around its children with a tabindex="-1"
to allow them to be programmatically focused. This can result in browsers rendering a blue outline around them to visually indicate that they are the elements in the document that is currently in focus.
This can be disabled using CSS similar to the following:
1div[tabindex="-1"]:focus { 2 outline: 0; 3}
If you believe you have found a bug or have a feature request, please open an issue.
If you're interested in helping out with the maintenance of @mkljczk/react-hotkeys
, open an issue or create a pull request.
All contributions are welcome and greatly appreciated - from contributors of all levels of experience.
The fork is maintained by Marcin Mikołajczak. Original credits:
All credit, and many thanks, goes to Chris Pearce for the inception of react-hotkeys
and all versions before 1.0.0
.
As of version 1.0.0
, Aleck Greenham is actively maintaining react-hotkeys
. Please be patient while he gets up to speed.
Thanks to @ccampbell for Mousetrap
No vulnerabilities found.
No security vulnerabilities found.