Styling the depth
Indent rows with the --tree-depth custom property every part carries, and keep the drop indicator's level in line with your indentation.
The tree is not nested visually by the package: each item's group is inside the item's element,
but nothing is indented. Every part of an item (Tree.Item, Tree.ItemContent, Tree.Group)
carries the item's depth twice: as the custom property --tree-depth (an inline style: 0 for a
root item, 1 for its children, …) and as the attribute data-depth. The indentation is a line
of CSS you own.
Indenting the row
Pad the row, not the item: the row is what the user sees and what a drag hit-tests, and the padding keeps the whole width clickable and highlightable.
.row {
padding-inline-start: calc(var(--tree-depth) * 1.25rem + 0.5rem);
}padding-inline-start follows the writing direction, so the same rule indents from the right in
a right-to-left tree. With Tailwind:
<Tree.ItemContent className="ps-[calc(var(--tree-depth)*1.25rem+0.5rem)]">{item.label}</Tree.ItemContent>Guide lines
A vertical line per level is a background on the row, sized by the same property:
.row {
--indent: 1.25rem;
padding-inline-start: calc(var(--tree-depth) * var(--indent) + 0.5rem);
background-image: repeating-linear-gradient(
to right,
transparent 0 calc(var(--indent) - 1px),
#e5e7eb calc(var(--indent) - 1px) var(--indent)
);
background-size: calc(var(--tree-depth) * var(--indent)) 100%;
background-repeat: no-repeat;
}Or select a depth with data-depth, for a style a level gets (a bold root level):
.row[data-depth="0"] {
font-weight: 600;
}Keeping drops in line
During a drag, the pointer's inline position at the end of a sublist picks the level the drop
lands on, and the drop indicator starts at (level - 1) * indentation + indicatorInset pixels
from the root's inline start. Tell drag and drop (dragAndDrop(),
Drag and drop) your indentation in pixels with indentation
(default 16), and where a row's label starts past its level with indicatorInset (default
0): the + 0.5rem above, a toggle, a grip. A line then starts at the label; the inside box
gets no inset.
import { DropIndicator, dragAndDrop } from "@fragiola/tree-react/drag-and-drop";
const features = [dragAndDrop({ indentation: 20, indicatorInset: 28 })] as const;
<Tree.Root<Types, typeof features> defaultTree={json} features={features} aria-label="Files">
<Tree.Items<Types>>{(item) => <FileItem item={item} />}</Tree.Items>
<DropIndicator className="drop-indicator" />
</Tree.Root>The examples' roots and rows start with 0.25rem of padding each, then a 1rem toggle and a
0.375rem gap (and a grip in drag and drop), so they pass where the
icon starts, in pixels, as dragAndDrop()'s indicatorInset: 30 (48 with the grip).
Accessible names
The packages write no text, so the tree's name, each item's name and every announcement come from you. Here is where each one goes.
The model and commands
The tree is a typed, immutable state that only commands change. Run them, ask whether they would succeed, read the state and listen to every commit.