Skip to main content
Light Dark System

Tree

<sc-tree> | ScTree
Since 2.0 stable

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.

Deciduous Birch Maple Field maple Red maple Sugar maple Oak Coniferous Cedar Pine Spruce Non-trees Bamboo Cactus Fern
<sc-tree>
  <sc-tree-item>
    Deciduous
    <sc-tree-item>Birch</sc-tree-item>
    <sc-tree-item>
      Maple
      <sc-tree-item>Field maple</sc-tree-item>
      <sc-tree-item>Red maple</sc-tree-item>
      <sc-tree-item>Sugar maple</sc-tree-item>
    </sc-tree-item>
    <sc-tree-item>Oak</sc-tree-item>
  </sc-tree-item>

  <sc-tree-item>
    Coniferous
    <sc-tree-item>Cedar</sc-tree-item>
    <sc-tree-item>Pine</sc-tree-item>
    <sc-tree-item>Spruce</sc-tree-item>
  </sc-tree-item>

  <sc-tree-item>
    Non-trees
    <sc-tree-item>Bamboo</sc-tree-item>
    <sc-tree-item>Cactus</sc-tree-item>
    <sc-tree-item>Fern</sc-tree-item>
  </sc-tree-item>
</sc-tree>
import ScTree from '@styledc-dev/styledc/dist/react/tree';
import ScTreeItem from '@styledc-dev/styledc/dist/react/tree-item';

const App = () => (
  <ScTree>
    <ScTreeItem>
      Deciduous
      <ScTreeItem>Birch</ScTreeItem>
      <ScTreeItem>
        Maple
        <ScTreeItem>Field maple</ScTreeItem>
        <ScTreeItem>Red maple</ScTreeItem>
        <ScTreeItem>Sugar maple</ScTreeItem>
      </ScTreeItem>
      <ScTreeItem>Oak</ScTreeItem>
    </ScTreeItem>

    <ScTreeItem>
      Coniferous
      <ScTreeItem>Cedar</ScTreeItem>
      <ScTreeItem>Pine</ScTreeItem>
      <ScTreeItem>Spruce</ScTreeItem>
    </ScTreeItem>

    <ScTreeItem>
      Non-trees
      <ScTreeItem>Bamboo</ScTreeItem>
      <ScTreeItem>Cactus</ScTreeItem>
      <ScTreeItem>Fern</ScTreeItem>
    </ScTreeItem>
  </ScTree>
);

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.
Single Multiple Leaf
Item 1 Item A Item Z Item Y Item X Item B Item C Item 2 Item 3
<sc-select id="selection-mode" value="single" label="Selection">
  <sc-option value="single">Single</sc-option>
  <sc-option value="multiple">Multiple</sc-option>
  <sc-option value="leaf">Leaf</sc-option>
</sc-select>

<br />

<sc-tree class="tree-selectable">
  <sc-tree-item>
    Item 1
    <sc-tree-item>
      Item A
      <sc-tree-item>Item Z</sc-tree-item>
      <sc-tree-item>Item Y</sc-tree-item>
      <sc-tree-item>Item X</sc-tree-item>
    </sc-tree-item>
    <sc-tree-item>Item B</sc-tree-item>
    <sc-tree-item>Item C</sc-tree-item>
  </sc-tree-item>
  <sc-tree-item>Item 2</sc-tree-item>
  <sc-tree-item>Item 3</sc-tree-item>
</sc-tree>

<script>
  const selectionMode = document.querySelector('#selection-mode');
  const tree = document.querySelector('.tree-selectable');

  selectionMode.addEventListener('sc-change', () => {
    tree.querySelectorAll('sc-tree-item').forEach(item => (item.selected = false));
    tree.selection = selectionMode.value;
  });
</script>
import ScTree from '@styledc-dev/styledc/dist/react/tree';
import ScTreeItem from '@styledc-dev/styledc/dist/react/tree-item';

