<?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>DEV Community: Karm0s</title>
    <description>The latest articles on DEV Community by Karm0s (@karm0s).</description>
    <link>https://dev.to/karm0s</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%2F179651%2F3865dbce-95ba-485c-b606-a3640fb73351.jpeg</url>
      <title>DEV Community: Karm0s</title>
      <link>https://dev.to/karm0s</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karm0s"/>
    <language>en</language>
    <item>
      <title>Flash Code#1 Drag&amp;Drop Component Using Vue.js</title>
      <dc:creator>Karm0s</dc:creator>
      <pubDate>Wed, 07 Oct 2020 13:33:45 +0000</pubDate>
      <link>https://dev.to/karm0s/flash-code-1-drag-drop-component-using-vue-js-5h1d</link>
      <guid>https://dev.to/karm0s/flash-code-1-drag-drop-component-using-vue-js-5h1d</guid>
      <description>&lt;p&gt;In this short article I will show you how to create a drag and drop component using &lt;strong&gt;Vue.js&lt;/strong&gt; and &lt;strong&gt;Tailwindcss&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preview
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;link&lt;/strong&gt;: &lt;a href="https://karm0s.github.io/FlashCode/" rel="noopener noreferrer"&gt;Flash Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Finb8rvp23q03n9qchmlf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Finb8rvp23q03n9qchmlf.png" alt="Drag Drop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's start with the UI:
&lt;/h2&gt;

&lt;p&gt;We will put a single div with a few tailwind classes:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
What those css classes do:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;bg-gray-200&lt;/strong&gt;, &lt;strong&gt;rounded-lg&lt;/strong&gt;, &lt;strong&gt;border-2&lt;/strong&gt;, &lt;strong&gt;border-dashed&lt;/strong&gt;, and &lt;strong&gt;border-gray-500&lt;/strong&gt;: These are Tailwindcss classes. They are pretty self explanatory but if you want more infos you can checkout Tailwind's &lt;a href="https://tailwindcss.com/docs/installation" rel="noopener noreferrer"&gt;docs&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;dropzone&lt;/strong&gt;: Sets the width to 400px and the height to 500px.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's what we get:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1xnwswmul1s8dpxcqyco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1xnwswmul1s8dpxcqyco.png" alt="Drop Area"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's add some text inside. We can do that by simply adding another div inside the first one.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
All the css classes are from Tailwindcss except for &lt;strong&gt;dropzone-child-elements&lt;/strong&gt;, this class prevents the user from interacting with the content inside the dropzone and it contains only one rule:&lt;br&gt;
      &lt;strong&gt;pointer-events: none;&lt;/strong&gt;

&lt;p&gt;This is what it looks like:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7gst9smfrs4mcsqvvhhi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7gst9smfrs4mcsqvvhhi.png" alt="Text Inside Drop Area"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  It's time to make it do something (the logic):
&lt;/h2&gt;

&lt;p&gt;1-&lt;strong&gt;Change the border color when dragging a file:&lt;/strong&gt;&lt;br&gt;
First, let's make the dropzone interactive by changing the border color when a file is dragged in. We will use the &lt;strong&gt;dragenter&lt;/strong&gt; and &lt;strong&gt;dragleave&lt;/strong&gt; events to detect if a file  is hovering the drop area. We will use a variable named &lt;strong&gt;fileOverDropArea&lt;/strong&gt; to control the border color.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;strong&gt;Let me explain how it works:&lt;/strong&gt;&lt;br&gt;
This code sets &lt;strong&gt;fileOverDropArea&lt;/strong&gt; to &lt;strong&gt;true&lt;/strong&gt; or &lt;strong&gt;false&lt;/strong&gt; when a file is entering or leaving the drop down area.&lt;br&gt;
&lt;strong&gt;.prevent&lt;/strong&gt; cancels the default actions when those events are fired.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Thanks to Vue we can bind the class attribute to add some classes only if certain conditions are met. In this case we are setting the border color to a dark gray if &lt;strong&gt;fileOverDropArea&lt;/strong&gt; is &lt;strong&gt;false&lt;/strong&gt; and to red if it is &lt;strong&gt;true&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;2- Handling the file upload:&lt;/strong&gt;&lt;br&gt;
Now that we got the hover animation to work, let's handle the file uploading part.&lt;br&gt;
This is the easiest part actually, all we have to do is to listen for another event called &lt;strong&gt;drop&lt;/strong&gt; and then call a method when the event is fired.&lt;br&gt;
To do that we will add this line of code to the dropzone div:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@drop.prevent="dropFile"
~~~
This is the code of the **dropFile** method:
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;h2&gt;
  
  
  Complete Component Code:
&lt;/h2&gt;

&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;h2&gt;
  
  
  A quick word about this series:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Flash Code&lt;/strong&gt; is a series of posts showing you how to build various components using Vuejs. This was the first post, and many will come out in the following weeks.&lt;br&gt;
I would love to hear your thoughts on this first post (this is actually my first blog post ever).&lt;br&gt;
Thanks for reading.&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
