Creating Switchable Lightning Design Tabs In Salesforce - Visualforce & Aura Components
Hello There! Welcome Back đ
The Lightning Design System is my personal favourite UI framework the main reason being its look and feel is native & lightweight. But, Salesforce says that âThe Lightning Design System is a pure CSS frameworkâ. Which means we have to write our own javascript for the Lightning Design System components. If you are using the Lightning Components framework, Salesforce had you covered with Most of the SLDS components(Base Components) as a part of the Lightning Component framework.
So, I am starting to write generic code for some of the Lightning Design System components(mostly for visualforce and may or may not apply to Lightning Components as demonstrated below) which you can use in your development projects and build great User Interfaces. I am using jQuery to write this code.
In this blog we are gonna make the Lightning Design System âTabsâ component interactive(switchable tabs).
Letâs get started!
For Visualforce
Things we will be using:
The Salesforce Lightning Design System
jQuery
Step 1: Gathering the resources we need.
Salesforce Lightning Design System:
Download the Custom Scoped CSS form here with your desired Scoping Class. (be sure to prefix your scoping class with a dot)
The Scoping class wraps your content using the Lightning Design System to avoid CSS conflicts.
I am using âsldsâ as a scoping class in this blog post.The file you download will be named âsalesforce-lightning-design-system.zipâ, just rename it to something convenient and short like âSLDSâ.
Upload the zip file as your Static Resource.
jQuery:
We will be using CDN for this here, you can always have it as a static resource. Either way is fine.
<script
src="
https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Now we have all the stuff we need, lets code what we want.
Step 2: The Code.
Set the StandardStylesheets to false and doctype to html-5.0 (Visualforce Code: Line 1)
We put all the resources we need into the page and set jQuery to noConflict mode. (Visualforce Code: Line 3 to 8)
Create SLDS Tabs Component as shown in the SLDS website here, I chose the deafult style, you can choose any type of tab (Visualforce Code: Lines 14 to 26)
To know more on getting started with The Lightning Design System you can refer my another blog post.The Most Important Step: including the code to make the SLDS Tabs Switchable. (Visualforce Code: Line 32 to 40)
<apex:page docType="html-5.0" standardStylesheets="false" title="Minerva18-SLDSTabsJS">
<!âjQuery CDNâ>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!âSalesforce Lightning Design System Static Resourceâ>
<apex:stylesheet value="{!URLFOR($Resource.SLDS203, âassets/styles/salesforce-lightning-design-system-vf.min.cssâ)}" />
<script>
var j$ = jQuery.noConflict();
</script>
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!âThe Salesforce Lightning Design System Wrapping Div with Scoping Class â>
<div class="minerva18">
<!âThe Salesforce Lightning Design System Tabs Componentâ>
<div class="slds-tabsâdefault">
<ul class="slds-tabsâdefault__nav" role="tablist">
<li class="slds-tabsâdefault__item slds-text-headingâlabel slds-active" title="Item One" role="presentation">
<a class="slds-tabsâdefault__link" href="javascript:void(0);" role="tab" tabindex="0" aria-selected="true" aria-controls="tab-default-1" id="tab-default-1__item">Item One</a>
</li>
<li class="slds-tabsâdefault__item slds-text-headingâlabel" title="Item Two" role="presentation">
<a class="slds-tabsâdefault__link" href="javascript:void(0);" role="tab" tabindex="-1" aria-selected="false" aria-controls="tab-default-2" id="tab-default-2__item">Item Two</a>
</li>
<li class="slds-tabsâdefault__item slds-text-headingâlabel" title="Item Three" role="presentation">
<a class="slds-tabsâdefault__link" href="javascript:void(0);" role="tab" tabindex="-1" aria-selected="false" aria-controls="tab-default-3" id="tab-default-3__item">Item Three</a>
</li>
</ul>
<div id="tab-default-1" class="slds-tabsâdefault__content slds-show" role="tabpanel" aria-labelledby="tab-default-1__item">Item One Content</div>
<div id="tab-default-2" class="slds-tabsâdefault__content slds-hide" role="tabpanel" aria-labelledby="tab-default-2__item">Item Two Content</div>
<div id="tab-default-3" class="slds-tabsâdefault__content slds-hide" role="tabpanel" aria-labelledby="tab-default-3__item">Item Three Content</div>
</div>
</div>
<script>
/*SLDS Tabs JS*/
j$(â.slds-tabsâdefault__link,.slds-tabsâscoped__linkâ).click(function(){
j$(this).parent().parent().find(â.slds-tabsâdefault__link,.slds-tabsâscoped__linkâ).attr(âaria-selectedâ,âfalseâ);
j$(this).attr(âaria-selectedâ,âtrueâ);
j$(this).parent().parent().find(â.slds-tabsâdefault__link,.slds-tabsâscoped__linkâ).attr(âtabindexâ,â-1â˛);
j$(this).attr(âtabindexâ,â0â˛);
j$(this).parent().addClass(âslds-activeâ).siblings().removeClass(âslds-activeâ);
j$(this).parent().parent().parent().find(â.â+j$(this).parent().parent().parent().find(â.slds-tabsâdefault__content,.slds-tabsâscoped__contentâ)[0].classList[0]).removeClass(âslds-showâ).addClass(âslds-hideâ);
j$(this).parent().parent().parent().find(â#â+j$(this).attr(âaria-controlsâ)).removeClass(âslds-hideâ).addClass(âslds-showâ);
});
/*SLDS Tabs JS*/
</script>
</html>
</apex:page>
The main javascript/jQuery code are Lines 32 to 40 in the code snippet above can make any type(default and scoped) and any number of tabs switchable.
You just have to include those eight lines of code(Lines 32 to 40 in the code snippet above) and it will take care of the rest.
For Lightning Aura Components
It is comparatively easy to do it in Lightning Components, like really really easy!
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes">
<lightning:tabset>
<lightning:tab label="Item One">
Item One Content
</lightning:tab>
<lightning:tab label="Item Two">
Item Two Content
</lightning:tab>
<lightning:tab label="Item Three">
Item Three Content
</lightning:tab>
</lightning:tabset>
</aura:component>
There it is! We just made the Lightning Design System Tabs switchable using jQuery.
Cool one, Right?
Resources
Salesforce Lightning Base Component Resource: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/lightning_overview.htm