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 lightning:tabCreated events when the app loads. The
component prevents more than 5 tabs from
opening.
<aura:component implements="lightning:backgroundUtilityItem">
<aura:attribute name="limit" default="5" type="Integer" />
<aura:handler event="lightning:tabCreated" action="{!c.onTabCreated}" />
<lightning:workspaceAPI aura:id="workspace" />
</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 leftmost tab automatically
closes.
({
onTabCreated: function(cmp) {
var workspace = cmp.find("workspace");
var limit = cmp.get("v.limit");
workspace.getAllTabInfo().then(function (tabInfo) {
if (tabInfo.length > limit) {
workspace.closeTab({
tabId: tabInfo[0].tabId
});
}
});
}
})
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 appears next to background
utility items on the utility item list. If you have only background utility items in
your utility bar, the utility bar doesn’t appear in your app. You need at least one
non-background utility item in your utility bar for it to appear.