Newer Version Available

This content describes an older version of this product. View Latest

Using Background Utility Items

Implement the lightning:backgroundUtilityItem interface to create a component that fires and responds to events without rendering in the utility bar.
This component implements lightning:backgroundUtilityItem and listens for llightning:tabCreated events when the app loads. The component prevents more than 5 tabs from opening.
1<aura:component implements="lightning:backgroundUtilityItem">
2    <aura:attribute name="limit" default="5" type="Integer" />
3    <aura:handler event="lightning:tabCreated" action="{!c.onTabCreated}" />
4    <lightning:workspaceAPI aura:id="workspace" />
5</aura:component>
When a tab is created, the event handler calls onTabCreated in the component’s controller and checks how many tabs are open. If the number of tabs is more than 5, the newly created tab automatically closes.
1({
2    onTabCreated: function(cmp) {
3        var workspace = cmp.find("workspace");
4        var limit = cmp.get("v.limit");
5        workspace.getAllTabInfo().then(function (tabInfo) {
6            if (tabInfo.length > limit) {
7                workspace.closeTab({
8                    tabId: tabInfo[0].tabId
9                });
10            }
11        });
12    }
13})

Background utility items are added to an app the same way normal utility items are, but they don’t appear in the utility bar. The Icon representing hidden status icon appears next to background utility items on the utility item list.