# Tree
[component-header:sl-tree]
Trees allow you to display a hierarchical list of selectable [tree items](/components/tree-item). Items with children can be expanded and collapsed as desired by the user.
```html preview
Deciduous
Birch
Maple
Field maple
Red maple
Sugar maple
Oak
Coniferous
Cedar
Pine
Spruce
Non-trees
Bamboo
Cactus
Fern
```
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => (
Deciduous
Birch
Maple
Field maple
Red maple
Sugar maple
Oak
Coniferous
Cedar
Pine
Spruce
Non-trees
Bamboo
Cactus
Fern
);
```
## Examples
### Selection Modes
Use the `selection` attribute to change the selection behavior of the tree.
- Set `single` to allow the selection of a single item (default).
- Set `multiple` to allow the selection of multiple items.
- Set `leaf` to allow the selection of a single leaf node. Clicking on a parent node will expand/collapse the node.
```html preview
Single
Multiple
Leaf
Item 1
Item A
Item Z
Item Y
Item X
Item B
Item C
Item 2
Item 3
```
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [selection, setSelection] = useState('single');
return (
<>
setSelection(event.target.value)}>
single
multiple
leaf
Item 1
Item A
Item Z
Item Y
Item X
Item B
Item C
Item 2
Item 3
>
);
};
```
### Showing Indent Guides
Indent guides can be drawn by setting `--indent-guide-width`. You can also change the color, offset, and style, using `--indent-guide-color`, `--indent-guide-style`, and `--indent-guide-offset`, respectively.
```html preview
Deciduous
Birch
Maple
Field maple
Red maple
Sugar maple
Oak
Coniferous
Cedar
Pine
Spruce
Non-trees
Bamboo
Cactus
Fern
```
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => (
Deciduous
Birch
Maple
Field maple
Red maple
Sugar maple
Oak
Coniferous
Cedar
Pine
Spruce
Non-trees
Bamboo
Cactus
Fern
);
```
### Lazy Loading
Use the `lazy` attribute on a tree item to indicate that the content is not yet present and will be loaded later. When the user tries to expand the node, the `loading` state is set to `true` and the `sl-lazy-load` event will be emitted to allow you to load data asynchronously. The item will remain in a loading state until its content is changed.
If you want to disable this behavior after the first load, simply remove the `lazy` attribute and, on the next expand, the existing content will be shown instead.
```html preview
Available Trees
```
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [childItems, setChildItems] = useState([]);
const [lazy, setLazy] = useState(true);
const handleLazyLoad = () => {
// Simulate asynchronous loading
setTimeout(() => {
setChildItems(['Birch', 'Cedar', 'Maple', 'Pine']);
// Disable lazy mode once the content has been loaded
setLazy(false);
}, 1000);
};
return (
Available Trees
{childItems.map(item => (
{item}
))}
);
};
```
### With Icons
Decorative icons can be used before labels to provide hints for each node.
```html preview
Root
Folder 1
File 1 - 1
File 1 - 2
File 1 - 3
Folder 2
File 2 - 1
File 2 - 2
File 1
```
```jsx react
import { SlIcon, SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [childItems, setChildItems] = useState([]);
const [lazy, setLazy] = useState(true);
const handleLazyLoad = () => {
// Simulate asynchronous loading
setTimeout(() => {
setChildItems(['Overview', 'Installation', 'Usage']);
// Disable lazy mode once the content has been loaded
setLazy(false);
}, 1000);
};
return (
Root
Folder 1
File 1 - 1
File 1 - 2
File 1 - 3
Folder 2
File 2 - 1
File 2 - 2
File 1
);
};
```
[component-metadata:sl-tree]