Skip to content
GetUrUI

File Tree

.md

Collapsible file tree where folders spring open, chevrons rotate, indent guides light up for the active folder and a selection pill glides between rows.

treefile treeexplorersidebarnavigationfolderskeyboardlayoutId

Installation

$npx shadcn@latest add @geturui/file-tree
Download source (.zip)

Usage

usage.tsx
1import { FileTree, type TreeNode } from "@/components/ui/file-tree";
2
3const files: TreeNode[] = [
4  { id: "app", name: "app", children: [{ id: "app/page.tsx", name: "page.tsx" }, { id: "app/globals.css", name: "globals.css" }] },
5  { id: "components", name: "components", children: [{ id: "components/ui/button.tsx", name: "button.tsx" }] },
6  { id: "package.json", name: "package.json" },
7];
8
9export function Explorer() {
10  return (
11    <FileTree
12      aria-label="Project files"
13      data={files}
14      defaultExpandedIds={["app"]}
15      onSelect={(node) => console.log(node.id)}
16    />
17  );
18}

Props

PropTypeDefaultDescription
data*TreeNode[]—Nodes as { id, name, children? }. Any node with children (even empty) is a folder.
selectedIdstring—Controlled selected node.
defaultSelectedIdstring—Selected node on mount when uncontrolled.
onSelect(node: TreeNode) => void—Called on click, Enter or Space.
expandedIdsstring[]—Controlled open folders.
defaultExpandedIdsstring[][]Open folders on mount when uncontrolled.
onExpandedChange(ids: string[]) => void—Called when a folder opens or closes.
rowHeightnumber30Row height in px.
indentnumber16Indent per level in px.
aria-labelstring—Names the tree.
classNamestring—Classes for the tree root.

Accessibility

  • role="tree" with role="treeitem" rows carrying aria-level, aria-posinset, aria-setsize, aria-selected and aria-expanded on folders.
  • Roving tabindex: Up and Down move, Right opens a folder or enters it, Left closes it or goes to the parent, Home and End jump, Enter or Space selects (and toggles folders), typing jumps to a matching name.
  • File-type icons are decorative; the name is always visible text. With reduced motion rows and the pill move instantly.

Related