Skip to the content
human-kit design
55 — Data display

Tree

Nested rows that open and close, to pick one or several.

Lab results
Blood panel, March
Hormone levels

Installation

pnpm dlx @human-kit/design add tree
npx @human-kit/design add tree
yarn dlx @human-kit/design add tree
bunx @human-kit/design add tree

The command copies the files to src/lib/components/ui/tree. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Tree from '$lib/components/ui/tree';
</script>

<Tree.Root aria-label="Patient files" defaultExpandedKeys={['labs']}>
	<Tree.Item id="labs" title="Lab results">
		<Tree.Item id="blood" title="Blood panel, March" />
		<Tree.Item id="hormones" title="Hormone levels" />
	</Tree.Item>
	<Tree.Item id="consent" title="Consent form" />
</Tree.Root>

Examples

Choose several, with the branches

With selectionMode="multiple" each row gets a checkbox, and the propagation is descendants: a branch chooses all its rows at once, shows checked when all of them are chosen, and a dash when only some are. Pass selectionPropagation="none" for rows that stand alone.

Medical records
Read
Edit
Export

A long tree

For thousands of rows, give the nodes as items, a snippet that draws one top-level node, and virtualizer: the tree draws only the open rows near its viewport. Rows are one height, 24px, the rowHeight it is told, and the tree is the element that scrolls, so give it a height. The arrows, Home, End, typing and opening a branch reach every row. Sections are not drawn, and rows do not slide open.

Cycle 001
Embryo 001-1 · day 5 · AA
Embryo 001-2 · day 6 · CB
Embryo 001-3 · day 5 · BC
Embryo 001-4 · day 6 · BB
Embryo 001-5 · day 5 · BA
Embryo 001-6 · day 6 · AB
Embryo 001-7 · day 5 · AA
Embryo 001-8 · day 6 · CB

Guidelines

  • Use a tree for things that nest and that the reader opens one level at a time: folders of files, a chart of accounts, permissions by area.
  • For a single level of groups, an Accordion.Root or a plain list is simpler. A tree earns its place from the second level down.
  • Use selectionMode="single" for a tree that drives the page, such as a folder list beside its files; none for a tree that only shows; multiple to pick several, as permissions.
  • Give a row icon when kinds mix in one tree, folders and files. Leave it out when every row is of one kind.
  • Use outline for a tree on its own card, ghost for one inside a panel or a sidebar that is already a box.
  • Open the branch that matters with defaultExpandedKeys, so the reader does not start from a closed list.

Accessibility

  • The tree is a role="tree" and each row a role="treeitem", with aria-expanded on a branch and aria-selected when it can be chosen.
  • The tree is one tab stop. ArrowUp and ArrowDown move through the rows, ArrowRight opens a branch or goes into it, ArrowLeft closes it or goes to its parent. Home and End go to the first and the last row.
  • Space and Enter choose the row, and with multiple, Ctrl or Cmd with A chooses every open row. Typing a letter goes to the next row that starts with it.
  • The chevron opens with the pointer and is named "Open" with the row title, but it is not a tab stop: the arrow keys do its work.
  • Name the tree with aria-label. A Tree.Section with a title is a role="group" named by it.

API reference

Tree.Root

PropTypeDefault
variant'outline' | 'filled' | 'ghost'

A card around the rows, a soft grey, or the rows straight on the surface.

'outline'
selectionMode'none' | 'single' | 'multiple'

How many rows can be chosen. multiple gives each row a checkbox.

'single'
selectionBehavior'replace' | 'toggle'

Whether a press replaces the choice or adds and takes away a row. The default follows the mode.

'replace', 'toggle' with multiple
selectionPropagation'none' | 'descendants'

With descendants, choosing a branch chooses every row under it, and a branch shows checked when all its rows are and a dash when only some are. With none each row stands alone. The default follows the mode.

'none', 'descendants' with multiple
selectedKeysIterable<string | number>

The ids of the chosen rows. Bindable.

—
defaultSelectedKeysIterable<string | number>

The chosen rows at the start, when you give no selectedKeys.

—
expandedKeysIterable<string | number>

The ids of the open branches. Bindable.

—
defaultExpandedKeysIterable<string | number>

The open branches at the start, when you give no expandedKeys.

—
disabledKeysIterable<string | number>

The ids of the rows to turn off.

—
disabledBehavior'all' | 'selection'

Whether a disabled row is out of reach, or only cannot be chosen and still opens and takes the focus.

'all'
disallowEmptySelectionboolean

Keeps one row chosen at least.

false
onSelectionChange(keys: Set<string | number>) => void

Called when the choice changes.

—
onExpandedKeysChange(keys: Set<string | number>) => void

Called when a branch opens or closes.

—
onAction(id: string | number) => void

Called when a row is pressed or Enter is hit on it, for a row that opens something.

—
itemTransitionTreeItemTransition

How the rows of a branch come in and go out when it opens and closes.

collapseMotion, 150ms

Plus every attribute of a <div>.

Tree.Item

PropTypeDefault
titlestring

The text of the row. Required.

—
idstring | number

The key of the row in selectedKeys, expandedKeys and disabledKeys.

—
iconSnippet

A small icon before the text: a folder, a file.

—
disabledboolean

Turns the row off, as disabledKeys would.

false
textValuestring

The text typeahead matches, when it is not the title.

—
childrenSnippet

The rows under this one. Giving them makes the row a branch that opens.

—

Plus every attribute of a <div>.

Tree.Section

PropTypeDefault
titlestring

A small heading over a group of top-level rows. Without it, give aria-label.

—

Plus every attribute of a <div>.