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

vue-global-events

v3.0.1

Published

Register global events using vue template shortcuts

Downloads

63,765

Readme

vue-global-events Build Status npm package

Add shortcuts by listening to events on the document, anywhere

This is the version for Vue 3, if you are looking for the Vue 2 version, take a look at the v1 branch

Sponsors

Bronze

Installation

yarn add vue-global-events
npm install vue-global-events

Demo

Idea

Thanks to Vue’s event modifiers, handling events is extremely easy however, you’re limited to DOM element events. We decided to change that, so now you can register global events (for example application shortcuts) just like you would listen to events on a component. No need to worry about removing them either. You can toggle the events with a single v-if. Works with SSR too.

Usage

import { GlobalEvents } from 'vue-global-events'

// register globally
app.component('GlobalEvents', GlobalEvents)

// or locally
export default {
  components: { GlobalEvents },
  // rest of your component
}

After that you can register global events like this:

<GlobalEvents
  v-if="listenersConnected"
  @keyup.ctrl.tab="nextTab"
  @keyup.ctrl.shift.tab="previousTab"
  @keyup.space="pause"
  @contextmenu="openMenu"
/>

Props

target

Target element where addEventListener is called on. It's a String that refers to a global variable like document or window. This allows you to add events to the window instead of document.

  • type: String
  • default: 'document'

Warning: This prop is not reactive. It should be provided as a static value. If you need it to be reactive, add a key attribute with the same value:

<GlobalEvents :target="target" :key="target" />

filter

Function to prevent any event from being executed based on anything related to the event like the element that triggered it, the name, or the handler.

  • type: Function
  • default: () => true
arguments
  • event: Native Event Object
  • handler: method passed to GlobalEvents component
  • eventName: event name that was attached to the target (See above)

filter should return false to prevent the execution of a handler. For example, you can avoid the calls if the event is triggered by an <input>:

<GlobalEvents
  :filter="(event, handler, eventName) => event.target.tagName !== 'INPUT'"
  @keyup.prevent.space.exact="nextTab"
/>

In the example above event would be the native keyup Event Object, handler would be the method nextTab and eventName would be the string keyup.

Advice / Caveats

  • Always .prevent events with .ctrl and other modifiers as browsers may be using them as shortcuts.
  • Do not use shortcuts that are used by the system or that the browser does not allow you to .preventDefault(). The list includes Ctrl+Tab/Cmd+Tab, Ctrl+W/Cmd+W. You can find more information in this StackOverflow answer.
  • Prefer using actual characters to keyCodes whenever possible: @keydown.+ for detecting the plus sign. This is important because symbols and numbers on the digit row will provide different keyCodes depending on the layout used.
  • About using keyup with modifiers like .ctrl or .shift: the keyup event is triggered when a key is released and that's also when the event.ctrlKey is checked, which if you just released, will be false. This is because ctrl, shift and alt are checked differently. If you want to trigger on the keyup event of a modifier, you need to use its keycode (check it here. For example, for the ctrl key, that would be: @keyup.17. You can also use the advice above this one to provide it a name like ctrlkey.

Development

Run tests in watch mode:

yarn jest --watch

Authors

Damian Dulisz @shentao

Eduardo San Martin Morote @posva

License

MIT