Newer Version Available

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

closeTab() for Lightning Experience

Closes a workspace tab or subtab. This method works only in Lightning console apps.

Arguments

The method provides the same argument for both Aura Components and Lightning Web Components (LWC).

Name Type Description
tabId string ID of the workspace tab or subtab to close.

LWC Sample Code

This component checks if it’s in a Lightning console app, returns information about the focused tab and closes it.

1import { LightningElement, wire } from 'lwc';
2import { IsConsoleNavigation, getFocusedTabInfo, closeTab } from 'lightning/platformWorkspaceApi';
3
4export class FocusedTabInfoExample extends LightningElement {
5    @wire(IsConsoleNavigation) isConsoleNavigation;
6
7    handleClick() {
8        if (this.isConsoleNavigation) {
9            getFocusedTabInfo().then((tabInfo) => {
10                closeTab(tabInfo.tabId);
11            }).catch(function(error) {
12                console.log(error);
13            });
14     }
15  }
16}

Example

The lwc-recipes repo has a workspaceAPICloseTab component that demonstrates usage of the closeTab() method.

Aura Components Sample Code

This component has a button that, when pressed, closes the currently focused tab.

Component code:

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>

Controller code:

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 true.