<?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: Hemanth Kumar</title>
    <description>The latest articles on DEV Community by Hemanth Kumar (@runtimeterror17).</description>
    <link>https://dev.to/runtimeterror17</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%2F358462%2F463d7b6c-f9f8-4be6-a9fe-4035c6b8cb36.jpg</url>
      <title>DEV Community: Hemanth Kumar</title>
      <link>https://dev.to/runtimeterror17</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/runtimeterror17"/>
    <language>en</language>
    <item>
      <title>How far can we go with web components [Part-1]</title>
      <dc:creator>Hemanth Kumar</dc:creator>
      <pubDate>Mon, 06 Apr 2020 15:20:30 +0000</pubDate>
      <link>https://dev.to/runtimeterror17/how-far-can-we-go-with-web-components-part-1-7el</link>
      <guid>https://dev.to/runtimeterror17/how-far-can-we-go-with-web-components-part-1-7el</guid>
      <description>&lt;p&gt;Web components are reusable custom elements that have a shadow dom attached to it.&lt;br&gt;&lt;br&gt;
Any web component can basically be divided into 3 main parts;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Custom element&lt;/strong&gt;: The javascript API's responsible for creating the element-tag and defining its behavior&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shadow DOM&lt;/strong&gt;: The main element tree that is used to render the custom element to the main-DOM&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTML templates&lt;/strong&gt;: The &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;slot&amp;gt;&lt;/code&gt; tags are used to define the HTML present inside the shadow DOM
To read more about web components click &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;p&gt;To follow along you should know javascript and a bit of &lt;a href="https://svelte.dev/"&gt;svelteJS&lt;/a&gt; or any other front-end framework which supports web components.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You don't need to know svelte or any other framework to create web components but a framework makes it a bit easier to create them&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Why svelte?
&lt;/h2&gt;

&lt;p&gt;Svelte compiles your code directly into vanilla javascript so if you write your web component in vanilla javascript the bundle size would be very similar and svelte comes with some cool features which we would be using.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;Get the svelte web component template and install the dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npx degit sveltejs/component-template my-new-component &lt;span class="c"&gt;# name of your component goes here&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;my-new-component
npm i &lt;span class="c"&gt;# or yarn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;Once downloading the template is done there are minor changes required to make it work properly&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Add the &lt;code&gt;customElement&lt;/code&gt; property in the &lt;code&gt;rollup.config.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nx"&gt;svelte&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;customElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}),&lt;/span&gt;
        &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;&amp;lt;svelte:options tag="your-tag-name"/&amp;gt;&lt;/code&gt; at the top of the &lt;code&gt;Component.svelte&lt;/code&gt; file in the &lt;code&gt;src&lt;/code&gt; directory and you should be good to go.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Hello World web component
&lt;/h2&gt;

&lt;p&gt;To create a web component you need to write some code in the &lt;code&gt;Component.svelte&lt;/code&gt;, something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;svelte:options&lt;/span&gt; &lt;span class="na"&gt;tag=&lt;/span&gt;&lt;span class="s"&gt;"hello-world"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;main&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello { name }!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;clearly &lt;code&gt;name&lt;/code&gt; is a prop and we will see how to send data into the component but for now just run &lt;code&gt;npm run build&lt;/code&gt;. This will create two files in the &lt;code&gt;dist&lt;/code&gt; folder namely &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;index.mjs&lt;/code&gt;. That's it your web component is created&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the hello-world component
&lt;/h2&gt;

&lt;p&gt;To the hello-world component that we just created we need to import the &lt;code&gt;index.js&lt;/code&gt; into a html and run it on a server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Web components&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./index.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;hello-world&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"world"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/hello-world&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;clearly the prop &lt;code&gt;name&lt;/code&gt; that needs to be passed is sent as an attribute to the web component and the web component recieves it and displays it, however if we don't pass anything it will display &lt;code&gt;undefined&lt;/code&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks for reading 😄
&lt;/h3&gt;

</description>
      <category>svelte</category>
      <category>webcomponenets</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
