I have this context menu which you can find online. I want to access the click event of the button in order to open a Bootstrap Modal. The problem is that when I set the @blur
attribute to the close function I cannot handle the click event anymore. It seems like the click event gets ingored because the div gets closed beforehand. Also the context menu closes itself even if I click on the button which is incorrect. It should only close when I click outside the div. Is there a workaround for it?
<template>
<div class="context-menu" v-show="show" :style="style" ref="context" tabindex="0" @blur="close">
<button @click="handle" class="btn btn-outline-secondary">Create Object</button>
</div>
</template>
<script>
import { nextTick } from '@vue/runtime-core';
export default {
name: 'ContextMenu',
data(){
return {
left: 0,
top: 0,
show: false,
}
},
computed: {
style() {
return {
top: this.top + 'px',
left: this.left + 'px',
};
},
},
methods: {
close() {
console.log("closed")
this.show = false;
this.left = 0;
this.top = 0;
},
open(e) {
this.left = e.pageX || e.clientX;
this.top = e.pageY || e.clientY;
nextTick(() => this.$el.focus())
this.show = true;
},
handle(){
console.log("clicked")
//Do something
this.close()
}
}
}
</script>
<style scoped>
.context-menu {
position: fixed;
background: white;
z-index: 999;
outline: none;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
cursor: pointer;
}
</style>