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

@simolation/vue-hotkey

v2.0.1

Published

Vue 3 keyboard shortcut library.

Downloads

145

Readme

Vue 3 keyboard shortcut library.

npm npm NPM

Also usable in Vue 2 with to VueDemi. For Vue 2 the @vue/composition-api is required.

Install

Install the package as a dependency in your project:

npm install --save @simolation/vue-hotkey
# or with yarn
yarn add @simolation/vue-hotkey

Usage

This package can be used as a Vue component or as a hook.

Use as a Vue component

The idea behind the component is to wrap, for example, a button with the Hotkey component to have a strong connection between the element, which would trigger the action without the hotkey. There will be no hotkey code scattered throughout your application.

Import the Hotkey component in your Vue file:

import { Hotkey } from "@simolation/vue-hotkey";

Register it as a component:

export default defineComponent({
  // ...
  components: {
    Hotkey,
  },
  // ...
  setup() {
    return {
      // This is the action that will be triggered when the hotkey is activated
      action: () => {
        console.log("Ctrl + s has been pressed!");
      },
    };
  },
});

Use it in your template:

<template>
  <Hotkey :keys="['ctrl', 's']" @hotkey="action">
    <!-- any content -->
  </Hotkey>
</template>
Click or focus element

It is also possible to click or focus the slot element:

<Hotkey :keys="['ctrl', 's']" v-slot="{ clickRef }">
  <button :ref="clickRef" @click="action">Hotkey or click</button>
</Hotkey>
<Hotkey :keys="['ctrl', 's']" v-slot="{ focusRef }">
  <input :ref="focusRef" type="text" />
</Hotkey>
Allow propagation

By default, the hotkey prevents the default action and is not propagated to the parent. With :propagate="true" the event will be passed to the parent listeners as well and trigger the default action.

<template>
  <Hotkey :keys="['ctrl', 's']" propagate @hotkey="action">
    <!-- any content -->
  </Hotkey>
</template>
Enable or Disable the hotkey

When the hotkey should not be usable, it can easily be disabled by setting disabled.

<template>
  <Hotkey :keys="['ctrl', 's']" disabled @hotkey="action">
    <!-- any content -->
  </Hotkey>
</template>
Exclude HTML elements

By default, input and textarea fields are excluded. This can be overwritten and changed by specifying the :excluded-elements="['radio', 'div']" option. It will prevent the hotkey when the currently focused HTML element is of the specified type.

<template>
  <Hotkey
    :keys="['ctrl', 's']"
    :excluded-elements="['radio', 'div']"
    @hotkey="action"
  >
    <!-- any content -->
  </Hotkey>
</template>
Key check

Only call a function when the hotkey is pressed. Can be used for special on-click actions based on a hotkey.

<template>
  <Hotkey :keys="['alt']" v-slot="{ keyCheck }">
    <button @click="keyCheck(onClick)">Click</button>
  </Hotkey>
</template>

Use as a hook

When there is no specific element tied to the hotkey, it can be used as a hook with useHotkey():

import { useHotkey } from "@simolation/vue-hotkey";

useHotkey({
  keys: ["ctrl" + "s"],
  handler: () => {
    console.log("Ctrl + s has been pressed!");
  },
  // optional:
  propagate: ref(false),
  enabled: ref(true),
});
Enable or Disable the hotkey

The hook returns three functions to enable, disable or destroy the hotkey. The hotkey does not need to be destroyed onUnmount, as the hook already takes care of that.

const { enable, disable, destroy } = useHotkey({ ... })

// Enable or disable the hotkey
enable()
// or
disable()

// Completely destroy the hotkey. It can not be enabled() again.
destroy()
Exclude HTML elements

The excluded elements can be specified with the hook as well. The default is again input and textarea.

useHotkey(
  {
    keys: ["ctrl" + "s"],
    handler: () => {
      console.log("Ctrl + s has been pressed!");
    },
  },
  ["radio", "div"]
);
Key check function

The function provided as a parameter to the keyCheckFn will only be called when the hotkey is pressed when calling the returned function

const { keyCheckFn } = useHotkey({ ... });

const doSomething = keyCheckFn((name: string, count: number) => {
  // do anything while the hotkey is pressed when doSomething is called
})

doSomething("myProps", 123);