2022-07-10 15:23:14 +00:00
# Tree
[component-header:sl-tree]
```html preview
< sl-tree >
2022-07-26 19:53:24 +00:00
< sl-tree-item >
Deciduous
< sl-tree-item > Birch< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< sl-tree-item >
2022-07-26 19:53:24 +00:00
Maple
< sl-tree-item > Field maple< / sl-tree-item >
< sl-tree-item > Red maple< / sl-tree-item >
< sl-tree-item > Sugar maple< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree-item >
2022-07-26 19:53:24 +00:00
< sl-tree-item > Oak< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree-item >
< sl-tree-item >
2022-07-26 19:53:24 +00:00
Coniferous
< sl-tree-item > Cedar< / sl-tree-item >
< sl-tree-item > Pine< / sl-tree-item >
< sl-tree-item > Spruce< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree-item >
2022-07-26 19:53:24 +00:00
< sl-tree-item >
Non-trees
< sl-tree-item > Bamboo< / sl-tree-item >
< sl-tree-item > Cactus< / sl-tree-item >
< sl-tree-item > Fern< / sl-tree-item >
< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree >
```
2022-07-26 19:53:24 +00:00
<!-- prettier - ignore -->
2022-07-10 15:23:14 +00:00
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< SlTree >
2022-07-26 19:53:24 +00:00
< SlTreeItem >
Deciduous
< SlTreeItem > Birch< / SlTreeItem >
< SlTreeItem >
Maple
< SlTreeItem > Field maple< / SlTreeItem >
< SlTreeItem > Red maple< / SlTreeItem >
< SlTreeItem > Sugar maple< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem > Oak< / SlTreeItem >
2022-07-10 15:23:14 +00:00
< / SlTreeItem >
< SlTreeItem >
2022-07-26 19:53:24 +00:00
Coniferous
< SlTreeItem > Cedar< / SlTreeItem >
< SlTreeItem > Pine< / SlTreeItem >
< SlTreeItem > Spruce< / SlTreeItem >
2022-07-10 15:23:14 +00:00
< / SlTreeItem >
2022-07-26 19:53:24 +00:00
< SlTreeItem >
Non-trees
< SlTreeItem > Bamboo< / SlTreeItem >
< SlTreeItem > Cactus< / SlTreeItem >
< SlTreeItem > Fern< / SlTreeItem >
< / SlTreeItem >
2022-07-10 15:23:14 +00:00
< / SlTree >
);
```
## Examples
2022-07-26 19:53:24 +00:00
### Selection Modes
2022-07-10 15:23:14 +00:00
2022-08-17 16:33:33 +00:00
The `selection` attribute lets you change the selection behavior of the tree.
2022-07-10 15:23:14 +00:00
2022-08-17 16:33:33 +00:00
- Use `single` to allow the selection of a single item (default).
- Use `multiple` to allow the selection of multiple items.
- Use `leaf` to only allow leaf nodes to be selected.
2022-07-10 15:23:14 +00:00
```html preview
2022-07-26 19:53:24 +00:00
< sl-select id = "selection-mode" value = "single" label = "Selection" >
< sl-menu-item value = "single" > Single< / sl-menu-item >
< sl-menu-item value = "multiple" > Multiple< / sl-menu-item >
< sl-menu-item value = "leaf" > Leaf< / sl-menu-item >
2022-07-10 15:23:14 +00:00
< / sl-select >
2022-07-26 19:53:24 +00:00
2022-07-10 15:23:14 +00:00
< br / >
2022-07-26 19:53:24 +00:00
< sl-tree class = "tree-selectable" >
< sl-tree-item >
Item 1
2022-07-10 15:23:14 +00:00
< sl-tree-item >
2022-07-26 19:53:24 +00:00
Item A
< sl-tree-item > Item Z< / sl-tree-item >
< sl-tree-item > Item Y< / sl-tree-item >
< sl-tree-item > Item X< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree-item >
2022-07-26 19:53:24 +00:00
< sl-tree-item > Item B< / sl-tree-item >
< sl-tree-item > Item C< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree-item >
2022-07-26 19:53:24 +00:00
< sl-tree-item > Item 2< / sl-tree-item >
< sl-tree-item > Item 3< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree >
2022-07-26 19:53:24 +00:00
2022-07-10 15:23:14 +00:00
< script >
2022-07-26 19:53:24 +00:00
const selectionMode = document.querySelector('#selection-mode');
const tree = document.querySelector('.tree-selectable');
selectionMode.addEventListener('sl-change', () => {
tree.querySelectorAll('sl-tree-item').forEach(item => (item.selected = false));
tree.selection = selectionMode.value;
});
2022-07-10 15:23:14 +00:00
< / script >
```
2022-07-26 19:53:24 +00:00
<!-- prettier - ignore -->
2022-07-10 15:23:14 +00:00
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => {
2022-07-26 19:53:24 +00:00
const [selection, setSelection] = useState('single');
2022-07-10 15:23:14 +00:00
return (
< >
2022-08-17 21:23:38 +00:00
< SlSelect label = "Selection" value = {selection} onSlChange = {event = > setSelection(event.target.value)}>
2022-07-10 15:23:14 +00:00
< SlMenuItem value = "single" > single< / SlMenuItem >
< SlMenuItem value = "multiple" > multiple< / SlMenuItem >
2022-07-26 19:53:24 +00:00
< SlMenuItem value = "leaf" > leaf< / SlMenuItem >
2022-07-10 15:23:14 +00:00
< / SlSelect >
2022-07-26 19:53:24 +00:00
2022-07-10 15:23:14 +00:00
< br / >
2022-07-26 19:53:24 +00:00
2022-08-17 21:23:38 +00:00
< SlTree selection = {selection} >
2022-07-26 19:53:24 +00:00
< SlTreeItem >
Item 1
2022-07-10 15:23:14 +00:00
< SlTreeItem >
2022-07-26 19:53:24 +00:00
Item A
< SlTreeItem > Item Z< / SlTreeItem >
< SlTreeItem > Item Y< / SlTreeItem >
< SlTreeItem > Item X< / SlTreeItem >
2022-07-10 15:23:14 +00:00
< / SlTreeItem >
2022-07-26 19:53:24 +00:00
< SlTreeItem > Item B< / SlTreeItem >
< SlTreeItem > Item C< / SlTreeItem >
2022-07-10 15:23:14 +00:00
< / SlTreeItem >
2022-07-26 19:53:24 +00:00
< SlTreeItem > Item 2< / SlTreeItem >
< SlTreeItem > Item 3< / SlTreeItem >
2022-07-10 15:23:14 +00:00
< / SlTree >
< />
);
};
```
2022-07-26 19:53:24 +00:00
### 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
< sl-tree class = "tree-with-lines" >
< sl-tree-item expanded >
Deciduous
< sl-tree-item > Birch< / sl-tree-item >
< sl-tree-item expanded >
Maple
< sl-tree-item > Field maple< / sl-tree-item >
< sl-tree-item > Red maple< / sl-tree-item >
< sl-tree-item > Sugar maple< / sl-tree-item >
< / sl-tree-item >
< sl-tree-item > Oak< / sl-tree-item >
< / sl-tree-item >
< sl-tree-item >
Coniferous
< sl-tree-item > Cedar< / sl-tree-item >
< sl-tree-item > Pine< / sl-tree-item >
< sl-tree-item > Spruce< / sl-tree-item >
< / sl-tree-item >
< sl-tree-item >
Non-trees
< sl-tree-item > Bamboo< / sl-tree-item >
< sl-tree-item > Cactus< / sl-tree-item >
< sl-tree-item > Fern< / sl-tree-item >
< / sl-tree-item >
< / sl-tree >
< style >
.tree-with-lines {
--indent-guide-width: 1px;
}
< / style >
```
<!-- prettier - ignore -->
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< SlTree class = "tree-with-lines" style = {{ ' --indent-guide-width ' : ' 1px ' } } >
< SlTreeItem expanded >
Deciduous
< SlTreeItem > Birch< / SlTreeItem >
< SlTreeItem expanded >
Maple
< SlTreeItem > Field maple< / SlTreeItem >
< SlTreeItem > Red maple< / SlTreeItem >
< SlTreeItem > Sugar maple< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem > Oak< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem >
Coniferous
< SlTreeItem > Cedar< / SlTreeItem >
< SlTreeItem > Pine< / SlTreeItem >
< SlTreeItem > Spruce< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem >
Non-trees
< SlTreeItem > Bamboo< / SlTreeItem >
< SlTreeItem > Cactus< / SlTreeItem >
< SlTreeItem > Fern< / SlTreeItem >
< / SlTreeItem >
< / SlTree >
);
```
### Lazy Loading
2022-07-10 15:23:14 +00:00
2022-07-26 19:53:24 +00:00
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.
2022-07-10 15:23:14 +00:00
2022-07-26 19:53:24 +00:00
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.
2022-07-10 15:23:14 +00:00
```html preview
2022-08-17 20:22:03 +00:00
< sl-tree >
2022-07-26 19:53:24 +00:00
< sl-tree-item lazy > Available Trees< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree >
< script type = "module" >
const lazyItem = document.querySelector('sl-tree-item[lazy]');
2022-07-26 19:53:24 +00:00
2022-07-10 15:23:14 +00:00
lazyItem.addEventListener('sl-lazy-load', () => {
2022-07-26 19:53:24 +00:00
// Simulate asynchronous loading
2022-07-10 15:23:14 +00:00
setTimeout(() => {
2022-07-26 19:53:24 +00:00
const subItems = ['Birch', 'Cedar', 'Maple', 'Pine'];
2022-07-10 15:23:14 +00:00
for (const item of subItems) {
const treeItem = document.createElement('sl-tree-item');
treeItem.innerText = item;
2022-07-26 19:53:24 +00:00
lazyItem.append(treeItem);
2022-07-10 15:23:14 +00:00
}
2022-07-26 19:53:24 +00:00
// Disable lazy mode once the content has been loaded
2022-07-10 15:23:14 +00:00
lazyItem.lazy = false;
2022-07-26 19:53:24 +00:00
}, 1000);
2022-07-10 15:23:14 +00:00
});
< / script >
```
```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(() => {
2022-07-26 19:53:24 +00:00
setChildItems(['Birch', 'Cedar', 'Maple', 'Pine']);
2022-07-10 15:23:14 +00:00
2022-07-26 19:53:24 +00:00
// Disable lazy mode once the content has been loaded
2022-07-10 15:23:14 +00:00
setLazy(false);
2022-07-26 19:53:24 +00:00
}, 1000);
2022-07-10 15:23:14 +00:00
};
return (
2022-08-17 20:22:03 +00:00
< SlTree >
2022-07-10 15:23:14 +00:00
< SlTreeItem lazy = {lazy} onSlLazyLoad = {handleLazyLoad} >
2022-07-26 19:53:24 +00:00
Available Trees
2022-07-10 15:23:14 +00:00
{childItems.map(item => (
< SlTreeItem > {item}< / SlTreeItem >
))}
< / SlTreeItem >
< / SlTree >
);
};
```
2022-08-26 13:11:09 +00:00
### Custom expand/collapse icons
2022-08-01 12:41:36 +00:00
2022-08-17 20:22:03 +00:00
Use the `expand-icon` and `collapse-icon` slots to change the expand and collapse icons, respectively.
2022-08-01 12:41:36 +00:00
```html preview
2022-08-17 20:22:03 +00:00
< sl-tree >
2022-08-26 13:11:09 +00:00
< sl-icon name = "plus-square" slot = "expand-icon" > < / sl-icon >
< sl-icon name = "dash-square" slot = "collapse-icon" > < / sl-icon >
2022-08-01 12:41:36 +00:00
< sl-tree-item >
Deciduous
< sl-tree-item > Birch< / sl-tree-item >
< sl-tree-item >
Maple
< sl-tree-item > Field maple< / sl-tree-item >
< sl-tree-item > Red maple< / sl-tree-item >
< sl-tree-item > Sugar maple< / sl-tree-item >
< / sl-tree-item >
< sl-tree-item > Oak< / sl-tree-item >
< / sl-tree-item >
< sl-tree-item >
Coniferous
< sl-tree-item > Cedar< / sl-tree-item >
< sl-tree-item > Pine< / sl-tree-item >
< sl-tree-item > Spruce< / sl-tree-item >
< / sl-tree-item >
< sl-tree-item >
Non-trees
< sl-tree-item > Bamboo< / sl-tree-item >
< sl-tree-item > Cactus< / sl-tree-item >
< sl-tree-item > Fern< / sl-tree-item >
< / sl-tree-item >
< / sl-tree >
```
<!-- prettier - ignore -->
```jsx react
import { SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< SlTree >
2022-08-26 13:11:09 +00:00
< SlIcon name = "plus-square" slot = "expand-icon" > < / SlIcon >
< SlIcon name = "dash-square" slot = "collapse-icon" > < / SlIcon >
2022-08-01 12:41:36 +00:00
< SlTreeItem >
Deciduous
< SlTreeItem > Birch< / SlTreeItem >
< SlTreeItem >
Maple
< SlTreeItem > Field maple< / SlTreeItem >
< SlTreeItem > Red maple< / SlTreeItem >
< SlTreeItem > Sugar maple< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem > Oak< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem >
Coniferous
< SlTreeItem > Cedar< / SlTreeItem >
< SlTreeItem > Pine< / SlTreeItem >
< SlTreeItem > Spruce< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem >
Non-trees
< SlTreeItem > Bamboo< / SlTreeItem >
< SlTreeItem > Cactus< / SlTreeItem >
< SlTreeItem > Fern< / SlTreeItem >
< / SlTreeItem >
< / SlTree >
);
```
2022-07-26 19:53:24 +00:00
### With Icons
2022-07-10 15:23:14 +00:00
2022-07-26 19:53:24 +00:00
Decorative icons can be used before labels to provide hints for each node.
2022-07-10 15:23:14 +00:00
```html preview
2022-07-26 19:53:24 +00:00
< sl-tree class = "tree-with-icons" >
2022-07-10 15:23:14 +00:00
< sl-tree-item expanded >
2022-07-26 19:53:24 +00:00
< sl-icon name = "folder" > < / sl-icon >
2022-08-26 13:11:09 +00:00
Documents
2022-07-10 15:23:14 +00:00
< sl-tree-item >
2022-07-26 19:53:24 +00:00
< sl-icon name = "folder" > < / sl-icon >
2022-08-26 13:11:09 +00:00
Photos
2022-07-26 19:53:24 +00:00
< sl-tree-item >
2022-08-26 13:11:09 +00:00
< sl-icon name = "image" > < / sl-icon >
birds.jpg
2022-07-26 19:53:24 +00:00
< / sl-tree-item >
2022-08-26 13:11:09 +00:00
< sl-tree-item >
< sl-icon name = "image" > < / sl-icon >
kitten.jpg
2022-07-26 19:53:24 +00:00
< / sl-tree-item >
< sl-tree-item >
2022-08-26 13:11:09 +00:00
< sl-icon name = "image" > < / sl-icon >
puppy.jpg
2022-07-26 19:53:24 +00:00
< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree-item >
< sl-tree-item >
2022-08-26 13:11:09 +00:00
< sl-icon name = "folder" > < / sl-icon >
Writing
2022-07-26 19:53:24 +00:00
< sl-tree-item >
2022-08-26 13:11:09 +00:00
< sl-icon name = "file" > < / sl-icon >
draft.txt
2022-07-26 19:53:24 +00:00
< / sl-tree-item >
< sl-tree-item >
2022-08-26 13:11:09 +00:00
< sl-icon name = "file-pdf" > < / sl-icon >
final.pdg
< / sl-tree-item >
< sl-tree-item >
< sl-icon name = "file-bar-graph" > < / sl-icon >
sales.txt
2022-07-26 19:53:24 +00:00
< / sl-tree-item >
2022-07-10 15:23:14 +00:00
< / sl-tree-item >
< / sl-tree-item >
< / sl-tree >
```
2022-07-26 19:53:24 +00:00
```jsx react
import { SlIcon, SlTree, SlTreeItem } from '@shoelace-style/shoelace/dist/react';
const App = () => {
return (
< SlTree class = "tree-with-icons" >
< SlTreeItem expanded >
< SlIcon name = "folder" / >
Root
< SlTreeItem >
< SlIcon name = "folder" / >
Folder 1< SlTreeItem >
< SlIcon name = "files" / >
File 1 - 1
< / SlTreeItem >
< SlTreeItem disabled >
< SlIcon name = "files" / >
File 1 - 2
< / SlTreeItem >
< SlTreeItem >
< SlIcon name = "files" / >
File 1 - 3
< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem >
< SlIcon name = "files" / >
Folder 2< SlTreeItem >
< SlIcon name = "files" / >
File 2 - 1
< / SlTreeItem >
< SlTreeItem >
< SlIcon name = "files" / >
File 2 - 2
< / SlTreeItem >
< / SlTreeItem >
< SlTreeItem >
< SlIcon name = "files" / >
File 1
< / SlTreeItem >
< / SlTreeItem >
< / SlTree >
);
};
```
2022-07-10 15:23:14 +00:00
[component-metadata:sl-tree]