--- meta: title: Tree description: Trees allow you to display a hierarchical list of selectable tree items. Items with children can be expanded and collapsed as desired by the user. layout: component --- ```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 from '@shoelace-style/shoelace/dist/react/tree'; import SlTreeItem from '@shoelace-style/shoelace/dist/react/tree-item'; const App = () => ( Deciduous Birch Maple Field maple Red maple Sugar maple Oak Coniferous Cedar Pine Spruce Non-trees Bamboo Cactus Fern ); ``` ## Examples ### Selection Modes The `selection` attribute lets you change the selection behavior of the tree. - 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. ```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 from '@shoelace-style/shoelace/dist/react/tree'; import SlTreeItem from '@shoelace-style/shoelace/dist/react/tree-item'; 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 ``` {% raw %} ```jsx:react import SlTree from '@shoelace-style/shoelace/dist/react/tree'; import SlTreeItem from '@shoelace-style/shoelace/dist/react/tree-item'; const App = () => ( Deciduous Birch Maple Field maple Red maple Sugar maple Oak Coniferous Cedar Pine Spruce Non-trees Bamboo Cactus Fern ); ``` {% endraw %} ### 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 from '@shoelace-style/shoelace/dist/react/tree'; import SlTreeItem from '@shoelace-style/shoelace/dist/react/tree-item'; 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} ))} ); }; ``` ### Customizing the Expand and Collapse Icons Use the `expand-icon` and `collapse-icon` slots to change the expand and collapse icons, respectively. To disable the animation, override the `rotate` property on the `expand-button` part as shown below. ```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 from '@shoelace-style/shoelace/dist/react/tree'; import SlTreeItem from '@shoelace-style/shoelace/dist/react/tree-item'; const App = () => ( Deciduous Birch Maple Field maple Red maple Sugar maple Oak Coniferous Cedar Pine Spruce Non-trees Bamboo Cactus Fern ); ``` ### With Icons Decorative icons can be used before labels to provide hints for each node. ```html:preview Documents Photos birds.jpg kitten.jpg puppy.jpg Writing draft.txt final.pdf sales.xls ``` ```jsx:react import SlIcon from '@shoelace-style/shoelace/dist/react/icon'; import SlTree from '@shoelace-style/shoelace/dist/react/tree'; import SlTreeItem from '@shoelace-style/shoelace/dist/react/tree-item'; const App = () => { return ( Root Folder 1 File 1 - 1 File 1 - 2 File 1 - 3 Folder 2 File 2 - 1 File 2 - 2 File 1 ); }; ```