keyboard-shortcut-component
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

KeyboardShortcut Component

tests GitHub

A handy React component that listens for keyboard combinations (hotkeys) when mounted.

Getting Started

Install

npm install keyboard-shortcut-component

Usage

Render <KeyboardShortcut /> component when you want to listen to keyboard shortcuts:

import { KeyboardShortcut } from 'keyboard-shortcut-component';

export function Component() {
  const [showDialog, setShowDialog] = useState(false);
  const openDialog = () => setShowDialog(true);
  const closeDialog = () => setShowDialog(false);
  return (
    <>
      <KeyboardShortcut combination="shift+k" onKeyDown={openDialog} />
      {showDialog ? (
        <>
          <KeyboardShortcut combination="esc" onKeyDown={closeDialog} />
          <SomeDialog />
        </>
      ) : null}
    </>
  );
}

API

<KeyboardShortcut
  combination="alt+shift+space"
  onKeyDown={(event: KeyboardEvent, combination: string) => void}
  disabled={false}
/>

Props

interface Props {
  combination: string;
  onKeyDown: (event: KeyboardEvent, combination: string) => void;
  disabled?: boolean;
}

Credits

This library uses the awesome minimalistic is-hotkey package under the hood.

Package Sidebar

Install

npm i keyboard-shortcut-component

Weekly Downloads

26

Version

1.0.1

License

MIT

Unpacked Size

24.8 kB

Total Files

18

Last publish

Collaborators

  • everdimension