Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nchutchind/vue-outside-events
Vue 2.x directive to help a specified element listen for specific events occurring outside of itself.
https://github.com/nchutchind/vue-outside-events
dom-events event events vue vue-directive vuejs vuejs2
Last synced: 6 days ago
JSON representation
Vue 2.x directive to help a specified element listen for specific events occurring outside of itself.
- Host: GitHub
- URL: https://github.com/nchutchind/vue-outside-events
- Owner: nchutchind
- License: mit
- Created: 2017-04-17T20:53:12.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-10-06T05:59:52.000Z (about 2 years ago)
- Last Synced: 2024-10-12T05:44:35.748Z (about 1 month ago)
- Topics: dom-events, event, events, vue, vue-directive, vuejs, vuejs2
- Language: JavaScript
- Homepage:
- Size: 461 KB
- Stars: 57
- Watchers: 4
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vue-outside-events
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
Vue 2.x directive to react on events outside of an element without stopping the event's propagation.
Works well for handling clicks outside of menus and popups. Can handle any DOM event or CustomEvent. Also able to capture jQuery events.
## Install
```js
npm install --save vue-outside-events
```## Demos
Check out the highly contrived demos here: [https://nchutchind.github.io/vue-outside-events/docs/index.html](https://nchutchind.github.io/vue-outside-events/docs/index.html)## Use
### Modular
```js
import Vue from 'vue'
import vOutsideEvents from 'vue-outside-events'Vue.use(vOutsideEvents)
``````html
export default {
methods: {
onClickOutside (e, el) {
console.log('onClickOutside');
console.log('click heard outside element:', el);
console.log('element clicked:', e.target);
console.log('event:', e);
},
onMouseOutside (e, el) {
console.log('onMouseOutside');
console.log('mouse moved outside element:', el);
console.log('element mouse moved over:', e.target);
console.log('event:', e);
},
onFoo (e, el, extras) {
console.log('onFoo');
console.log('fooEvent happened outside element:', el);
console.log('element that triggered foo:', e.target);
console.log('event:', e);
console.log('extras:', extras);
console.log('bar:', extras.bar);
}
}
};
```
### Scripts
```html
new Vue({
el: '#app',
methods: {
onClickOutside (e, el) {
console.log('onClickOutside');
console.log('click heard outside element:', el);
console.log('element clicked:', e.target);
console.log('event:', e);
},
onMouseOutside (e, el) {
console.log('onMouseOutside');
console.log('mouse moved outside element:', el);
console.log('element mouse moved over:', e.target);
console.log('event:', e);
},
onFoo (e, el, extras) {
console.log('onFoo');
console.log('fooEvent happened outside element:', el);
console.log('element that triggered foo:', e.target);
console.log('event:', e);
console.log('extras:', extras);
console.log('bar:', extras.bar);
}
}
});```
## Events
| Event | Event Name | Directive | Binding |
| ------------------ | ----------- | -------------------- | ---------------------------------------------- |
| Click | click | v-click-outside | ="handlerName" |
| Double-Click | dblclick | v-dblclick-outside | ="handlerName" |
| Focus | focusin | v-focus-outside | ="handlerName" |
| Blur | focusout | v-blur-outside | ="handlerName" |
| Mouse Over / Enter | mouseover | v-mouseover-outside | ="handlerName" |
| Mouse Move | mousemove | v-mousemove-outside | ="handlerName" |
| Mouse Up | mouseup | v-mouseup-outside | ="handlerName" |
| Mouse Down | mousedown | v-mousedown-outside | ="handlerName" |
| Mouse Out | mouseout | v-mouseout-outside | ="handlerName" |
| Key Down | keydown | v-keydown-outside | ="handlerName" |
| Key Up | keyup | v-keyup-outside | ="handlerName" |
| Key Press | keypress | v-keypress-outside | ="handlerName" |
| Change | change | v-change-outside | ="handlerName" |
| Select | select | v-select-outside | ="handlerName" |
| Submit | submit | v-submit-outside | ="handlerName" |
| Custom | "eventName" | v-event-outside | ="{ name: 'eventName', handler: handlerName }" |## Extras
Add additional key/value pairs to the custom event to pass data to the event handler.```html
``````js
onFoo (e, el, extras) {
console.log('onFoo');
console.log('fooEvent happened outside element:', el);
console.log('element that triggered foo:', e.target);
console.log('event:', e);
console.log('extras:', extras);
console.log('bar:', extras.bar);
}
```## Modifiers
Add the `jquery` modifier to allow the directive to handle jQuery triggering of custom events. jQuery must be present in the window for this to work.```html
$(document).trigger("onFoo"); // onFooOutside will be called for #myDiv2, but not #myDiv1
var event = document.createEvent('Event');
event.initEvent('onFoo', true, true);
document.dispatchEvent(event); // onFooOutside will be called for #myDiv1 and #myDiv2```
## License
[MIT License](https://github.com/nchutchind/vue-outside-events/blob/master/LICENSE)## Style
[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)