<?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: Tebin Raouf</title>
    <description>The latest articles on DEV Community by Tebin Raouf (@tebin).</description>
    <link>https://dev.to/tebin</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%2F893083%2F5babc91d-d58b-44ee-a99b-c37a641c5283.jpeg</url>
      <title>DEV Community: Tebin Raouf</title>
      <link>https://dev.to/tebin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tebin"/>
    <language>en</language>
    <item>
      <title>Building Web Components 101 - Part 4</title>
      <dc:creator>Tebin Raouf</dc:creator>
      <pubDate>Mon, 26 Sep 2022 04:52:56 +0000</pubDate>
      <link>https://dev.to/tebin/building-web-components-101-part-4-o0j</link>
      <guid>https://dev.to/tebin/building-web-components-101-part-4-o0j</guid>
      <description>&lt;p&gt;Part 3 covers how to create custom attributes. Let's continue in this article on how to create custom events for our custom web components.&lt;/p&gt;

&lt;p&gt;I will start with where we left off in Part 3. To access the source code in this article, you can clone the project at &lt;a href="https://github.com/tebinraouf/learning-fast/tree/building-web-components-101-part-4" rel="noopener noreferrer"&gt;https://github.com/tebinraouf/learning-fast/tree/building-web-components-101-part-4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;or you can use Stackblitz&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://stackblitz.com/github/tebinraouf/learning-fast/tree/building-web-components-101-part-4?file=index.html" rel="noopener noreferrer"&gt;
      stackblitz.com
    &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;We use attributes to send/pass data to the web component. Events are useful to get data or send data to the consumer of the web component so that they can use it to build application logic.&lt;/p&gt;

&lt;p&gt;To create a custom event, there are two steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Identify the element in the web component template to receive the custom event.&lt;/li&gt;
&lt;li&gt;Emit the event so that users can add the custom event and get any data. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here, we want to add &lt;code&gt;onClick&lt;/code&gt; to the &lt;code&gt;h1&lt;/code&gt; element so that every time we click, we send new data to the customer. In this case, when we click, we send back a new Albert Einstein quote. It's up to the user how to handle this new quote. In our example, we update the quote.&lt;/p&gt;

&lt;p&gt;Steps in detail:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update &lt;code&gt;h1.template.ts&lt;/code&gt; and add an &lt;code&gt;onClick&lt;/code&gt; event listener. In FAST, the syntax to add an event listener is &lt;code&gt;@click="${x =&amp;gt; x.handler()}"&lt;/code&gt; where &lt;code&gt;@click&lt;/code&gt; is the event name, which is &lt;code&gt;onClick&lt;/code&gt;, and &lt;code&gt;x.handler()&lt;/code&gt; is the handler/controller on the element class, which will be defined in &lt;code&gt;h1.ts&lt;/code&gt; where the custom element class is created.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;import { html } from "@microsoft/fast-element";
&lt;/span&gt;&lt;span class="err"&gt;

