npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

keybound-component

v0.1.0

Published

A React higher-order component for simple, declarative keybindings

Downloads

14

Readme

keybound-component Build Status npm version codecov

This library contains a higher-order React component that you can use to wrap your own component, allowing you to easily add keybindings to your React application in a more declarative, idiomatic way.

For a simple, self-contained example, check out CountKeys.

Why keybound-component over other solutions?

All other solutions on NPM have two main issues. Either...

  1. They rely on Mixins and React.createClass({}), and while Mixins are convenient, Facebook has plans to deprecate React.createClass() which will subsequently remove the Mixin functionality, or...
  2. They rely on naming events with constants, and then switching on those event names to determine the proper action to take, which adds an unnecessary step in your event loop.

Creating a KeyboundComponent provides the most declarative, flexible, and reliable method of binding event listeners. Listeners can be added at any point in the code, whether you want listeners added on mount or not until a user interacts with a particular element on your page, all listeners are safely removed on unmount, and keybound-component's simple API keeps your code clean and modular.

registerListener

registerListener(string, function) is passed to the wrapped component as a prop, and accepts two arguments: the key command to listen to and a callback.

The first argument is a plus-delimited string indicating the relevant modifier keys, if any, and a non-modifier key to trigger the event. Unmodified keys (think delete, or GitHub's "press T to search" functionality) are also supported. Modifier keys accepted are cmd, command, ctrl, control, and shift. This allows commands such as a, cmd+a, ctrl+a, shift+a, cmd+shift+a, etc.

The second argument is a callback to be executed when the key command from the first argument is recognized. Callbacks can use all of the typical React concepts (props, state, etc.) as the callbacks are automatically bound to the wrapped component.

Currently this HOC can reliably register command/control, shift, command/control+shift, and default (no modifier) with any alphanumeric key on the keyboard. This is order agnostic, and case insensitive (i.e. 'cmd+a' === 'cmd+A' === 'a+cmd' === 'a+cMd' etc).

Both command short-hand (cmd, ctrl) and long-form names (command, control) are accepted.

For the purposes of this plugin, key commands are cross-browser, cross-OS, meaning that both cmd and ctrl will map to the "command" key on a Mac and the "control" key on a PC. This is for simplicity's sake and to ensure that users across all platforms can use key commands in the same way they would within their respective OS. Simply, 'cmd+a' === 'ctrl+a' inside of this plugin.

Usage

import React, { PropTypes } from 'react';
import KeyboundComponent from 'keybound-component';

const MyComponent = React.createClass({
  propTypes: {
    registerListener: PropTypes.func
  },

  componentDidMount() {
    this.props.registerListener('cmd+a', this.alertUser);
  },

  alertUser(e) {
    e.preventDefault();
    alert('you pressed command+a!');
  },

  render() {
    return <h2>press cmd+a</h2>;
  }
});

export default KeyboundComponent(MyComponent);