Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":1330,"date":"2018-06-20T18:00:38","date_gmt":"2018-06-20T18:00:38","guid":{"rendered":"http:\/\/www.salesforcenextgen.com\/?p=1330"},"modified":"2020-12-28T19:33:40","modified_gmt":"2020-12-28T19:33:40","slug":"how-to-create-a-modal-box-in-lightning-component","status":"publish","type":"post","link":"https:\/\/salesforcenextgen.com\/how-to-create-a-modal-box-in-lightning-component\/","title":{"rendered":"How to create a Modal box in Lightning Component?"},"content":{"rendered":"

How to create a Modal box in Lightning Component?<\/strong><\/h1>\n

\"MODAL<\/p>\n

In this post we are going to create a on demand Modal box or Pop Over, using the standard library provided by salesforce.<\/p>\n

Modal boxes are used to display messages which requires user\u2019s attention and is therefore displayed in foreground interrupting user\u2019s workflow and seeking attention.<\/p>\n

For the Modal we use a single tag provided to us by salesforce i.e. <lightning:overlayLibrary aura:id=”overlayLib”\/> , we need to make sure that we use this tag only in lightning experience, lightning Communities and salesforce app only.<\/p>\n

This single tag is used wherever we want to display the Message or popover view. There are some more custom components required for this tag to work properly which we are going to create as shown below.<\/p>\n

What the content of this Modal box contains, for this we create a lightning component with name modalContent and will have two attributes for now i.e. a icon and the content to display. For this example, I have written static text, as shown below.<\/p>\n

<aura:component implements=”force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction” access=”global” ><\/p>\n

<lightning:icon size=”medium” iconName=”action:approval” alternativeText=”Approved” \/><\/p>\n

Your application has been approved.<\/p>\n

<\/aura:component><\/p>\n

Now in the main component we call this modalComponent and the parent component will display the modal content. Below is the code of the parent component.<\/p>\n

<aura:component implements=”force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction” access=”global” ><\/p>\n

<lightning:overlayLibrary aura:id=”overlayLib”\/><\/p>\n

<lightning:button name=”modal” label=”Show Modal” onclick=”{!c.handleShowModal}”\/><\/p>\n

<\/aura:component><\/p>\n

You see here we do not directly place the modalContent in the component but we create the modalContent on click of the button in the controller of the Parent component as shown below.<\/p>\n

({<\/p>\n

handleShowModal: function(component, evt, helper) {<\/p>\n

var modalBody;<\/p>\n

debugger;<\/p>\n

$A.createComponent(“c:modalContent”, {},<\/p>\n

function(content, status) {<\/p>\n

if (status === “SUCCESS”) {<\/p>\n

modalBody = content;<\/p>\n

component.find(‘overlayLib’).showCustomModal({<\/p>\n

header: “Application Confirmation”,<\/p>\n

body: modalBody,<\/p>\n

showCloseButton: true,<\/p>\n

cssClass: “mymodal”,<\/p>\n

closeCallback: function() {<\/p>\n

alert(‘You closed the alert!’);<\/p>\n

}<\/p>\n

})<\/p>\n

}<\/p>\n

});<\/p>\n

}<\/p>\n

})<\/p>\n

Now we can put this component anywhere in the lightning experience and it will work immediately without writing the SLDS for the Modal box.<\/p>\n

Let\u2019s discuss the methods provided by the salesforce in this example, first we used the method $A.createComponents(), this method is used to create the lightning component on the go i.e. dynamically. Then in this method we call our custom component modalContent as \u2018c:modalContent\u2019. Next in the callback function we find our tag using the aura:id and method component.find(). After this we call method showCustomModal() which is a callback method and this method take couple of attributes such as header, body, showclosebutton, cssClass and closeCallback. This is how it looks as shown below.<\/p>\n

\"MODAL<\/p>\n

Open Modal Button<\/p>\n

\n

\"MODAL<\/p>\n

Modal Box<\/p>\n

\"MODAL<\/p>\n

Alert after closing Modal Box<\/p>\n

\n

\n

As soon as we click on the lightning button, handleshowModal method is called from the client-side controller. And this how you use the lightning:overlayLibrary for Modal box.<\/p>\n

Happy Coding.<\/p>\n

Also, Have a look at the below resources:<\/strong><\/h1>\n
    \n
  1. \n

    Best Salesforce Interview Questions book with Apex and Visualforce concept explained<\/a><\/h3>\n<\/li>\n<\/ol>\n

    Also, Have a look at the below learning resources:<\/strong><\/h1>\n
      \n
    1. \n

      SOQL (Salesforce Object Query Language)<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    2. \n

      Apex Trigger Best Practices and the Trigger Framework<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    3. \n

      Salesforce Interview Question and Answers Part 2<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    4. \n

      Salesforce Interview Questions on Test Class<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    5. \n

      Salesforce-lightning-interview-questions-2018<\/cite><\/span><\/a><\/strong><\/em><\/h3>\n<\/li>\n<\/ol>\n

      \u00a0 \u00a0 \u00a06.\u00a0Salesforce Interview Questions Batch Clas<\/a><\/strong><\/em><\/h3>\n","protected":false},"excerpt":{"rendered":"

      How to create a Modal box in Lightning Component? In this post we are going to create a on demand Modal box or Pop Over, using the standard library provided by salesforce. Modal boxes are used to display messages which requires user\u2019s attention and is therefore displayed in foreground interrupting user\u2019s workflow and seeking attention. …
      Continue reading How to create a Modal box in Lightning Component?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1331,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[446],"tags":[316,329,318,333,294,295,130,313,315,310,326,332,289,304,320,309,290,300,298,291,296,341,302,301,293,292,297,306,312,334,339,337,340,338,305,307,336,321,335,346,345,343,344,328,317,319,288,342,108,286,287,308,324,325,323,311,314,322,327,330,331],"_links":{"self":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1330"}],"collection":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/comments?post=1330"}],"version-history":[{"count":2,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1330\/revisions"}],"predecessor-version":[{"id":2170,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1330\/revisions\/2170"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media\/1331"}],"wp:attachment":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media?parent=1330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/categories?post=1330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/tags?post=1330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}