Direactree

Search & navigation

Find a node, bring it into view from code, select one or many, open files, and remember which folders were open.

showSearch adds a search field under the toolbox. searchTerm sets the term from outside instead, with onSearchChange reporting what was typed into the built-in field.

<Direactree structure={structure} showSearch />

// or your own field
<input value={term} onChange={(event) => setTerm(event.target.value)} />
<Direactree structure={structure} searchTerm={term} searchMode="highlight" />
TRY ITSearch

Every folder starts closed. Folders that hold a match open on their own and close again when the search ends. Try tsx, use, or .css.

props in use
{
  "showSearch": true,
  "searchTerm": "(built-in field: \"\")",
  "searchMode": "filter",
  "filterNode": "default name match"
}
  • searchMode="filter" (default) shows only matches and the folders that hold them. "highlight" keeps every row and marks matches.
  • Folders that hold a match open on their own, without changing the open state you go back to when the search ends. A folder can still be closed by hand during a search.
  • The matching part of the name is wrapped in <mark class="direactree-match">.
  • filterNode(node, term) replaces the default case-insensitive name match. Wrap it in useCallback so large trees are not searched again on every render.
  • In the built-in field, ↓ or Enter moves to the first match, and Escape clears the term.
  • Only nodes already in structure are searched. Children that are not loaded yet are not.

Reveal from code

Pass a ref to get the handle. revealNode opens every ancestor, scrolls the row into view, and selects it:

const ref = useRef<DireactreeHandle>(null);

<Direactree ref={ref} structure={structure} />

ref.current?.revealNode("src/components/Button.tsx", { focus: true, block: "center" });
TRY ITReveal a node from code

Each button calls ref.current.revealNode(id). The folders above it open, the row scrolls into view inside the small panel, and it gets selected.

getSelectedIds() / getExpandedIds()
{
  "selected": [],
  "expanded": []
}
MethodUse
revealNode(id, { select?, focus?, block? })Open the way to a node, scroll to it, select it (default), and optionally focus it. block is "nearest" (default), "start", "center", or "end". Returns false when the id is not in the tree.
expand(id), collapse(id)Open or close one folder.
expandAll(), collapseAll()Open or close every folder.
select(id | null)Select a node, opening the way to it, or clear the selection.
focus()Move keyboard focus to the selected row, or the first row.
getSelectedIds(), getExpandedIds()Read the current selection and open folders.

Selection

Single selection

By default one node is selected at a time. onSelectedNodeChange receives its NodePath, or null. Pass selectedId to control it; when it points into a closed folder, the folders open.

const [selectedId, setSelectedId] = useState<string | null>(null);

<Direactree
  structure={structure}
  selectedId={selectedId}
  onSelectedNodeChange={(node) => setSelectedId(node?.id ?? null)}
/>

Multiple selection

selectionMode="multiple" adds the usual desktop gestures. onSelectionChange receives every selected id and their paths, and selectedIds makes it controlled:

GestureResult
ClickSelect only this row.
Ctrl/Cmd + click, or Ctrl/Cmd + SpaceAdd or remove this row.
Shift + clickSelect the range from the last clicked row.
Shift + ↑/↓/Home/EndExtend the range.
Ctrl/Cmd + ASelect every visible row.
Click outside the treeClear the selection.

The built-in Rename and Delete act on the focused row. Bulk actions get the ids through toolboxActions, the ref handle, or onSelectionChange.

Opening a file

onNodeActivate(node, path) runs on a double click and on Enter. On a folder it also opens or closes the folder.

<Direactree
  structure={structure}
  onNodeActivate={(node) => node.type === "file" && openTab(node.id)}
/>
TRY ITSelection, opening, and type-ahead

Ctrl/Cmd+click, Shift+click, Shift+arrows, and Ctrl/Cmd+A select several rows. Double click or press Enter to open a file. Type letters to jump.

  • my-app
  • src
  • components
  • Button.tsx
  • Modal.tsx
  • Tooltip.tsx
  • index.ts
  • styles.css
  • package.json
  • README.md
onSelectionChange ids
[]
onNodeActivate
Nothing yet. Use the tree.

Open folders

Direactree remembers which folders are open. TreeNode.isExpanded or defaultExpandedIds sets the first state. expandedIds with onExpandedChange makes it controlled, for example to keep it in the URL or in storage. isAllExpanded keeps every folder open while it is true.

const [expanded, setExpanded] = useState<string[]>(() => readSaved() ?? []);

<Direactree
  structure={structure}
  expandedIds={expanded}
  onExpandedChange={(ids) => {
    setExpanded(ids);
    save(ids);
  }}
/>
TRY ITControlled open folders

Open and close folders, then reload the page. With remember on, expandedIds comes back from localStorage.

  • my-app
  • package.json
  • README.md
expandedIds
[
  "app"
]

Keyboard

The tree is one tab stop. Focus lands on the selected row, or the first row.

KeyAction
↓ / ↑Next or previous visible row.
→Open a closed folder, or move to its first child.
←Close an open folder, or move to the parent.
Home / EndFirst or last visible row.
EnterSelect, open or close a folder, and call onNodeActivate.
SpaceSelect the row.
LettersJump to the next row whose name starts with what you typed. Type quickly to match more letters, or press one letter again to cycle.
F2Rename, when allowed.
DeleteDelete, when allowed.
Shift + F10 or the menu keyOpen the context menu, when contextMenu is set.

In the name editor, Enter saves and Escape cancels. In the delete confirmation and the context menu, Escape closes it. In a virtualized tree, rows outside the window scroll in as the keyboard reaches them.