How does Lightning Web Component JS file work?

JS file is included by default in Lightning Web Component file tree structure and this JS file must be needed to execute Lightning Web Component. The naming convention of this file is componentName.js. JS files are based on ES6 modules.

The structure is:

import { LightningElement } from 'lwc';
export default class WelcomeWindow extends LightningElement {}

Here the componentName is “welcomeWindow“. If you see the welcomeWindow.js file, you can see the class name is WelcomeWindow which is in Pascal case as discussed in previous session. First two lines in the JS file are the default lines provided by Salesforce.com.

import statement is used to import a class or function or variable and export is used to call this class or component from other component  as per the general web standard rule. Here, import statement imports a custom wrapper of the standard HTML custom element, LightningElement from the core module lwc and export helps to expose your Lightning Web Component. By default, everything is locally scoped to the module in JS file.

Let’s say, we can take the same component mentioned in the previous session. Now, we will try to show welcome message using property which will be set from component js file.

Step 1: We can use the same Lightning Component named “welcomeWindow“, otherwise we need to create new component. First, we will set the metadata XML file to set the visibility in Lightning App Builder or in Lightning Experience.

welcomeWindow.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="welcomeWindow">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

Step 2: Then need to focus on HTML file.

welcomeWindow.html

<template>
    <lightning-card title="Welcome My Friends">    
        <lightning-layout>
                <lightning-layout-item flexibility="auto" padding="around-small">                    
                    {welcomeMessage}
                </lightning-layout-item>
            </lightning-layout>
    </lightning-card>     
</template>

Here, we have declared a property called “welcomeMessage“.

Step 3: Then we will update the JS file.

welcomeWindow.js

import { LightningElement } from 'lwc';

export default class WelcomeWindow extends LightningElement {
    welcomeMessage = 'Welcome to LWC Training Class';
}

In this JS file, we have set the property “welcomeMessage” to certain value such as “Welcome to LWC Training Class“.

Result

 

 

 

 

 

Let’s discuss about the properties of JS file in the next session.

11,449 total views, 3 views today

Pascal Case, Camel Case, Kebab Case & Snake Case

In Lightning Web Component, we need to maintain the case for Class Name, Variable Name, Component Name etc.
Before going to describe, we need to focus on the respective cases.

Camel Case: Each word in the middle of the respective phrase begins with a capital letter.

Pascal Case: It is a subset of Camel Case where first letter always is capitalized.

Kebab Case: Respective phrase will be transferred to all lowercase with hyphen(-) separating words.

Snake Case: Respective phrase will be separated with underscore(_), no spaces, initial letter with lowercase and first letter with lowercase or uppercase.

So, we will check each case with this phrase “welcomeWindow“.

Camel CasePascal CaseKebab CaseSnake Case
welcomeWindowWelcomeWindowwelcome-windowwelcome_window
Generally, it's used in variable names.Generally, it's used in class names.At the time of component rendering or calling a component or set attribute name from other component in LWC, kebab case is used.Generally, it's used in C/Python language.

26,843 total views, 3 views today

Create First Lightning Web Component

Every Lightning Web Component (LWC) has the following files such as

  1. html [defines the UI layer with html content]
  2. js [defines the behavior of the LWC]
  3. css [defines the look & feel]
  4. svg [defines the Scalable Vector Graphics for the Lightning Web Component]
  5. xml [defines the metadata of the LWC to set the component visibility in the Lightning Experience along with configuration of design attributes for Lightning App Builder]

Let’s say, we are going to create one Lightning Web Component named “welcomeWindow“. So, following files will be created by default in the root folder of “welcomeWindow” Lightning Web Component.

welcomeWindow.html
welcomeWindow.js
welcomeWindow.js-meta.xml

You can create the welcomeWindow.css and welComeWindow.svg file, but it’s optional.

1. Let’s talk about welcomeWindow.js-meta.xml first.

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="welcomeWindow">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

