1.0.1 • Published 3 years ago
react-keypress-shortcuts v1.0.1
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-shortcutsUse
- Import
ShortcutContextProviderfromreact-keypressand wrap around youAppcomponent
import { ShortcutContextProvider } from "react-keypress-shortcuts";
...
<ShortcutContextProvider>
<App />
</ShortcutContextProvider>
...- To register a shortcut for your component, import
KeyboardShortcutand use it like this
<KeyboardShortcut
combo="ctrl d"
description="Description that you want to provide"
callback={() => console.log("You clicked ctrl d")}
/>KeyboardShortcutaccepts 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
KeyboardShortcutcomponent as above with different comboTo get the list of all shortcuts registered, import
ShortcutContextand use it as a context. This context provides aactiveShortcutsvariable
import { ShortcutContext } from "react-keypress-shortcuts";
...
const { activeShortcuts } = useContext(ShortcutContext);