&lt;/span&gt;&lt;span class="p"&gt;export const template = html`
&lt;/span&gt;&lt;span class="gd"&gt;-    &amp;lt;h1 style="color: ${x =&amp;gt; x.color}"&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+    &amp;lt;h1 style="color: ${x =&amp;gt; x.color}" @click="${x =&amp;gt; x.handler()}"&amp;gt;
&lt;/span&gt;        &amp;lt;slot&amp;gt;&amp;lt;/slot&amp;gt;
    &amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Update &lt;code&gt;h1.ts&lt;/code&gt; to include a private method &lt;code&gt;handler&lt;/code&gt;. This could be any name.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;handler&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="nf"&gt;$emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;colorhandler&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing so, now our web component consumer can add a custom event &lt;code&gt;colorhandler&lt;/code&gt; to the web component like below:&lt;/p&gt;

&lt;p&gt;We added an id to the custom element.&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="nt"&gt;&amp;lt;tebin-h1&lt;/span&gt; &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"blue"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blue-title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then use &lt;code&gt;addEventListener&lt;/code&gt; to add a custom event, which is named &lt;code&gt;colorhandler&lt;/code&gt;.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blue-title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&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;colorhandler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//business logic&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to send data to the web element consumer?
&lt;/h2&gt;

&lt;p&gt;In this example, we are randomly sending a new quote through our custom event &lt;code&gt;colorhandler&lt;/code&gt;. To do so, when we &lt;code&gt;emit&lt;/code&gt; the &lt;code&gt;colorhandler&lt;/code&gt;, we send the data with it like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;quotes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We cannot solve our problems with the same thinking we used when we created them&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The true sign of intelligence is not knowledge but imagination.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I have no special talent. I am only passionately curious.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The only reason for time is so that everything doesn't happen at once.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Only a life lived for others is a life worthwhile.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;random&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;quotes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="nf"&gt;$emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;colorhandler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quotes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;random&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;p&gt;The data can be anything. In this case, we send back a string.&lt;/p&gt;

&lt;p&gt;Lastly, the web element consumer can get the quote through the event detail i.e. &lt;code&gt;e.detail&lt;/code&gt; like below.&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="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blue-title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&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;colorhandler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ae-quote&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;detail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, every time someone clicks on the custom web element title, the description changes with a new quote.&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%2Fuploads%2Farticles%2F8udzb5lyvx2eou4f9r80.gif" 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%2Fuploads%2Farticles%2F8udzb5lyvx2eou4f9r80.gif" alt="A gif that shows different quotes"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webcomponents</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Building Web Components 101 - Part 3</title>
      <dc:creator>Tebin Raouf</dc:creator>
      <pubDate>Mon, 26 Sep 2022 02:52:15 +0000</pubDate>
      <link>https://dev.to/tebin/building-web-components-101-part-3-2e8k</link>
      <guid>https://dev.to/tebin/building-web-components-101-part-3-2e8k</guid>
      <description>&lt;p&gt;In Part 2, I went through the fundamentals of FAST and created a custom component using FAST Element. In this article, I will explain how to create custom attributes in FAST.&lt;/p&gt;

&lt;p&gt;I will start with where we left off in Part 2. To access the source code in this article, you can clone the project at &lt;a href="https://github.com/tebinraouf/learning-fast/tree/building-web-components-101-part-3" rel="noopener noreferrer"&gt;https://github.com/tebinraouf/learning-fast/tree/building-web-components-101-part-3&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;or you can use Stackblitz &lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://stackblitz.com/github/tebinraouf/learning-fast/tree/building-web-components-101-part-3?file=index.html" rel="noopener noreferrer"&gt;
      stackblitz.com
    &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  How to create a custom attribute?
&lt;/h2&gt;

&lt;p&gt;In Part 2, we create a custom &lt;code&gt;&amp;lt;tebin-h1&amp;gt;&lt;/code&gt; component where we hard coded the heading colour. In this article, we will create a custom attribute so that we can define the custom attribute value when the component is used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;tebin-h1&amp;gt;Albert Einstein&amp;lt;/tebin-h1&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&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%2Fuploads%2Farticles%2Fa9am78ygeyb27q1gtwkt.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%2Fuploads%2Farticles%2Fa9am78ygeyb27q1gtwkt.png" alt="A custom Albert Einstein title"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, the title is brown since we defined it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//h1.style.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@microsoft/fast-element&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="s2"&gt;`
    :host {
        display: block;
    }
    h1 {
        color: brown
    }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to create a &lt;code&gt;color&lt;/code&gt; attribute so that we can set it when the component is used like below:&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="nt"&gt;&amp;lt;tebin-h1&lt;/span&gt; &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"yellow"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;tebin-h1&lt;/span&gt; &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"#41aea9"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;tebin-h1&lt;/span&gt; &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"blue"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to create custom attributes in FAST?
&lt;/h2&gt;

&lt;p&gt;FAST offers &lt;code&gt;@attr&lt;/code&gt; decorator to create attributes. Let's go through the steps required to use &lt;code&gt;@attr&lt;/code&gt; decorator:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a property on the custom component class. In this case, the class for &lt;code&gt;tebin-h1&lt;/code&gt; component is defined in &lt;code&gt;h1.ts&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;import { FASTElement, attr } from "@microsoft/fast-element";
import { styles } from "./h1.style.js";
import { template } from "./h1.template.js";
&lt;/span&gt;&lt;span class="err"&gt;

