Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A method to manage keyboard shortcuts in react

Keyboard shortcuts are a bit tricky to manage in web applications.

consider a Widget component.

I want to be able to focus certain elements, and run functions on this component, based on keyboard shorcuts.

class Widget extends React.Component {
  componentDidMount() {
    this.setBindings()
  },
  componentWillUnmount() {
    this.removeBindings();
  }
} 

setBindings and removeBindings, would use a library like mousetrap to bind specific keyboard shortcuts

Now, there's two problems with the above solution:

  1. It makes keyboard shortcut behavior unpredictable
    • Consider the case where two Widgets mount, one would override the other
  2. Widget becomes tightly coupled with the shortcuts -- now if someone doesn't want to use shortcuts, they have to have some sort of flag on Widget. This destroy's the 'granularity' of the code -- ideally a user should be able to use Widget, then WidgetWithShortcuts, or something like this

Another potential solution, is to pass an instance

const widgetShortcuts = (widgetInstance) => {
  return {
   'ctrl i': () => widgetInstance.focusInput(),
  }
}

The problem with the second solution is:

  1. widgetInstance will have to expose a lot of publicly accessible methods, like focusSomeThing, or invokeProp, etc

  2. if Widget wants to have some tooltip, that shows the keyboard shortcuts at certain places, the info about the keyboard shorcuts will be duplicated in different places. It will become possible to change the shortcuts in one place, and forget to do so in another places

Is there a best practice, or some ideas on how keyboard shortcuts can be implemented with solutions to the problems above?

like image 896
Stepan Parunashvili Avatar asked Sep 20 '16 21:09

Stepan Parunashvili


People also ask

How do you implement keyboard shortcuts in react?

Simply replace the console. log by the action you want on key press and you'll have a fully functioning keyboard shortcut in your app.

How do I manage keyboard shortcuts?

To assign a keyboard shortcut do the following: Begin keyboard shortcuts with CTRL or a function key. In the Press new shortcut key box, press the combination of keys that you want to assign. For example, press CTRL plus the key that you want to use.

How do you handle keyboard events in react JS?

The onKeyPress event in ReactJS occurs when the user presses a key on the keyboard but it is not fired for all keys e.g. ALT, CTRL, SHIFT, ESC in all browsers. To use the onKeyPress event in ReactJS we will use the predefined onKeyPress method.

What is control's keyboard?

Alternatively referred to as Control+S, ^s, and C-s, Ctrl+S is a keyboard shortcut most often used to save changes to a file. Tip. On Apple computers, the keyboard shortcut used to save is Command + S .


2 Answers

React-hotkeys is not a method, but a tool that sounds like it would solve the problem you're facing.

Some things to note:

  • You use it by wrapping your component in a custom <HotKeys> component that has the hotkeys attached to it.
  • Methods aren't made public, but are instead assigned to hotkeys through a keymap & handler
  • Hotkeys added to child-components will take priority over parent component hotkeys

One issue I'm facing with this plugin is that there doesn't seem to be a way to keep hotkeys from firing when typing in an input field, but other than that it seems to work pretty great.

like image 85
Zach Avatar answered Sep 29 '22 12:09

Zach


I think your best bet is setup your keyboard shortcut listener once at the top level and pass down info to components who may or may not care that a shortcut happened. This solves problem 1 where you may bind listeners more than once, and this also precludes the need to expose any component functions.

class ShortcutProvider extends Component {
   state = { shortcut: null }

   componentDidMount() {
     // shortcut library listener
     onShortcut(shortcut => this.setState({ shortcut })
   }

   render() {
     <App shortcut={this.state.shortcut} />
   }
}

Then your widget can react (or not react) to the prop changes:

class Widget extends Component {
  ...

  componentWillReceiveProps(nextProps) {
    if (this.state.shouldReactToShortcut) {
      if (nextProps.shortcut === 'ctrl i') {
        // do something
      }
    }
  }

  ...
}

If you're passing the shortcut prop down many components it may be worth it to put the shortcut state into context.

like image 31
azium Avatar answered Sep 29 '22 12:09

azium