---
schema_version: "1"
id: c545e2e301970f6041e88181ab178aa589c5f7e9304be4f34e851e4d2f56be4b
title: Tree
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/lightning-base-components/lightning-design-system/guides/lwc/components/lightning/tree/lightning-tree.md
canonical_url: https://developer.salesforce.com/docs/platform/lightning-component-reference/guide/lightning-tree.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 370894cb2bc0c6b1732e66668c3169a73a58191f2374b169e6baf3360a5f810b
products:
  - Lightning Component Reference
---

# Tree

**`lightning-tree`**

Displays a nested tree.

**For Use In**

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

::::::tabset{tabs='["Example", "Develop", "Specifications"]'}

:::::tab

::component-playground{component="tree" model="lwc" namespace="lightning"}


:::::

:::::tab

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.

|                | SLDS 1                                                                                                                                | SLDS 2                                                                                                          |
| :------------- | :------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------- |
| **Design**     | [Trees](https://v1.lightningdesignsystem.com/components/trees/)                                                                       | [Trees](https://www.lightningdesignsystem.com/components/trees/)                                                |
| **For Use In** | Lightning Experience, Experience Builder sites, Salesforce mobile app, Lightning Out (Beta), Standalone Lightning app, Mobile Offline | [Lightning Experience](https://help.salesforce.com/s/articleView?id=xcloud.customize_ui_enhancedlex.htm&type=5) |

## Usage

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.

<!-- prettier-ignore-start -->
```html
<template>
    <lightning-tree items={treeList} onselect={handleSelect}>
    </lightning-tree>
</template>
```
<!-- prettier-ignore-end -->

Define the tree items in your JavaScript file. Use the `detail` property to retrieve the name of the selected tree item.

```javascript
import { LightningElement } from "lwc";

const items = [
  {
    label: "Western Sales Director",
    name: "1",
    expanded: true,
    items: [
      {
        label: "Western Sales Manager",
        name: "2",
        expanded: true,
        items: [
          {
            label: "CA Sales Rep",
            name: "3",
            expanded: true,
            items: [],
          },
          {
            label: "OR Sales Rep",
            name: "4",
            expanded: true,
            items: [],
          },
        ],
      },
    ],
  },
  {
    label: "Eastern Sales Director",
    name: "5",
    expanded: false,
    items: [
      {
        label: "Eastern Sales Manager",
        name: "6",
        expanded: true,
        items: [
          {
            label: "NY Sales Rep",
            name: "7",
            expanded: true,
            items: [],
          },
          {
            label: "MA Sales Rep",
            name: "8",
            expanded: true,
            items: [],
          },
        ],
      },
    ],
  },
];

const mapping = {
  1: "Western Sales Director",
  2: "Western Sales Manager",
  3: "CA Sales Rep",
  4: "OR Sales Rep",
  5: "Eastern Sales Director",
  6: "Eastern Sales Manager",
  7: "NY Sales Rep",
  8: "MA Sales Rep",
};

export default class TreeExample extends LightningElement {
  treeList = items;
  selected = "";

  handleSelect(event) {
    //set the name of selected tree item
    this.selected = mapping[event.detail.name];
  }
}
```

## Add or Remove Items in a Tree

You can add or remove items in a tree. Let's say you have a tree that looks
like this, with a button to add a nested item to the tree.

<!-- prettier-ignore-start -->
```html
<template>
    <lightning-button
        label="Add to Tree"
        id="change-button"
        onclick={handleClick}
    >
    </lightning-button>
    <lightning-tree items={treeList}> </lightning-tree>
</template>
```
<!-- prettier-ignore-end -->

Define the items in your JavaScript code.

```javascript
import { LightningElement } from "lwc";

const items = [
  {
    label: "Go to Record 1",
    href: "#record1",
    items: [],
    expanded: true,
  },
  {
    label: "Go to Record 2",
    href: "#record2",
    items: [],
    expanded: true,
  },
  {
    label: "Go to Record 3",
    href: "#record3",
    items: [],
    expanded: true,
  },
];

export default class AddRemoveExample extends LightningElement {
  treeList = items;
}
```

This example `handleClick()` function adds a nested item at the end of the tree when the button is clicked.

```javascript
export default class AddRemoveExample extends LightningElement {
  treeList = items;

  handleClick(e) {
    const newItems = Array.from(this.treeList);
    const branch = newItems.length;
    const label = "New item added at Record" + branch;
    const newItem = {
      label: label,
      expanded: true,
      disabled: false,
      items: [],
    };
    newItems[branch - 1].items.push(newItem);
    this.treeList = newItems;
  }
}
```

When providing an `href` value to an item, the `onselect` event handler is
triggered before navigating to the hyperlink.

## Select a Tree Item Programmatically

To select a tree item using JavaScript, pass in the tree item name using `selected-item`.

This example selects the **United States Sales** tree item on load. Press the **Change Selected** button to select the **Americas** tree item.

<!-- prettier-ignore-start -->
```html
<template>
    <lightning-tree
        items={treeList}
        selected-item={selected}
    ></lightning-tree>
    <lightning-button
        label="Change Selected"
        onclick={handleClick}
    ></lightning-button>
</template>
```
<!-- prettier-ignore-end -->

Define the items in your JavaScript code and pass in the selected item name.

```javascript
import { LightningElement } from "lwc";

const items = [
  {
    label: "Asia Pacific Sales",
    name: "Asia Pacific Sales",
    items: [
      {
        label: "Asia Sales",
        name: "Asia Sales",
        items: [],
      },
    ],
  },
  {
    label: "Europe Sales",
    name: "Europe Sales",
    items: [
      {
        label: "UK Sales",
        name: "UK Sales",
        items: [],
      },
      {
        label: "EU Sales",
        name: "EU Sales",
        items: [],
      },
    ],
  },
  {
    label: "Americas",
    name: "Americas",
    items: [
      {
        label: "Northern America Sales",
        name: "Northern America Sales",
        items: [
          {
            label: "United States Sales",
            name: "United States Sales",
            items: [],
          },
        ],
      },
    ],
  },
];

export default class SelectItemExample extends LightningElement {
  treeList = items;
  selected = "United States Sales";

  handleClick() {
    this.selected = "Americas";
  }
}
```

## Expand or Collapse A Branch

To expand or collapse a branch programmatically, get the tree items and update its `expanded` property.

```javascript
// expand the first branch
this.template.querySelector("lightning-tree").items[0].expanded = true;

// collapse the first branch
this.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. |

## LWC Recipes

The [LWC Recipes GitHub repository](https://github.com/trailheadapps/lwc-recipes) contains code examples for Lightning Web Components that you can test in an org.

For a recipe that uses `lightning-tree`, see the `c-wire-get-picklist-values-by-record-type` component in the LWC Recipes repo.


:::::

:::::tab
::component-specification{component="tree" model="lwc" namespace="lightning"}

:::::

::::::