&lt;/span&gt;&lt;span class="p"&gt;export class H1Component extends FASTElement {
&lt;/span&gt;&lt;span class="gi"&gt;+    @attr color: string = 'brown'
&lt;/span&gt;}
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;export const h1component = H1Component.compose({
&lt;/span&gt;    name: "tebin-h1",
    template,
    styles,
});
&lt;span class="err"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default the value is set to brown. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The next step is to update the template so that the value is set to the &lt;code&gt;h1&lt;/code&gt; tag. We are going to use inline style for this demo. However, you would normally use the the css tag or scss to style your components. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;h1.template.ts&lt;/code&gt; now looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;import { html } from "@microsoft/fast-element";
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;export const template = html`
&lt;/span&gt;&lt;span class="gd"&gt;-    &amp;lt;h1&amp;gt;
&lt;/span&gt;&lt;span class="gi"&gt;+    &amp;lt;h1 style="color: ${x =&amp;gt; x.color}"&amp;gt;
&lt;/span&gt;        &amp;lt;slot&amp;gt;&amp;lt;/slot&amp;gt;
    &amp;lt;/h1&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;FAST uses MVVM - &lt;em&gt;Model-View-ViewModel&lt;/em&gt; pattern. Since we defined the &lt;code&gt;color&lt;/code&gt; property on the class, which is the Model, then we can bind it on the view by using &lt;code&gt;${x =&amp;gt; x.color}&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly, we will delete the style that we defined in the &lt;code&gt;h1.style.ts&lt;/code&gt; file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;import { css } from "@microsoft/fast-element";
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;export const styles = css`
&lt;/span&gt;    :host {
        display: block;
    }
