tree

tree is a nested list that opens and closes, such as a file tree. A person walks it with the arrow keys.

src
index.ts
app.tsrx
README.md

Last change: nothing yet

import { state } from '@markless/core';import { lucide, tree } from '@markless/ui';export default function Files() @{	const openSrc = true;	let last = state('nothing yet');	<div class="desk">		<tree.root class="files" aria-label="Project files">			<tree.item				class="row"				open={openSrc}				onChange={(open: boolean) => {					last = open ? 'src opened' : 'src closed';				}}			>				<div class="line">					<tree.itemtrigger class="arrow">						<tree.itemindicator class="arrow-mark">							<lucide.chevronright class="arrow-icon" aria-hidden="true" />						</tree.itemindicator>					</tree.itemtrigger>					<lucide.folder class="kind" aria-hidden="true" />					<tree.itemlabel>src</tree.itemlabel>				</div>				<tree.itemcontent class="group">					<tree.item class="row" leaf level={2}>						<div class="line">							<lucide.file class="kind" aria-hidden="true" />							<tree.itemlabel>index.ts</tree.itemlabel>						</div>					</tree.item>					<tree.item class="row" leaf level={2}>						<div class="line">							<lucide.file class="kind" aria-hidden="true" />							<tree.itemlabel>app.tsrx</tree.itemlabel>						</div>					</tree.item>				</tree.itemcontent>			</tree.item>			<tree.item				class="row"				onChange={(open: boolean) => {					last = open ? 'docs opened' : 'docs closed';				}}			>				<div class="line">					<tree.itemtrigger class="arrow">						<tree.itemindicator class="arrow-mark">							<lucide.chevronright class="arrow-icon" aria-hidden="true" />						</tree.itemindicator>					</tree.itemtrigger>					<lucide.folder class="kind" aria-hidden="true" />					<tree.itemlabel>docs</tree.itemlabel>				</div>				<tree.itemcontent class="group">					<tree.item class="row" leaf level={2}>						<div class="line">							<lucide.file class="kind" aria-hidden="true" />							<tree.itemlabel>guide.md</tree.itemlabel>						</div>					</tree.item>				</tree.itemcontent>			</tree.item>			<tree.item class="row" leaf>				<div class="line">					<lucide.file class="kind" aria-hidden="true" />					<tree.itemlabel>README.md</tree.itemlabel>				</div>			</tree.item>		</tree.root>		<p class="readout">			Last change: <span class="readout-value">{last}</span>		</p>	</div>;}

Click a row, then walk the tree with the arrow keys. On docs, Right opens it. Press r, and focus jumps to README.md. Typing a letter finds the next row whose label starts with it. The line under the tree shows the last row that opened or closed.

A level does not need new parts. A tree.item holds a tree.itemcontent, and that holds more tree.items. Each item keeps its own open or closed state.

When to use it

Use tree for nested data that a person opens one branch at a time, such as folders. The family offers no selection. It writes no aria-selected, and it keeps any aria-selected you write.

Parts

tree anatomy
Anatomy perspectiveFlat
Layers
WorkspaceBrandColors✓Logos
tree.root

The tree container.

  • tree.root is the container, with role="tree". The whole tree is one Tab stop.
  • tree.item is one row, with role="treeitem". Mark a row with no children as leaf.
  • tree.itemtrigger is the button that opens and closes a row. Tab never stops on it. The arrow keys and Enter press it for you.
  • tree.itemcontent holds a row's children, with role="group". A closed group gets hidden. It stays in the page.
  • tree.itemlabel is the row's name. The trigger takes its name from it, and typing a letter matches it.
  • tree.itemindicator is a mark, such as an arrow, that shows whether the row is open.
  • tree.label is a heading for the tree. It does not name the tree.

Write a level on every nested tree.item. It defaults to 1, the top level. The family does not count the depth for you, because a row cannot read the row around it. level becomes aria-level.

Styling hooks

Attribute On Present when
ui-open item, itemtrigger, itemcontent, itemindicator The row is open.
ui-closed item, itemtrigger, itemcontent, itemindicator The row is closed.
ui-leaf item The row is a leaf.
ui-disabled root, item disabled is set there.
ui-inside root Focus is on a control inside a row, not on the row itself.

A leaf writes neither ui-open nor ui-closed on its row. It also has no aria-expanded, so a screen reader can tell "nothing to open" from "closed".

Keyboard

The family's tests check each of these rows. The arrows reach only the rows a person can see.

Key What it does
Down arrow or Up arrow Moves to the next or the previous visible row. It never opens anything.
Home or End Moves to the first or the last visible row.
Right arrow Opens a closed row. On an open row, moves to its first child. On a leaf, does nothing.
Left arrow Closes an open row. On a closed row or a leaf, moves to its parent.
Enter or Space Opens or closes the row.
* Opens every closed row at the same level as this one.
Letters Moves to the next visible row whose label starts with what you typed.

When focus is on a link or a button inside a row, the arrows belong to that control. The tree walks again once focus is back on the row. The walk skips a disabled row, and no key opens it.

Gotchas

API reference

tree.root

disabledboolean
Nobody can open or close anything while this is set. Default false.

tree.item

openboolean
This node's children are showing. Omit it and the node starts closed. Default false.
leafboolean
This node has no children. A leaf reports no open state at all, which is the WAI-ARIA rule a reader needs to tell "nothing to open" from "closed". Default false.
levelnumber
How deep this node sits, counting from 1. It is written rather than read off the enclosing node, because a component that roots an instance of a family cannot also read the enclosing instance of that same family. Default 1.
disabledboolean
Nobody can open or close this node, and the walk steps over it. A locked node written open stays open, and the nodes inside it are still reachable. Default false.
onChange(open: boolean) => void
Called with the new state when a person opens or closes this node.

tree.itemcontent

No props of its own.

tree.itemindicator

No props of its own.

tree.itemlabel

No props of its own.

tree.itemtrigger

No props of its own.

tree.label

No props of its own.