StartFoundationsComponentsContent

Tree

A tree shows nested items, such as files and folders, in rows that open and close to any depth.

Stable Figma
OverviewUsageAPIAccessibility
<tk-tree label="Files"></tk-tree>
<tk-tree label="Files"></tk-tree>
<script type="module">
  const tree = document.querySelector('tk-tree');
  tree.data = [
    { id: 'src', label: 'src', children: [
      { id: 'components', label: 'components', children: [{ id: 'button', label: 'button.ts' }] },
      { id: 'index', label: 'index.ts' },
    ] },
    { id: 'readme', label: 'README.md' },
  ];
  tree.expanded = ['src'];
</script>

Anatomy

Part What it is
tk-tree the whole tree; one Tab stop
node one row with a label; nodes come from the data property
chevron on a node with children; turns when the node opens
group a node's children, indented one step

A node is { id, label, children? }. A node without children is an end node. The tree never changes your data; it keeps track of what is open and selected itself.

Selection

selection What it does
none (default) nothing is selectable; for navigating
single one node at a time; selecting another replaces it
multiple any number of nodes
tree.selection = 'multiple';
tree.selected = ['button', 'index'];
tree.addEventListener('tk-selection-change', (e) => console.log(e.detail.selected));

Selecting is not opening. Space or a click selects; Enter or a click also fires tk-activate, which means "open this one". A file browser usually uses both.

The arrow keys never change the selection, so you can select scattered nodes with the keyboard: arrow to one, press Space, arrow to the next, press Space again.

Sizes

There is one size. For a denser list, set --tk-tree-row-height on the tree; change the indent with --tk-tree-indent.