&lt;span class="gd"&gt;-    h1 {
-        color: brown
-    }
&lt;/span&gt;`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With only a few changes, our custom element now has a custom attribute. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Markup:&lt;/strong&gt;&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="nt"&gt;&amp;lt;tebin-h1&lt;/span&gt; &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"yellow"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Life is like riding a bicycle. To keep your balance, you must keep moving.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;tebin-h1&lt;/span&gt; &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"#41aea9"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Life is like riding a bicycle. To keep your balance, you must keep moving.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;tebin-h1&lt;/span&gt; &lt;span class="na"&gt;color=&lt;/span&gt;&lt;span class="s"&gt;"blue"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Life is like riding a bicycle. To keep your balance, you must keep moving.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&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%2Fuploads%2Farticles%2F8fbt11c2xbw31qj563p6.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%2Fuploads%2Farticles%2F8fbt11c2xbw31qj563p6.png" alt="Showing Einstein title in different colours"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the next article, we will talk about how to bind events in FAST.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>webcomponents</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Building Web Components 101 - Part 2</title>
      <dc:creator>Tebin Raouf</dc:creator>
      <pubDate>Wed, 14 Sep 2022 05:53:37 +0000</pubDate>
      <link>https://dev.to/tebin/building-web-components-101-part-2-1ima</link>
      <guid>https://dev.to/tebin/building-web-components-101-part-2-1ima</guid>
      <description>&lt;p&gt;In &lt;a href="https://dev.to/tebin/building-web-components-101-part-1-5p5"&gt;Part 1&lt;/a&gt;, I went through the basics of Web Components and the problem it solves. In this article, I will explore &lt;a href="https://www.fast.design/"&gt;FAST&lt;/a&gt; to create Web Components or custom elements. &lt;/p&gt;

&lt;p&gt;As I stated in Part 1, there is no need for abstraction layers, however, abstraction layers provide patterns and best practices for engineers to follow. Patterns and best practices take time to learn but they will speed up developments and they will help to make our code more readable by ourselves in the future or by anyone else. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is FAST?
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="https://www.fast.design/"&gt;https://www.fast.design/&lt;/a&gt;, FAST is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The adaptive interface system for modern web experiences&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's clarify that. FAST is a system to build web experiences which include custom elements, design systems, single page applications, or any other front end sites. With FAST, we get to follow some basic patterns to build components and sites that scale easily. &lt;/p&gt;

&lt;h2&gt;
  
  
  FAST as a system includes the following:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;FAST Element:&lt;/strong&gt; an extension of HTMLElement to create custom elements. This is a thin layer of abstraction to make it easier to create custom elements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FAST Foundations:&lt;/strong&gt; a set of style-less web components such as Modal, button, badge, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FAST Toolings:&lt;/strong&gt; a set of tools/CLI help scaffold component creations and design system creations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FAST Integrations and Utilities:&lt;/strong&gt; additional and optional libraries to add even more functionalities to the FAST system such as Routing, SSR, Animations, Colors, React wrapper, Blazer wrapper, and more.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to build a component with FAST?
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://dev.to/tebin/building-web-components-101-part-1-5p5"&gt;Part 1&lt;/a&gt;, we built a custom brown &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag and we named it &lt;code&gt;&amp;lt;tebin-h1&amp;gt;&lt;/code&gt;. Let's build the same component with FAST. &lt;/p&gt;

&lt;p&gt;To create a custom element, we will be using &lt;code&gt;fast-element@v2.0.0-beta.4&lt;/code&gt; package. &lt;/p&gt;

&lt;p&gt;To go through how to build with FAST, I've setup a basic project with Webpack. In the spirit of using as much as possible from FAST, I used FAST's &lt;a href="https://github.com/microsoft/fast/tree/master/examples/todo-app"&gt;ToDo's example&lt;/a&gt; to start with. Kudos to &lt;strong&gt;&lt;em&gt;FAST team&lt;/em&gt;&lt;/strong&gt; for providing these examples.&lt;/p&gt;

&lt;p&gt;You can find the repository here (&lt;a href="https://github.com/tebinraouf/learning-fast/tree/building-web-components-101-part-2"&gt;https://github.com/tebinraouf/learning-fast/tree/building-web-components-101-part-2&lt;/a&gt;) to follow up with the rest of this article.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/tebinraouf/learning-fast.git
&lt;span class="nb"&gt;cd &lt;/span&gt;learning-fast
yarn or npm &lt;span class="nb"&gt;install
&lt;/span&gt;yarn build
yarn dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or use &lt;a href="https://stackblitz.com/github/tebinraouf/learning-fast/tree/building-web-components-101-part-2?file=index.html"&gt;Stackblitz&lt;/a&gt; for the project.&lt;/p&gt;

&lt;p&gt;The project structure is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
├── README.md
├── index.html
├── package.json
├── src
│   ├── components
│   │   └── h1
│   │       ├── h1.style.ts
│   │       ├── h1.template.ts
│   │       ├── h1.ts
│   │       └── index.ts
│   └── index.ts
├── tsconfig.json
└── webpack.config.cjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's focus on the component directory. FAST breaks down component creation into 4 pieces: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Component styles, which are defined in &lt;code&gt;*.style.ts&lt;/code&gt; file. In this case, &lt;code&gt;h1.style.ts&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Component template, which is defined in &lt;code&gt;*.template.ts&lt;/code&gt; file. In this case, &lt;code&gt;h1.template.ts&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Component composition, which is defined in &lt;code&gt;*.ts&lt;/code&gt; file. In this case, &lt;code&gt;h1.ts&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Component definition or registration, which is defined in index.ts file. In this case, &lt;code&gt;h1/index.ts&lt;/code&gt; file.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note: these 4 separate files are not required. You can combine all four into one single file, which could be difficult to maintain in a large project. These 4 different files provide clear separation of concern.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's dive deep into each part of the component:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;h1.style.ts&lt;/code&gt; holds all the styles for your component. Out of the box, we can provide CSS as string or CSS through FAST's &lt;code&gt;css&lt;/code&gt; tag like below:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@microsoft/fast-element&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="s2"&gt;`
    :host {
        display: block;
    }
    h1 {
        color: brown;
    }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or CSS as string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`
    :host {
        display: block;
    }
    h1 {
        color: brown;
    }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or with some configurations, you could use SASS. For future articles, &lt;/p&gt;

&lt;p&gt;&lt;em&gt;I will write about how to configure SASS for FAST. However, the idea is to convert SASS to CSS string, which can then be used by the FAST component directly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzmLvEWb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emvdkn6oi9bhc7qy2j4p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzmLvEWb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emvdkn6oi9bhc7qy2j4p.png" alt="A flow diagram to show converting SASS to CSS for FAST component" width="880" height="157"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;h1.template.ts&lt;/code&gt; holds the template for the custom component. Out of the box, FAST provides &lt;code&gt;html&lt;/code&gt; tag build templates like below:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;import { html } from "@microsoft/fast-element";

export const template = html`
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;slot&amp;gt;&amp;lt;/slot&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;slot&lt;/code&gt; is an HTML element. It allows us to provide dynamic content or markup to our custom element. We can think of &lt;code&gt;slot&lt;/code&gt; as placeholder. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_02QEsoq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc7lpr6m0sbiujlm6j6w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_02QEsoq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc7lpr6m0sbiujlm6j6w.png" alt="A diagram to show slot as a placeholder" width="880" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there is one slot in the template, this is considered as the default slot. Text or mark up can be used in place of a slot. &lt;/p&gt;

