<p>The same contract before <code>enhance</code> runs: nested lists, every level shown, every link a link. No roles a reader would expect keys for.</p>
<div class="tree-sample">
<nav aria-label="Binder, no script">
<ul class="cap-tree" data-variant="surface">
<li class="cap-tree-item" data-value="ch-01">
<a class="cap-tree-row" href="#ch-01"><span class="cap-tree-label">01 The riverbed</span><span class="cap-tree-meta">6,210</span></a>
<ul class="cap-tree-group">
<li class="cap-tree-item" data-value="ch-01/01"><a class="cap-tree-row" href="#ch-01-01" aria-current="page"><span class="cap-tree-label">01 Low water</span><span class="cap-tree-meta">2,140</span></a></li>
<li class="cap-tree-item" data-value="ch-01/02"><a class="cap-tree-row" href="#ch-01-02"><span class="cap-tree-label">02 Tracks in the limestone</span><span class="cap-tree-meta">2,610</span></a></li>
</ul>
</li>
<li class="cap-tree-item" data-value="notes"><a class="cap-tree-row" href="#notes"><span class="cap-tree-label">Notes</span></a></li>
</ul>
</nav>
</div>
Several chosen, no links, one row not available
<p>Space chooses or clears a row; the page hears each choice. The published chapter cannot be chosen for export again.</p>
<div class="tree-sample">
<ul class="cap-tree" data-cap="tree" data-selection="multi" data-variant="surface" aria-label="Sections to export" id="export">
<li class="cap-tree-item" data-value="front">
<span class="cap-tree-row"><span class="cap-tree-label">Front matter</span></span>
<ul class="cap-tree-group">
<li class="cap-tree-item" data-value="front/title"><span class="cap-tree-row" data-selected><span class="cap-tree-label">Title page</span></span></li>
<li class="cap-tree-item" data-value="front/dedication"><span class="cap-tree-row"><span class="cap-tree-label">Dedication</span></span></li>
</ul>
</li>
<li class="cap-tree-item" data-value="body">
<span class="cap-tree-row" data-selected><span class="cap-tree-label">Chapters</span></span>
<ul class="cap-tree-group">
<li class="cap-tree-item" data-value="body/01"><span class="cap-tree-row" aria-disabled="true"><span class="cap-tree-label">01 The riverbed</span><span class="cap-tree-meta">published</span></span></li>
<li class="cap-tree-item" data-value="body/02"><span class="cap-tree-row"><span class="cap-tree-label">02 Flood stage</span></span></li>
</ul>
</li>
</ul>
<output class="tree-sample-out" id="export-out" for="export">Chosen: title page, chapters.</output>
</div>
Row states: under the pointer, keyboard focus, dragged, and where a drop lands
<p>Set from the page, not from a pointer: a row under the pointer, the focus ring, the row being dragged, and the three drop marks, a line before, a ring inside a branch, a line after.</p>
<div class="tree-sample">
<ul class="cap-tree" data-variant="surface" role="tree" aria-label="Row states">
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-selected="false" tabindex="-1" id="st-hover"><span class="cap-tree-label">Under the pointer</span></span></li>
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-selected="false" tabindex="0" id="st-focus"><span class="cap-tree-label">Keyboard focus</span></span></li>
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-selected="true" tabindex="-1" id="st-chosen"><span class="cap-tree-label">Chosen</span><span class="cap-tree-meta">1,460</span></span></li>
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-selected="false" data-dragging tabindex="-1" id="st-drag"><span class="cap-tree-label">Being dragged</span></span></li>
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-selected="false" data-drop="before" tabindex="-1" id="st-before"><span class="cap-tree-label">A drop before this row</span></span></li>
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-selected="false" aria-expanded="false" data-drop="inside" tabindex="-1" id="st-inside"><span class="cap-tree-toggle" aria-hidden="true"><svg class="cap-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="m9 6 6 6-6 6"/></svg></span><span class="cap-tree-label">A drop inside this branch</span></span></li>
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-selected="false" data-drop="after" tabindex="-1" id="st-after"><span class="cap-tree-label">A drop after this row</span></span></li>
<li class="cap-tree-item" role="none"><span class="cap-tree-row" role="treeitem" aria-level="1" aria-disabled="true" tabindex="-1" id="st-disabled"><span class="cap-tree-label">Not available</span><span class="cap-tree-meta">published</span></span></li>
</ul>
</div>
In React: a series of posts, moved and saved by the page
<p>The React wrapper holds which branches are open; the page holds the nodes and saves each move (here, it writes the new order below).</p>
<div data-mount="react"></div>
API reference
Tree
Prop
Type
Required
Does
nodes
TreeNode[]
Yes
aria-label
string
aria-labelledby
string
selection
TreeSelection
selected
string[]
defaultSelected
string[]
onSelectedChange
(ids: string[], node: TreeNode) => void
expanded
string[]
defaultExpanded
string[]
onExpandedChange
(ids: string[]) => void
onMove
(move: TreeMove, next: TreeNode[]) => void
Turns on moving, by Alt with an arrow and by dragging a row. The caller saves the move.
canMove
(move: TreeMove) => boolean
Refuses a move before it is offered (a scene may not sit at the top level, say).
variant
"surface"
Draws its own border and surface, for a tree on its own.
className
string
Functions
Function
Signature
Does
createTree
(el: HTMLElement): Tree
treeFor
(el: Element | null): Tree | undefined
The controller enhance made for this element, if any.
enhance
(root?: ParentNode): () => void
Attaches to every [data-cap="tree"] under root not yet marked data-cap-ready. Reads data-selection ("none", "single" by default, "multi") and data-movable. Returns a function that detaches them all.
CSS variables
Variable
Default
--tree-depth
0
--tree-indent
calc(var(--space-5) + var(--space-1))
Parts and data attributes
Class
Is
.cap-tree
the root
.cap-tree-group
a part
.cap-tree-icon
a part
.cap-tree-label
a part
.cap-tree-meta
a part
.cap-tree-row
a part
.cap-tree-toggle
a part
Attribute
Values
data-dragging
present or absent
data-drop
afterbeforeinside
data-movable
present or absent
data-sorting
present or absent
data-variant
surface
State
Read from
:focus-visible
the platform
:hover
the platform
[aria-current="page"]
an attribute
[aria-disabled="true"]
an attribute
[aria-expanded="true"]
an attribute
[aria-selected="true"]
an attribute
[hidden]
an attribute
Accessibility
Key
Does
Down arrow, Up arrow
Next, previous visible row
Right arrow
Opens a closed branch; on an open one, goes to its first child
Left arrow
Closes an open branch; otherwise goes to the parent
Home, End
First, last visible row
*
Opens every branch beside the current one
A letter
Goes to the next row starting with the letters typed
Enter
Opens a link row (the browser follows it); chooses any other row
Space
Chooses the row; with several, adds or removes it
Alt + Up arrow, Alt + Down arrow
Moves the row before or after its neighbor (data-movable)
Alt + Right arrow
Moves the row into the branch just above it, at the end
Alt + Left arrow
Moves the row out of its branch, to just after it
role="tree" named by aria-label or aria-labelledby; each row role="treeitem" with aria-level, aria-posinset and aria-setsize; a branch aria-expanded and aria-owns naming its role="group"; the list items role="none". aria-selected on every row that can be chosen; aria-multiselectable with data-selection="multi"; aria-disabled="true" on one that cannot.
The chevron is decoration (aria-hidden); the state is aria-expanded. The note at the end is part of the row's name ("Low water, Revised, 2,140").
A move, a refused move ("Low water is already first here.") and a canceled drag are said in a role="status" region that exists before it changes.
Dragging is never the only way: every drag has a keyboard move. A host whose people move rows by touch adds a one-pointer command (a "Move to" menu) that calls the same move, as WCAG 2.5.7 asks.