This page as markdown: /tree.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt

Tree

Status: stable

The object list a tool is built around — layers, scene graph, outline — on one shared grid, so every left edge in the panel resolves to the rail or an exact multiple of the indent step.

Keywords: tree, layers, outline, scene graph, hierarchy, nesting, indent, disclosure, workspace, panel, file tree, files, folders, explorer, treeview

Usage

Use Tree for the hierarchy panel in a workspace: layers, objects, an inspector outline. It owns the geometry that makes nesting legible — the rail, the disclosure and type columns, and the indent step — via the density ladder (scope the panel with data-density, e.g. data-density="compact"), so nothing in the panel computes its own inset. It is also the file tree: nest rows with subtree, let folders manage their own open state with defaultExpanded, and mark kind with the icon convention (folder for a row with a subtree, file for a leaf). Reach for Accordion when the content is prose sections rather than nodes in a hierarchy.

Best practices

Anatomy

Part Required Description
Tree yes The container. Does not scroll itself — put it in a scrolling Panel, or make it the scrolling region with className.
TreeSection no A titled group. Header on the rail, hairline above, divider suppressed on the first section.
TreeRow yes One node: rail, disclosure column, type column, name, right-rail actions.
TreeAddRow no The affordance that ends a branch. Mark in the type column, label in the name column.
TreeEmpty no A section's empty state at panel scale: one line, one action, optionally a dashed drop region.

Subcomponents