At the time of saving this XML file, please set the value for isExposed tag as true. That means, this component will be visible into Lightning App Builder. If you focus the targets, you can see that we have passed the values for the target as lightning__AppPage, lightning__RecordPage, lightning__HomePage.

lightning_AppPage: LWC is visible for Lightning App.
lightning_RecordPage: LWC is visible for Lightning Record Page.
lightning_HomePage: LWC is visible for Lightning Home Page.

Later we will discuss about other separate configuration tags belongs to XML file.

2. Then, we need to focus on welcomeWindow.html file.

<!-- @name: welcomeWindow @description: used to show only welcome message by Lightning Web Component @author: Santanu Pal @date: 10 FEB 2019 Copyright (c) 2019 -->

<template>
    <lightning-card title="Welcome My Friends">    
        <lightning-layout>
                <lightning-layout-item flexibility="auto" padding="around-small">                    
                        Welcome to LWC Training Class
                </lightning-layout-item>
            </lightning-layout>
    </lightning-card>     
</template>

3. For now, we will not touch the welcomeWindow.js & welcomeWindow.css file.

4. You can see this Lightning Web Component (LWC) in any Lightning App Page, Lightning Record Page & Lightning Home Page based on welcomeWindow.js-meta.xml file.

Result

So, we have created our first Lightning Web Component which is currently displaying only welcome message.

Now, if we will inspect our first Lightning Web Component, we will see the name of our Lightning Web Component is replaced with this <c-welcome-window> in the default namespace. Please see this image:

 

 

 

 

Now, why welcome-window??  Let’s go to the next chapter.

 

Thanks for your time.

8,935 total views, 3 views today

Shadow DOM in Web Component

Shadow DOM is a mechanism to encapsulate the DOM Tree. Shadow DOM is not a part of the main DOM tree. It prevents overriding the style or behavioral of the respective markup elements.

Following image describes the Shadow DOM from the main DOM tree.

Let’s assume, Document is the main root node of the main DOM tree.

Shadow Host is the link node between Shadow Tree and the main DOM tree. Shadow Tree is used to hidden DOM trees from the regular main DOM tree. Shadow Tree starts from Shadow Root(root node of the Shadow DOM tree) which is attached to Shadow Host.

Shadow Boundary is the boundary line where regular DOM tree begins and Shadow DOM Tree ends.

 

 

 

 

 

 

 

 

 

 

 

We can take the same example as described in previous session. Let’s first focus on the HTML file.
We have declared two header statements [Template in HTML 5, (learning web components)] with styleClass named “pageHeaderStyle“.

index.html | to build the customer registration page

<script src="index.js"></script>
<link rel="stylesheet" href="index.css"></link>
<body onload="loadTmplBody();"> 
<template id="tmplId" >
        <p class="pageHeaderStyle">Template in HTML 5</p>
        <p class="pageHeaderStyle">(learning web components)</p>
        <div id="firstdivId">
           <nice-form header="Customer Registration Form" subHeader="A unique venture company" id="niceformId">

           </nice-form>             
        </div> 
</template>
</body>

Now, we are trying to apply the style for styleClass named “pageHeaderStyle”.

index.css | to change the font color for the header contents

.pageHeaderStyle{
    color:red;
}

.pageHeaderStyle{
    color:green;
}

We have written same styleClass in CSS file. Let’s check what will be the result after execution of the HTML file?

We are keeping the same JS file.

index.js | to activate the template with custom element

function loadTmplBody(){   

    var tmpl = document.querySelector("#tmplId");
    tmpl.content.querySelector("#firstdivId").style = "width:100%; height:800px;background: linear-gradient(to bottom right, #ffffff 0%, #003366 100%);";
    var clonTmpl = tmpl.content.cloneNode(true); 
    document.body.appendChild(clonTmpl);

}
class NiceFormClass extends HTMLElement{
    constructor(){
        super();

        this.innerHTML = `<h1>${this.header}</h1>
                          <h3><i>(${this.subHeader})</i></h3>
                          <hr/>
                          <div style="border-style: solid;
                          border-width: 1px;
                          margin-left:3px;
                          border-color: black;background-color:#ccc; width:99.4%;height:200px;"></div>
                          <hr/>
                         `;

    }

