<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Victor Rønnow</title>
    <description>The latest articles on Forem by Victor Rønnow (@victorronnow).</description>
    <link>https://forem.com/victorronnow</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F193187%2Fb571d7b0-ead8-46bd-a9f7-049bebad99cc.jpg</url>
      <title>Forem: Victor Rønnow</title>
      <link>https://forem.com/victorronnow</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/victorronnow"/>
    <language>en</language>
    <item>
      <title>Dropdowns in Vue: The Right Way</title>
      <dc:creator>Victor Rønnow</dc:creator>
      <pubDate>Wed, 02 Oct 2019 14:02:13 +0000</pubDate>
      <link>https://forem.com/victorronnow/dropdowns-in-vue-the-right-way-19j9</link>
      <guid>https://forem.com/victorronnow/dropdowns-in-vue-the-right-way-19j9</guid>
      <description>&lt;p&gt;In this tutorial, we’ll go through an example of how you can implement a dropdown component using Vue. Like this:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/dropdown-2fzh6"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;p&gt;The most common way of making a dropdown in Vue has always been this way:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, you define a toggler (usually a &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; or an &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;) with a click event that will call a toggle method.&lt;/li&gt;
&lt;li&gt;You define the menu with a &lt;code&gt;v-if&lt;/code&gt; directive that is bound to an active state.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
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.



&lt;p&gt;I will show you my way of making dropdowns in a clean and reusable way.&lt;br&gt;
I’m going to assume that you already have a vue project set up. If not, I recommend creating one using the  &lt;a href="https://cli.vuejs.org/"&gt;vue-cli&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We’ll start by scaffolding out the structure of our dropdown component:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
We will then create 3 components:

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;AppDropdown&lt;/code&gt; component, which will act as the wrapper component. It will contain both the toggler and the menu.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;AppDropdownContent&lt;/code&gt; component, which will act as the toggable menu.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;AppDropdownItem&lt;/code&gt; component, which will be the actionable item inside the menu.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s open the &lt;code&gt;AppDropdown.vue&lt;/code&gt; file and write some code.&lt;/p&gt;

&lt;p&gt;In here we’ll define a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; that will contain the whole dropdown. We’ll add a slot with the name “&lt;code&gt;toggler&lt;/code&gt;” and a button inside of it which will act as the default button toggle if none is provided.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Now, let’s open the &lt;code&gt;AppDropdownContent.vue&lt;/code&gt; file.

&lt;p&gt;We’ll add a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; which will wrap the menu and use the &lt;code&gt;v-if&lt;/code&gt; directive to only display it when it is active. &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You might ask yourself: where is the &lt;code&gt;active&lt;/code&gt; state coming from?&lt;/p&gt;

&lt;p&gt;Here comes the fun part: We need the &lt;code&gt;AppDropdownContent&lt;/code&gt; component to receive that value in some way. The most logical way would be from the main &lt;code&gt;AppDropdown&lt;/code&gt; component itself. But how in the hell could we do this?&lt;/p&gt;

&lt;p&gt;For that, we’ll use the amazing &lt;em&gt;&lt;code&gt;provide&lt;/code&gt;&lt;/em&gt; and &lt;em&gt;&lt;code&gt;inject&lt;/code&gt;&lt;/em&gt; features provided by Vue. In our case the &lt;code&gt;AppDropdown&lt;/code&gt; will “provide” the &lt;code&gt;active&lt;/code&gt; state and the &lt;code&gt;AppDropdownContent&lt;/code&gt; will “inject” it in itself.&lt;/p&gt;

&lt;p&gt;Let’s go back to our &lt;code&gt;AppDropdown.vue&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;We’ll add a sharedState object to the data with the property &lt;code&gt;active&lt;/code&gt; in it, that will be set as &lt;code&gt;false&lt;/code&gt; by default. We’ll define a &lt;code&gt;toggle()&lt;/code&gt; method, that will switch the &lt;code&gt;active&lt;/code&gt; state. After that, we’ll add a &lt;code&gt;@click&lt;/code&gt; event to the main div that will call the &lt;code&gt;toggle()&lt;/code&gt; method. Finally, we’ll &lt;code&gt;provide ()&lt;/code&gt; the sharedState to every component inside the main &lt;code&gt;AppDropdown&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In the &lt;code&gt;AppDropdownContent&lt;/code&gt; component, we now have the possibility to  &lt;code&gt;inject&lt;/code&gt; the &lt;code&gt;sharedState&lt;/code&gt; provided by the &lt;code&gt;AppDropdown&lt;/code&gt;. Let’s create a computed property &lt;code&gt;active&lt;/code&gt; and set it’s value to the one &lt;code&gt;sharedState&lt;/code&gt; provides.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
And voilà!  You now how a fully working dropdown component that is fully customizable.

&lt;p&gt;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?&lt;/p&gt;

&lt;p&gt;Fortunately, there is this great plugin called &lt;code&gt;vue-clickaway&lt;/code&gt; that allows us to do just this. Let’s install it: &lt;code&gt;npm i vue-clickaway&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;NB: You might want to recompile your project after the install.&lt;/p&gt;

&lt;p&gt;We’ll add the directive provided by &lt;code&gt;vue-clickaway&lt;/code&gt; the &lt;code&gt;AppDropdown&lt;/code&gt;. We’ll define an &lt;code&gt;away()&lt;/code&gt;method and call it when click away is triggered.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
And that’s it! Now, you can simply add some styles to the dropdown and make it look shiny.

&lt;p&gt;If you have any questions, don’t hesitate to reach out to me on twitter  &lt;a href="https://twitter.com/victorronnow"&gt;@victorronnow&lt;/a&gt; &lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>ux</category>
      <category>design</category>
    </item>
  </channel>
</rss>
