Newer Version Available

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

getFocusedTabInfo() for Lightning Experience

Returns information about the focused workspace tab or subtab. This method works only in Lightning console apps.

To retrieve information about the workspace tab or the subtab that a component is rendered in, don’t use getFocusedTabInfo(). When lifecycle hooks such as renderedCallback() or connectedCallback() are invoked for the component, the tab enclosing that component isn’t guaranteed to be in focus, so getFocusedTabInfo() sometimes doesn’t return that tab’s information.

Instead, first use getEnclosingTabId() for Aura components or the EnclosingTabId wire adaptor for LWC. Then call getTabInfo() and use the enclosing tab’s ID as the argument.

We recommend that you continue to use getFocusedTabInfo() with components in the utility bar, because the utility bar doesn’t have an enclosing tab ID.

Note

Arguments

None.

LWC Sample Code

This component checks if it’s in a Lightning console app, using the IsConsoleNavigation wire adapter, and returns information about the focused tab or subtab. It uses the tabId and highlighted properties from the tabInfo return object to toggle highlighting on the tab.

1import { LightningElement, wire } from 'lwc';
2import { IsConsoleNavigation, getFocusedTabInfo, setTabHighlighted } from 'lightning/platformWorkspaceApi';
3
4export class FocusedTabInfoExample extends LightningElement {
5    @wire(IsConsoleNavigation) isConsoleNavigation;
6
7    async handleFocusToggleHighlight() {
8        if (!this.isConsoleNavigation) {
9            return;
10        }
11        try {
12            let { tabId, highlighted } = await getFocusedTabInfo();
13            highlighted = highlighted ? false : true;
14            setTabHighlighted(tabId, highlighted);
15        } catch (error) {
16            console.log(error);
17        }
18    }
19}

For another example, see closeTab().

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 clicked, closes the currently focused tab.

This is the component code. The lightning:workspaceAPI component provides access to Lightning console methods. When clicked, the lightning:button base component executes the closeFocusedTab action in the component’s client-side controller.

1<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
2    <lightning:workspaceAPI aura:id="workspace" />
3    <lightning:button label="Close Focused Tab" onclick="{! c.closeFocusedTab }" />
4</aura:component>

This is the client-side controller code. The closeFocusedTab action retrieves the tab ID of the tab in focus, and then closes the tab with that ID.

1({
2    closeFocusedTab : function(component, event, helper) {
3        var workspaceAPI = component.find("workspace");
4        workspaceAPI.getFocusedTabInfo().then(function(response) {
5            var focusedTabId = response.tabId;
6            workspaceAPI.closeTab({tabId: focusedTabId});
7       })
8        .catch(function(error) {
9            console.log(error);
10        });
11    }
12})

Response

This method returns a promise that, upon success, resolves to a tabInfo object that represents the focused 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.
1{
2     tabId: string,
3     url: string (URL),
4     pinned: boolean,
5     closeable: boolean,
6     title: string,
7     icon: string (SLDS iconKey),
8     iconAlt: string,
9     customTitle: string (optional),
10     customIcon: string (optional),
11     customIconAlt: string (optional),
12     highlighted: boolean,
13     pageReference: object,
14     isSubtab: boolean,
15     parentTabId: string,
16     subtabs: [
17         {
18             tabId: string,
19             url: string (URL),
20             pinned: boolean,
21             closeable: boolean,
22             title: string,
23             icon: string (SLDS iconKey),
24             iconAlt: string,
25             customTitle: string (optional),
26             customIcon: string (optional),
27             customIconAlt: string (optional),
28             highlighted: boolean,
29             pageReference: object,
30             isSubtab: boolean,
31             parentTabId: string,
32             focused: boolean,
33             recordId: string,
34          },
35           ... 
36     ],
37     focused: boolean,
38     recordId: string
39}