tree
tree is a nested list that opens and closes, such as a file tree. A person walks it with the
arrow keys.
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>;}
.desk { display: grid; gap: 0.7em; width: min(20rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.readout { margin: 0;}.readout-value { font-family: var(--font-mono);}.files { display: grid; gap: 0.2em; box-sizing: border-box; width: min(20rem, 100%); padding: 0.6em; border: 1px solid var(--ink); border-radius: 6px; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.files:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px;}.row { display: grid; gap: 0.2em; outline: none;}.line { display: flex; gap: 0.5em; align-items: center; padding: 0.35em 0.6em; border-radius: 6px; cursor: default; transition: background 140ms ease;}.row[ui-leaf] > .line { padding-inline-start: 2.45em;}.line:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.row:focus > .line { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.row:focus-visible > .line { outline: 2px solid var(--ink); outline-offset: -2px;}.row[ui-disabled] > .line { opacity: 0.55; cursor: not-allowed;}.row[ui-disabled] > .line:hover { background: transparent;}.arrow { display: grid; place-items: center; width: 1.35em; height: 1.35em; padding: 0; border: 0; border-radius: 4px; background: transparent; color: var(--ink); cursor: pointer;}.arrow:disabled { cursor: not-allowed;}.arrow-mark { display: grid; place-items: center; transition: rotate 140ms ease;}.arrow-mark[ui-open] { rotate: 90deg;}.arrow-icon,.kind { width: 1.1em; height: 1.1em; flex: none;}.group { display: grid; gap: 0.2em; margin-inline-start: 1.25em; padding-inline-start: 0.4em; border-inline-start: 1px solid var(--ink);}.group[hidden] { display: none;}@media (prefers-reduced-motion: reduce) { .line, .arrow-mark { transition: none; }}
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
The tree container.
tree.rootis the container, withrole="tree". The whole tree is one Tab stop.tree.itemis one row, withrole="treeitem". Mark a row with no children asleaf.tree.itemtriggeris the button that opens and closes a row. Tab never stops on it. The arrow keys and Enter press it for you.tree.itemcontentholds a row's children, withrole="group". A closed group getshidden. It stays in the page.tree.itemlabelis the row's name. The trigger takes its name from it, and typing a letter matches it.tree.itemindicatoris a mark, such as an arrow, that shows whether the row is open.tree.labelis 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
disabledbooleanNobody can open or close anything while this is set.Defaultfalse.
tree.item
openbooleanThis node's children are showing. Omit it and the node starts closed.Defaultfalse.leafbooleanThis 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".Defaultfalse.levelnumberHow 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.Default1.disabledbooleanNobody can open or close this node, and the walk steps over it. A locked node writtenopenstays open, and the nodes inside it are still reachable.Defaultfalse.onChange(open: boolean) => voidCalled 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.