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

@ahmedayob/duck-shortcut

v0.0.2

Published

A lightweight React component for easily binding and handling keyboard shortcuts in your React applications.

Downloads

49

Readme

@ahmedayob/duck-shortcut

A lightweight React component for easily binding and handling keyboard shortcuts in your React applications.

Features

  • Easy Integration: Integrate keyboard shortcuts into your React application effortlessly. Simply use the useDuckShortcut hook and pass the required props to handle key combinations or sequences.
  • Support for Key Combinations: Handle key combinations like Ctrl+S or Command+K with ease. The component can take a single string or an array of key combinations.
  • Support for Key Sequences: React to key sequences such as Up Up Down Down Left Right B A Enter. You can define these sequences as strings or arrays.
  • Mixed Key Combinations and Sequences: Combine both key combinations and sequences to define more complex shortcuts.
  • Global Keyboard Event Listener: The component adds a global keyboard event listener that works anywhere in the component tree without preventing events from bubbling or capturing.
  • TypeScript Support: Fully implemented in TypeScript with type definitions, ensuring a smooth development experience with type safety.
  • Case Insensitivity: The component handles key shortcuts in a case-insensitive manner, so you don’t need to worry about key case.
  • No Conflict with Other Components: Use multiple instances of useDuckShortcut for different shortcuts without conflicts.

Installation

To install the toolkit, use npm or yarn:

npm install @ahmedayob/duck-shortcut
# or
yarn add @ahmedayob/duck-shortcut

Usage

Here's a quick guide on how to use the library:

import { useDuckShortcut } from '@ahmedayob/duck-shortcut'

const App = () => {
  useDuckShortcut({
    keys: ['ctrl+s'], // Key combinations or sequences
    onKeysPressed: () => {
      console.log('Ctrl+S was pressed!')
    },
  })

  return <div>Press Ctrl+S to trigger the shortcut</div>
}

Handling Multiple Shortcuts

You can handle multiple key combinations or sequences by passing them as an array:

import { useDuckShortcut } from '@ahmedayob/duck-shortcut'
const App = () => {
  useDuckShortcut({
    keys: ['ctrl+s', 'command+k'], // Multiple shortcuts
    onKeysPressed: () => {
      console.log('Ctrl+S or Command+K was pressed!')
    },
  })
  return <div>Press Ctrl+S or Command+K to trigger the shortcut</div>
}

API Reference

Here’s an API reference for @ahmedayob/duck-shortcut in MDX format, detailing the types and usage of the useDuckShortcut hook:

useDuckShortcut

The useDuckShortcut hook allows you to bind keyboard shortcuts to callback functions in your React components.

Importing

import { useDuckShortcut } from '@ahmedayob/duck-shortcut'

Usage

useDuckShortcut({
  keys: /* Key combinations or sequences */,
  onKeysPressed: /* Callback function */,
});

Parameters

keys

  • Type: string | string[]
  • Description: Defines the keyboard shortcuts to listen for. You can specify key combinations (e.g., 'ctrl+s', 'command+k') or key sequences (e.g., 'Up Up Down Down Left Right B A Enter').
Example
keys: 'ctrl+s'

or

keys: ['ctrl+s', 'command+k']

or

keys: 'Up Up Down Down Left Right B A Enter'

onKeysPressed

  • Type: () => void
  • Description: Callback function that gets called when the specified key combinations or sequences are pressed.
Example
onKeysPressed: () => {
  console.log('Shortcut triggered!')
}

Example

import { useDuckShortcut } from '@ahmedayob/duck-shortcut'

const App = () => {
  useDuckShortcut({
    keys: ['ctrl+s', 'command+k'], // Define your shortcuts
    onKeysPressed: () => {
      // Callback when shortcuts are pressed
      console.log('Shortcut triggered!')
    },
  })

  return <div>Press Ctrl+S or Command+K</div>
}

Notes

  • Key Combinations: These are key presses that need to happen simultaneously (e.g., 'ctrl+s', 'command+shift+P').
  • Key Sequences: These are key presses that need to happen in sequence (e.g., 'Up Up Down Down Left Right B A Enter').
  • Case Sensitivity: Key names are case-insensitive.

Type Definitions

DuckShortcutProps

interface DuckShortcutProps {
  keys: string | string[] // Key combinations or sequences
  onKeysPressed: () => void // Callback when shortcuts are pressed
}

useDuckShortcut

declare function useDuckShortcut(props: DuckShortcutProps): void

License

This library is available under the MIT License.

Contributing

This reference includes type definitions for DuckShortcutProps, as well as detailed information on how to use the useDuckShortcut hook.

Contributions are welcome! Please open issues and pull requests on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.