    get header(){
        return this.getAttribute('header');
    }

    set header(val){
        this.setAttribute('header', val);
    }

    get subHeader(){
        return this.getAttribute('subHeader');
    }

    set subHeader(val){
        this.setAttribute('subHeader', val);
    }
    
}

customElements.define('nice-form', NiceFormClass);

Now, run the index.html file and you will get the color of both header contents [Template in HTML 5 and (learning web components)] have been overridden as green color, because, the styles are not encapsulated in the main DOM tree.

Result 1

How can we overcome this problem?
We can think about Shadow DOM. Now we will apply the Shadow DOM for this assignment.

We have modified the HTML to define the ids for one header content.

index.html | to apply the html id for the header content (learning lightning component)

<script src="index.js"></script>
<link rel="stylesheet" href="index.css"></link>
<body onload="loadTmplBody();"> 
<template id="tmplId" >
        <p class="pageHeaderStyle">Template in HTML 5</p>
        <p id="secondPgraphId">(learning web components)</p>
        <div id="firstdivId">
           <nice-form header="Customer Registration Form" subHeader="A unique venture company" id="niceformId">
		   </nice-form>             
        </div> 
</template>
</body>

We are keeping the same CSS file as described earlier. You can remove the duplicate pageHeaderStyle class from CSS file.

index.js | to apply the shadow DOM [Line no.8-13]

function loadTmplBody(){   

    var tmpl = document.querySelector("#tmplId");
    tmpl.content.querySelector("#firstdivId").style = "width:100%; height:800px;background: linear-gradient(to bottom right, #ffffff 0%, #003366 100%);";
    var clonTmpl = tmpl.content.cloneNode(true); 
    document.body.appendChild(clonTmpl);

    let host = document.querySelector('#secondPgraphId');
    let shadowRoot = host.createShadowRoot();
    let newP = document.createElement('p');
    newP.className="pageHeaderStyle1";
    newP.textContent="(learning web components)";
    shadowRoot.appendChild(newP);
}
class NiceFormClass extends HTMLElement{
    constructor(){
        super();

        this.innerHTML = `<h1>${this.header}</h1>
                          <h3><i>(${this.subHeader})</i></h3>
                          <hr/>
                          <div style="border-style: solid;
                          border-width: 1px;
                          margin-left:3px;
                          border-color: black;background-color:#ccc; width:99.4%;height:200px;"></div>
                          <hr/>
                         `;

    }

    get header(){
        return this.getAttribute('header');
    }

    set header(val){
        this.setAttribute('header', val);
    }

    get subHeader(){
        return this.getAttribute('subHeader');
    }

    set subHeader(val){
        this.setAttribute('subHeader', val);
    }
    
}

customElements.define('nice-form', NiceFormClass);

Following syntax has been used to apply Shadow DOM.

    let host = document.querySelector('#secondPgraphId');
    let shadowRoot = host.createShadowRoot();
    let newP = document.createElement('p');
    newP.className="pageHeaderStyle1";
    newP.textContent="(learning web components)";
    shadowRoot.appendChild(newP);

First, we got the DOM element for secondPgraphId by using querySelector and created Shadow Root for a particular Shadow Host. Then we have inserted new DOM into the Shadow DOM by declaration of document properties.

You can apply attachShadowRoot() instead of using createshadowRoot() function to apply Shadow DOM.

Result 2

You can see, now the style of second header content (learning web components) can not be changed due to the presence of Shadow DOM.

Now, if you do the inspect for a particular content in the browser, you can see the Shadow Root definition. Please see the following image.

 

18,375 total views, 3 views today

What is Custom Element?

One of the most important feature in HTML 5 is Custom Element. We can define our own tag with the help of Custom Element. To build the Custom Element, we need to create following components.

1. Define the DOMString name of the Custom Element in HTML file. Remember, Custom Element Name will be separated with hyphen(-). You can set the predefined attributes in your own custom element tag.
2. Define the Class Name which will extend the out of the box feature from HTMLElement.
3. Associate the Class Name with the Custom Element Name.

Let’s we have taken the previous example where we will define the custom element.

Objective: To make the Custom HTML form tag by Custom Element in HTML 5.

Step 1: We have created the Custom Element named “nice-form” with certain attributes such as “header” and “subHeader” in index.html file.

index.html | to define the HTML with custom element named “nice-form”

<script src="index.js"></script>
<body onload="loadTmplBody();">
    <template id="tmplId" >
        Template in HTML 5
        <div id="firstdivId">
            <nice-form header="Customer Registration Form" subHeader="A unique venture company" id="niceformId" >
                 
            </nice-form>
        </div>
    </template>    
</body>

Step 2: After that, we have created the Class and associate the Class with the Custom Element in JS file.

index.js | to define the class for the custom element

function loadTmplBody(){
    var tmpl = document.querySelector("#tmplId");
    tmpl.content.querySelector("#firstdivId").style = "width:100%; height:800px;background: linear-gradient(to bottom right, #ffffff 0%, #003366 100%);";
    var clonTmpl = tmpl.content.cloneNode(true);
    document.body.appendChild(clonTmpl);
}
class NiceFormClass extends HTMLElement{
    constructor(){
        super();

        this.innerHTML = `
<h1>${this.header}</h1>

                          
<h3><i>(${this.subHeader})</i></h3>

                          <hr/>
                          
<div style="border-style: solid; border-width: 1px; margin-left:3px; border-color: black;background-color:#ccc; width:99.4%;height:200px;"></div>

                          <hr/>
                         `;

    }

    get header(){
        return this.getAttribute('header');
    }

    set header(val){
        this.setAttribute('header', val);
    }

    get subHeader(){
        return this.getAttribute('subHeader');
    }

    set subHeader(val){
        this.setAttribute('subHeader', val);
    }
    
}

customElements.define('nice-form', NiceFormClass);

We have created a JS Class named “NiceFormClass” for the custom element “nice-form”. As well as we have defined getter and setter methods for header & subHeader properties. We have declared the innerHTML into the constructor of the class to render the HTML when custom element will be executed.

Result

Advantages of Using Custom Element

You can create own custom tag in HTML along with this you can also create API for your own HTML elements.
Re-usability has been increased due to the use of custom element. You can create common custom element and reuse this custom element in the HTML applications.

9,480 total views, 3 views today

How to use HTML Template as a part of Web Component?

HTML template is a mechanism to hold the client-side content but it’s not rendered during page loading until it’s forcefully instantiated from the JavaScript.

Let’s say, we are going to create html and js files to understand the HTML 5 template feature. To do this exercise, we are using VS Code or you can use any HTML editor available in the market now. For now, just we are going to build a background for a HTML file.

Step 1: We have created a html file called index.html

index.html | to define the template

<template id="tmplId" >
        Template in HTML 5
        <div id="firstdivId">
            
        </div>
</template>

Here, we have defined a template tag in which div tag is declared to set the background color.

Step 2: Run this index.html file to see the output. You will not see any output, because template will not be visible by default. We need to write JS to instantiate the template.

Step 3: We have created a JS file called index.js or you can write the script in the same HTML file.

index.js | to push the template into the main DOM

function loadTmplBody(){
    var tmpl = document.querySelector("#tmplId");
    tmpl.content.querySelector("#firstdivId").style = "width:100%; height:800px;background: linear-gradient(to bottom right, #ffffff 0%, #003366 100%);";
    var clonTmpl = tmpl.content.cloneNode(true);
    document.body.appendChild(clonTmpl);
}

