Styling with plain CSS
Style every part with class names and attribute selectors on the state the primitives expose - no build step, no framework, no overrides to fight.
The primitives render plain elements with no styles of their own, so there is nothing to override: give each part a class name and select on its state attributes. Everything below is ordinary CSS.
A complete stylesheet
function FileItem({ item }: { item: ItemOf<Types> }) {
return (
<Tree.Item item={item} className="tree-item">
<Tree.ItemContent className="tree-row">
<Tree.Toggle className="tree-toggle">
<ChevronRight aria-hidden />
</Tree.Toggle>
<span className="tree-label">{item.label}</span>
</Tree.ItemContent>
<Tree.Group<Types>>{(child) => <FileItem item={child} />}</Tree.Group>
</Tree.Item>
);
}.tree {
--tree-indent: 1rem;
--tree-accent: #2563eb;
font: 0.875rem/1.5 system-ui, sans-serif;
padding: 0.25rem;
overflow: auto;
}
.tree-item {
outline: none; /* the ring goes on the row */
}
.tree-row {
display: flex;
align-items: center;
gap: 0.375rem;
height: 1.875rem;
padding-inline: calc(var(--tree-depth) * var(--tree-indent) + 0.25rem) 0.5rem;
border-radius: 6px;
cursor: default;
user-select: none;
}
.tree-row:hover {
background: #f3f4f6;
}
.tree-item:focus-visible > .tree-row {
box-shadow: inset 0 0 0 2px var(--tree-accent);
}
.tree-row[data-selected] {
background: #dbeafe;
color: #1e3a8a;
}
.tree-row[data-disabled] {
opacity: 0.5;
}
.tree-row[data-dragging] {
opacity: 0.4;
}
.tree-toggle {
display: inline-flex;
width: 1rem;
transition: rotate 150ms;
}
.tree-toggle[data-leaf] {
visibility: hidden;
}
.tree-toggle[data-expanded] {
rotate: 90deg;
}
.tree-label {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}Why the row, not the item
The item's element holds its row and its group of children. A style on .tree-item (a
background, a hover) would cover the whole subtree, and [data-selected] on an item would match
its descendants through a descendant selector. Every part carries its own item's state, so style
the row with its own attributes, and only the focus ring through the item
(.tree-item:focus-visible > .tree-row).
Selecting on state
Every attribute is present or absent, never "false", so the plain attribute selector is the
whole test: [data-expanded], [data-checked], [data-indeterminate], [data-loading],
[data-drop-target][data-drop-position="inside"]. ARIA works too: [aria-expanded="true"].
The full list is on State attributes.
Motion
A group is unmounted while its item is collapsed (in Angular, its element stays, empty and
hidden with display: none). To animate the opening, keep it mounted with
keepMounted (it is then hidden with display: none when collapsed), or animate its entry with
@starting-style:
[data-tree-part="group"] {
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}Right-to-left trees
A tree in Arabic, Hebrew or Persian needs no option - the engine reads the root's direction, and logical CSS properties mirror the rest.
Styling with Tailwind
Style the tree with Tailwind's data and ARIA variants, arbitrary values for the depth, and one class string per part kept next to the component.