const App = () => {
  const [selection, setSelection] = useState('single');

  return (
    <>
      <ScSelect label="Selection" value={selection} onScChange={event => setSelection(event.target.value)}>
        <ScMenuItem value="single">single</ScMenuItem>
        <ScMenuItem value="multiple">multiple</ScMenuItem>
        <ScMenuItem value="leaf">leaf</ScMenuItem>
      </ScSelect>

      <br />

      <ScTree selection={selection}>
        <ScTreeItem>
          Item 1
          <ScTreeItem>
            Item A
            <ScTreeItem>Item Z</ScTreeItem>
            <ScTreeItem>Item Y</ScTreeItem>
            <ScTreeItem>Item X</ScTreeItem>
          </ScTreeItem>
          <ScTreeItem>Item B</ScTreeItem>
          <ScTreeItem>Item C</ScTreeItem>
        </ScTreeItem>
        <ScTreeItem>Item 2</ScTreeItem>
        <ScTreeItem>Item 3</ScTreeItem>
      </ScTree>
    </>
  );
};

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.

Deciduous Birch Maple Field maple Red maple Sugar maple Oak Coniferous Cedar Pine Spruce Non-trees Bamboo Cactus Fern
<sc-tree class="tree-with-lines">
  <sc-tree-item expanded>
    Deciduous
    <sc-tree-item>Birch</sc-tree-item>
    <sc-tree-item expanded>
      Maple
      <sc-tree-item>Field maple</sc-tree-item>
      <sc-tree-item>Red maple</sc-tree-item>
      <sc-tree-item>Sugar maple</sc-tree-item>
    </sc-tree-item>
    <sc-tree-item>Oak</sc-tree-item>
  </sc-tree-item>

  <sc-tree-item>
    Coniferous
    <sc-tree-item>Cedar</sc-tree-item>
    <sc-tree-item>Pine</sc-tree-item>
    <sc-tree-item>Spruce</sc-tree-item>
  </sc-tree-item>

  <sc-tree-item>
    Non-trees
    <sc-tree-item>Bamboo</sc-tree-item>
    <sc-tree-item>Cactus</sc-tree-item>
    <sc-tree-item>Fern</sc-tree-item>
  </sc-tree-item>
</sc-tree>

<style>
  .tree-with-lines {
    --indent-guide-width: 1px;
  }
</style>
import ScTree from '@styledc-dev/styledc/dist/react/tree';
import ScTreeItem from '@styledc-dev/styledc/dist/react/tree-item';

const App = () => (
  <ScTree class="tree-with-lines" style={{ '--indent-guide-width': '1px' }}>
    <ScTreeItem expanded>
      Deciduous
      <ScTreeItem>Birch</ScTreeItem>
      <ScTreeItem expanded>
        Maple
        <ScTreeItem>Field maple</ScTreeItem>
        <ScTreeItem>Red maple</ScTreeItem>
        <ScTreeItem>Sugar maple</ScTreeItem>
      </ScTreeItem>
      <ScTreeItem>Oak</ScTreeItem>
    </ScTreeItem>

    <ScTreeItem>
      Coniferous
      <ScTreeItem>Cedar</ScTreeItem>
      <ScTreeItem>Pine</ScTreeItem>
      <ScTreeItem>Spruce</ScTreeItem>
    </ScTreeItem>

    <ScTreeItem>
      Non-trees
      <ScTreeItem>Bamboo</ScTreeItem>
      <ScTreeItem>Cactus</ScTreeItem>
      <ScTreeItem>Fern</ScTreeItem>
    </ScTreeItem>
  </ScTree>
);

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 sc-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.

Available Trees
<sc-tree>
  <sc-tree-item lazy>Available Trees</sc-tree-item>
</sc-tree>

