In this tutorial, we’ll go through an example of how you can implement a dropdown component using Vue. Like this:
The most common way of making a dropdown in Vue has always been this way:
- First, you define a toggler (usually a
<button>
or an<a>
) with a click event that will call a toggle method. - You define the menu with a
v-if
directive that is bound to an active state. - You define a method toggle that will be triggered when clicking on the toggler and change the active state to true and consequently show the menu.
Now, this way of doing is totally working and there is nothing wrong doing it. The problem is that you would need to define an active state on every component that has a dropdown. This kind of defeat the purpose of vue and the reusability of components.
I will show you my way of making dropdowns in a clean and reusable way.
I’m going to assume that you already have a vue project set up. If not, I recommend creating one using the vue-cli.
We’ll start by scaffolding out the structure of our dropdown component:
We will then create 3 components:
- The
AppDropdown
component, which will act as the wrapper component. It will contain both the toggler and the menu. - The
AppDropdownContent
component, which will act as the toggable menu. - The
AppDropdownItem
component, which will be the actionable item inside the menu.
Let’s open the AppDropdown.vue
file and write some code.
In here we’ll define a <div>
that will contain the whole dropdown. We’ll add a slot with the name “toggler
” and a button inside of it which will act as the default button toggle if none is provided.
Now, let’s open the
AppDropdownContent.vue
file.
We’ll add a <div>
which will wrap the menu and use the v-if
directive to only display it when it is active.
You might ask yourself: where is the active
state coming from?
Here comes the fun part: We need the AppDropdownContent
component to receive that value in some way. The most logical way would be from the main AppDropdown
component itself. But how in the hell could we do this?
For that, we’ll use the amazing provide
and inject
features provided by Vue. In our case the AppDropdown
will “provide” the active
state and the AppDropdownContent
will “inject” it in itself.
Let’s go back to our AppDropdown.vue
file.
We’ll add a sharedState object to the data with the property active
in it, that will be set as false
by default. We’ll define a toggle()
method, that will switch the active
state. After that, we’ll add a @click
event to the main div that will call the toggle()
method. Finally, we’ll provide ()
the sharedState to every component inside the main AppDropdown
component.
In the
AppDropdownContent
component, we now have the possibility to inject
the sharedState
provided by the AppDropdown
. Let’s create a computed property active
and set it’s value to the one sharedState
provides.And voilà! You now how a fully working dropdown component that is fully customizable.
But wouldn’t be nice if you didn’t need to close it by clicking on the toggle again and just click outside of it?
Fortunately, there is this great plugin called vue-clickaway
that allows us to do just this. Let’s install it: npm i vue-clickaway
NB: You might want to recompile your project after the install.
We’ll add the directive provided by vue-clickaway
the AppDropdown
. We’ll define an away()
method and call it when click away is triggered.
And that’s it! Now, you can simply add some styles to the dropdown and make it look shiny.
If you have any questions, don’t hesitate to reach out to me on twitter @victorronnow
Top comments (3)
Not really working. nothing happens on toggle click.
What is the difference in using a native browser ?
Pure Gold :)