Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»How to Build a Generic Modal Window in Lightning Web Component

    How to Build a Generic Modal Window in Lightning Web Component

    Dhanik Lal SahniBy Dhanik Lal SahniMay 26, 2025Updated:May 26, 2025No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Build a Generic Modal Window in Lightning Web Component | SalesforceCodex
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Creating reusable, dynamic components is the backbone of efficient development in Salesforce Lightning Web Components (LWC). The modal window is one of the most used components in custom lightning development. It is used to gather user input, display messages, or show dynamic content pulled from Apex classes. A modal window gives us a user-friendly way to interact with users without navigating away from the current view. This post will provide step-by-step details to create Generic Modal Window in Lightning Web Component.

    What is a Modal Window?

    A modal window is a UI overlay that appears on top of the main content, typically used for user communication. Modals are extensively used to:

    • Display forms (e.g., creating or editing records).
    • Confirm actions (e.g., delete, submit, prompt).
    • Show notifications or messages.
    • Render dynamic content fetched asynchronously.

    Importance of Modals in Salesforce Applications

    In Salesforce development, the modal window plays a crucial role in boosting the user experience without breaking the flow. Below are key advantages:

    • Seamless UX: Modals avoid page reloads, improving interactivity.
    • Consistency: Using a standard modal component boosts maintainability.
    • Performance: Developers conserve time by recycling a common modal across elements.

    Creating Generic Modal Windows in Lightning Web Component

    We can create a Lightning Modal component using the LWC lightning/modal module. We have to extend LightningModal using helper lightning-modal-* components to provide a header, footer, and the body of the modal.

    • lightning-modal-header: This will be used to add the header for the model component.
    • lightning-modal-body: This will be used to add the body section for the model component.
    • lightning-modal-footer: This helper component will be used to create a footer section for the model component.

    To create a generic/reusable component, we will create public properties to add content dynamically.

    Generic Modal Component Code

    How to use this Modal Dialog Component?

    LightningModal module provides a method open() to open a model. This will return a promise result of user interaction on the Model dialog. This method will create a the new instance of dialog every time. When we close the modal, it will destroy the dialog instance.

    Test Component

    Let us test the above component with a normal dialog box where header, body, and footer text will be shown. On clicking the button, dialog will be shown.

    Generic Modal Window in Lightning Web Component | SalesforceCodex

    Opening LWC (Lightning Web Component) in Dialog

    Let us say we want to display some form control or other custom component in the dialog. In that scenario, we have to pass components in the body section.

    To handle this use case, we will change in generic modal dialog. We will add capability to show component in LWC (Lightning Web Component). We will render LWC dynamically using lwc:component.

    Read Here to dynamically instantiate component: Dynamically Instantiate Components in LWC

    Test Component to show another component in LWC Dialog

    We have an accountSelector component developed to show accounts in the datatable. We want to display this component in the above generic dialogue.

    Let us show this component in the above generic dialog box. For this, we will pass the component name in dialog open method.

    LWC Component in Dialog | Render LWC inside LWC Modal Dialog | SalesforceCodex

    References

    • LightningModal
    • Dynamically Instantiate Components in LWC
    • Dynamically Instantiate Components

    Related Posts

    • How to Export Data in Excel with SheetJS in LWC
    • Ultimate Guide to URL Accessibility in LWC
    • Generic Notification Component in LWC
    • Reusable Custom Calendar LWC using FullCalendar Js Library
    • Custom Toast with custom duration In LWC
    • Dynamic Interaction Between Two LWCs
    • Sending Wrapper object to Apex from LWC
    • HeatMap Chart In LWC
    • Generate and Create Signature in LWC
    • Option Group in LWC Select
    • Generate OTP in LWC
    • Show Category wise Knowledge Article using LWC
    • Custom Salesforce Knowledge Component using LWC
    • How to Implement Basic Queueable Chaining in Salesforce Apex

    apex dialog dynamic lwc lightning component Lightning web component lwc modal dialog salesforce
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleTop 10 Salesforce Flow Features of Salesforce Summer ’25
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.