<?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: berlikaliku</title>
    <description>The latest articles on DEV Community by berlikaliku (@berlikaliku).</description>
    <link>https://dev.to/berlikaliku</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%2F1128241%2F34a0c052-c574-4d13-acad-a2fb1634330d.jpg</url>
      <title>DEV Community: berlikaliku</title>
      <link>https://dev.to/berlikaliku</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/berlikaliku"/>
    <language>en</language>
    <item>
      <title>Understanding Web Components and It's Implementation Without Frameworks</title>
      <dc:creator>berlikaliku</dc:creator>
      <pubDate>Sun, 06 Aug 2023 16:10:40 +0000</pubDate>
      <link>https://dev.to/berlikaliku/understanding-web-components-and-its-implementation-without-frameworks-58cp</link>
      <guid>https://dev.to/berlikaliku/understanding-web-components-and-its-implementation-without-frameworks-58cp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Ever wondered how custom HTML elements function in modern frameworks such as React, Angular, Vue, and more?&lt;/strong&gt; In this series, I'll explain how they work and even demonstrate how to replicate their behavior without using any frameworks. All you need is HTML and vanilla JavaScript. And don't worry, we'll cover styling in the next episode. Stay tuned!&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;custom HTML element&lt;/strong&gt;, also known as a &lt;strong&gt;Web Component&lt;/strong&gt;, is a self-defined tag in HTML that extends the browser's built-in elements. It lets developers create custom functionality and behavior encapsulated within a single tag. These elements can be reused across projects, making code more maintainable and scalable without being tied to specific frameworks or libraries. Web Components aim to enhance web development by providing a standardized way to create and share reusable components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Custom HTML Elements Work:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Custom HTML elements, or Web Components, leverage the &lt;strong&gt;Shadow DOM&lt;/strong&gt; and the &lt;strong&gt;Custom Elements API&lt;/strong&gt; to function effectively. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Shadow DOM:&lt;/strong&gt; The Shadow DOM allows encapsulation by creating a hidden DOM subtree within a custom element. This isolation prevents styles and scripts from leaking in or out, ensuring that the component's internal structure and styling do not interfere with the rest of the page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Custom Elements API:&lt;/strong&gt; This API enables developers to define new custom elements and their behavior. By using the &lt;code&gt;customElements.define()&lt;/code&gt; method, developers can register a new tag name along with its corresponding class constructor. This association allows the browser to recognize and instantiate the custom element whenever it appears in the HTML code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once registered, developers can define the custom element's lifecycle callbacks, such as &lt;code&gt;connectedCallback()&lt;/code&gt; and &lt;code&gt;disconnectedCallback()&lt;/code&gt;, to handle element insertion and removal events. Additionally, custom elements can have their own properties, attributes, and methods to manage their behavior.&lt;/p&gt;

&lt;p&gt;By combining the Shadow DOM and the Custom Elements API, developers can create reusable and encapsulated components with custom functionality, unleashing the potential of Web Components in modern web development. We'll talk about Shadow DOM later, so let's get started!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a New Project Folder and Opening Your Preferred Code Editor:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To begin, initiate a fresh folder for our project. Next, navigate to the folder using your file explorer and open your preferred code editor. This will serve as the starting point for our development journey.&lt;/p&gt;

&lt;p&gt;After setting up the project folder and launching your favorite code editor, proceed to create two files named &lt;code&gt;index.html&lt;/code&gt; and &lt;code&gt;app.js&lt;/code&gt;. These files will serve as the foundation for our project, enabling us to build and structure the web application effectively.&lt;/p&gt;

&lt;p&gt;Here's our &lt;code&gt;index.html&lt;/code&gt; file should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Custom HTML Elements&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;"app.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"module"&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;/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;If you notice I declare the type of the script as &lt;code&gt;module&lt;/code&gt; because I want to import all of the &lt;strong&gt;Custom HTML Elements&lt;/strong&gt; there.&lt;/p&gt;

&lt;p&gt;And here's how our &lt;code&gt;app.js&lt;/code&gt; file should look like for a starter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOM Content Loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMContentLoaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In this file, we will import all of our custom HTML elements and load them only when the &lt;code&gt;DOMContentLoaded&lt;/code&gt; event occurs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's get started with creating the new folder and organizing our custom HTML elements.&lt;/p&gt;

&lt;p&gt;Step 1: Create a new folder in the project root directory. We'll call it &lt;code&gt;components&lt;/code&gt; or anything that suits you.&lt;/p&gt;

&lt;p&gt;Step 2: Inside the &lt;code&gt;components&lt;/code&gt; folder, create another folder called &lt;code&gt;hello-world&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Step 3: Within the &lt;code&gt;hello-world&lt;/code&gt; folder, create the necessary JS files.&lt;/p&gt;

&lt;p&gt;Once you've completed these steps, your project structure should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- project-root
  - components
    - hello-world
      - hello-world.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a look at &lt;code&gt;hello-world.js&lt;/code&gt;, it's empty, right? Now let's write some code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;HelloWorld&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;HTMLElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;connectedCallback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;p&amp;gt;hello world&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;customElements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello-world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HelloWorld&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this code, we've defined a class &lt;code&gt;HelloWorld&lt;/code&gt; that extends &lt;code&gt;HTMLElement&lt;/code&gt;. Inside the constructor, we call &lt;code&gt;super()&lt;/code&gt; to ensure proper inheritance. Then, in the &lt;code&gt;connectedCallback&lt;/code&gt; method, we set the inner HTML of the custom element to &lt;code&gt;&amp;lt;p&amp;gt;hello world&amp;lt;/p&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, when you include the &lt;code&gt;hello-world.js&lt;/code&gt; script in your HTML file and use the custom element &lt;code&gt;&amp;lt;hello-world&amp;gt;&lt;/code&gt;, it will render the "hello world" message as specified in the &lt;code&gt;connectedCallback&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Your &lt;code&gt;index.html&lt;/code&gt; file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Custom HTML Elements&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;"app.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"module"&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&amp;gt;&amp;lt;/hello-world&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;"components/hello-world/hello-world.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;/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;And that's it! We've had a blast exploring the basics of web components and how to create our very own custom element from scratch, all without any frameworks!&lt;/p&gt;

&lt;p&gt;But guess what? We're just getting started! In my next blog series, I'm thrilled to take you on a journey to the land of dynamic components. 🚀 We'll learn how to make our web components interactive and data-driven, giving them superpowers to create even cooler and more flexible web applications.&lt;/p&gt;

&lt;p&gt;So, grab your coding gear and get ready for some serious fun! Stay tuned for the upcoming blog series, where we'll dive into the exciting world of dynamic web components. Until then, happy coding, and I'll catch you in the next installment! 😉&lt;/p&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>webcomponents</category>
    </item>
  </channel>
</rss>
