react-keypress-shortcuts
v1.0.1
Published
## Keyboard Shortcut library for React App
Downloads
2
Readme
react-keypress-shortcuts
Keyboard Shortcut library for React App
Table of contents
General info
This is a wrapper around keypress.js to add keyboard shortcuts in your react app.
Technologies
Setup
To use this package, install it using npm:
npm install react-keypress-shortcuts
Use
- Import
ShortcutContextProvider
fromreact-keypress
and wrap around youApp
component
import { ShortcutContextProvider } from "react-keypress-shortcuts";
...
<ShortcutContextProvider>
<App />
</ShortcutContextProvider>
...
- To register a shortcut for your component, import
KeyboardShortcut
and use it like this
<KeyboardShortcut
combo="ctrl d"
description="Description that you want to provide"
callback={() => console.log("You clicked ctrl d")}
/>
KeyboardShortcut
accepts 3 props
combo
- This is the key that you want to add shortcut fordescription
- Add meaningful description to the shortcutcallback
- action that you want to be performed when the shortcut is activated
If you want more than 1 shortcuts to register then simply add
KeyboardShortcut
component as above with different comboTo get the list of all shortcuts registered, import
ShortcutContext
and use it as a context. This context provides aactiveShortcuts
variable
import { ShortcutContext } from "react-keypress-shortcuts";
...
const { activeShortcuts } = useContext(ShortcutContext);