React Shortcuts
React Keyboard shortcuts using React's synthetic events.
Getting Started
Install react-shortcut-manager
npm i react-shortcut-managerORyarn add react-shortcut-manager
Define shortcuts
Keymap definition
Namespace
should ideally be the component’sdisplayName
.Action
describes what will be happening. For exampleMODAL_CLOSE
.Keyboard shortcut
can be a string, array of strings or an object which specifies platform differences (Windows, OSX, Linux, other). The shortcut may be composed of single keys (a
,6
,…) or combinations of a key and modifiers (command+shift+k
).
keymap
definition
Example TODO_ITEM: MOVE_LEFT: "left" MOVE_RIGHT: "right" MOVE_UP: "up" "w" DELETE: osx: "command+backspace" "k" windows: "delete" linux: "delete" ;
Example
; const keymap = TODO_LIST: CLEAR_ALL: "ctrl+del" SHOW_ALL: "shift+a" ; Component { return <ShortcutProvider shortcuts=keymap> <RootComponent /> </ShortcutProvider> ; }
; Component { } { return <Shortcuts name="TODO_LIST" handler=thishandleShortcuts> <TodoItem todo=.../> ... </Shortcuts> }
Props
ShortcutsProvider
Props | Default Value | Description |
---|---|---|
shortcuts | Required | An object containing keymaps for events |
withGlobals | false | Enable global shortcuts |
tabIndex | 0 | tabIndex of root div when withGlobals=true |
- When
withGlobals=true
any other prop will be passed to the root div
Shortcuts
Props | Default Value | Description |
---|---|---|
name | Required | name of the keygroup |
handler | Required | The function to handle keyboard events. Receieves 2 parameters
|
global | false | Whether the current shortcuts are global or not |
headless | false | Applicable only when global=true Whether to render a container div or not. |
tabIndex | 0 | tabIndex of the element |
stopPropagation | false | Whether to stopPropagation for all of the current actions*Can be done in handler function also |
preventDefault | false | Whether to preventDefault for all of the current actions*Can be done in handler function also |
alwaysFire | false | Whether to fire these actiona when an input like element is in focus |
- Any other prop will be passed to the root div
Notes
- Take care of tabIndex and focus style of the components
- Any similarities to
react-shortcuts
is not accidental