
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
react-keyboard-shortcuts
Advanced tools
A declarative library for handling hotkeys based on explicit priority in React applications
A declarative library for handling hotkeys based on explicit priority in React applications.
Most hotkey or shortcut managing libraries follow the focus tree for event handling. Meaning that the element that has focus tries its handler and if it does not process the hotkey allows its parent to try and so on up do the document root. However, there are many cases where one element has focus but another has the handler but they are not in each other linage.
+ root
|
+---+--------+
| |
| |
+---+---+ +
| | Text
| |
+ +
Back Next
For example, let say that you have a text element that currently have focus but you want the hotkeys alt+b and alt+n to work for the back and next buttons. These two buttons are not in the focus tree but are cousins. You could in those other libraries set the handlers on the root, but that often means that the root either has to have those same redux actions or has to have a reference to the next and back button to make those handlers work. Both are messy. Instead react-keyboard-shortcuts allows the Back and Next button to register hotkeys that work globally (regardless of what element has focus).
When the Back and Next buttons mount their hotkeys are register. When they unmount their hotkeys are unregister (via a higher-order-component). Also if two or more components want to have handlers for the same sequence they each provide a priority explicitly that determines who gets first chance (think of it like the z-index for css).
npm install react-keyboard-shortcuts
yarn add react-keyboard-shortcuts
import React from 'react'
import {hotkeys} from 'react-keyboard-shortcuts'
class NextButton extends React.PureComponent {
static propTypes = {
onClick: PropTypes.func.isRequired,
}
hot_keys = {
'alt+n': { // combo from mousetrap
priority: 1,
handler: (event) => this.props.onClick(),
},
}
render () {
return (
<button onClick={this.props.onClick}> Next</button>
)
}
}
export default hotkeys(NextButton)
hotkeys([component], [options = {}])
hot_key_property_name
- defaults to hot_keys
connect(mapStateToProps, actions)(hotkeys(NextButton))
import {hotkey_display} from 'react-keyboard-shortcuts'
const tooltip = hotkey_display('alt+n') // on a mac this will return 'option'
const tooltip = hotkey_display('meta+n') // on a mac this will return '⌘'
Thanks to @ccampbell for Mousetrap
MIT
1.1.3 (2018-03-22)
<a name="1.1.2"></a>
FAQs
A declarative library for handling hotkeys based on explicit priority in React applications
The npm package react-keyboard-shortcuts receives a total of 668 weekly downloads. As such, react-keyboard-shortcuts popularity was classified as not popular.
We found that react-keyboard-shortcuts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.