# @kovarjan/react-shortcut-manager

> Keyboard shortcuts for React

Latest version **2.0.4** (published 2025-03-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @kovarjan/react-shortcut-manager
pnpm add @kovarjan/react-shortcut-manager
yarn add @kovarjan/react-shortcut-manager
bun add @kovarjan/react-shortcut-manager
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2025-03-04 |
| First published | 2023-08-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 55.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Hadeeb Farhan |
| Maintainers | kovarjan |
| Keywords | react, keyboard, shortcuts |

## Links

- npm: https://www.npmjs.com/package/@kovarjan/react-shortcut-manager
- Repository: https://github.com/kovarjan/react-shortcut-manager
- Homepage: https://github.com/kovarjan/react-shortcut-manager#readme
- Issues: https://github.com/kovarjan/react-shortcut-manager/issues
- npm.io page: https://npm.io/package/@kovarjan/react-shortcut-manager

## Dependencies (2)

- [invariant](https://npm.io/package/invariant.md) ^2.2.4
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.1.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.4 (latest) — 2025-03-04
- 2.0.3 — 2023-08-01

## README

# React Shortcuts - with React 18 support

React Keyboard shortcuts using React's synthetic events.

## Getting Started

Install `react-shortcut-manager`

```bash
npm i @kovarjan/react-shortcut-manager
```

https://www.npmjs.com/package/@kovarjan/react-shortcut-manager

## Define shortcuts

### Keymap definition

```json
{
  "Namespace": {
    "Action": "Shortcut",
    "Action_2": ["Shortcut", "Shortcut"],
    "Action_3": {
      "osx": "Shortcut",
      "windows": ["Shortcut", "Shortcut"],
      "linux": "Shortcut",
      "other": "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, other). The
  shortcut may be composed of single keys (`a`, `6`,…) or combinations of a key and modifiers (`command+shift+k`).

#### Example `keymap` definition

```javascript
export default {
  TODO_ITEM: {
    MOVE_LEFT: "left",
    MOVE_RIGHT: "right",
    MOVE_UP: ["up", "w"],
    DELETE: {
      osx: ["command+backspace", "k"],
      windows: "delete",
      linux: "delete"
    }
  }
};
```

## Example

```js
import { ShortcutProvider } from "react-shortcut-manager";

const keymap = {
  TODO_LIST: {
    CLEAR_ALL: "ctrl+del",
    SHOW_ALL: "shift+a"
  }
};

class App extends React.Component {
  render() {
    return (
      <ShortcutProvider shortcuts={keymap}>
        <RootComponent />
      </ShortcutProvider>
    );
  }
}
```

```js
import { Shortcuts } from "react-shortcut-manager";

class TodoList extends React.Component{
  handleShortcuts(action,event){
    switch(action){
      case 'CLEAR_ALL':
        ...
        break;
      case 'SHOW_ALL':
        ...
        break;
      default:
    }
  }
  render(){
    return(
      <Shortcuts
       name="TODO_LIST"
       handler={this.handleShortcuts}>
        <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.<br/>Receieves 2 parameters<br/><ul><li>`action:String` : The action being fired</li><li>`event:KeyboardEvent`:The keyboard event invoked the action</li></ul> |
| global          | false         | Whether the current shortcuts are global or not                                                                                                                                                       |
| headless        | false         | Applicable only when `global=true`<br/>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<br/>\*Can be done in handler function also                                                                                                |
| preventDefault  | false         | Whether to `preventDefault` for all of the current actions<br/>\*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

[`react-shortcuts`]: https://github.com/avocode/react-shortcuts

---
_Source: https://npm.io/package/@kovarjan/react-shortcut-manager · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