Here, we have declared a JS method called loadTmplBody where we have instantiated template. First, we got the template node by running querySekector using template Id i.e., tmplId. After that, we have made style for this template content. Then, we are cloning the template content and append into the main DOM by document.body.appendChild(clonTmpl).

Step 4: Now, we have updated the index.html file.

index.html | to call the JS function during onload

<script src="index.js"></script>
<body onload="loadTmplBody();">
    <template id="tmplId" >
        Template in HTML 5
        <div id="firstdivId">
            
        </div>
    </template>    
</body>

We have attached the JS file and set the JS method called loadTmplBody() during onload. So, at the time of page loading that method will be invoked and JS function will be used to activate the template.
Result

Please run the index.html file and you will get the following result with good background screen in the browser.

Advantages of using template

By default, template content will not display until it’s instantiated through JavaScript, because it’s not attached with the main DOM until it’s externally pushed to the main DOM by JavaScript. So, at the time of page loading browser will detect the respective JS function to activate the template instead of loading the whole DOM in a single instance. Hence, browser performance has been improved using template.

Notes

Instead of calling the JavaScript method from during the page load, you can create one custom button and during onclick event you can call the respective JavaScript method to activate or instantiate the template.

10,017 total views, 3 views today

Communication Types for Lightning Web Components

Same Type of Communication in Lightning Component exists in Lightning Web Component also. Following are the mechanisms to communicate between Lightning Web Components.

Direct Call Lightning Web Component with certain attributes – Remember this communication is in one direction, not by bi-directional or back & forth. In Lightning Component (in Aura framework), the communication can be bi-directional using bound expressions, that’s why it was very hard to maintain and performance was not good during using of unnecessarily/accidentally bi-directional communication. This is a primary difference between Lightning Component & Lightning Web component with respect to Communication mechanism.

Using Method – We can call the method with parameters of the Lightning Web Component from other Lightning Web Component. The flow of this communication is from Parent to Child Component.

Using Custom Event – We can call the Parent Component from Child Component with some properties. The flow of this communication is from Child to Parent Component.

Using Publish-Subscriber model – This is just like Application Event communication like in Lightning Component (in Aura Framework).

Using Slot – This is the HTML markup communication by which we can send the HTML markup from Parent Component to Child Component where <slot> tag is defined.

First of all, we need to understand the Owner, Container & Containment Hierarchy for Communication Purpose. 

 

Okay, let’s start. We will discuss all the above communication process one by one.

 

 

 

11,064 total views, 3 views today

Web Component in HTML 5

Introduction

Before going to understand the Lightning Web Component, we need to focus on some basic features of Web Component introduced at the part of HTML 5. Web Components provide standard component model for the Web and allow encapsulation of standard DOM Tree in HTML. We can encapsulate and create our own APIs, elements in web component.  Following are the primary features provided by Web Component such as

  • HTML Templates: It’s not rendered by default until it’s instantiated from JavaScript.
  • Custom Elements: Own Tags or APIs can be created to describe new HTML elements.
  • Shadow DOM: Encapsulated the main DOM of the HTML.
  • HTML Import/ Export: Able to import or export any HTML files in the main HTML file.

We will learn one by one in the next sessions.

Why Web Component? 

  • We can reuse the elements in the application by import/export mechanism. That means, Web Component increases re-usability of elements in Web Applications.
  • Web Component depends on modular based. So, maintainability is much easier than normal HTML component.
  • We can define APIs by using Custom Element to enhance the productivity as per business.
  • Web Component can extend the existing browser elements to maintain the browser’s accessibility.
  • You can encapsulate the main DOM tree to avoid the same styling for all similar classes.

Browser Support

Web Components are supported in almost all modern browsers now. However, Web Component is best suited for Google Chrome and Opera browser.

 

Are you excited to learn? right?

Hold on…be prepare with paper and pen…

Let’s start our journey……  

12,124 total views, 6 views today