Implement and Use Lightning Service Components
As the Lightning ecosystem evolves, I have noticed and adopted a valuable architecture pattern: Lightning service components. In this post, I will present this concept, illustrate how it is increasingly used in base components and provide tips and best practices on how developers can create their own service components.
Understanding Lightning service components
Before diving into the specifics of Lightning service components, let’s define what a service component is: A service component is a component that provides an API for a set of functionalities. Ideally, the service should be specialized, generic and reusable.
Another important thing that differentiates a service component from other components is the fact that it does not have a graphical representation. Unlike other components, it is not visible by default. However, it can display some graphics (like a modal dialog or a toast notification) upon request if it’s a UI service.
We will use the term “caller” throughout this post to designate a component calling a service.
An overview of base service components
Since the introduction of the Lightning Data Service (the first base service component) in Winter ’17, the number of base service components has steadily increased over releases. As of Summer ’18 there are 13 base service components and this number is likely to grow in the upcoming releases.
Summer ’18 base service components
Here is an overview of the Summer ’18 base service components. Notice that the terms “library,” “API” and “service” are used interchangeably but these are all service components.
Name Component Category Description Lightning Data Service force:recordData Data Provides the ability to create, read, update, and delete Salesforce records in Lightning without the use of an Apex controller. Notification Library lightning:notificationsLibrary UI Displays messages via notices and toasts. Overlay Library lightning:overlayLibrary UI Displays messages via modals and popovers. Workspace API lightning:workspaceAPI UI API for accessing/manipulating workspaces (Tabs and Subtabs). Utility Bar API lightning:utilityBarAPI UI API for the Utility Bar. Navigation Service lightning:navigation UI Allows to navigate to a given page or to generate a page URL. Navigation Item API lightning:navigationItemAPI UI Allows to control navigation items in Lightning console apps, where navigation items display in an item menu. Quick Action API lightning:quickActionAPI UI Allows to control actions in Lightning Experience on record pages. Conversation ToolkitAPI lightning:conversationToolkitAPI Service Cloud Console integration API for Live Agent. Omni-Channel Toolkit API lightning:omniToolkitAPI Service Cloud Provides access to the API for the Omni-channel toolkit. Minimized API lightningsnapin:minimizedAPI Service Cloud Enables customization of the user interface for the minimized snap-in in Snap-ins for web. Pre-chat API lightningsnapin:prechatAPI Service Cloud Enables customization of the user interface for the pre-chat page in Snap-ins Chat. Settings API lightningsnapin:settingsAPI Service Cloud Enables to fetch certain settings from within custom components for Snap-ins for web. Using a base service component
All of the base components are documented with examples. For the sake of brevity we won’t examine all of them in detail in this post but let’s have a look at an example: displaying a notification with the notifications library.