Coder Social home page Coder Social logo

lbdremy / react-hotkeys-hook Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johannesklauss/react-hotkeys-hook

0.0 1.0 0.0 7.18 MB

React hook for using keyboard shortcuts in components.

Home Page: https://react-hotkeys-hook.vercel.app/

License: MIT License

TypeScript 99.65% JavaScript 0.35%

react-hotkeys-hook's Introduction

react-hotkeys-hook

React hook for using keyboard shortcuts in components. This is a hook version for the hotkeys package.

Documentation & Live Examples

If you use this package please share your thoughts on how we can improve this hook with version 4. Please engage at the corresponding Github issue.

Installation

npm install react-hotkeys-hook

or

yarn add react-hotkeys-hook

Make sure that you have at least version 16.8 of react and react-dom installed, or otherwise hooks won't work for you.

Usage

export const ExampleComponent = () => {
  const [count, setCount] = useState(0);
  useHotkeys('ctrl+k', () => setCount(prevCount => prevCount + 1));
    
  return (
    <p>
      Pressed {count} times.
    </p>
  );
};

The hook takes care of all the binding and unbinding for you. As soon as the component mounts into the DOM, the key stroke will be listened to. When the component unmounts it will stop listening.

Call Signature

useHotkeys(keys: string, callback: (event: KeyboardEvent, handler: HotkeysEvent) => void, options: Options = {}, deps: any[] = [])

Parameters

  • keys: string: Here you can set the key strokes you want the hook to listen to. You can use single or multiple keys, modifier combination, etc. See this section on the hotkeys documentation for more info.
  • callback: (event: KeyboardEvent, handler: HotkeysEvent) => void: Gets executed when the defined keystroke gets hit by the user. Important: Since version 1.5.0 this callback gets memoised inside the hook. So you don't have to do this anymore by yourself.
  • options: Options = {}
    • filter: (event: KeyboardEvent): boolean is used to filter if a callback gets triggered depending on the keyboard event. Breaking Change in 3.0.0! Prior to version 3.0.0 the filter settings was one global setting that applied to every hook. Since 3.0.0 this behavior changed. The filter option is now locally scoped to each call of useHotkeys.
    • filterPreventDefault: boolean is used to prevent/allow the default browser behavior for the keystroke when the filter return false (default value: true)
    • enableOnTags: string[] is used to enable listening to hotkeys in form fields. Available values are INPUT, TEXTAREA and SELECT.
    • splitKey: string is used to change the splitting character inside the keys argument. Default is +, but if you want to listen to the + character, you can set splitKey to i.e. - and listen for ctrl-+
    • keyup: boolean Determine if you want to listen on the keyup event
    • keydown: boolean Determine if want to listen on the keydown event
    • enabled: boolean is used to prevent installation of the hotkey when set to false (default value: true)
  • deps: any[] = []: The dependency array that gets appended to the memoisation of the callback. Here you define the inner dependencies of your callback. If for example your callback actions depend on a referentially unstable value or a value that will change over time, you should add this value to your deps array. Since most of the time your callback won't depend on any unstable callbacks or changing values over time you can leave this value alone since it will be set to an empty array by default. See the Memoisation section to learn more and see an example where you have to set this array.

isHotkeyPressed function

This function allows us to check if the user is currently pressing down a key.

import { isHotkeyPressed } from 'react-hotkeys-hook';

isHotkeyPressed('return'); // Returns true if Return key is pressed down.

Support

  • Ask your question in the Github Discussions
  • Ask your question on StackOverflow

If you'd like to support me, please buy me a ko-fi:

Found an issue or have a feature request?

Open up an issue or pull request and participate.

Local Development

Checkout this repo, run yarn or npm i and then run the test script to test the behavior of the hook.

Authors

  • Johannes Klauss

Contributors


MIT License.


react-hotkeys-hook's People

Contributors

johannesklauss avatar renovate-bot avatar dependabot[bot] avatar minoyu avatar godspeedelbow avatar ggascoigne avatar jameschao avatar louisrli avatar wheeler avatar rosalie-liu avatar t3dotgg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.