Created
May 15, 2019 20:20
-
-
Save artlili/d4bf83e9e4c90983593ff99fddbc956d to your computer and use it in GitHub Desktop.
click outside
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vue.directive('click-outside', { | |
bind () { | |
this.event = event => this.vm.$emit(this.expression, event) | |
this.el.addEventListener('click', this.stopProp) | |
document.body.addEventListener('click', this.event) | |
}, | |
unbind() { | |
this.el.removeEventListener('click', this.stopProp) | |
document.body.removeEventListener('click', this.event) | |
}, | |
stopProp(event) { event.stopPropagation() } | |
}) | |
Usage: | |
<div v-click-outside="nameOfCustomEventToCall"> | |
Some content | |
</div> | |
In the component: | |
events: { | |
nameOfCustomEventToCall: function (event) { | |
// do something - probably hide the dropdown menu / modal etc. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment