项目作者: dashed

项目描述 :
Bind to keyboard shortcuts in React.
高级语言: JavaScript
项目地址: git://github.com/dashed/react-combokeys.git
创建时间: 2017-09-20T22:11:17Z
项目社区:https://github.com/dashed/react-combokeys

开源协议:MIT License

下载


react-combokeys Build Status npm version

Greenkeeper badge

Bind to keyboard shortcuts in React.

NOTE: This React component library wraps combokeys which is a fork of mousetrap. combokeys handles the capturing of keyboard shortcuts/combos/keypresses.

Install

  1. $ yarn add react-combokeys
  2. # npm v5+
  3. $ npm install react-combokeys
  4. # before npm v5
  5. $ npm install --save react-combokeys

Usage

  1. // 3rd-party imports
  2. import ReactDOM from "react-dom";
  3. import React, { Component } from "react";
  4. import ComboKeys from "react-combokeys";
  5. // function as child component
  6. ReactDOM.render(
  7. <ComboKeys bind={["a", "command+shift+k"]} keyup="b" keydown="c">
  8. {({ combo }) => {
  9. return <div>{`Combo: ${combo || "none yet"}`}</div>;
  10. }}
  11. </ComboKeys>,
  12. mountNode
  13. );
  14. // render prop
  15. const render = ({ combo }) => {
  16. return <div>{`Combo: ${combo || "none yet"}`}</div>;
  17. };
  18. ReactDOM.render(
  19. <ComboKeys
  20. bind={["a", "command+shift+k"]}
  21. keyup="b"
  22. keydown="c"
  23. render={render}
  24. ></ComboKeys>,
  25. mountNode
  26. );

Props

render (optional)

An optional function that is called whenever a keyboard shortcut/combo has been detected, or when internal state for tracking keyboard shortcut/combo has changed.

It’s expected that render function returns a single React element.
This has same API semantics as React.Component.render().

If render function is given, it has precedence over any given child component:

  1. // TODO: add example

Function as child component (optional)

Same semantics as render prop function (see above).

If render function is not given, then the child component will be invoked as a function.

  1. // TODO: add example

onCombo (optional)

An optional function that is called whenever a keyboard shortcut/combo has been detected.

The onCombo function is invoked with an object argument: ({ event, combo, action }).

Credits

Some parts of code (e.g. getRootProps()) were blatantly copied from: https://github.com/paypal/downshift

Kudos to @kentcdodds and his article here, for inspiring me to create this React component library using the ideas that he (and others) have preached.

License

MIT.