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

react-keybind

v0.10.0

Published

Global keybindings for your React application

Downloads

2,396

Readme

react-keybind ⌨️

npm

A lightweight library to manage global keyboard shortcuts for your React application. Just how lightweight is it?

minified size minzipped size

Who should use this library?

  • Your application contains many components that require keyboard shortcuts
  • Your application frequently changes keyboard shortcuts depending on the screen
  • Your application needs a list of all active shortcuts on the screen
  • Your application needs a simple way to manage keyboard shortcuts

Why another keyboard shortcut library?

We wrote react-keybind with a few main goals:

  • No External Dependencies - We wanted full control over the experience and size of the library
  • No RFC/Experimental Features - We wanted to build on top of a stable API
  • TypeScript Support - We wanted to support TypeScript

Features

  • Register shortcuts for single keypresses
  • Register shortcuts for combination keypresses (e.g. ctrl+c, ctrl+alt+a)
  • Register shortcuts for keypresses held after a duration
  • Register shortcuts for sequenced keypresses (e.g. up, up, down, down, enter)
  • Creates one listener for all keyboard shortcuts - fast and lightweight!

Roadmap

  • Focus - Support executing shortcuts when a particular element is focused

Installation

$ npm i react-keybind --save

Requirements

This library uses React Context which requires React 16.3+.

TypeScript

This library utilizes TypeScript and exposes a full set of TypeScript definitions.

Usage

This library exposes a default withShortcut Higher-Order Component which is used to wrap any component that wants to utilize keyboard shortcuts.

Your main application should be wrapped in the exposed <ShortcutProvider />.

Example

import { useCallback, useEffect, useState } from 'react';
import { useShortcut } from 'react-keybind';

// Component that implements a shortcut
const MyComponent = () => {
    const {registerShortcut, unregisterShortcut} = useShortcut();
    const [state, setState] = useState({
        isSaved: false,
    });

    const save = useCallback(async (e) => {
        setState(nextState => ({
            ...nextState,
            isSaved: true,
        }));
    }, [state]);

    useEffect(() => {
        registerShortcut(save, ['ctrl+s', 'cmd+s'], 'Save', 'Save the file')
        return () => {
            unregisterShortcut(['ctrl+s', 'cmd+s'])
        }
    }, [])

    return (
        <div>
            The file is saved: {state.isSaved ? 'true' : 'false'}
        </div>
    );
}

// Root application
const App = () => (
    <ShortcutProvider>
        <MyComponent />
    </ShortcutProvider>
);

API

react-keybind exposes a small set of Components to use in your application.

<ShortcutProvider />

Initializes the main provider for shortcuts. This component should be placed at the root of your application where you want to start listening on keyboard shortcuts.

Props

| Prop | Type | Default | Description | | ------------------- | -------- | ----------- | ----------------------------------------------------------------- | | ignoreKeys | string[] | [] | Array of keys to ignore (e.g. ['shift', 'ctrl']) | | ignoreTagNames | string[] | ['input'] | Array of tagNames to ignore (e.g. ['input', 'article']) | | preventDefault | boolean | true | Call preventDefault() automatically when a shortcut is executed | | sequenceTimeout | number | 2000 | How long to wait before checking if a sequence is complete |

useShortcut()

Hook to consume shortcuts. Provides the following interface:

{
  registerShortcut: (
    method: (e?: React.KeyboardEvent<any>) => any,
    keys: string[],
    title: string,
    description: string,
    holdDuration?: number,
  ) => void;
  registerSequenceShortcut: (
    method: () => any,
    keys: string[],
    title: string,
    description: string,
  ) => void;
  shortcuts: Shortcut[];
  triggerShortcut: (key: string) => any;
  unregisterShortcut: (keys: string[]) => void;
  setEnabled: (enabled: boolean) => void;
}

Use Cases

This library was built specifically for dddice, an online platform to roll dice for tabletop roleplaying games.

The dddice platform contains many different screens than handle a wide variety of purposes. Each screen in dddice might contain several dozens of keyboard shortcuts.

Instead of managing each screen individually and keeping track of which shortcuts are used where, we simplify the process by letting components decide which shortcuts they want to define and tracking the list of active shortcuts globally. This is especially useful for rendering a quick "Shortcut Menu" for our users no matter where the user might be in the application.

We open-sourced this library in hopes that other projects might find it useful 💙

License

MIT