A simple library for handling keyboard shortcuts with Alpine.js.

Overview

Help support the maintenance of this package by sponsoring me.

Alpine.js Mousetrap

A simple library for handling keyboard shortcuts with Alpine.js.

GitHub tag (latest by date) Monthly downloads via CDN

About

This plugin adds a new x-mousetrap directive to all of your Alpine components that can be used to register keyboard shortcuts.

Installation

CDN

Include the following <script> tag in the <head> of your document, just before Alpine.

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

NPM

npm install @danharrin/alpine-mousetrap --save-dev

Add the x-mousetrap directive to your project by importing the package before Alpine.js.

import Alpine from 'alpinejs'
import Mousetrap from '@danharrin/alpine-mousetrap'

Alpine.plugin(Mousetrap)

window.Alpine = Alpine
window.Alpine.start()

Usage

To register keyboard shortcuts for a button or other clickable element:

<div x-data="{}">
    <button
        type="button"
        x-mousetrap.command+k.ctrl+k
    >
        Button
    </button>
</div>

Check out the original Mousetrap documentation for more shortcut examples. If you're registering more than one, separate them with a period.

Alternatively, you can invoke a custom function when the keyboard shortcut is hit:

<div x-data="{}">
    <button
        type="button"
        x-mousetrap.command+k.ctrl+k="console.log('command k or control k')"
    >
        Button
    </button>
</div>

Global bindings

You may listen for "global" keyboard events that will work anywhere, including inside input fields:

<div x-data="{}">
    <button
        type="button"
        x-mousetrap.global.command+k.ctrl+k
    >
        Button
    </button>
</div>

Versioning

This projects follow the Semantic Versioning guidelines.

License

Copyright (c) Dan Harrin

Licensed under the MIT license, see LICENSE.md for details.

You might also like...

A vscode extension to quickly print variable, variable type, tensor shape etc by using shortcuts

A vscode extension to quickly print variable, variable type, tensor shape etc by using shortcuts

Quick Python Print This repo is inspired by "Python Quick Print". "Python Quick Print" can quickly print out variables on the console by using shortcu

Oct 28, 2022

Craftcuts - a Shortcuts eXtension for Craft

Craftcuts - a Shortcuts eXtension for Craft

Craftcuts Welcome to the repository of Craftcuts my Shortcuts eXtension for Craft. Please read through the instructions, especially the installation c

Dec 31, 2022

Easily create key board shortcuts for your JS functions. Built using JS only with no other dependency. Inspired from MacOS spotlight

floodlightjs Inspired from macOS spotlight, floodlight is simple JS library that will show a search area. How the search is handled is completely on y

Aug 12, 2022

Obsidian plugin: Type text shortcuts that expand into javascript generated text.

Obsidian plugin: Type text shortcuts that expand into javascript generated text.

Obsidian Plugin - Text Expander JS (open beta) This Obsidian plugin allows the user to type text shortcuts that are replaced by (or "expanded into") j

Dec 27, 2022

 Shortcuts → Cloudflare Images → GitHub Action

shortcuts-cfimages-action This repository guides you through setting up a workflow to upload photos from your Apple device to Cloudflare Images, then

Sep 21, 2022

A Deno library for interacting with the mouse 🖱️ keyboard ⌨️ and screen 💻

A Deno library for interacting with the mouse 🖱️ keyboard ⌨️ and screen 💻 . Litebot provides a simple API for creating kbm events, macros, & working with displays. Litebot leverages Deno's FFI to allow speedy low level control in C & C++ while having a typescript API exposed to the user.

Aug 30, 2022

➷ A robust Javascript library for capturing keyboard input. It has no dependencies.

Hotkeys HotKeys.js is an input capture library with some very special features, it is easy to pick up and use, has a reasonable footprint (~3kb) (gzip

Jan 4, 2023

Keyboard shortcut handler. KISS, light and library free.

Shortcut.js Shortcut.js is a JavaScript ES6 component that offers a global keyboard event handler. This way one can declare and react to any combinati

Sep 28, 2022

Add focal point alignment of images to your Alpine 3.x components with a custom directive.

Alpine Focal Add focal point alignment of images to your Alpine 3.x components with a custom directive. This package only supports Alpine v3.x. About

Oct 12, 2021
Comments
  • How do I use this with blade components?

    How do I use this with blade components?

    Handy little thing, thanks!

    I get this error in my blade file:

    syntax error, unexpected token "endif", expecting end of file
    

    Any way to get around that when working with blade components?

    <x-button x-mousetrap.ctrl+n>Button</x-button>
    

    Do I need to set an attribute that is keycommand="x-mousetrap.ctrl+n" and add the property inside the component if set?

    Thanks!

    opened by seabasss 4
  • Any way to only trigger keycommand if element is visible?

    Any way to only trigger keycommand if element is visible?

    Is there a way to only trigger the shortcut if the element it's on is visible?

    And is there a way to prioritize the shortcuts if there are multiple ones with the same command on the screen? Ex. I trigger a button click with ctrl+n which opens a modal and inside the modal is another button that also has the same key command that should now be prioritized until the modal is closed? Like a keycommand trap basically.

    Thank you!

    opened by seabasss 1
Owner
Dan Harrin
Lead Developer at Stagent ⚡️ Building Filament, Envault & Squire 🧙‍♂️ Co-hosting the Undefined Variable podcast.
Dan Harrin
Browser extension to add more keyboard shortcuts to TweetDeck.

TweetDeck Shortcut Plus Browser extension to add more keyboard shortcuts to TweetDeck. Keyboard shortcuts Browse Browse tweet (default: alt+b) Browse

Ryo Nakamura 5 May 26, 2022
Browser extension to add more keyboard shortcuts to twitter.com.

Twitter Shortcut Plus Browser extension to add more keyboard shortcuts to twitter.com. Keyboard shortcuts Browse Browse links in background (default:

Ryo Nakamura 6 Jun 4, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
⏱ Simple Alpine.js plugin to display the human-readable distance between a date and now.

⏱ Alpine TimeAgo ⏱ An Alpine.js plugin to return the distance between a given date and now in words (like "3 months ago", "about 2 hours ago" or "in a

Marc Reichel 47 Dec 22, 2022
Dead-simple CORS handling for any itty-router API (test with Cloudflare Workers, but works anywhere)!

Simple CORS-handling for any itty-router API. Designed on Cloudflare Workers, but works anywhere. Features Tiny. Currently ~600 bytes, with zero-depen

Kevin R. Whitley 6 Dec 16, 2022
GraphErr is an open-source error handling library for GraphQL implementations in Deno. It's a lightweight solution that provides developers with descriptive error messages, reducing ambiguity and improving debugging.

GraphErr Descriptive GraphQL error handling for Deno/Oak servers. Features Provides additional context to GraphQL's native error messaging for faster

OSLabs Beta 35 Nov 1, 2022
Pressure is a JavaScript library for handling both Force Touch and 3D Touch on the web

Pressure is a JavaScript library for handling both Force Touch and 3D Touch on the web, bundled under one library with a simple API that makes working with them painless.

Stuart Yamartino 2.9k Dec 29, 2022
Wrap selected text in custom tags with shortcuts.

Obsidian Wrap With Shortcuts Wrap the selected text in customized tags with shortcuts. Underline is provided with Ctrl-u(Cmd-u) as default wrappers. P

Manic Chuang 24 Dec 28, 2022