1.0.10 • Published 1 year ago

react-keyboard v1.0.10

Weekly downloads
30
License
MIT
Repository
github
Last release
1 year ago

Introduction

react-keyboard is wrap of mousetrap.js in React, it offers keyboard shortcuts handling in React application.

react-keyboard

Getting started

Install

npm install react-keyboard

Usage Example

Defined keyMap

const keyMap = {
  cmdK: {
    combo: 'command+k',
    eventType: 'keyup',
  },
  deleteNode: ['del', 'backspace'],
  left: 'left',
}

A KeyMap is an object which value is the key sequence. The key sequence can be:

  1. string which can be a single key left or combination of keys command+k
  2. array which is an array of multiple key commands: ['del', 'backspace']
  3. object only use an object if you need to listen to specific event type: {combo: 'command+k', eventType: 'keyup'}

Use HotKeys Component

import { HotKeys, Handlers } from 'react-keyboard'

export class MyHotKeys extends React.Component {

  showDocumentation = () => {
    console.log('show doc')
  }
  deleteNode = () => {
    console.log('deleted')
  }
  moveLeft = () => {
    console.log('move left')
  }
  showChildDocumentation = () => {
    console.log('show child doc')
  }

  handlersParent = {
    cmdK: this.showDocumentation,
    deleteNode: this.deleteNode,
  }

  handlersChild = {
    cmdK: this.showChildDocumentation,
    left: this.moveLeft,
  }

  render() {
    return <HotKeys keyMap={keyMap} handlers={this.handlersParent}>
      <span>this is my hotkeys</span>
      <HotKeys handlers={this.handlersChild}>A child</HotKeys>
    </HotKeys>
  }
}

Note: Child HotKeys components can inherit keyMap from their parents. You don't necessarily define keyMap for each child if parents already have the shortcuts you need.

1.0.10

1 year ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-beta11

5 years ago

1.0.0-beta10

5 years ago

1.0.0-beta9

5 years ago

1.0.0-beta8

5 years ago

1.0.0-beta7

5 years ago

1.0.0-beta5

5 years ago

1.0.0-beta4

6 years ago

1.0.0-beta3

6 years ago

1.0.0-beta2

6 years ago

1.0.0-beta1

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago