I am working on a dropdown menu using NUXT JS and Tailwind CSS. However, I have encountered an issue with nuxt-js - since it does not change pages using SSR, the dropdown does not close when navigating to a new page. How can I make the dropdown automatically close when a menu item is clicked?
Here 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>
...
</div>
And here 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>