File Tree
Magic UI File Tree composes tree, folder and file primitives using identified elements.
Practical React example
Show a compact project hierarchy with explicit initial selection and expansion.
Before you copy and paste
Match the imported component’s client or server rendering boundary before using the example. Copy components/magicui/file-tree.tsx and its returned support files from the Code tab; preserve the project-relative paths. Install the listed primitives: pnpm dlx shadcn@latest add button scroll-area.
Use the Code tab for the component files and installation instructions, then adapt the imports in this example to your project.
Show a compact project hierarchy with explicit initial selection and expansion
// Usage adapted from Magic UI's pinned MIT source.
// Copyright (c) Magic UI. See ./LICENSES.md.
'use client';
import { Tree } from "@/components/magicui/file-tree"
import type { TreeViewElement } from "@/components/magicui/file-tree"
export default function FileTreeDemo() {
return (
<div className="bg-background relative flex h-[300px] w-full max-w-sm flex-col items-center justify-center overflow-hidden rounded-lg border">
<Tree
className="bg-background overflow-hidden rounded-md p-2"
initialSelectedId="button"
initialExpandedItems={["src", "app", "components", "ui", "lib"]}
elements={ELEMENTS}
/>
</div>
)
}
const ELEMENTS: TreeViewElement[] = [
{
id: "src",
type: "folder",
isSelectable: true,
name: "src",
children: [
{
id: "lib",
type: "folder",
isSelectable: true,
name: "lib",
children: [
{
id: "utils",
isSelectable: true,
name: "utils.ts",
},
],
},
{
id: "app",
type: "folder",
isSelectable: true,
name: "app",
children: [
{
id: "page",
isSelectable: true,
name: "page.tsx",
},
{
id: "layout",
isSelectable: true,
name: "layout.tsx",
},
],
},
{
id: "components",
type: "folder",
isSelectable: true,
name: "components",
children: [
{
id: "header",
isSelectable: true,
name: "header.tsx",
},
{
id: "ui",
type: "folder",
isSelectable: true,
name: "ui",
children: [
{
id: "button",
isSelectable: true,
name: "button.tsx",
},
],
},
{
id: "footer",
isSelectable: true,
name: "footer.tsx",
},
],
},
],
},
]
Usage example adapted from Magic UI's pinned MIT source. Retain the source licence notices when copying.
Use stable IDs throughout the elements and rendered children, and distinguish initial values from controlled state when integrating the component.
Customization
- TreeContextProps.selectedId
- Required; string | undefined.
- TreeContextProps.expandedItems
- Required; string[] | undefined.
- TreeContextProps.indicator
- Required; boolean.
- TreeContextProps.handleExpand
- Required; (id: string) => void.
- TreeContextProps.selectItem
- Required; (id: string) => void.
- TreeContextProps.setExpandedItems
- Optional; React.Dispatch<React.SetStateAction<string[] | undefined>>.
- TreeContextProps.openIcon
- Optional; React.ReactNode.
- TreeContextProps.closeIcon
- Optional; React.ReactNode.
- TreeContextProps.direction
- Required; "rtl" | "ltr".
- TreeViewProps.initialSelectedId
- Optional; string.
- TreeViewProps.indicator
- Optional; boolean.
- TreeViewProps.elements
- Optional; TreeViewElement[].
- TreeViewProps.initialExpandedItems
- Optional; string[].
- TreeViewProps.openIcon
- Optional; React.ReactNode.
Integration notes
- This tree is a presentation of supplied data, not a filesystem browser with built-in permissions.
- Check keyboard interaction and what happens when the selected item is removed or renamed.
Component source: Magic UI documentation . These examples describe the version imported into Rendyn. Upstream APIs may have changed; retain the source licence notices when adapting the code.
Give your agent taste.
The best way to use Rendyn is through MCP. Let your agent find the right component, compare previews and bring the source into your project.
All components are free. Sign up only to connect MCP.