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-hook/hotkey

v3.1.0

Published

A React hook for invoking a callback when hotkeys are pressed

Downloads

5,998

Readme

A React hook for invoking a callback when hotkeys are pressed. This hook also provides interop between event.key and event.which - you provide a string, and the library turns it into an event.which key code if it has to.

For better TypeScript support, this library doesn't have a special syntax à la the is-hotkey package. It uses plain JS objects and your build will fail if you've included a typo.

Quick Start

import * as React from 'react'
import {useHotkey, useHotkeys} from '@react-hook/hotkey'

const OneHotkey = () => {
  const ref = React.useRef(null)
  const save = () => {}
  // creates a hotkey for Command|Control + S keys
  useHotkey(ref, ['mod', 's'], save)
  return <textarea ref={ref} />
}

const OneHotkeySingleCharacter = () => {
  const ref = React.useRef(null)
  const exit = () => {}
  // creates a hotkey for the escape key
  useHotkey(ref, 'esc', exit)
  return <textarea ref={ref} />
}

const MultipleHotkeys = () => {
  const ref = React.useRef(null)

  useHotkeys(
    // Hotkey map
    [
      [['mod', 's'], save],
      [['mod', 'p'], print],
      ['esc', blur],
      ['enter', submit],
    ]
  )

  return <textarea ref={ref} />
}

API

useHotkey(hotkey, callback)

This is a hook for creating a single hotkey.

Arguments

| Argument | Type | Required? | Description | | -------- | ---------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | target | React.RefObject<T> | T | Window | Document | null | Yes | The React ref, window, or document to add the hotkey to | | hotkey | Hotkey | Hotkey[] | Yes | When the key and all of the modifiers in a keydown event match those defined here, the callback below will be invoked. See SpecialCodes, Aliases, and Modifiers for possible keys in addition the standard keys. | | callback | HotkeyCallback | Yes | A callback that takes action on the hotkey event. |

Returns React.MutableRefObject<T>

useHotkeys(hotkeyMapping)

This is a hook for creating multiple hotkeys that respond to a singular keyboard event.

Arguments

| Argument | Type | Required? | Description | | ------------- | ---------------------------------------------------------------------------------------- | --------- | -------------------------------------------------------------------------------- | | target | React.RefObject<T> | T | Window | Document | null | Yes | The React ref, window, or document to add the hotkey to | | hotkeyMapping | [Hotkey | Hotkey[], HotkeyCallback][] | Yes | These are the same arguments defined in useHotkey, but in a mapped array form. |

Returns React.MutableRefObject<T>

Types

HotkeyCallback

type HotkeyCallback = (event: KeyboardEvent) => void

Hotkey

type Hotkey =
  | keyof SpecialCodes
  | keyof Modifiers
  | keyof Aliases
  | 1
  | 2
  | 3
  | 4
  | 5
  | 6
  | 7
  | 8
  | 9
  | 0
  | '1'
  | '2'
  | '3'
  | '4'
  | '5'
  | '6'
  | '7'
  | '8'
  | '9'
  | '0'
  | 'a'
  | 'b'
  | 'c'
  | 'd'
  | 'e'
  | 'f'
  | 'g'
  | 'h'
  | 'i'
  | 'j'
  | 'k'
  | 'l'
  | 'm'
  | 'n'
  | 'o'
  | 'p'
  | 'q'
  | 'r'
  | 's'
  | 't'
  | 'u'
  | 'v'
  | 'w'
  | 'x'
  | 'y'
  | 'z'

SpecialCodes

interface SpecialCodes {
  backspace: 8
  tab: 9
  enter: 13
  shift: 16
  control: 17
  alt: 18
  pause: 19
  capslock: 20
  escape: 27
  ' ': 32
  pageup: 33
  pagedown: 34
  end: 35
  home: 36
  arrowleft: 37
  arrowup: 38
  arrowright: 39
  arrowdown: 40
  insert: 45
  delete: 46
  meta: 91
  numlock: 144
  scrolllock: 145
  ';': 186
  '=': 187
  ',': 188
  '-': 189
  '.': 190
  '/': 191
  '`': 192
  '[': 219
  '\\': 220
  ']': 221
  "'": 222
  f1: 112
  f2: 113
  f3: 114
  f4: 115
  f5: 116
  f6: 117
  f7: 118
  f8: 119
  f9: 120
  f10: 121
  f11: 122
  f12: 123
  f13: 124
  f14: 125
  f15: 126
  f16: 127
  f17: 128
  f18: 129
  f19: 130
  f20: 131
}

Aliases

interface Aliases {
  break: 'pause'
  cmd: 'meta'
  command: 'meta'
  ctrl: 'control'
  del: 'delete'
  down: 'arrowdown'
  esc: 'escape'
  left: 'arrowleft'
  // will respond to the `command` key on a mac and
  // to the `control` key everywhere else
  mod: 'meta' | 'control'
  option: 'alt'
  return: 'enter'
  right: 'arrowright'
  space: ' '
  spacebar: ' '
  up: 'arrowup'
  windows: 'meta'
}

Modifiers

interface Modifiers {
  alt: 'altKey'
  control: 'ctrlKey'
  meta: 'metaKey'
  shift: 'shiftKey'
}

Credits

Full credit for the key code interop portion goes to Ian Storm Taylor and his library is-hotkey.

LICENSE

MIT