How to close dropdown after clicking on menu item

Viewed 1514

I have a dropdown menu using NUXT JS and tailwind CSS but the problem with nuxt-js, as it doesn't change pages using SSR the dropdown doesn't close how do I make the dropdown close when I click on the menu item

This is the template

      <!-- dropdown -->
      <button class="mt-1 block px-2 py-1 text-white font-semibold rounded hover:bg-gray-800 sm:mt-0 sm:ml-2"
        type="button" v-on:click="toggleDropdown()" ref="btnDropdownRef">
        Applications
      </button>
      <div v-bind:class="{'hidden': !dropdownPopoverShow, 'block': dropdownPopoverShow}"
        class="bg-gray-800 w-full md:w-1/5 text-white z-50 float-left py-2 list-none text-left rounded shadow-lg mt-1"
         ref="popoverDropdownRef">
        <NuxtLink class="mt-1 font-semibold block px-2 py-1 text-white  rounded hover:bg-gray-800 sm:mt-0 sm:ml-2" to="/applications/education">
        education
        </NuxtLink>
        <NuxtLink class="mt-1 font-semibold block px-2 py-1 text-white  rounded hover:bg-gray-800 sm:mt-0 sm:ml-2" to="/applications/lifescience">
        life sciences 
        </NuxtLink>
        <NuxtLink class="mt-1 font-semibold block px-2 py-1 text-white  rounded hover:bg-gray-800 sm:mt-0 sm:ml-2" to="/applications/education">
        media
        </NuxtLink>
        <NuxtLink class="mt-1 font-semibold block px-2 py-1 text-white  rounded hover:bg-gray-800 sm:mt-0 sm:ml-2" to="/">
        industries
        </NuxtLink>
        <NuxtLink class="mt-1 font-semibold block px-2 py-1 text-white  rounded hover:bg-gray-800 sm:mt-0 sm:ml-2" to="/">
        agriculture
        </NuxtLink>
      </div>

and this is the script

<script>
  import {
    createPopper
  } from "@popperjs/core";

  export default {
    data() {
      return {
        isOpen: false,
        dropdownPopoverShow: false,
      }
    },
    methods: {
      toggleDropdown: function () {
        if (this.dropdownPopoverShow) {
          this.dropdownPopoverShow = false;
        } else {
          this.dropdownPopoverShow = true;
          createPopper(this.$refs.btnDropdownRef, this.$refs.popoverDropdownRef, {
            placement: "bottom-start"
          });
        }
      }
    }
  }
</script>
2 Answers

I ended up using the watch route method for it

 watch: {
    '$route' () {
      // this will close the dropdown
      this.dropdownPopoverShow = false,  
      // this will close the mobile menu on page change
      this.isOpen = false
    }

You can set a method to run every time a page is changed, using the afterEach hook on the Vue router instance. I’d recommend setting this up as a plug-in within Nuxt, and moving dropdownPopoverShow from your nav component into the store.

Move dropdownPopoverShow into the store, so you can reference it outside of your nav component.

export const state = () => ({
  dropdownPopoverShow: false
})

export const mutations = {
  toggleDropdown(state) {
    // no need for an 'if/then', just toggle the Boolean
    state.dropdownPopoverShow = !state.dropdownPopoverShow
  },
  hideDropdown(state) {
    state.dropdownPopoverShow = false
  }
}

Within your nav component, call the mutation in the store.

methods: {
  toggleDropdown() {
    this.$store.commit('toggleDropdown')
  }
}

And finally, create a plug-in that sets the dropdownPopoverShow to false after every route change.

// plugins/hideDropdown.js:
export default async ({ app, store }) => {
  app.router.afterEach((to, from) => {
    store.commit('hideDropdown')
  });
}

Remember to add the plug-in to your nuxt config:

plugins: [ { src: '~/plugins/hideDropdown.js', mode: 'client' } ]
Related