salesforce lwc modal sfdcMonkey.com

Display Popup/Modal Window In Salesforce Lightning Web Component

Lightning Web Component

Hi guys welcome back with another post on lightning web component series. today in this post we are going to learn that how we can create custom modal or popup window in salesforce lightning web component (LWC).

Modals are used to display content in a layer above the app. This paradigm is used in cases such as the creation or editing of a record, as well as various types of messaging and wizards.

Checkout : How to Create Lightning Web Component And Deploy To The Salesforce Org

lec modal sfdcmonkey.com

modalLwc.html [LWC HTML File]

<template>
   <!-- lightning button for open modal window -->
   <lightning-button variant="brand"
      label="Open Modal"
      title="Open Modal"
      onclick={openModal}
      class="slds-m-left_x-small"></lightning-button>

   <!-- modal start -->        
   <template if:true={bShowModal}>
      <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
         <div class="slds-modal__container">
            <!-- modal header start -->
            <header class="slds-modal__header">
               <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" title="Close" onclick={closeModal}>
                  <lightning-icon icon-name="utility:close"
                     alternative-text="close"
                     variant="inverse"
                     size="small" ></lightning-icon>
                  <span class="slds-assistive-text">Close</span>
               </button>
               <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">Modal Header with LWC</h2>
            </header>
            <!-- modal body start -->
            <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
               <p>Sit nulla est ex deserunt exercitation anim occaecat. Nostrud ullamco deserunt aute id consequat veniam incididunt duis in sint irure nisi. Mollit officia cillum Lorem ullamco minim nostrud elit officia tempor esse quis. Cillum sunt ad dolore
                  quis aute consequat ipsum magna exercitation reprehenderit magna. Tempor cupidatat consequat elit dolor adipisicing.
               </p>
               <p>Dolor eiusmod sunt ex incididunt cillum quis nostrud velit duis sit officia. Lorem aliqua enim laboris do dolor eiusmod officia. Mollit incididunt nisi consectetur esse laborum eiusmod pariatur proident. Eiusmod et adipisicing culpa deserunt nostrud
                  ad veniam nulla aute est. Labore esse esse cupidatat amet velit id elit consequat minim ullamco mollit enim excepteur ea.
               </p>
            </div>
            <!-- modal footer start-->
            <footer class="slds-modal__footer">
               <button class="slds-button slds-button_neutral" onclick={closeModal}>Cancel</button>
            </footer>
         </div>
      </section>
      <div class="slds-backdrop slds-backdrop_open"></div>
   </template>
   <!-- modal end -->

</template>

modalLwc.js [LWC JavaScript File]

import { LightningElement,track } from 'lwc';

export default class ModalLwc extends LightningElement {
     @track bShowModal = false;

    /* javaScipt functions start */ 
    openModal() {    
        // to open modal window set 'bShowModal' tarck value as true
        this.bShowModal = true;
    }

    closeModal() {    
        // to close modal window set 'bShowModal' tarck value as false
        this.bShowModal = false;
    }
    /* javaScipt functions end */ 
}
  • @track : Marks a property for internal monitoring. A template or function using
    this property forces a component to rerender when the property’s value changes.
    Use this to store values locally, especially as a user interacts with your
    component.

modalLwc.js-meta.xml [LWC Metadata XML File]

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="modalLwc">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
  <!-- With following targets make component available for lightning app page, record page and home page in salesforce -->
    <targets> 
     <target>lightning__AppPage</target>
     <target>lightning__RecordPage</target>
     <target>lightning__HomePage</target>
  </targets>
</LightningComponentBundle>

Output :

lwc modal output sfdcMonkey.com

Related Resources :
Other Popular Post :

Like our facebook page for new post updates. & Don’t forget to bookmark this site for your future reference.

if you have any suggestions or issue with it, you can post in comment box.

Happy Learning ?

4 comments

Leave a Reply