A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/shentao/vue-global-events/tree/v1 below:

GitHub - shentao/vue-global-events at v1

Add shortcuts by listening to events on the document, anywhere

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

npm install vue-global-events

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 unregistration either. You can toggle the events with a single v-if. Works with SSR too.

import GlobalEvents from 'vue-global-events'

// register globally
Vue.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"
/>

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.

filter should return false to prevent the execution of a handler:

<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. eventName can contain key modifiers if used

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.

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" />

Run tests in watch mode:

Build the library with:

And then open the index.html file

Damian Dulisz @shentao

Eduardo San Martin Morote @posva

MIT


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4