Declarative Keyboard Shortcuts for React

2.1.0 · abandoned · verified Tue Apr 21

React Shortcuts (version 2.1.0) is a library designed to manage keyboard shortcuts declaratively within React components. It provides a structured way to define application-wide and component-specific keymaps, which can include platform-specific variations and sequences. The library uses `mousetrap` (Combokeys) under the hood for robust key detection. Key differentiators include centralizing shortcut definitions and abstracting away boilerplate `addEventListener`/`removeEventListener` logic. However, the package is effectively abandoned, with its last release over 7 years ago and no recent development activity. It relies heavily on React's now-deprecated Legacy Context API (`getChildContext`), making it incompatible with modern React versions (17 and above).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a keymap, creating a ShortcutManager, providing it via `getChildContext` in a parent component, and consuming shortcuts in a child component using the `<Shortcuts>` component. It shows basic shortcut actions and platform-specific keybindings.

import React from 'react';
import PropTypes from 'prop-types';
import { ShortcutManager, Shortcuts } from 'react-shortcuts';

const keymap = {
  TODO_ITEM: {
    MOVE_LEFT: 'left',
    MOVE_RIGHT: 'right',
    MOVE_UP: ['up', 'w'],
    DELETE: {
      osx: ['command+backspace', 'k'],
      windows: 'delete',
      linux: 'delete',
      other: 'delete'
    }
  }
};

const shortcutManager = new ShortcutManager(keymap);

class App extends React.Component {
  getChildContext() {
    return { shortcuts: shortcutManager };
  }

  render() {
    return (
      <div>
        <h1>React Shortcuts Demo</h1>
        <TodoItem />
      </div>
    );
  }
}

App.childContextTypes = {
  shortcuts: PropTypes.object.isRequired
};

class TodoItem extends React.Component {
  _handleShortcuts = (action, event) => {
    switch (action) {
      case 'MOVE_LEFT':
        console.log('Moving left!');
        break;
      case 'MOVE_RIGHT':
        console.log('Moving right!');
        break;
      case 'MOVE_UP':
        console.log('Moving up!');
        break;
      case 'DELETE':
        console.log('Deleting item!');
        break;
      default:
        console.log(`Unhandled action: ${action}`);
    }
    event.preventDefault(); // Prevent default browser behavior
  };

  render() {
    return (
      <Shortcuts name='TODO_ITEM' handler={this._handleShortcuts}>
        <div style={{ padding: '20px', border: '1px solid #ccc', margin: '10px' }}>
          <h3>My Todo Item</h3>
          <p>Try pressing 'left', 'right', 'up', or 'command+backspace' (macOS) / 'delete' (Windows/Linux).</p>
          <p>Focus this area to activate shortcuts.</p>
          <input type="text" placeholder="Type here, shortcuts should be ignored" />
        </div>
      </Shortcuts>
    );
  }
}

export default App;

view raw JSON →