Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i make a dropdown menu close when i open another one in Vuejs 3 (without plugins)

I have two dropdowns menu in a component and i want to close one when the other is open
I’m new to Vuejs so i dont want to use any plugin, i want to build it from scratch, if anyone could help me with this ?
here’s my actual code

<template>
<ul class="menu">
    <li v-for="menu in menus" :key="menu.id">
        <button class="button is-white" @click="toggle">
            {{menu.name}}
            <ion-icon name="chevron-down-outline"></ion-icon>
        </button>
        <div class="dropdown" v-show="open">
            <router-link to="/" v-for="item in menu.subMenu">
                <div class="options">
                    {{item}}
                </div>
            </router-link>
        </div>
    </li>
</ul>
data() {
    return {
        open: false,
        menus: [
            {
                id: 1,
                name: 'My Profile',
                subMenu: ['Dashboard', 'My Profile', 'Logout']
            },
            {
                id: 2,
                name: ' My Orders',
                subMenu: ['Order']
            }
        ],
        
    }
},
created() {
    window.addEventListener("click", this.close);
},
beforeDestroy() {
    window.removeEventListener("click", this.close);
},
methods: {
    toggle() {
        this.open = !this.open
    },
    close(e) {
        if (!this.$el.contains(e.target)) {
        this.open = false;
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use id instead of boolean for open/close:

const app = Vue.createApp({
  data() {
    return {
      open: null,
      menus: [{id: 1, name: 'My Profile', subMenu: ['Dashboard', 'My Profile', 'Logout']}, {id: 2, name: ' My Orders', subMenu: ['Order']}],  
    }
  },
  methods: {
    toggle(id) {
      this.open = this.open === id ? null : id
    },
  }
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <ul class="menu">
    <li v-for="menu in menus" :key="menu.id">
      <button class="button is-white" @click="toggle(menu.id)">
        {{ menu.name }}
        <ion-icon name="chevron-down-outline"></ion-icon>
      </button>
      <div class="dropdown" v-show="menu.id === open">
        <div to="/" v-for="item in menu.subMenu">
          <div class="options">
            {{ item }}
          </div>
        </div>
      </div>
    </li>
  </ul>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading