Workspace API in LWC - Piyush Aneja
Workspace API in LWC - Piyush Aneja
Here are some of the key methods available in the LWC Workspace API:
Note: Lightning Web Security must be enabled in your Salesforce org because Lightning Locker
doesn't support the LWC Workspace API.
Example Code
Below is an example of how you can determine if a component is in a Lightning console app:
<template>
<!-- Lightning card to display the result -->
<lightning-card title="Is console app?">
<div class="slds-p-around_medium">
<!-- Display whether the component is inside a Console App or
not -->
<p>I am inside a Console App - {isConsoleNavigation}</p>
</div>
</lightning-card>
Result -
Conclusion
By using the Workspace API in LWC, you can easily determine if your component is within a
Lightning console app. This allows you to tailor your component's behavior and user experience
based on the environment. This small but powerful check can make your components smarter
and more adaptable to different user contexts.
Example Code
Below is an example of how you can open a subtab in a Lightning console app using LWC:
import { LightningElement, wire } from 'lwc';
import { IsConsoleNavigation, openTab } from
'lightning/platformWorkspaceApi';
Result -
Example Code
Below is an example of how you can open a subtab in a Lightning console app using LWC:
<template>
<!-- Lightning card to contain the buttons for opening new subtabs -->
<lightning-card title="Open New SubTab">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to open a subtab using recordId -->
<lightning-button
label="Open SubTab using recordId"
onclick={openTabRecordId}>
</lightning-button>
Result-
Example Code
Below is an example of how you can close a tab in a Lightning console app using LWC:
<template>
<!-- Lightning card to contain the buttons for closing tabs -->
<lightning-card title="Close Tab">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to close tab -->
<lightning-button
label="Close Tab"
onclick={closeHandler}>
</lightning-button>
Result-
With the Workspace API, closing a subtab within a Lightning console app is straightforward.
Here's how you can do it:
<template>
<!-- Lightning card to contain the buttons for closing subtabs -->
<lightning-card title="Close Subtabs">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to close all subtabs -->
<lightning-button
label="Close All Subtabs"
onclick={closeAllSubtabsHandler}>
</lightning-button>
Result -
Conclusion
By Implementing the Workspace API in Lightning Web Components, developers can seamlessly
close sub tabs within a Lightning console app. Whether it's for maintaining workspace
organization, focusing on relevant tasks, or optimizing performance, this functionality empowers
Piyush Aneja | Workspace API in LWC
16
users to tailor their workspace and enhance productivity. With just a few lines of code, you can
provide users with a streamlined and efficient interface for managing their tasks and data within
the console app.
Example Code
Below is an example demonstrating how to refresh a tab in a Lightning console app using LWC:
<template>
<!-- Lightning card to contain the button for refreshing the focused
tab -->
<lightning-card title="Refresh Focused Tab">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to refresh the currently focused tab -->
<lightning-button
label="Refresh tab"
onclick={refreshTabHandler}>
</lightning-button>
</div>
</lightning-card>
</template>
Result:
Conclusion
With the Workspace API, setting tab labels within a Lightning console app is straightforward.
Here's how you can do it:
Example Code
Below is an example demonstrating how to set a tab label in a Lightning console app using
LWC:
<template>
<!-- Lightning card to contain the button for setting the label of the
focused tab -->
<lightning-card title="Set Label">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to set the label of the currently focused tab -->
<lightning-button
label="Click to set Label"
onclick={setLabelHandler}>
</lightning-button>
</div>
</lightning-card>
</template>
Result:
Example Code
<template>
<!-- Lightning card to contain the button for setting focus to the next
tab -->
<lightning-card title="Focus Tab">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to set focus to the next tab -->
<lightning-button
label="Set Focus"
onclick={setFocusHandler}>
</lightning-button>
</div>
</lightning-card>
Conclusion
By Implementing the Workspace API in Lightning Web Components, developers can seamlessly
focus tabs within a Lightning console app. Whether it's for quick navigation, enhancing user
experience, or streamlining workflows, this functionality empowers users to efficiently manage
their tasks and data. With just a few lines of code, you can provide users with a more intuitive
and productive interface for navigating their workspace in the console app.
Example Code
Piyush Aneja | Workspace API in LWC
23
Below is an example demonstrating how to set a tab icon in a Lightning console app using LWC:
import { LightningElement, wire } from 'lwc';
import { IsConsoleNavigation, getFocusedTabInfo, setTabIcon } from
'lightning/platformWorkspaceApi';
<template>
<!-- Lightning card to contain the button for setting the icon of the
focused tab -->
<lightning-card title="Set Icon">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to set the icon of the currently focused tab -->
<lightning-button
label="Set Icon"
onclick={setIconHandler}>
</lightning-button>
</div>
</lightning-card>
</template>
Conclusion
With the Workspace API, highlighting tabs with different background colors within a Lightning
console app is simple. Here's how you can do it:
Example Code
Below is an example demonstrating how to highlight a tab with a different background color in a
Lightning console app using LWC:
<template>
<!-- Lightning card to contain the buttons for tab highlighting -->
<lightning-card title="Tab Highlight Demo">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Button to highlight the tab with success state -->
<lightning-button
label="Highlight with Success"
onclick={successHandler}>
</lightning-button>
Result:
Example Code
<template>
<!-- Lightning card to contain the toggle input for tab disablement -->
<lightning-card title="Disable Tab Demo">
<!-- Padding around the content -->
<div class="slds-p-around_medium">
<!-- Toggle input for enabling or disabling tab close -->
<lightning-input
type="toggle"
label="Toggle Disable"
onchange={disableHandler}>
</lightning-input>
</div>
</lightning-card>
</template>
Result:
By implementing the Workspace API in Lightning Web Components, developers can create
highly interactive, efficient, and user-friendly Lightning console apps that meet the unique needs
of their users and organizations. With its ease of use and powerful capabilities, the Workspace
API is a game-changer for Salesforce developers looking to take their console apps to the next
level.