<script type="module">
  const lazyItem = document.querySelector('sc-tree-item[lazy]');

  lazyItem.addEventListener('sc-lazy-load', () => {
    // Simulate asynchronous loading
    setTimeout(() => {
      const subItems = ['Birch', 'Cedar', 'Maple', 'Pine'];

      for (const item of subItems) {
        const treeItem = document.createElement('sc-tree-item');
        treeItem.innerText = item;
        lazyItem.append(treeItem);
      }

      // Disable lazy mode once the content has been loaded
      lazyItem.lazy = false;
    }, 1000);
  });
</script>
import ScTree from '@styledc-dev/styledc/dist/react/tree';
import ScTreeItem from '@styledc-dev/styledc/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 (
    <ScTree>
      <ScTreeItem lazy={lazy} onScLazyLoad={handleLazyLoad}>
        Available Trees
        {childItems.map(item => (
          <ScTreeItem>{item}</ScTreeItem>
        ))}
      </ScTreeItem>
    </ScTree>
  );
};

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.

Deciduous Birch Maple Field maple Red maple Sugar maple Oak Coniferous Cedar Pine Spruce Non-trees Bamboo Cactus Fern
<sc-tree class="custom-icons">
  <sc-icon name="plus-square" slot="expand-icon"></sc-icon>
  <sc-icon name="dash-square" slot="collapse-icon"></sc-icon>

  <sc-tree-item>
    Deciduous
    <sc-tree-item>Birch</sc-tree-item>
    <sc-tree-item>
      Maple
      <sc-tree-item>Field maple</sc-tree-item>
      <sc-tree-item>Red maple</sc-tree-item>
      <sc-tree-item>Sugar maple</sc-tree-item>
    </sc-tree-item>
    <sc-tree-item>Oak</sc-tree-item>
  </sc-tree-item>

  <sc-tree-item>
    Coniferous
    <sc-tree-item>Cedar</sc-tree-item>
    <sc-tree-item>Pine</sc-tree-item>
    <sc-tree-item>Spruce</sc-tree-item>
  </sc-tree-item>

  <sc-tree-item>
    Non-trees
    <sc-tree-item>Bamboo</sc-tree-item>
    <sc-tree-item>Cactus</sc-tree-item>
    <sc-tree-item>Fern</sc-tree-item>
  </sc-tree-item>
</sc-tree>

<style>
  .custom-icons sc-tree-item::part(expand-button) {
    /* Disable the expand/collapse animation */
    rotate: none;
  }
</style>
import ScTree from '@styledc-dev/styledc/dist/react/tree';
import ScTreeItem from '@styledc-dev/styledc/dist/react/tree-item';

const App = () => (
  <ScTree>
    <ScIcon name="plus-square" slot="expand-icon"></ScIcon>
    <ScIcon name="dash-square" slot="collapse-icon"></ScIcon>

    <ScTreeItem>
      Deciduous
      <ScTreeItem>Birch</ScTreeItem>
      <ScTreeItem>
        Maple
        <ScTreeItem>Field maple</ScTreeItem>
        <ScTreeItem>Red maple</ScTreeItem>
        <ScTreeItem>Sugar maple</ScTreeItem>
      </ScTreeItem>
      <ScTreeItem>Oak</ScTreeItem>
    </ScTreeItem>

    <ScTreeItem>
      Coniferous
      <ScTreeItem>Cedar</ScTreeItem>
      <ScTreeItem>Pine</ScTreeItem>
      <ScTreeItem>Spruce</ScTreeItem>
    </ScTreeItem>

    <ScTreeItem>
      Non-trees
      <ScTreeItem>Bamboo</ScTreeItem>
      <ScTreeItem>Cactus</ScTreeItem>
      <ScTreeItem>Fern</ScTreeItem>
    </ScTreeItem>
  </ScTree>
);

With Icons

Decorative icons can be used before labels to provide hints for each node.

