Tree
Nested rows that open and close, to pick one or several.
Installation
pnpm dlx @human-kit/design add treenpx @human-kit/design add treeyarn dlx @human-kit/design add treebunx @human-kit/design add treeThe 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.
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.
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.Rootor 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;nonefor a tree that only shows;multipleto pick several, as permissions. - Give a row
iconwhen kinds mix in one tree, folders and files. Leave it out when every row is of one kind. - Use
outlinefor a tree on its own card,ghostfor 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 arole="treeitem", witharia-expandedon a branch andaria-selectedwhen it can be chosen. - The tree is one tab stop.
ArrowUpandArrowDownmove through the rows,ArrowRightopens a branch or goes into it,ArrowLeftcloses it or goes to its parent.HomeandEndgo to the first and the last row. SpaceandEnterchoose the row, and withmultiple,CtrlorCmdwithAchooses 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. ATree.Sectionwith atitleis arole="group"named by it.
API reference
Tree.Root
| Prop | Type | Default |
|---|---|---|
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. | '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 | 'none', 'descendants' with multiple |
selectedKeys | Iterable<string | number> The ids of the chosen rows. Bindable. | — |
defaultSelectedKeys | Iterable<string | number> The chosen rows at the start, when you give no | — |
expandedKeys | Iterable<string | number> The ids of the open branches. Bindable. | — |
defaultExpandedKeys | Iterable<string | number> The open branches at the start, when you give no | — |
disabledKeys | Iterable<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' |
disallowEmptySelection | boolean 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 | — |
itemTransition | TreeItemTransition 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
| Prop | Type | Default |
|---|---|---|
title | string The text of the row. Required. | — |
id | string | number The key of the row in | — |
icon | Snippet A small icon before the text: a folder, a file. | — |
disabled | boolean Turns the row off, as | false |
textValue | string The text typeahead matches, when it is not the title. | — |
children | Snippet The rows under this one. Giving them makes the row a branch that opens. | — |
Plus every attribute of a <div>.
Tree.Section
| Prop | Type | Default |
|---|---|---|
title | string A small heading over a group of top-level rows. Without it, give | — |
Plus every attribute of a <div>.