Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A lightning-tree component shows visualization of a structural hierarchy,
such as a sitemap for a website or a role hierarchy in an organization. Items
are presented as hyperlinks and items in the hierarchy can be nested. Items
with nested items are also known as branches.
Design
lightning-tree implements the trees blueprint in the Salesforce Lightning Design System (SLDS). The trees adapt to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.
To create a tree, pass in an array of key-value pairs to the items
attribute.
Key
Type
Description
label
string
Required. The title and label for the hyperlink.
metatext
string
Text to provide users with supplemental information and aid with identification or disambiguation.
items
object
Nested items as an array of key-value pairs.
name
string
The unique name for the item for the onselect event handler to return the tree item that was clicked.
href
string
The URL for the link.
expanded
boolean
Specifies whether a branch is expanded. An expanded branch displays its nested items visually. The default is false.
disabled
boolean
Specifies whether an item is disabled. A disabled item is grayed out and can’t be focused or perform any action. The default is false.
Here’s an example of a tree with more than one level of nesting. To retrieve the selected item ID, use the onselect event handler. The select event is also fired when you select an item with an href value.
To expand or collapse a branch programmatically, get the tree items and update its expanded property.
1// expand the first branch2this.template.querySelector("lightning-tree").items[0].expanded = true;34// collapse the first branch5this.template.querySelector("lightning-tree").items[0].expanded = false;
Design Guidelines
Use lightning-tree if your app has layered navigation that can’t be represented in a simple tab set. A tree helps users navigate to pages and quickly find a nested child page without loading each page.
You can use lightning-tree with lightning-breadcrumbs to further help users navigate the hierarchy.
Trees can have unlimited nesting, but we recommend flatter trees as they’re generally easier to navigate.
Not all items in the list need a corresponding page. Instead, you can group related pages together using a label header without providing an unnecessary landing page.
Accessibility
You can use the keyboard to navigate the tree. Tab into the tree and use the
Up and Down Arrow key to focus on tree items. To collapse an expanded branch,
press the Left Arrow key. To expand a branch, press the Right Arrow key.
Pressing the Enter key or Space Bar is similar to an onclick event, and
performs the default action on the item.
Custom Events
select
The event fired when a tree item is selected and before navigating to a given hyperlink.
The select event returns the following parameter.
Parameter
Type
Description
name
string
The label of the selected tree item.
The event properties are as follows.
Property
Value
Description
bubbles
true
This event bubbles up through the DOM.
cancelable
true
This event can be canceled. You can call preventDefault() on this event.
composed
true
This event propagates outside of the component in which it was dispatched.
For a recipe that uses lightning-tree, see the c-wire-get-picklist-values-by-record-type component in the LWC Recipes repo.
Attributes
Name
Description
Type
Default
Required
header
The text that's displayed as the tree heading.
string
heading-level
Changes the 'aria-level' attribute value for the <h2> markup tag in the card's title element. Supported values are (1, 2, 3, 4, 5, 6).
string | number
2
items
An array of key-value pairs that describe the tree. See the Documentation tab for more information.
array
selected-item
Selects and highlights the specified tree item. Tree item names are case-sensitive. If the tree item is nested, selecting this item also expands the parent branches.