&lt;p&gt;If there are more than one slot, we would need to name them to identify the slot. To get more details, you can navigate to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;h1.ts&lt;/code&gt; is used to compose or combine the template and the style. We also define the custom component name, which will be used when the web component is used.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;*.compose({})&lt;/code&gt; returns the custom element definition, which we will use to register the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FASTElement&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@microsoft/fast-element&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./h1.style.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./h1.template.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;H1Component&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;FASTElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;h1component&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;H1Component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tebin-h1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//the component will be used like &amp;lt;tebin-h1&amp;gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;styles&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;ul&gt;
&lt;li&gt;
&lt;code&gt;components/index.ts&lt;/code&gt; is used to define/register the component based on the component definition that we get from &lt;code&gt;*.compose()&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;h1component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./h1.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;h1component&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, to use the custom component, we will add the custom element to &lt;code&gt;index.html&lt;/code&gt;&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="nt"&gt;&amp;lt;tebin-h1&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Life is like riding a bicycle. To keep your balance, you must keep moving.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;tebin-h1&amp;gt;&lt;/span&gt;Pablo Picasso&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Everything you can imagine is real.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DOM Element:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S0JM_u-1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufzh1qv7s6jn4qmhn7mz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S0JM_u-1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufzh1qv7s6jn4qmhn7mz.png" alt="DOM Element for tebin-h1 custom element" width="880" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLMVos0n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3goskn0th1eblzwwmxh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLMVos0n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3goskn0th1eblzwwmxh.png" alt="A quote from Albert Einstein and Pablo Picasso" width="880" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn more, FAST provides excellent documentations on their official site at &lt;a href="https://www.fast.design/"&gt;https://www.fast.design/&lt;/a&gt;. I will encourage you to check it out.&lt;/p&gt;

&lt;p&gt;Lastly, by following a few simple patterns and structure, we were able to create a FAST custom element with ease. This article allows us to talk about more advance topics in the future such as directives, observables, and states. In future articles, I will talk about these and other FAST offerings.&lt;/p&gt;

</description>
      <category>fastdesign</category>
      <category>webcomponents</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Building Web Components 101 - Part 1</title>
      <dc:creator>Tebin Raouf</dc:creator>
      <pubDate>Sat, 10 Sep 2022 05:50:01 +0000</pubDate>
      <link>https://dev.to/tebin/building-web-components-101-part-1-5p5</link>
      <guid>https://dev.to/tebin/building-web-components-101-part-1-5p5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Web Components is a suite of different technologies allowing you to create reusable custom elements — with their functionality encapsulated away from the rest of your code — and utilize them in your web apps. ~ &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's clarify what that really means. Currently, there are many ways to build re-usable components such as using React, Vue, Angular, etc... However, if a component is built with React, it does not work in Angular, though there are some libraries to make it work. However, natively React components do not work in Angular and vice versa. Web Components addresses this challenge. &lt;/p&gt;

&lt;p&gt;Since Web Component is an HTML standard, it can be understood or rendered by the browser without any libraries. &lt;/p&gt;

&lt;p&gt;To better understand this, let's look at a standard HTML tag such as&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="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Cool Title&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For libraries and frameworks to render &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, as developers we do not need to load any scripts. We expect the library and framework to simply build the page and the browser to simply render it.  &lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;React&lt;/strong&gt;, we could use an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./style.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Cool Title&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;strong&gt;Angular&lt;/strong&gt;, we could use an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag like below:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Input&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&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="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;h1&amp;gt;Cool Title&amp;lt;/h1&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;`h1 { font-family: Lato; }`&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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;p&gt;And a browser will render it because it understands HTML standard &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag. &lt;/p&gt;

