Folder tree
Folders as a tree: arrows, typeahead, children loaded on first opening, drops of items and files.
import { FolderTree } from "@forgeron/ui";- My files
- Contracts
- Leases
- Photos
- Sales
- Operations
import { Briefcase01Icon, Home01Icon, TruckIcon } from "@forgeron/icons";
import { FolderTree, type FolderNode } from "@forgeron/ui";
const folders: FolderNode[] = [
{
id: "my",
name: "My files",
icon: Home01Icon,
children: [
{
id: "contracts",
name: "Contracts",
children: [{ id: "leases", name: "Leases", children: [] }],
},
{ id: "photos", name: "Photos", children: [] },
],
},
{
id: "sales",
name: "Sales",
icon: Briefcase01Icon,
children: [{ id: "quotes", name: "Quotes", children: [] }],
},
{ id: "ops", name: "Operations", icon: TruckIcon, children: [] },
];
export default function Default() {
return (
<div style={{ width: 240 }}>
<FolderTree
folders={folders}
defaultExpanded={["my", "contracts"]}
defaultSelected="leases"
/>
</div>
);
}- My files
- Archive
- Contracts
- Invoices
SelectedActions
Dock plan.pdf240 KB
Tariffs 2027.xlsx52 KB
// Subfolders load on first opening (a promise shows the wait); drag a file onto a folder, or
// drop files from your computer. Invoices is an app's folder: read-only, it takes no drop.
import { Home01Icon, Invoice01Icon } from "@forgeron/icons";
import { FileBrowser, FolderTree, type DriveItem, type FolderNode } from "@forgeron/ui";
import { useState } from "react";
const wait = (ms: number) => new Promise((done) => setTimeout(done, ms));
export default function LazyAndDrop() {
const [folders, setFolders] = useState<FolderNode[]>([
{
id: "my",
name: "My files",
icon: Home01Icon,
children: [
{ id: "archive", name: "Archive", hasChildren: true },
{ id: "contracts", name: "Contracts", children: [] },
],
},
{ id: "invoices", name: "Invoices", icon: Invoice01Icon, readOnly: true, children: [] },
]);
const [current, setCurrent] = useState("contracts");
const [log, setLog] = useState("Drag a file onto a folder");
const files: DriveItem[] = [
{ id: "a", name: "Dock plan.pdf", size: 240_000, modified: "2026-11-18 09:12" },
{ id: "b", name: "Tariffs 2027.xlsx", size: 52_400, modified: "2026-11-12 16:40" },
];
const load = async (id: string) => {
await wait(900);
setFolders((all) =>
all.map((root) => ({
...root,
children: root.children?.map((c) =>
c.id === id
? {
...c,
children: [
{ id: "2024", name: "2024", children: [] },
{ id: "2025", name: "2025", children: [] },
],
}
: c,
),
})),
);
};
return (
<div style={{ display: "grid", gridTemplateColumns: "220px 1fr", gap: 16, width: "100%" }}>
<FolderTree
folders={folders}
defaultExpanded={["my"]}
selected={current}
onSelectedChange={setCurrent}
onLoadChildren={load}
onDrop={(to, drop) =>
setLog(
drop.files.length
? `${drop.files.length} file(s) to upload to ${to}`
: `Moved ${drop.ids.join(", ")} to ${to}`,
)
}
/>
<div style={{ display: "grid", gap: 8, alignContent: "start" }}>
<FileBrowser items={files} onMove={() => {}} columns={["size"]} lasso={false} />
<span style={{ color: "var(--ink-dim)", font: "400 11px/16px var(--font-mono)" }}>
{log}
</span>
</div>
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
folders | { id, name, icon?, children?, hasChildren?, readOnly? }[] | — | children absent with hasChildren: loaded on first opening. readOnly: a lock, no drop. |
expanded / defaultExpanded / onExpandedChange | string[] | — | The open folders. Right opens, Left closes or climbs, * opens the siblings. |
selected / defaultSelected / onSelectedChange | string | null | — | The current folder: a click, Space or Enter. |
onOpen | (id) => void | — | Enter or a double click. |
onLoadChildren | (id) => void | Promise | — | Called on the first opening; a promise shows Loading until it settles. |
onDrop / canDrop | (folderId, { ids, files }) => void; (folderId) => boolean | — | Items from a FileBrowser or files from the computer. A closed folder opens after a moment under the pointer. |
disabledIds | string[] | — | Seen, not picked, no drop: the items being moved. |
className | string | — | Extra classes. |