Lightning Design System in Visualforce Part 1 - Getting Started
Introduction
A short while ago I wrote a Medium post containing tips for Visualforce developers moving over to Lightning Components. A pre-requisite for developing Lightning Components is being able to write JavaScript to some level, which isn’t something that every existing Visualforce developer has, especially those that from a functional background that have gained enough of an understanding of Visualforce and Apex to become productive, but aren’t in a position to quickly train themselves up on a new programming language.
If you are in this boat, the good news is that if you don’t have JavaScript you can still create a custom user interface matching the Lightning Experience look and feel - simply carry on building Visualforce pages but use the Salesforce Lightning Design System (LDS) to style them.
Get the LDS
LDS is automatically included in various Lightning Component scenarios, but to use it with Visualforce you still need a static resource. Previously it was possible just to download a zip file, but now a custom scope is required in the CSS so you have to generate your own download via the CSS customizer tool. I chose BBLDS - if you go with something else, remember to update the CSS style classes in the sample code. Once you’ve generated the file, upload it to Salesforce as a static resource - I’ve gone with the name BBLDS_W17, again if you change this remember to update the references in the sample code.
Create the Basic Page
I create a new Visualforce page, making sure to check the box so that my page is available in LEX:
and change the <apex:page /> tag to the following:
<apex:page showHeader="false" sidebar="false" standardStylesheets="false" standardController="Account" applyHTmlTag="false">
Note that I have to turn off all aspects of standard Visualforce styling - no header, no sidebar and no standard stylesheets. The example page displays some basic details of an Account so I specify the Account standard controller. Finally, the applyHtmlTag attribute is set to false - this gives me control over the <html>, <head> and <body> tags, which is necessary to use the SVG icons in the LDS. Next, I add the <html> tag, specifying the SVG namespace information, followed by the <body> tag:
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<body>
Next, I include the LDS - as noted above, if you’ve chosen a different name to BBLDS_W17, remember to change the name (and remember I die a little inside when my recommendations are rejected, but I accept that everyone has free will):
<apex:stylesheet value="{!URLFOR($Resource.BBLDS_W17, 'assets/styles/salesforce-lightning-design-system-vf.min.css')}" />
Then I add the Lord of the Divs - one Div to rule them all - that wraps all content and specifies the LDS namespace. Again, if you haven’t used the same name as me (which hurts, it’s like a knife to my heart) remember to update the code:
<div class="BBLDS">
then I close everything off - the <div>, <body>, <html> and <page> tags.
Add LDS Components
The next task is to add some LDS components. Here I use the time-honoured approach of finding something in the LDS samples that looks appropriate, copying the sample and hacking it around, and I’d certainly recommend that approach when you first start out.
The first element I’ve added is the Record Home Page Header - I’m not going to cover the code in detail here, save for a couple of key points.
First - this is a Visualforce page so I can just drop in merge syntax to display details of the Account, for example:
<li class="slds-page-header__detail-block"> <p class="slds-text-title slds-truncate slds-m-bottom--xx-small" title="Description">Description</p> <p class="slds-text-body--regular slds-truncate" title="{!Account.Description}">{!Account.Description}</p> </li>
gives me:
Second, SVG icons requires me to specify the location of the icon in the static resource file:
<div class="slds-media__figure"> <svg aria-hidden="true" class="slds-icon slds-icon-standard-account"> <use xlink:href="{!URLFOR($Resource.BBLDS_W17,
'/assets/icons/standard-sprite/svg/symbols.svg#account')}">
</use>
</svg>
</div>
Third - I can still use standard component tags to generate formatted output:
<div class="slds-form-element slds-hint-parent slds-has-divider--bottom"> <span class="slds-form-element__label">Created By</span> <div class="slds-form-element__control"> <span class="slds-form-element__static">{!Account.CreatedBy.Name}, <apex:outputField value="{!Account.CreatedDate}"/> </span> </div> </div>
Note that I don’t use an output field tag to generate the details of the user that created the Account, as this has a side effect of adding a hover popup to the output link, which won’t be styled correctly as I’m not using any of the default Visualforce styling. Always consider the full effects of any standard component you use, and make sure you are happy with them - you don’t want to confuse your users with broken or half-styled elements.
Boring - where’s the final page?
Here’s a screenshot of the final page - pretty simplistic, but definitely styled appropriately for LEX users:
Enough talk, show me the code
As usual with LDS posts, the code is in my LDS Samples Github Repository. There’s also an unmanaged package available to save wasting time copying and pasting - see the README.
In Conclusion
This post is in no way intended to suggest that you avoid learning Lightning Components in favour of making your Visualforce look like LEX. Lightning Components are the future and they are in and of LEX rather than iframed in from a different server, so you’ll be able to do a lot more with them in the future. I’d also imagine there isn’t a large team developing Visualforce nowadays, more likely a small team working on essential maintenance items, so you are unlikely to see much in the way of new features.
Related Posts
- LDS Activity Timeline, Lightning Components and Visualforce
- Responsive Design with the Lighting Design System
- Lightning Design System - Edit Parent and Child Records
- Lightning, Visualforce and the DOM
- Lightning Components and JavaScript Libraries
- Lighting Components and CSS Media Queries
- Lightning Component Events
- Lightning Components and Unobtrusive JavaScript
- Lightning Components and Custom Apex Classes
Very intuitive, Nicely written
ReplyDeleteCan we use "apex:slds" instead and get rid of the line :-> xmlns:xlink
Do we need to include svg4everybody script in order to use svg icons ?
ReplyDelete