&lt;p&gt;What if we were able to build our own tags such as &lt;code&gt;&amp;lt;tebin-h1&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;fancy-h1&amp;gt;&lt;/code&gt; tags, etc...and tell the browser to render them exactly as it would render &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Web Components, we can build custom tags or elements and then inform the browser to render them. These custom tags are called Web Components.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Web Components are re-usable. The same way an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag is re-usable, a custom web component is also re-usable because it is natively supported by the browser. &lt;/p&gt;

&lt;p&gt;Browser compatibility of Web Components and its different feature compatibility can be found &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components#browser_compatibility" rel="noopener noreferrer"&gt;here&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Since Web Components are supported natively by browsers, they can be used in any libraries and frameworks either directly or with configurations. &lt;a href="https://custom-elements-everywhere.com/" rel="noopener noreferrer"&gt;https://custom-elements-everywhere.com/&lt;/a&gt; is a great site to check custom elements support status by different libraries and frameworks.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to build Web Components
&lt;/h2&gt;

&lt;p&gt;Since Web Components are natively supported by browsers, we technically can write vanilla HTML, JavaScript, and CSS to build our custom web component. Below is a complete &lt;code&gt;index.html&lt;/code&gt; page to create a custom tag that I named &lt;code&gt;&amp;lt;tebin-h1&amp;gt;&lt;/code&gt;. This custom &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag has a brown colour by default.&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="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 Component 101 - Part 1&lt;span class="nt"&gt;&amp;lt;/title&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;h1&amp;gt;&lt;/span&gt;Quotes&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;tebin-h1&amp;gt;&lt;/span&gt;Albert Einstein&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Life is like riding a bicycle. To keep your balance, you must keep moving.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;tebin-h1&amp;gt;&lt;/span&gt;Pablo Picasso&lt;span class="nt"&gt;&amp;lt;/tebin-h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Everything you can imagine is real.&lt;span class="nt"&gt;&amp;lt;/p&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;script&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;// Create a class for the element&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomTebinH1&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;HTMLElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Always call super first in constructor&lt;/span&gt;
            &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// Create a style tag&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;style&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1 {color: brown}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// Create an h1 tag&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;h1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&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;textContent&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;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// Create a shadow root&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shadow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;attachShadow&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;open&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

            &lt;span class="c1"&gt;// Append it to the shadow root&lt;/span&gt;
            &lt;span class="nx"&gt;shadow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h1&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="c1"&gt;// Define the new element&lt;/span&gt;
    &lt;span class="nx"&gt;customElements&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tebin-h1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;CustomTebinH1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&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;Result:&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%2Fuploads%2Farticles%2Fpl562xj5mwg28e5zgmyd.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%2Fuploads%2Farticles%2Fpl562xj5mwg28e5zgmyd.png" alt="A screenshot to show how the HTML above is rendered. It shows two quotes by Albert Einstein and Pablo Picasso"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's break it down. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Usage: &lt;code&gt;&amp;lt;tebin-h1&amp;gt;&amp;lt;/tebin-h1&amp;gt;&lt;/code&gt; we use custom elements or web components the same way we use standard HTML elements. &lt;/li&gt;
&lt;li&gt;Build: There are two steps: &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Create a JavaScript class for the custom component functionality.&lt;br&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Define or register the custom element so that the browser understands how to render the custom element. This is done using &lt;code&gt;customElements.define('tebin-h1', CustomTebinH1);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Obviously, this is a very simple custom element. When we add more functionalities, our class becomes more complex. As engineers, we like to define patterns or follow best practices to build software. This is where abstraction layers become very useful and saves a lot of time. &lt;/p&gt;

&lt;p&gt;There are already great libraries that provide abstractions and good patterns to speed up development some of these libraries are &lt;a href="https://www.fast.design/docs/fast-element/getting-started/" rel="noopener noreferrer"&gt;FAST&lt;/a&gt;, &lt;a href="https://lit.dev/" rel="noopener noreferrer"&gt;Lit&lt;/a&gt;, &lt;a href="https://tmorin.github.io/ceb/index.html" rel="noopener noreferrer"&gt;Custom Element Builder&lt;/a&gt;, etc...&lt;/p&gt;

&lt;p&gt;In future posts, I will dive deep into FAST and how it helps to build custom elements FASTer.&lt;/p&gt;

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