How to Add Lightning Web Components as Custom Tabs in Salesforce?

In Salesforce, the Lightning Web Components can be displayed as a custom tab; for that, we need to define the target as lightning_tab, so that the Lightning component will be enabled to be deployed as a tab on the Lightning pages.

In this Salesforce tutorial, we will learn how to add Lightning web components as custom tabs.

Add Lightning Web Component as a Custom Tab in Salesforce

In the release of Winter ’20, Salesforce has allowed LWC components to be exposed or deployed as custom tabs.

In the steps below, we will create a custom LWC component, and after that, we will create a custom tab where the component will be deployed. Lastly, the tab will be added to a Salesforce app where users can view it.

In this example, I will display sample data as the current date. If you have an existing LWC component, then go to step three to configure the meta.xml file.

  1. Create an LWC component and enter the code below in the HTML file.
<template>
         <lightning-card title="LWC Tab">
             <div class="slds-p-around_medium">
                Welcome to the custom LWC tab! Today's date is {currentDate}.
             </div>
         </lightning-card>
    </template>
  1. Enter the code below in the JS file to handle the logic of displaying the date.
 import { LightningElement } from 'lwc';

     export default class LwcTab extends LightningElement {
         get currentDate() { 
             return new Date().toLocaleDateString();
         }
     }
  1. Enter the code below inside the <LightningComponentBundle> tag in the meta.xml file to make the LWC component exposed to deploy as a custom tab.

For the existing Lightning web component, you have to make this change only.

<isExposed>true</isExposed>
         <targets>
             <target>lightning__Tab</target>
         </targets>

The tag <target>lightning__Tab</target> enables the component to be used as a custom tab.

Create a Custom Tab For the Lightning Web Component

After exposing the Lightning Web Component to the Lightning tabs, we will create a custom tab where the LWC component will be deployed.

Now, navigate to the Setup page of Salesforce Lightning and follow the steps below.

  1. In the quick find box, search and then select Tabs. Then, in the tabs setup window, scroll down to the Lightning Component Tabs and click on the New button.
    Add LWC component as Custom Tab in Salesforce
    1. In this step, select the LWC component in the Lightning Component field. Then, enter the tab name and label. Select the tab style to display an icon on the tab.

    After this, click on the Next button.

    How to deploy a LWC component as Tab in Lightning app
    1. Select the User Profiles for which the new lightning component tab will be available. For this, you can apply one default tab visibility for all profiles or select a different tab visibility for each profile.
    Deploy Salesforce LWC component as Custom tab

    This creates the LWC component as a Lightning tab. Now, we need to deploy this tab to the Lightning application to make it visible to users.

    Add Custom Lightning Web Component Tab to Salesforce Application

    To make the tab visible in Lightning Experience or the Salesforce mobile app, we need to add it to a Salesforce app’s navigation menu, and for that, navigate to the setup page and follow the steps below.

    1. In the quick find box, search and select App Manager.
    Add Lwc component tab on Salesforce lightning app
    1. In the app manager, go to the standard or custom application, click on the dropdown next to it, and select Edit.
    1. In the app settings, select Navigation Items. Then, from the column Available Items, select the LWC component tab and move it to the Selected Items column with the arrow button key.
    How to add LWC tab on the Salesforce application
    1. We can also adjust the tab position using the up and down arrow keys. At last, click Save to apply the changes.

    Now, navigate to the application from the app launcher, and there you will see the LWC component tab in the navigation bar.

    Add custom LWC tab on Salesforce application

    This way, we can deploy the Lightning Web Component as a custom tab in Salesforce.

    Conclusion

    In this Salesforce tutorial, we have learned how to add a Lightning Web Component as a custom tab in Salesforce. In the above example, we have created a custom LWC component and exposed it to the lightning tabs using the <target>lightning__Tab</target> target attribute.

    After this, we created a custom LWC component tab and deployed it on the Lightning application from the app settings.

    You may also like to read:

    Agentforce in Salesforce

    DOWNLOAD FREE AGENTFORCE EBOOK

    Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

    Salesforce flows complete guide

    FREE SALESFORCE FLOW EBOOK

    Learn how to work with flows in Salesforce with 5 different real time examples.