Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use v-on:blur for making disappear a context menu...but it doesn't work

I am coding a simple context menu in vue.js. When I do a right click on a peculiar element, it opens the menu (using @contextmenu.prevent).This works.

But when I click outside of the menu I want it to disappear. This doesn't work... I am using v-on:blur for this, also tried @blur . None of them doesn't work. Here is my html :

<!-- my context menu -->
<ul class="context-menu"
    ref="contextMenuTrack"
    v-if="openedMenu === 'contextMenuTrack'"
    v-bind:style="{top: top, left: left}"
    v-on:blur="closeMenu()">
    <li v-on:click="removeTrack(project.structure.tracks.indexOf(targetOfMenu));closeMenu()">delete track</li>
</ul>
<div>

    [ ...... stuff here ......]

    <!-- Where the menu is called-->
    <li class="track"
         v-for="track in project.structure.tracks">
        <div class="track-name-row">
            <li @contextmenu.prevent="openContextMenuTrack(track,$event)"
                v-on:click="expandTrack(project.structure.tracks.indexOf(track))"
                class="track-color-viewer"></li>

                [ .... other li tags .....]
        </div>
    </li>

    [ ...... stuff here ......]

</div>

Here is the datas used for the menu of my Vue component:

data() {
    return {
        //the kind of menu which is opened
        openedMenu: undefined,
        //the coordinate of the menu
        top: "0px",
        left: "0px",
        //the element which is targeted by the menu
        targetOfMenu: undefined
    };
},

And here are the methods used for the menu in my Vue.js component :

 methods: {

    setMenu(top, left) {
        this.top = top - 170 + "px";
        this.left = left + "px";
    },

    // opening menu : calling set menu whith x and y
    openContextMenuTrack(track, event) {
        this.openedMenu = "contextMenuTrack";
        this.targetOfMenu = track;

        this.$nextTick((() => {
            this.$refs.contextMenuTrack.focus();
            this.setMenu(event.y, event.x);
        }).bind(this));
    },

    closeMenu() {
        this.openedMenu = undefined;
        this.targetOfMenu = undefined;
    }
}
like image 584
Bénédicte Lagouge Avatar asked Aug 23 '17 11:08

Bénédicte Lagouge


2 Answers

the blur event only exists for form controls (<input> etc.).

Your problem is generally solved by creating a custom directive that runs a method when you click outside of the menu.

Something like this:

https://www.npmjs.com/package/v-click-outside

<ul class="context-menu"
    ref="contextMenuTrack"
    v-if="openedMenu === 'contextMenuTrack'"
    v-bind:style="{top: top, left: left}"

    v-click-outside="closeMenu()">

    <li v-on:click="removeTrack(project.structure.tracks.indexOf(targetOfMenu));closeMenu()">delete track</li>
</ul>

Hope this helps

Edit:

An example with a better package (vue-clickaway):

https://jsfiddle.net/Linusborg/hqkgp4hm/

like image 51
Linus Borg Avatar answered Sep 23 '22 03:09

Linus Borg


This would help if you are looking for unrecommended way ;) $refs would be tricky on having the same result.

let x = document.querySelector('.targetClass).addEventListener('click', (e) => 
{
  if(e.target.className == 'targetClass') {
    this.close()
  }
 })
like image 39
Kaan KÜÇÜK Avatar answered Sep 19 '22 03:09

Kaan KÜÇÜK