Interactive example
A complete file explorer in the browser: search, preview, create, rename, delete, drag to move or reorder, and a right-click menu.
File explorer demo
The tree lives in useDireactree and its contents load from mock files. Every change stays in your browser; Reset tree brings the first state back.
Double click a file, press Enter on it, or right-click and choose Open preview.
Operations to try
Open a file
Double click project-notes.txt, config.json, or REST-API.md, or focus it and press Enter. onNodeActivate receives the node and the example loads its content. Right-click a file for the custom Open preview item.
Search
Type json or api in the search field. Only matching rows and their folders stay visible, and the match is marked. ↓ or Enter in the field moves to the first match; Escape clears it.
Create, rename, delete
Select a folder and use the toolbox, the right-click menu, F2, or Delete. Type a name that already exists in the folder, or one with /, and Save stays disabled with a message. Delete asks for confirmation first. The folders are kept sorted with folders first, because the hook has sort: "foldersFirst".
Move and reorder
Drag a row onto a folder to move it inside, or near the top or bottom edge of a row to drop it before or after. A line or a dashed box shows where it will land. The root folder Desktop is protected: it cannot be renamed, deleted, or dragged.
Use the keyboard
Click a row, then use the arrow keys. → opens a folder or moves into it, ← goes back to the parent, Home and End jump to the ends, and typing letters jumps to the next row that starts with them. Shift+F10 opens the menu.
How it is built
The whole explorer is one hook and props. No tree helpers are written by hand:
const tree = useDireactree(initialStructure, { sort: "foldersFirst" });
const ref = useRef<DireactreeHandle>(null);
<Direactree
ref={ref}
structure={tree.structure}
onSave={tree.save}
onDelete={tree.remove}
onNodeMove={tree.move}
allowReorder
confirmDelete
showSearch
toolboxSticky
renderIcon={(node, { expanded }) => <FileIcon node={node} expanded={expanded} />}
contextMenu={(node, defaults) =>
node.type === "file"
? [{ id: "open", label: "Open preview", onSelect: openFile }, "separator", ...defaults]
: defaults
}
getNodePermissions={(node, path) =>
path.parent === null ? { rename: false, delete: false, move: false } : {}
}
toolboxActions={[
{ id: "expand", label: "Expand all", icon: "⊞", onClick: () => ref.current?.expandAll() },
{ id: "collapse", label: "Collapse all", icon: "⊟", onClick: () => ref.current?.collapseAll() },
]}
onNodeActivate={openFile}
/>
Each prop is covered in the guides: useDireactree, names & permissions, search & navigation, and rows, menus & toolbox.
Persisting changes
With a backend, return a Promise from the handler and call the hook after the server agrees. Direactree shows the request as pending, disables the controls until it settles, and keeps the editor or confirmation open with the error if it rejects.
const tree = useDireactree(initialStructure);
async function handleSave(change: SaveProps) {
const response = await fetch("/api/tree", {
method: change.actionType === "create" ? "POST" : "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(change),
});
if (!response.ok) throw new Error("The change could not be saved.");
const saved = await response.json();
tree.save(change, { id: saved.id }); // use the server id for a new node
}
async function handleNodeMove(node: TreeNode, target: TreeNode, info: MoveInfo) {
await api.move(node.id, info.parentId, info.index);
tree.move(node, target, info);
}
A failed move has no editor to show its error in, so use onActionError:
<Direactree
structure={tree.structure}
onNodeMove={handleNodeMove}
onActionError={(error, action) => {
if (action === "move") showToast("The item could not be moved.");
}}
/>
Check permissions, names, and parent relationships on the server as well. Direactree keeps the UI from making invalid requests, but your server is the authority.