I am using Salesforce Lightning Design system and I'm currently working on a local html file and would like to create a button that when clicked, will cause a modal to open. I'd like to have a similar output like bootstrap modal that is triggered when a modal button is clicked. Is it possible to create a similar behavior without controllers? We will integrate it to salesforce later on.
1 Answer
Yup, what you asked is very much possible. Code Below:
<div class="slds">
<div class="slds-modal" aria-hidden="false" role="dialog" id="modal">
<div class="slds-modal__container">
<div class="slds-modal__header">
<button class="slds-button slds-button--icon-inverse slds-modal__close" onclick="closeModal()">
<svg aria-hidden="true" class="slds-button__icon slds-button__icon--large">
<use xlink:href="/assets/icons/action-sprite/svg/symbols.svg#close"></use>
</svg>
<span class="slds-assistive-text">Close</span>
</button>
<h2 class="slds-text-heading--medium">Modal Header</h2>
</div>
<div class="slds-modal__content slds-p-around--medium">
<div>
<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>
</div>
</div>
<div class="slds-modal__footer">
<button class="slds-button slds-button--neutral" onclick="closeModal()">Cancel</button>
<button class="slds-button slds-button--neutral slds-button--brand">Save</button>
</div>
</div>
</div>
<div class="slds-backdrop" id="backdrop"></div>
<!-- Button To Open Modal -->
<button class="slds-button slds-button--brand" id="toggleBtn">Open Modal</button>
</div>
<!-- Modal Toggle Script -->
<script>
j$ = jQuery.noConflict();
//Modal Open
j$('#toggleBtn').click(function(){
j$('#backdrop').addClass('slds-backdrop--open');
j$('#modal').addClass('slds-fade-in-open');
});
//Modal Close
function closeModal(){
j$('#modal').removeClass('slds-fade-in-open');
j$('#backdrop').removeClass('slds-backdrop--open');
}
</script>
I hope it helps :)
-
would you still recommend doing this way as it works but is it the best approach? Commented Apr 14, 2019 at 16:02
-
Yes, if you are using it in an html file. If you are using it in Lightning Components, you can either use the lightning:overlayLibrary or write the markup as above and use $A.util.removeClass/$A.util.addClass methods instead of jQuery. Let me know if that answers your question. Commented Apr 19, 2019 at 8:59