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

@ramstack/alpinegear-hotkey

v1.0.0

Published

@ramstack/alpinegear-hotkey provides 'x-hotkey' Alpine.js directive, allowing easily handle keyboard shortcuts.

Downloads

167

Readme

@ramstack/alpinegear-hotkey

@ramstack/alpinegear-hotkey is a plugin for Alpine.js that provides the x-hotkey directive.

This directive allows you to easily handle keyboard shortcuts within your Alpine.js components or application.

Uses @ramstack/hotkey package under the hood.

Installation

Using CDN

To include the CDN version of this plugin, add the following <script> tag before the core alpine.js file:

<!-- alpine.js plugin -->
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-hotkey@1/alpinegear-hotkey.min.js" defer></script>

<!-- alpine.js -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script>

Using NPM

Alternatively, you can install the plugin via npm:

npm install --save @ramstack/alpinegear-hotkey

Then initialize it in your bundle:

import Alpine from "alpinejs";
import hotkey from "@ramstack/alpinegear-hotkey";

Alpine.plugin(hotkey);
Alpine.start();

Usage

Define a hotkey combination using the directive by specifying key modifiers (such as Ctrl, Alt, Shift) with a + sign (e.g., Ctrl+Alt+Shift+S).

Here's a simple example:

<div x-data x-hotkey.shift+f.window="console.log($event.hotkey)">
    Hello, World!
</div>

[!TIP] The hotkey is case-insensitive. Standard key names are used.

You can find a list of key names here Key values for keyboard events

Event Object

The x-hotkey directive provides access to the native JavaScript event object via the magic $event property.

<div x-data x-hotkey.shift+f.window="console.log($event)"></div>

Also, the x-hotkey automatically passes the event object as the first argument to the method handler:

<div x-data x-hotkey.shift+f.window="e => console.log(e)"></div>

<!--  OR  -->
<div x-data x-hotkey.shift+f.window="handle"></div>

<script>
    function handle(e) {
        console.log(e);
    }
</script>

Event Modifiers

To simplify common tasks like calling event.preventDefault() or event.stopPropagation(), the x-hotkey directive supports following event modifiers:

  • .prevent - calls event.preventDefault() before calling the event handler.
  • .stop - call event.stopPropagation(), stopping the event from propagating further.
  • .passive - indicates that the handler will never call event.preventDefault(), which improves scrolling performance for touch and wheel events.
  • .capture - calling the event handler in the capture phase instead of the bubbling phase.
  • .once - ensures the event handler is called only once.
<!-- prevent the default behavior for the keyboard event -->
<div x-hotkey.ctrl+s.prevent="save($event)"></div>

<!-- the event's propagation will be stopped -->
<div x-hotkey.ctrl+s.stop="save($event)"></div>

<!-- modifiers can be chained -->
<div x-hotkey.ctrl+s.prevent.stop="save($event)"></div>

Global Event Listening

Use the window or document modifiers to listen for hotkeys globally, across the entire page:

<div x-hotkey.ctrl+s.window.prevent="save($event)"></div>

Defining Alternative Hotkeys

You can assign multiple hotkeys to a single action by separating them with a dot (.). For instance, in the example below, the same action is triggered by both Ctrl + Shift + S and Alt + U.

To determine which hotkey triggered the event, use the hotkey property from the event object. This property contains the string representation of the hotkey.

<div x-data x-hotkey.ctrl+shift+f.alt+u.window="console.log($event.hotkey)">
    Hello, World!
</div>

Specific Events Listening

To change the event that x-hotkey listens for, use a directive argument. By default, the event is keydown, but you can specify keyup, keypress, or others:

<div x-hotkey:keyup.alt+u="console.log('Search...')"></div>

Exclude Elements

If you want to prevent hotkey handling from being triggered by specific elements, add the data-hotkey-ignore attribute to those elements:

<div x-hotkey.shift+k="...">
    ...
    <!-- Ignoring hotkeys from the input element -->
    <input type="text" data-hotkey-ignore>
</div>

You can also exclude a group of elements by applying the attribute to their parent:

<div x-hotkey.shift+k="...">
    ...

    <!-- Ignoring hotkeys from all elements within the form -->
    <form data-hotkey-ignore>
    ...
    </form>
</div>

Source code

You can find the source code for this plugin on GitHub:

https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/hotkey

Contributions

Bug reports and contributions are welcome.

License

This package is released as open source under the MIT License. See the LICENSE file for more details.