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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mbolli/datastar-attribute-on-keys

v1.1.0

Published

Datastar plugin for binding keyboard events to actions with support for key combinations

Downloads

33

Readme

@mbolli/datastar-attribute-on-keys

npm version Release GitHub Pages License: MIT

A Datastar plugin that provides keyboard event binding with support for key combinations and multiple keys.

About

Datastar is a hypermedia-focused framework that brings reactive signals and declarative DOM manipulation to your HTML. While it includes great event handling, this plugin extends keyboard event capabilities to support:

  • Key combinations (e.g., Alt-Q, Ctrl-Shift-S)
  • Multiple key bindings (e.g., Escape.Enter to trigger on either key)

Installation

npm install @mbolli/datastar-attribute-on-keys

Demo

View Interactive Demo →

Usage

This plugin requires an import map to resolve the datastar module. Set up your HTML like this:

<script type="importmap">
{
  "imports": {
    "datastar": "https://cdn.jsdelivr.net/gh/starfederation/[email protected]/bundles/datastar.js"
  }
}
</script>
<script type="module">
  // Import the plugin - it will auto-register with datastar
  import 'https://cdn.jsdelivr.net/npm/@mbolli/datastar-attribute-on-keys@1/dist/index.js';
</script>

Note: Using @1 will automatically use the latest v1.x.x version.

What it does

This plugin adds an on-keys attribute to Datastar that allows you to bind keyboard events to reactive actions.

Single Key Binding

<div data-on-keys:escape="$modal.close()">
  <!-- Triggers when Escape key is pressed -->
</div>

Key Combinations

<div data-on-keys:alt-q="$app.quit()">
  <!-- Triggers when Alt+Q is pressed -->
</div>

<div data-on-keys:ctrl-shift-s="$document.save()">
  <!-- Triggers when Ctrl+Shift+S is pressed -->
</div>

Multiple Keys

<div data-on-keys:escape.enter="$dialog.close()">
  <!-- Triggers when either Escape OR Enter is pressed -->
</div>

<div data-on-keys:space.enter.alt-q="$action.execute()">
  <!-- Triggers on Space, Enter, or Alt+Q -->
</div>

Global Events (Default)

By default, key events are listened for globally on the window:

<div data-on-keys:escape="$modal.close()">
  <!-- Listens for Escape key globally -->
</div>

Element-Specific Events

Use the el modifier to listen only when the element has focus:

<input data-on-keys:enter__el="$form.submit()">
  <!-- Only triggers when this input is focused -->
</input>

Event Modifiers

The plugin supports several modifiers:

  • el - Listen on the element instead of window (requires focus)
  • noprevent - Allow default browser behavior (default prevents)
  • stop - Call stopPropagation() on the event
  • up - Listen for keyup instead of keydown (default)
  • capture - Use capture phase
  • passive - Use passive event listener
  • once - Only trigger once
<div data-on-keys:tab__noprevent="$counter++">
  <!-- Count tab presses but still allow tab navigation -->
</div>

Supported Key Names

Key Name Mapping

Common key aliases are automatically mapped to standard JavaScript key names:

Special Keys:

  • space → Space character
  • enter / return → Enter
  • escape / esc → Escape
  • tab → Tab
  • backspace → Backspace
  • delete / del → Delete

Navigation Keys:

  • up → ArrowUp
  • down → ArrowDown
  • left → ArrowLeft
  • right → ArrowRight
  • pageup → PageUp
  • pagedown → PageDown

Other Keys:

  • Letters: a, b, c, etc.
  • Numbers: 1, 2, 3, etc.
  • Function keys: f1, f2, etc. → F1, F2, etc.
  • Modifiers: ctrl/control, alt, shift, meta/cmd/command

Examples

Modal with Escape Key

<div class="modal" data-on-keys:escape="$modal.close()">
  <!-- Modal content -->
</div>

Counter with Space Bar

<div data-on-keys:space="$counter++">
  Count: <span data-text="$counter"></span>
</div>

Keyboard Shortcuts

<div data-on-keys:ctrl-s="$document.save()">
  <!-- Ctrl+S to save - global and prevents browser save dialog -->
</div>
<div data-on-keys:ctrl-z="$document.undo()">
  <!-- Ctrl+Z to undo - global and prevents browser undo -->
</div>

Testing

Run the automated tests:

pnpm test

Or open index.html locally in a browser to interactively test the plugin with Datastar.

Development & Releases

This project uses automated releases via GitHub Actions. When you push to main:

  1. Tests run automatically - Build and tests must pass
  2. Version bumping - Add to your commit message:
    • [major] for breaking changes (1.0.0 → 2.0.0)
    • [minor] for new features (1.0.0 → 1.1.0)
    • Default: patch for bug fixes (1.0.0 → 1.0.1)
  3. Automatic publishing - Package is published to npm
  4. GitHub Release created - With auto-generated release notes

License

MIT