Created
February 6, 2019 18:39
-
-
Save jasonvarga/580918b259fec82c3cea58eb7dfd6f02 to your computer and use it in GitHub Desktop.
Vue component that handles single and double clicks
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
export default { | |
render() { | |
return this.$scopedSlots.default({}); | |
}, | |
data() { | |
return { | |
listener: null, | |
timer: null, | |
clicks: 0, | |
} | |
}, | |
mounted() { | |
this.listener = () => this.click(); | |
this.$el.addEventListener('click', this.listener); | |
}, | |
beforeDestroy() { | |
this.$el.removeEventListener('click', this.listener); | |
}, | |
methods: { | |
click() { | |
this.clicks++; | |
if (this.clicks === 1) { | |
this.timer = setTimeout(() => { | |
this.$emit('click'); | |
this.clicks = 0; | |
}, 150); | |
} else { | |
clearTimeout(this.timer); | |
this.$emit('dblclick'); | |
this.clicks = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment