Sets the label of the specified tab. This method works only in Lightning console apps.
Arguments
| Name | Type | Description |
|---|---|---|
tabId |
string | The ID of the tab for which to set the label. |
label |
string | The label of the workspace tab or subtab. |
LWC Sample Code
This component has a function that sets a label on a specified tab. It uses the tabId property from getFocusedTabInfo().
import { LightningElement, wire } from 'lwc';
import {
IsConsoleNavigation,
getFocusedTabInfo,
setTabLabel
} from 'lightning/platformWorkspaceApi';
const TAB_LABEL = 'Awesome Label';
export default class WorkspaceAPISetTabLabel extends LightningElement {
@wire(IsConsoleNavigation) isConsoleNavigation;
async setTabLabel() {
if (!this.isConsoleNavigation) {
return;
}
const { tabId } = await getFocusedTabInfo();
setTabLabel(tabId, TAB_LABEL);
}
}
This example is the workspaceAPISetTabLabel component from the lwc-recipes repo.
To make your component available for use in a Lightning console app, specify the lightning__AppPage target in the component’s configuration file.
Aura Components Sample Code
This component has a button that, when pressed, sets the label of the focused tab to “Focused Tab”.
Component code:
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
<lightning:workspaceAPI aura:id="workspace" />
<lightning:button label="Set Focused Tab with Label" onclick="{! c.setFocusedTabLabel }" />
</aura:component>
Controller code:
({
setFocusedTabLabel : function(component, event, helper) {
var workspaceAPI = component.find("workspace");
workspaceAPI.getFocusedTabInfo().then(function(response) {
var focusedTabId = response.tabId;
workspaceAPI.setTabLabel({
tabId: focusedTabId,
label: "Focused Tab"
});
})
.catch(function(error) {
console.log(error);
});
}
})
Response
This method returns a promise that, upon success, resolves to a tabInfo object representing the modified tab. A tabInfo object is a JSON array of information about a workspace tab, with nested arrays of information on each subtab. This is the structure of a tabInfo object.
{
tabId: string,
url: string (URL),
pinned: boolean,
closeable: boolean,
title: string,
icon: string (SLDS iconKey),
iconAlt: string,
customTitle: string (optional),
customIcon: string (optional),
customIconAlt: string (optional),
highlighted: boolean,
pageReference: object,
isSubtab: boolean,
parentTabId: string,
subtabs: [
{
tabId: string,
url: string (URL),
pinned: boolean,
closeable: boolean,
title: string,
icon: string (SLDS iconKey),
iconAlt: string,
customTitle: string (optional),
customIcon: string (optional),
customIconAlt: string (optional),
highlighted: boolean,
pageReference: object,
isSubtab: boolean,
parentTabId: string,
focused: boolean,
recordId: string,
},
...
],
focused: boolean,
recordId: string
}