
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-shortcuts
Advanced tools
Manage keyboard shortcuts from one place.
Managing keyboard shortcuts can sometimes get messy. Or always, if not implemented the right way.
Real problems:
addEventListeners
, removeEventListeners
, ...)With react-shortcuts
you can declaratively manage shortcuts for each one of your React components.
Important parts of React Shortcuts:
keymap
definitionShortcutManager
which handles keymap
<Shortcut>
component for handling shortcutsDEMO: http://avocode.github.io/react-shortcuts/
npm install react-shortcuts
Create a new JS, Coffee, JSON or CSON file wherever you want (which probably is your project root). And define the shortcuts for your React component.
Keymap definition
{
"Namespace": {
"Action": "Shortcut",
"Action_2": ["Shortcut", "Shortcut"],
"Action_3": {
"osx": "Shortcut",
"windows": ["Shortcut", "Shortcut"],
"linux": "Shortcut"
}
}
}
Namespace
should ideally be the component’s displayName
.Action
describes what will be happening. For example MODAL_CLOSE
.Keyboard shortcut
can be a string, array of strings or an object which
specifies platform differences (Windows, OSX, Linux). The
shortcut may be composed of single keys (a
, 6
,…), combinations
(command+shift+k
) or sequences (up up down down left right left right B A
).Mousetrap is used under the hood for handling the shortcuts. Read more about how you can specify keys.
keymap
definition (in CoffeeScript):module.exports =
TodoItem:
MOVE_LEFT: 'left'
MOVE_RIGHT: 'right'
MOVE_UP: ['up', 'w']
COPY:
osx: 'command+c'
windows: 'ctrl+c'
linux: 'ctrl+c'
Save this file as keymap.[js|coffee|json|cson]
and require it into your main
file.
keymap = require './keymap'
Define your keymap in whichever supported format but in the end it must be an
object. ShortcutsManager
can’t parse JSON and will certainly not be happy
about the situation.
keymap = require './keymap'
ShortcutsManager = require 'react-shortcuts'
shortcutManager = new ShortcutsManager(keymap)
# Or like this
shortcutManager = new ShortcutsManager()
shortcutManager.setKeymap(keymap)
shortcutManager
into getChildContext of some parent component. So that <shortcuts>
can receive it.App = React.createClass
displayName: 'App'
childContextTypes:
shortcuts: React.PropTypes.object.isRequired
getChildContext: ->
shortcuts: shortcutManager
You need to require the component in the file you want to use shortcuts in.
For example <TodoItem>
.
Shortcuts = require `react-shortcuts/component`
TodoItem = React.createClass
displayName: 'TodoItem'
_handleShortcuts: (action, event) ->
switch action
when 'MOVE_LEFT' then console.log('moving left')
when 'MOVE_RIGHT' then console.log('moving right')
when 'MOVE_UP' then console.log('moving up')
when 'COPY' then console.log('copying stuff')
render: ->
div className: 'todo-item',
Shortcuts
name: @constructor.displayName
handler: @_handleShortcuts,
div null,
'Buy some milk'
The
<Shortcuts>
component creates a<shortcuts>
element in HTML, binds listeners and adds tabIndex to the element so that it’s focusable._handleShortcuts
is invoked when some of the defined shortcuts fire.
<Shortcuts>
componenthandler
: func.isRequiredname
: string.isRequiredtabIndex
: number
-1
className
: stringeventType
: string
stopPropagation
: boolpreventDefault
: booltargetNode
: DOM Node
isGlobal
: bool
CMD+Q
.This library is inspired by Atom Keymap.
FAQs
React shortcuts
The npm package react-shortcuts receives a total of 13,119 weekly downloads. As such, react-shortcuts popularity was classified as popular.
We found that react-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.