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

@bigbinary/neeto-hotkeys

v1.0.9

Published

React hook for handling keyboard shortcuts, offering a customizable and platform-aware solution, supporting global and scoped modes.

Downloads

9,404

Readme

@bigbinary/neeto-hotkeys

The neeto-hotkeys package provides the useHotKeys hook, a versatile utility for managing hotkeys in an application. This hook allows you to define specific hotkey combinations and associate them with corresponding handler functions. The associated handler is invoked upon pressing the configured hotkey(s), enabling you to execute actions in response to keyboard input.

Installation

yarn add @bigbinary/neeto-hotkeys

Arguments that the useHotKeys hook accepts:

  • hotkey: A string specifying the hotkey(s) to listen for. Hotkeys can be defined in three formats: sequential, simultaneous & single.

    1. Sequential: Any keys separated by a space are considered sequential. The handler is invoked when the user presses the keys in sequence. For example, s r means that when the user presses s followed by an r the corresponding handler will be invoked.
    2. Simultaneous: Simultaneous hotkeys require all specified keys to be pressed at the same time to trigger the handler. For example, command+shift+r means that when command, shift, r are pressed at the same time the handler will be invoked.
    3. Single: As the name indicates, this denotes single keys like r , k, return etc. Whenever r is pressed its corresponding handler will be called.

    The hotkey should be in MacOS format, the hook will take care of converting it to the users platform(eg: command -> ctrl for windows).

    If we want to bind multiple hotkeys to the same handler we can pass in an array like so useHotkeys(['a', 'b'], handler).

  • handler: The function that should be invoked when the hotkey is pressed.

    Handler will receive the original key event. This can be used to stop the default browser action like so event.preventDefault().

  • config: A config object which has 3 properties mode, unbindOnUnmount & enabled.

    1. mode: The available values for mode are default, global & scoped.
      • default: It is the default mode. Handlers will only be called if the user is outside of a textarea, input, or select element.
      • global: Handlers will be fired even if the user is inside form elements like textarea.
      • scoped: It is used for scoping a hotkey to a DOM element. You can use this option if you want to invoke the handlers only if the user is focused in a specific div, button, textarea and so on. When this mode is set the hook will return a ref, you need to attached that ref to the element to which you need to scope the hotkey.
    2. unbindOnUnmount: By default its value will be true. If you don't want a handler to be unregistered when a component is unmounted then set the value to false.
    3. enabled: By default its value will be true. Setting this to false will not register the hotkey.
    4. document: This is an optional property. If you want to listen for hotkeys on an external document (e.g., an iframe), pass the reference of that document using the document property in the config object. If you do not provide this property, the hook will listen for hotkeys on the current document by default.

Return value:

  • inputRef: A ref which needs to be attached to the input element to be listened to.

Usage:

Following illustrates the usage of useHotKeys hook in implementing shortcut for Sidebar opening.

import useHotKeys from "@bigbinary/neeto-hotkeys";

// openSidebar function will only be called if the user is focused inside the textarea and performs the key combination.
const ref = useHotKeys("command+shift+r", openSidebar, {
  mode: "scoped",
});

return (
  <div>
    <div>Hello world</div>
    <textarea ref={ref}></textarea>
  </div>
);

Hotkeys are a fundamental aspect of many applications, enhancing user efficiency and interactivity. The useHotKeys hook simplifies the implementation of these hotkeys by allowing you to specify the hotkey combinations in various formats, associated handlers, and configuration options.