Direactree

Getting started

Install Direactree, add its stylesheet, pick the right entry point, and render an editable tree in a few lines.

Installation

npm install direactree

TypeScript definitions ship with the package, so no @types package is needed. Direactree works with React 17 and newer, and has no other dependencies.

Useful links:

Stylesheet

Import the stylesheet once. Since 0.4 the JavaScript does not add any styles, so this line is required:

import "direactree/direactree.css";

In a Next.js App Router project, put it in the root layout.tsx. It also means the tree is styled before JavaScript runs and works under a strict Content-Security-Policy.

There is a second file with the same rules inside a CSS cascade layer:

import "direactree/direactree.layer.css";

With it, any CSS in your app that is not in a layer wins over the tree's rules, in any order, without !important. Skip both files to style the tree from scratch. See Styling.

Framework imports

React applications

import Direactree, { useDireactree } from "direactree";
import type { TreeNode } from "direactree";

Next.js 13 and newer

App Router components are Server Components by default, and Direactree must run on the client.

1. Use the Next.js entry point (recommended). It carries the "use client" directive:

import Direactree, { useDireactree } from "direactree/next";
import type { TreeNode } from "direactree";

2. Or use the default entry inside your own Client Component:

"use client";

import Direactree from "direactree";

Your first tree

structure takes an array of TreeNode objects. Each node needs a stable id, a name, and a type. Only folders have children.

"use client";

import Direactree from "direactree/next";
import type { TreeNode } from "direactree";

const structure: TreeNode[] = [
  {
    id: "root",
    name: "Project",
    type: "folder",
    isExpanded: true,
    children: [
      { id: "readme", name: "README.md", type: "file" },
      {
        id: "src",
        name: "src",
        type: "folder",
        children: [{ id: "app", name: "App.tsx", type: "file" }],
      },
    ],
  },
];

export function ProjectTree() {
  return <Direactree structure={structure} />;
}

Without handlers the tree is read-only: it can be browsed, searched, and selected, but the toolbox buttons are disabled and nothing can be dragged.

isExpanded sets the first open state of a folder. Use stable ids from your own data. Ids must be unique across the whole tree, because selection, editing, and moves identify nodes by id.

Make it editable

useDireactree keeps the tree in React state and returns updaters that match the callbacks one to one:

"use client";

import Direactree, { useDireactree } from "direactree/next";

export function ProjectTree() {
  const tree = useDireactree(structure);

  return (
    <Direactree
      structure={tree.structure}
      onSave={tree.save}
      onDelete={tree.remove}
      onNodeMove={tree.move}
    />
  );
}
TRY ITuseDireactree, wired straight in

Create, rename, delete, and drag nodes. The JSON on the right is tree.structure. Try sort: foldersFirst, then create a file and a folder.

  • Desktop
  • Projects
  • Web Applications
  • project-notes.txt
  • config.json
  • utils.ts
  • Documents
  • REST-API.md
  • DireactreeUsage.tsx
tree.structure
[
  {
    "id": "0",
    "name": "Desktop",
    "type": "folder",
    "children": [
      {
        "id": "1",
        "name": "Projects",
        "type": "folder",
        "children": [
          {
            "id": "1.1",
            "name": "Web Applications",
            "type": "folder",
            "children": [
              {
                "id": "sample.txt",
                "name": "project-notes.txt",
                "type": "file"
              },
              {
                "id": "example.json",
                "name": "config.json",
                "type": "file"
              },
              {
                "id": "sample.ts",
                "name": "utils.ts",
                "type": "file"
              }
            ]
          }
        ]
      },
      {
        "id": "2",
        "name": "Documents",
        "type": "folder",
        "children": [
          {
            "id": "REST-API.md",
            "name": "REST-API.md",
            "type": "file"
          }
        ]
      },
      {
        "id": "DireactreeUsage.tsx",
        "name": "DireactreeUsage.tsx",
        "type": "file"
      }
    ]
  }
]

Each control turns on when you pass its handler: onSave enables create and rename, onDelete enables delete, and onNodeMove enables dragging. To send a change to a server first, call the updater after the request succeeds. See useDireactree & helpers.

State ownership

structure is controlled: your component owns it, and Direactree only reports what the user asked for. Direactree keeps UI state only: selection, which folders are open, the open editor or confirmation, and pending and error state. Selection and open folders can be controlled too, with selectedId, selectedIds, and expandedIds.

onCreateFolder, onCreateFile, and onRename only mean the name editor opened. The create or rename itself arrives in onSave after the user confirms. Return a Promise from onSave, onDelete, or onNodeMove to make the tree wait for your API. See Props & callbacks.

Next steps