How to open/close a modal in Salesforce Lightning App?

Einsamer picture Einsamer · May 29, 2017 · Viewed 14.8k times · Source

I saw this document about modals.

But there is no guild-line for waking up a modal from button or anything like bootstrap.

Do you guys know how could I do it?

Answer

Chinmaya Hegde picture Chinmaya Hegde · May 30, 2017

In Salesforce lightning we don't have any built in function to open/close Modals, This one is working for me.

Component,

<div aura:id="exampleModal"  class="slds-modal slds-fade-in-open hideDiv" aria-hidden="false" role="dialog">
        <div class="slds-modal__container" style="max-width:50rem;">
            <div class="slds-modal__header">
                <button class="slds-button slds-button--icon-inverse slds-modal__close" onclick="{!c.hideExampleModal}">
                    <lightning:icon iconName="utility:close" size="medium" variant="bare"/>
                    <span class="slds-assistive-text">Close</span>
                </button>
                <h2 class="slds-text-heading--medium">Example Modal</h2>
            </div>
            <div class="slds-modal__content slds-p-around--medium">
                <div class="modalContent">
                   <p>Content goes here</p> 
                </div>
            </div>
            <div class="slds-modal__footer">
                <button class="slds-button slds-button--neutral slds-button--brand" onclick="{!c.hideExampleModal}">Close</button>
            </div>
        </div>
 </div>

Controller.js

    hideExampleModal : function(component, event, helper) {
    	helper.hideExampleModal(component);
    },
	
	showExampleModal : function(component, event, helper) {
    	helper.showExampleModal(component);
    },

Helper.js

    showExampleModal : function(component) {      
        var modal = component.find("exampleModal");
        $A.util.removeClass(modal, 'hideDiv');        
    },
    
    hideExampleModal : function(component) {
        var modal = component.find("exampleModal");
        $A.util.addClass(modal, 'hideDiv');
    },

In Style,

.THIS.hideDiv {
    display: none;
}