Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-hotkeys

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-hotkeys

A declarative library for handling hotkeys and focus within a React application

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
243K
decreased by-16.66%
Maintainers
2
Weekly downloads
 
Created

What is react-hotkeys?

The react-hotkeys package allows developers to add keyboard shortcuts to their React applications. It provides a simple and declarative way to define and manage hotkeys, making it easier to enhance user interactions through keyboard navigation.

What are react-hotkeys's main functionalities?

Defining Global Hotkeys

This feature allows you to define global hotkeys that can be triggered from anywhere within the application. The `GlobalHotKeys` component takes a `keyMap` object that maps action names to key combinations and a `handlers` object that maps action names to handler functions.

import { GlobalHotKeys } from 'react-hotkeys';

const keyMap = {
  'SAVE': 'ctrl+s',
  'OPEN': 'ctrl+o'
};

const handlers = {
  'SAVE': (event) => { console.log('Save action triggered'); },
  'OPEN': (event) => { console.log('Open action triggered'); }
};

function App() {
  return (
    <div>
      <GlobalHotKeys keyMap={keyMap} handlers={handlers} />
      <h1>My Application</h1>
    </div>
  );
}

export default App;

Defining Component-Specific Hotkeys

This feature allows you to define hotkeys that are specific to a particular component. The `HotKeys` component works similarly to `GlobalHotKeys` but is scoped to the component it wraps.

import { HotKeys } from 'react-hotkeys';

const keyMap = {
  'DELETE': 'del'
};

const handlers = {
  'DELETE': (event) => { console.log('Delete action triggered'); }
};

function MyComponent() {
  return (
    <HotKeys keyMap={keyMap} handlers={handlers}>
      <div>
        <p>Press the delete key to trigger an action.</p>
      </div>
    </HotKeys>
  );
}

export default MyComponent;

Using Key Sequences

This feature allows you to define key sequences (e.g., 'ctrl+c' for copy) that trigger specific actions. The `HotKeys` component can be used to wrap elements like text areas to provide keyboard shortcuts for common actions.

import { HotKeys } from 'react-hotkeys';

const keyMap = {
  'COPY': 'ctrl+c',
  'PASTE': 'ctrl+v'
};

const handlers = {
  'COPY': (event) => { console.log('Copy action triggered'); },
  'PASTE': (event) => { console.log('Paste action triggered'); }
};

function TextEditor() {
  return (
    <HotKeys keyMap={keyMap} handlers={handlers}>
      <textarea placeholder="Type something here..." />
    </HotKeys>
  );
}

export default TextEditor;

Other packages similar to react-hotkeys

Keywords

FAQs

Package last updated on 12 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc