Giter VIP home page Giter VIP logo

react-keybinding's Introduction

react-keybinding

build status

Declarative, lightweight, and robust keybindings mixin for React.

  • Straightforward '⌘S' string syntax for declaring bindings
  • Automatically binds & unbinds keybindings when components mount and unmount
  • Allows listing of all currently-active keybindings
  • Run a function when a keybinding is hit or pass an action to the keybinding method of that component
  • Doesn't fire keybindings accidentally triggered in inputs, select boxes, or textareas.
  • Optionally coerce platform specific keybindings (i.e. '⌘S' on Mac to '^S' on Windows)

Installation

Install with npm and use in your React projects with either browserify or webpack.

$ npm install react-keybinding

Example

var React = require('react'),
    Keybinding = require('../');
var HelloMessage = React.createClass({
  mixins: [Keybinding],
  keybindingsPlatformAgnostic: true,
  keybindings: {
    '⌘S': function(e) {
      console.log('save!');
      e.preventDefault();
    },
    '⌘C': 'COPY'
  },
  keybinding: function(event, action) {
    // event is the browser event, action is 'COPY'
    console.log(arguments);
  },
  render: function() {
    return React.createElement("div", null, "Hello");
  }
});
React.render(React.createElement(HelloMessage, {name: "John"}), document.body);

There's a runnable example in the ./examples directory: to run it,

$ npm install
$ cd example
$ npm install
$ npm start

See tmcw/ditty for an example of react-keybinding in an application.

API

This module exposes a single mixin called Keybinding.

Where you use this mixin on Components, it expects a property called keybindings of the format:

keybindings: {
  // keys are strings: they can contain meta and shift symbols,
  // numbers, strings, etc
  '⌘S': function(e) {
    // bindings can map to functions that they call directly
  },
  // or to constants that are passed to the component's
  // 'keybinding' method.
  '⌘C': 'COPY'
}

Platform agnostic keybindings will automatically listen for the 'Ctrl' equivalent of 'Cmd' keybindings, and vice-versa. To automatically coerce platform specific keybindings, provide a property called keybindingsPlatformAgnostic of the format:

keybindingsPlatformAgnostic: true,
keybindings: { ... }

The mixin provides a method for components called .getAllKeybindings(): this yields an array of all keybindings properties on all active components.

The full range of codes and modifiers supported is listed in SYNTAX.md.

Tests

$ npm test

react-keybinding's People

Contributors

aristidesstaffieri avatar dlindenkreuz avatar gregeinfrank avatar scothis avatar tmcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-keybinding's Issues

advice on how to implement a more complex arrangement

Hi, I am sorry if this was elaborated on somewhere and I missed it.

I have what I think is a typical use case for a library like this. A single page application that uses react-router.I have some global key bindings, and then some keybindings which may only be relevant when that page's component is mounted / active

For the purposes of discussion, say it looks like:

- App keybindings: {"ctrl+s": "SAVE"}
  - Router
    - Page
    - Page
    - Page keybindings: {"ctrl+s": "SPECIAL_SAVE" }

Should I not attempt to implement globals and just push all of the keybinding config into the components, and then have the components handle using the global behavior manually in my handler definition?

Please advise. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.