Name Type Description
TreeSection { title: ReactNode; hint?: string; action?: ReactNode } A titled group of rows, with the header on the rail.
TreeRow { depth?: number; selected?: boolean; accent?: boolean; dimmed?: boolean; expandable?: boolean; expanded?: boolean; defaultExpanded?: boolean; onExpandedChange?: (expanded: boolean) => void; subtree?: ReactNode; toggleLabel?: string; expandLabel?: (name?: string) => string; collapseLabel?: (name?: string) => string; disclosureIcon?: ReactNode; icon?: ReactNode; actions?: ReactNode } One node in the hierarchy (role=treeitem inside a Tree). Controlled via expanded/onExpandedChange or uncontrolled via defaultExpanded; nest children with subtree. Always reserves both leading columns (disclosure and type), so a leaf's name lines up with its expandable siblings.
TreeAddRow { depth?: number; label: ReactNode; icon?: ReactNode } The add affordance, shaped as a row and aligned to the branch it adds to. Its mark sits in the type column (level with the siblings' kind markers), not the name column. icon replaces the built-in add mark.
TreeEmpty { message: ReactNode; action?: ReactNode; dropTarget?: boolean; depth?: number } A section's or a branch's empty state, at panel scale rather than page scale. One-line message, one action slot; dropTarget renders it as a dashed drop region and sets data-drop-target for styling. depth (inherited inside a subtree) starts the line on the edge of the rows that branch would hold.
treeIndent (depth: number) => string The left inset for a depth, as a calc() expression reading the density ladder. Use this rather than computing pixels.

Props

Name Type Default Description
depth number 0 Nesting level, on TreeRow and TreeAddRow. 0 sits on the rail; each level adds one --tree-indent.
expandable boolean true with subtree, else false Whether TreeRow renders a disclosure control. The column is reserved either way, so leaf and parent names stay aligned.
expanded boolean — Controlled open state for a TreeRow. Pair with onExpandedChange; omit both for an uncontrolled row.
defaultExpanded boolean true Uncontrolled initial open state, used when expanded is not passed. Pass false for a file tree's closed folders.
onExpandedChange (expanded: boolean) => void — Called when a TreeRow opens or closes — from the disclosure, ArrowRight/ArrowLeft, or activating a row that has no onClick. Fires in both controlled and uncontrolled use.
subtree ReactNode — The rows nested under a TreeRow, rendered after it while it is expanded. Nested rows inherit depth + 1 unless they pass their own depth, so a file tree never computes depth by hand.
selected boolean false Selection fill and accent bar on a TreeRow. Inside a Tree it is exposed as aria-selected on the treeitem; a row rendered outside a Tree is a plain div and exposes aria-current instead.
toggleLabel string — Names the thing being disclosed, for the toggle's accessible label — "Expand Background" rather than "Expand group". Every example passes it; a screen-reader user hearing twenty identical "Expand group" buttons has no way to tell them apart.
expandLabel (name?: string) => string (name = "group") => Expand ${name} The disclosure's accessible name while collapsed, built from toggleLabel. Replace it to translate the verb ("Développer Background").
collapseLabel (name?: string) => string (name = "group") => Collapse ${name} The disclosure's accessible name while expanded, built from toggleLabel.
icon ReactNode — The kind marker for a TreeRow, rendered in the type column at 1em. Use a semantic Icon so it follows the app icon set.
disclosureIcon ReactNode — Replaces TreeRow's semantic chevron-down icon. Omit to use the chevron-down from the app's IconProvider set.
actions ReactNode — Right-rail controls for a TreeRow: visibility, solo, a row menu.
label ReactNode — TreeAddRow's visible label. Name what gets added — 'Add effect', not 'Add'.
motion "full" | "subtle" | "off" "full" On Tree. How much motion this component uses: full (default) inherits the app's policy, subtle keeps feedback and drops the flourish, off stills it. It can only turn motion down — it never overrides a reader's reduce-motion setting. Under subtle expanded rows appear at once instead of unfolding; the chevron still turns.
selectionFollowsFocus boolean false On Tree. The arrow keys select the row they land on, as a layers panel does, instead of only moving focus. The row is activated as a click carrying the key's Shift, so its own onClick (reading event.shiftKey for a range) extends the selection on Shift+Arrow; Ctrl/⌘+Arrow moves without selecting. Only rows with an onClick are selected — a disclosure-only row, a TreeAddRow or a row with selectOnFocus={false} is just focused. Pass aria-multiselectable on the Tree when more than one row can be selected.
selectOnFocus boolean true On TreeRow. Pass false for a row whose click does more than select — it switches the active document, or opens itself — so the Tree's selectionFollowsFocus only focuses it as the arrows pass and the tree doesn't reshape under the cursor. Enter/Space and a pointer click still activate it.

Tokens used

panel-raised, hairline, foreground, muted-foreground, primary, ring

Motion

Accessibility

Related

Examples

File tree

import { Icon, Tree, TreeRow } from "@sableui/react"

// Nest with subtree; folders own their open state via defaultExpanded.
// A row with no onClick toggles when clicked or activated, like a folder in
// a file explorer. Depth is inherited — never computed by hand.
<Tree aria-label="Files" className="max-w-xs">
  <TreeRow
    icon={<Icon name="folder" />}
    toggleLabel="src"
    subtree={
      <>
        <TreeRow icon={<Icon name="file" />} onClick={() => open("index.ts")}>index.ts</TreeRow>
        <TreeRow
          icon={<Icon name="folder" />}
          toggleLabel="components"
          defaultExpanded={false}
          subtree={
            <TreeRow icon={<Icon name="file" />} selected onClick={() => open("button.tsx")}>
              button.tsx
            </TreeRow>
          }
        >
          components
        </TreeRow>
      </>
    }
  >
    src
  </TreeRow>
  <TreeRow icon={<Icon name="file" />} onClick={() => open("README.md")}>README.md</TreeRow>
</Tree>

Layer tree with per-node effects

import { Button, Icon, Tree, TreeAddRow, TreeRow, TreeSection } from "@sableui/react"

<Tree aria-label="Layers" className="min-h-0 flex-1 overflow-y-auto scrollbar-quiet">
  <TreeSection title="Document effects" hint="Applied to the flattened composite">
    <TreeRow depth={0} icon={<Icon name="sparkles" />}>Channel shift</TreeRow>
    <TreeAddRow depth={0} label="Add effect" />
  </TreeSection>

  <TreeSection title="Layers" action={<span className="text-ui-num text-muted-foreground">2</span>}>
    <TreeRow
      depth={0}
      expandable
      expanded
      toggleLabel="Backdrop"
      icon={<Icon name="image-stack" />}
      actions={<Button size="icon-xs" variant="ghost" aria-label="Hide Backdrop"><Icon name="eye" /></Button>}
    >
      Backdrop
    </TreeRow>
    <TreeRow depth={1} icon={<Icon name="sparkles" />}>Block displacement</TreeRow>
    <TreeAddRow depth={1} label="Add effect" />

    <TreeRow depth={0} icon={<Icon name="image-stack" />} dimmed>Overlay</TreeRow>
    <TreeAddRow depth={0} label="Add image" />
  </TreeSection>
</Tree>

Empty state — one line, one action

import { TreeAddRow, TreeEmpty, TreeSection } from "@sableui/react"

// A section with nothing in it offers exactly ONE way forward. Not EmptyState:
// that is page-register (px-6 py-12 around a size-12 disc) and in a 240px panel
// it spends 24px of inset and ~96px of height on nothing.
<TreeSection title="Layers">
  <TreeEmpty message="No images yet" action={<TreeAddRow depth={0} label="Add image" />} />
</TreeSection>

// As a drop region — the app owns the drag handlers; TreeEmpty owns the frame
// and sets data-drop-target so a data-[drop-active] style can respond.
<TreeEmpty
  dropTarget
  message="Drop an image"
  data-drop-active={dragging || undefined}
  onDragOver={(e) => { e.preventDefault(); setDragging(true) }}
  onDragLeave={() => setDragging(false)}
  onDrop={handleDrop}
  className="data-[drop-active]:border-primary"
/>

Selection and disclosure

import { Icon, Tree, TreeRow } from "@sableui/react"

<Tree aria-label="Layers">
  <TreeRow
    depth={0}
    expandable
    expanded={open}
    onExpandedChange={setOpen}
    toggleLabel="Group 1"
    icon={<Icon name="folder" />}
    selected
    onClick={() => select("group-1")}
  >
    Group 1
  </TreeRow>
  {open ? <TreeRow depth={1} icon={<Icon name="file" />}>Child</TreeRow> : null}
</Tree>

Selection follows the arrows, Shift extends

import { Icon, Tree, TreeRow } from "@sableui/react"

// A layers panel: the arrows select as they move. The row's own onClick is
// the one selection path; Shift+Arrow arrives as a click with shiftKey.
<Tree aria-label="Layers" aria-multiselectable selectionFollowsFocus>
  {layers.map((layer) => (
    <TreeRow
      key={layer.id}
      icon={<Icon name="image-stack" />}
      selected={selection.includes(layer.id)}
      onClick={(event) => (event.shiftKey ? extendTo(layer.id) : selectOnly(layer.id))}
    >
      {layer.name}
    </TreeRow>
  ))}
</Tree>

An empty branch

import { Icon, TreeAddRow, TreeEmpty, TreeRow } from "@sableui/react"

// A layer with no effects: TreeEmpty at the branch's depth lands on the
// same edge its effect rows would — no hand-computed inset.
<TreeRow depth={0} icon={<Icon name="image-stack" />} toggleLabel="Overlay" subtree={
  <>
    <TreeEmpty message="No effects yet" />
    <TreeAddRow label="Add effect" />
  </>
}>
  Overlay
</TreeRow>

Aligning a custom affordance

import { treeIndent } from "@sableui/react"

// Anything else that needs a tree inset reads it from the same place the rows do.
<div style={{ paddingInlineStart: treeIndent(1) }} className="flex h-(--control-xs) items-center">
  <span className="text-ui-body text-muted-foreground">Drag a layer here</span>
</div>