Documents Photos birds.jpg kitten.jpg puppy.jpg Writing draft.txt final.pdf sales.xls
<sc-tree class="tree-with-icons">
  <sc-tree-item expanded>
    <sc-icon name="folder"></sc-icon>
    Documents

    <sc-tree-item>
      <sc-icon name="folder"> </sc-icon>
      Photos
      <sc-tree-item>
        <sc-icon name="image"></sc-icon>
        birds.jpg
      </sc-tree-item>
      <sc-tree-item>
        <sc-icon name="image"></sc-icon>
        kitten.jpg
      </sc-tree-item>
      <sc-tree-item>
        <sc-icon name="image"></sc-icon>
        puppy.jpg
      </sc-tree-item>
    </sc-tree-item>

    <sc-tree-item>
      <sc-icon name="folder"></sc-icon>
      Writing
      <sc-tree-item>
        <sc-icon name="file"></sc-icon>
        draft.txt
      </sc-tree-item>
      <sc-tree-item>
        <sc-icon name="file-pdf"></sc-icon>
        final.pdf
      </sc-tree-item>
      <sc-tree-item>
        <sc-icon name="file-bar-graph"></sc-icon>
        sales.xls
      </sc-tree-item>
    </sc-tree-item>
  </sc-tree-item>
</sc-tree>
import ScIcon from '@styledc-dev/styledc/dist/react/icon';
import ScTree from '@styledc-dev/styledc/dist/react/tree';
import ScTreeItem from '@styledc-dev/styledc/dist/react/tree-item';

const App = () => {
  return (
    <ScTree class="tree-with-icons">
      <ScTreeItem expanded>
        <ScIcon name="folder" />
        Root
        <ScTreeItem>
          <ScIcon name="folder" />
          Folder 1<ScTreeItem>
            <ScIcon name="files" />
            File 1 - 1
          </ScTreeItem>
          <ScTreeItem disabled>
            <ScIcon name="files" />
            File 1 - 2
          </ScTreeItem>
          <ScTreeItem>
            <ScIcon name="files" />
            File 1 - 3
          </ScTreeItem>
        </ScTreeItem>
        <ScTreeItem>
          <ScIcon name="files" />
          Folder 2<ScTreeItem>
            <ScIcon name="files" />
            File 2 - 1
          </ScTreeItem>
          <ScTreeItem>
            <ScIcon name="files" />
            File 2 - 2
          </ScTreeItem>
        </ScTreeItem>
        <ScTreeItem>
          <ScIcon name="files" />
          File 1
        </ScTreeItem>
      </ScTreeItem>
    </ScTree>
  );
};

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/tree/tree.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/tree/tree.js';

To import this component using a bundler:

import '@styledc-dev/styledc/dist/components/tree/tree.js';

To import this component as a React component:

import ScTree from '@styledc-dev/styledc/dist/react/tree';

Slots

Name Description
(default) The default slot.
expand-icon The icon to show when the tree item is expanded. Works best with <sc-icon>.
collapse-icon The icon to show when the tree item is collapsed. Works best with <sc-icon>.

Learn more about using slots.

Properties

Name Description Reflects Type Default
selection The selection behavior of the tree. Single selection allows only one node to be selected at a time. Multiple displays checkboxes and allows more than one node to be selected. Leaf allows only leaf nodes to be selected. 'single' | 'multiple' | 'leaf' 'single'
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
sc-selection-change onScSelectionChange Emitted when a tree item is selected or deselected. { selection: ScTreeItem[] }

Learn more about events.

Custom Properties

Name Description Default
--indent-size The size of the indentation for nested items. var(–sc-spacing-medium)
--indent-guide-color The color of the indentation line. var(–sc-color-neutral-200)
--indent-guide-offset The amount of vertical spacing to leave between the top and bottom of the indentation line’s starting position. 0
--indent-guide-style The style of the indentation line, e.g. solid, dotted, dashed. solid
--indent-guide-width The width of the indentation line. 0

Learn more about customizing CSS custom properties.

Parts

Name Description
base The component’s base wrapper.

Learn more about customizing CSS parts.