项目作者: avocode

项目描述 :
Manage keyboard shortcuts from one place
高级语言: JavaScript
项目地址: git://github.com/avocode/react-shortcuts.git
创建时间: 2015-08-21T09:09:16Z
项目社区:https://github.com/avocode/react-shortcuts

开源协议:MIT License

下载


React Shortcuts

Manage keyboard shortcuts from one place.

Build Status

Intro

Managing keyboard shortcuts can sometimes get messy. Or always, if not implemented the right way.

Real problems:

  • You can’t easily tell which shortcut is bound to which component
  • You have to write a lot of boilerplate code (addEventListeners, removeEventListeners, …)
  • Memory leaks are a real problem if components don’t remove their listeners properly
  • Platform specific shortcuts is another headache
  • It’s more difficult to implement feature like user-defined shortcuts
  • You can’t easily get allthe application shortcuts and display it (e.g. in settings)

React shortcuts to the rescue!

With react-shortcuts you can declaratively manage shortcuts for each one of your React components.

Important parts of React Shortcuts:

  • Your keymap definition
  • ShortcutManager which handles keymap
  • <Shortcut> component for handling shortcuts

Try online demo

Edit l40jjo48nl

Quick tour

1. npm install react-shortcuts

2. Define application shortcuts

Create a new JS, Coffee, JSON or CSON file wherever you want (which probably is your project root). And define the shortcuts for your React component.

Keymap definition

  1. {
  2. "Namespace": {
  3. "Action": "Shortcut",
  4. "Action_2": ["Shortcut", "Shortcut"],
  5. "Action_3": {
  6. "osx": "Shortcut",
  7. "windows": ["Shortcut", "Shortcut"],
  8. "linux": "Shortcut",
  9. "other": "Shortcut"
  10. }
  11. }
  12. }
  • 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,…), combinations
    (command+shift+k) or sequences (up up down down left right left right B A).

Combokeys is used under the
hood for handling the shortcuts. Read more about how you can
specify keys.

Example keymap definition:
  1. export default {
  2. TODO_ITEM: {
  3. MOVE_LEFT: 'left',
  4. MOVE_RIGHT: 'right',
  5. MOVE_UP: ['up', 'w'],
  6. DELETE: {
  7. osx: ['command+backspace', 'k'],
  8. windows: 'delete',
  9. linux: 'delete',
  10. },
  11. },
  12. }

Save this file as keymap.[js|coffee|json|cson] and require it into your main
file.

  1. import keymap from './keymap'

3. Rise of the ShortcutsManager

Define your keymap in whichever supported format but in the end it must be an
object. ShortcutsManager can’t parse JSON and will certainly not be happy
about the situation.

  1. import keymap from './keymap'
  2. import { ShortcutManager } from 'react-shortcuts'
  3. const shortcutManager = new ShortcutManager(keymap)
  4. // Or like this
  5. const shortcutManager = new ShortcutManager()
  6. shortcutManager.setKeymap(keymap)

4. Include shortcutManager into getChildContext of some parent component. So that <shortcuts> can receive it.

  1. class App extends React.Component {
  2. getChildContext() {
  3. return { shortcuts: shortcutManager }
  4. }
  5. }
  6. App.childContextTypes = {
  7. shortcuts: PropTypes.object.isRequired
  8. }

5. Require the component

You need to require the component in the file you want to use shortcuts in.
For example <TodoItem>.

  1. import { Shortcuts } from `react-shortcuts`
  2. class TodoItem extends React.Component {
  3. _handleShortcuts = (action, event) => {
  4. switch (action) {
  5. case 'MOVE_LEFT':
  6. console.log('moving left')
  7. break
  8. case 'MOVE_RIGHT':
  9. console.log('moving right')
  10. break
  11. case 'MOVE_UP':
  12. console.log('moving up')
  13. break
  14. case 'COPY':
  15. console.log('copying stuff')
  16. break
  17. }
  18. }
  19. render() {
  20. return (
  21. <Shortcuts
  22. name='TODO_ITEM'
  23. handler={this._handleShortcuts}
  24. >
  25. <div>Make something amazing today</div>
  26. </Shortcuts>
  27. )
  28. }
  29. }

The <Shortcuts> component creates a <shortcuts> element in HTML, binds
listeners and adds tabIndex to the element so that it’s focusable.
_handleShortcuts is invoked when some of the defined shortcuts fire.

Custom props for <Shortcuts> component

  • handler: func
    • callback function that will fire when a shortcut occurs
  • name: string
    • The name of the namespace specified in keymap file
  • tabIndex: number
    • Default is -1
  • className: string
  • eventType: string
    • Just for gourmets (keyup, keydown, keypress)
  • stopPropagation: bool
  • preventDefault: bool
  • targetNodeSelector: DOM Node Selector like body or .my-class
    • Use this one with caution. It binds listeners to the provided string instead
      of the component.
  • global: bool
    • Use this when you have some global app wide shortcuts like CMD+Q.
  • isolate: bool
    • Use this when a child component has React’s key handler (onKeyUp, onKeyPress, onKeyDown). Otherwise, React Shortcuts stops propagation of that event due to nature of event delegation that React uses internally.
  • alwaysFireHandler: bool
    • Use this when you want events keep firing on the focused input elements.

Thanks, Atom

This library is inspired by Atom Keymap.