Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jasonvarga
Created February 6, 2019 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonvarga/580918b259fec82c3cea58eb7dfd6f02 to your computer and use it in GitHub Desktop.
Save jasonvarga/580918b259fec82c3cea58eb7dfd6f02 to your computer and use it in GitHub Desktop.
Vue component that handles single and double clicks
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