All Projects → ayrton → React Key Handler

ayrton / React Key Handler

React component to handle keyboard events 🔑

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Key Handler

React Absolute Grid
An absolutely positioned, animated, filterable, sortable, drag and droppable, ES6 grid for React.
Stars: ✭ 910 (+135.75%)
Mutual labels:  react-components, react-component, reactjs-components
react-circle-flags
🚀 A React component with a collection of 300+ minimal circular SVG country flags. Wrapper of HatScripts/circle-flags
Stars: ✭ 29 (-92.49%)
Mutual labels:  react-component, react-components
delete-react-zombies
CLI to search and delete unimported 🧟components in your react ⚛️ files
Stars: ✭ 70 (-81.87%)
Mutual labels:  react-component, react-components
reactjs-portfolio
Welcome to my portfolio react.js repository page.
Stars: ✭ 109 (-71.76%)
Mutual labels:  react-component, react-components
fluent-windows
🌈 React components that inspired by Microsoft's Fluent Design System.
Stars: ✭ 122 (-68.39%)
Mutual labels:  react-component, react-components
react-crud-icons
57 SVG icons for CRUD applications, packaged as a React component with light & dark themes and tooltip.
Stars: ✭ 19 (-95.08%)
Mutual labels:  react-component, react-components
light-ui
A lightly React UI library
Stars: ✭ 38 (-90.16%)
Mutual labels:  react-component, react-components
best-of-react
🏆 A ranked list of awesome React open-source libraries and tools. Updated weekly.
Stars: ✭ 364 (-5.7%)
Mutual labels:  react-component, react-components
React Event Components
🛰 A set of React components designed to handle global events (interval, keyboard, touch, mouse, etc)
Stars: ✭ 271 (-29.79%)
Mutual labels:  keyboard, react-components
React Hotkeys
React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts.
Stars: ✭ 279 (-27.72%)
Mutual labels:  keyboard, react-component
Eo Locale
🌏Internationalize js apps 👔Elegant lightweight library based on Internationalization API
Stars: ✭ 290 (-24.87%)
Mutual labels:  react-components, reactjs-components
tiny-ui
⚛️ A friendly UI component set for React.js
Stars: ✭ 202 (-47.67%)
Mutual labels:  react-component, react-components
git-issue-react-electronjs
⚙️. ⚛️. A desktop application created with Electronjs and Reactjs to be cross-platform to manage and track GitHub issues.
Stars: ✭ 21 (-94.56%)
Mutual labels:  react-component, react-components
animation-wrapper-view
Declarative animations with imperative controls for RN/RNW.
Stars: ✭ 53 (-86.27%)
Mutual labels:  react-component, react-components
Registration-and-Login-using-MERN-stack
Simple Registration and Login component with MERN stack
Stars: ✭ 210 (-45.6%)
Mutual labels:  react-component, react-components
react-native-value-picker
Cross-Platform iOS(ish) style picker for react native.
Stars: ✭ 18 (-95.34%)
Mutual labels:  react-component, react-components
React Js Pagination
Stars: ✭ 308 (-20.21%)
Mutual labels:  react-components, react-component
React-Jupyter-Viewer
A react component to embed .ipyb notebooks in a blog or something
Stars: ✭ 50 (-87.05%)
Mutual labels:  react-component, react-components
shared-react-components-example
An example of a mono-repository of shared React components libraries!
Stars: ✭ 85 (-77.98%)
Mutual labels:  react-component, react-components
Flawwwless Ui
Flawwwless ui library for React.js.
Stars: ✭ 265 (-31.35%)
Mutual labels:  react-components, react-component

react-key-handler 🔑

npm version License Build Status

React component to handle keyboard events (such as keyup, keydown & keypress).

Testimonials

“Happy to see that react-key-handler is SSR safe 👍”
[Veselin Todorov](https://github.com/vesln), Chai.js core

Table of Contents

  1. Installation
  2. Usage
    1. Higher-order Components
    2. Component
    3. Form key handling
  3. Key event names
  4. keyValue, code and keyCode
  5. Development
    1. Setup
    2. Getting started
    3. Tests
  6. Contributing
  7. License

Installation

$ npm install react-key-handler --save

Usage

You can use react-key-handler library in two flavours:

Higher-order Components

This library includes two similar higher-order components, but with a different puprose:

Higher-order Component Purpose
keyHandler Handles key changes
keyToggleHandler Handles key toggles

Both have the same API and will decorate the given component with a keyValue, code and keyCode property.

Internally the KeyHandler component is used, for a full understanding be sure to check out the implementation.

import React from 'react';
import { keyHandler, KEYPRESS } from 'react-key-handler';

function Demo({ keyValue }) {
  return (
    <div>
      {keyValue === 's' && (
        <ol>
          <li>hello</li>
          <li>world</li>
        </ol>
      )}
    </div>
  );
}

export default keyHandler({ keyEventName: KEYPRESS, keyValue: 's' })(Demo);

The prop types of the KeyHandler component are:

Name Type Required Default
keyEventName string no 'keyup' 'keydown', 'keypress' or 'keyup'
keyValue string yes * Any given KeyboardEvent.key
code string yes * Any given KeyboardEvent.code
keyCode† number yes * Any given KeyboardEvent.keyCode

* You should pass at least one of these props.

Note that the keyCode is frequently browser specific and has therefore be set as deprecated, see MDN for details.

Examples

Component

import React from 'react';
import KeyHandler, { KEYPRESS } from 'react-key-handler';

export default class Demo extends React.Component {
  state = { showMenu: false };

  render() {
    const { showMenu } = this.state;

    return (
      <React.Fragment>
        <KeyHandler
          keyEventName={KEYPRESS}
          keyValue="s"
          onKeyHandle={this.toggleMenu}
        />

        {showMenu && (
          <ol>
            <li>hello</li>
            <li>world</li>
          </ol>
        )}
      </React.Fragment>
    );
  },

  toggleMenu = (event) => {
    event.preventDefault();

    this.setState({ showMenu: !this.state.showMenu });
  };
}

The prop types of the KeyHandler component are:

Name Type Required Default
keyEventName string no 'keyup' 'keydown', 'keypress' or 'keyup'
keyValue string yes * Any given KeyboardEvent.key
code string yes * Any given KeyboardEvent.code
keyCode† number yes * Any given KeyboardEvent.keyCode
onKeyHandle function yes Function that is called when they key is handled

* You should pass at least one of these props.

Note that the keyCode is frequently browser specific and has therefore be set as deprecated, see MDN for details.

Example

Form key handling

This library does not handle key events for form elements such as <input /> and <textarea />.

React does a fine job supporting these already via keyboard events.

Examples

Key event names

TODO: explain the differences between the different key events.

keyValue, code and keyCode

The three available key events are

  • keyValue This corresponds to the true value. This is the value of the key pressed by the user while taking into considerations the state of modifier keys such as the shiftKey as well as the keyboard locale/layout
  • code This corresponds to the physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value which isn't altered by keyboard layout or the state of the modifier keys. The value is a string specific to the key, e.g. 'Digit0'
  • keyCode This is similar to code but numeric and also deprecated.

We recommend you to use the new Web standard KeyboardEvent.key or the KeyboardEvent.code over the deprecated KeyboardEvent.keyCode.

Note that in React key is a reserved property, and thus we use keyValue when referring to the key property.

Browser support:

There's no need to worry about browser support because internally we normalize deprecated HTML5 keyValue values and translate from legacy keyCode values, similar to how React does this for their SyntheticKeyboardEvent.

More information:

W3C Working Draft.

Development

Setup

$ git clone <this repo>
$ cd react-key-handler
$ npm install

Getting started

To start the server:

$ npm demo

This starts a development server, which will automatically rebuild the demo app as you change files and supports hot module replacement for fast development:

$ open http://localhost:1234

Tests

To run all tests:

$ npm test

Or you can run the linters, unit tests and check for type errors individually:

$ npm run test:lint
$ npm run test:unit
$ npm run test:flow

Contributing

Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

 _________________
< The MIT License >
 -----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].