Understanding Modals/Dialog Windows in Web Pages

Modals, also known as modal windows or lightboxes, are essential components in web design that create a focused experience for users. A modal window disables the main window while keeping it visible, acting as a child window in front of it. This approach allows users to complete specific tasks without needing to display all the information on the main screen. While these messages aren't strictly locked, users can click outside the modal to dismiss it.

Let's Code Together!

In this section, we will create a simple generic modal component using Lightning Web Components (LWC).

1. HTML Template: genericModal.html

<template> <template if:true={showModal}> <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"> <header class="slds-modal__header"> <lightning-button-icon class="slds-modal__close" title="Close" icon-name="utility:close" icon-class="slds-button_icon-inverse" onclick={handleClose}> </lightning-button-icon> <h2 id="modal-heading-01" class="slds-modal__title slds-hyphenate">{header}</h2> </header> <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1"> <p>{content}</p> </div> <footer class="slds-modal__footer slds-modal__footer_directional"> <lightning-button label="Close" title="Non-primary action" onclick={handleClose} class="slds-button"></lightning-button> <lightning-button variant="brand" label="Save" title="Primary action" class="slds-button"> </lightning-button> </footer> </div> </section> <div class="slds-backdrop slds-backdrop_open"></div> </template> </template>

2. JavaScript Controller: genericModal.js

javascript
import { LightningElement, api } from 'lwc'; export default class GenericModal extends LightningElement { showModal = false; @api header; @api content; @api show() { this.showModal = true; } @api hide() { this.showModal = false; } handleClose() { const isClose = new CustomEvent('isClose'); this.dispatchEvent(isClose); this.hide(); } }

3. Using the Generic Modal: genericModalUseCase.html

To utilize the generic modal component, we need to create a new component as shown below:

<template> <lightning-card title="Generic Modal Use Case" icon-name="custom:custom33"> <div class="slds-var-m-around_medium"> <lightning-input label="Enter Header Details" type="text" value={header} class="header-jest" onchange={handleHeaderChange}></lightning-input> <br /> <lightning-button label="Show Modal" onclick={handleOpenModal}></lightning-button> <c-genericModal header={header} content={content}> <div slot="footer"> <lightning-button label="Close" variant="neutral" onclick={handleClose}></lightning-button> </div> </c-genericModal> </div> </lightning-card> </template>

4. JavaScript Controller for Use Case: genericModalUseCase.js


import { LightningElement } from 'lwc'; export default class GenericModalUseCase extends LightningElement { content = 'Sit nulla est ex deserunt exercitation anim occaecat. Nostrud ullamco deserunt aute id consequat veniam incididunt duis in sint irure nisi'; header = 'Generic Modal'; handleHeaderChange(event) { this.header = event.target.value; } handleOpenModal() { const modal = this.template.querySelector('c-generic-modal'); modal.show(); } handleClose() { const modal = this.template.querySelector('c-generic-modal'); modal.hide(); } }

Conclusion

Modals provide a streamlined way for users to interact with specific information without navigating away from the current page. By following this coding example, you can create a reusable generic modal component that enhances user experience in your Lightning web applications. Happy coding!

Comments

Popular posts from this blog

Best Practices for Creating Salesforce Roll-Up Summary Triggers

CREATE WEB-FORM USING LIGHTNING WEB COMPONENTS SALESFORCE

Utilizing a Generic Pagination Class in Lightning Web Components - Part 1