File Tree
Install: npx shadcn@latest add https://jiguang132.com/r/magic-ui/file-tree.json
Use case
Render a local expandable file/folder tree.
Native props used by the examples
Extracted from all local examples for this entry, grouped by imported module and native owner, including other Magic components used by compound demos. This is not an exhaustive API or a universal host variant. Inspect spread object declarations and the source types/defaults below.
| Owning component prop | Explanation | Example source |
|---|---|---|
Tree.className (file-tree) | Classes on this native component; initialize Tailwind and its required CSS first. | examples/file-tree-demo.tsx |
Tree.initialSelectedId (file-tree) | Tree's initial selected item id. | examples/file-tree-demo.tsx |
Tree.initialExpandedItems (file-tree) | Tree's initially expanded folder ids. | examples/file-tree-demo.tsx |
Tree.elements (file-tree) | Native tree data with ids, names and optional children. | examples/file-tree-demo.tsx |
Interaction and state checks
- File Tree Demo —
file-tree-demo
These names identify real example entries, not new component props or extra components. Check pointer/keyboard behavior, themes, small screens and failures for the selected example. Decorative effects must not invent disabled/error/submission states. Complete example installation includes the actual dependency closure.
Business integration boundary
elements is structured data with unique ids. Initial selection/expansion are initial values, not a file-system connection or upload capability.
Host and motion setup
Initialize Tailwind CSS v4, semantic CSS variables and the dark variant; import the independent local Registry namespace. Reuse an existing next-themes Provider without substituting JGuang UI/Base UI/HeroUI APIs. Mount effects in view and unmount offscreen; provide static alternatives for unavailable Canvas/WebGL or failed remote media. Respect prefers-reduced-motion and verify timer/RAF/observer/listener cleanup: stopping CSS alone does not prove the engine stopped. Examples provide no backend, login, payment or real AI stream. This practical guide is bilingual; detailed upstream references and demo text retain their original language.
Available examples
file-tree-demo
Demo state and mock data do not provide a backend service. Inspect the component props before wiring real data.
Installation
<Tabs defaultValue="cli"> <TabsList> <TabsTrigger value="cli">CLI</TabsTrigger> <TabsTrigger value="manual">Manual</TabsTrigger> </TabsList> <TabsContent value="cli">npx shadcn@latest add @magicui/file-tree
</TabsContent>
<TabsContent value="manual">
<Steps>
<Step>Copy and paste the following code into your project.</Step>
<Step>Update the import paths to match your project setup.</Step>
</Steps> </TabsContent> </Tabs>Usage
import { Tree } from "@/components/ui/file-tree"
import type { TreeViewElement } from "@/components/ui/file-tree"
const elements: TreeViewElement[] = [
{
id: "src",
type: "folder",
name: "src",
children: [
{ id: "components", type: "folder", name: "components" },
{ id: "app", type: "folder", name: "app" },
{ id: "page", name: "page.tsx" },
{ id: "layout", name: "layout.tsx" },
],
},
]
<Tree elements={elements} />
When you pass elements and omit children, the tree is rendered from data and siblings are sorted automatically using a folders-first alphabetical order. Use sort="none" to preserve the input order, or pass a custom comparator.
Props
Tree
| Prop | Type | Default | Description |
|---|---|---|---|
initialSelectedId | string | - | The ID of the initially selected item. |
indicator | boolean | true | Whether to show the tree indicator line. |
elements | TreeViewElement[] | - | An array of tree view elements to render when children are omitted. |
initialExpandedItems | string[] | - | An array of IDs for items that should be initially expanded. |
openIcon | React.ReactNode | - | Custom icon for open folders. |
closeIcon | React.ReactNode | - | Custom icon for closed folders. |
sort | "default" | "none" | ((a, b) => number) | "default" | Sorting mode used for data-driven trees rendered from elements. |
dir | "rtl" | "ltr" | "ltr" | The text direction of the tree. |
TreeViewElement
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | - | A unique identifier for the node. |
name | string | - | The label rendered for the node in data-driven mode. |
type | "file" | "folder" | - | Explicit node type. Use "folder" for empty folders. |
isSelectable | boolean | true | Whether the node can be selected. |
children | TreeViewElement[] | - | Child nodes for folders. |
Folder
| Prop | Type | Default | Description |
|---|---|---|---|
element | string | - | The name of the folder. |
value | string | - | The unique identifier for the folder. |
isSelectable | boolean | true | Whether the folder can be selected. |
isSelect | boolean | - | Whether the folder is currently selected. |
File
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | - | The unique identifier for the file. |
isSelectable | boolean | true | Whether the file can be selected. |
isSelect | boolean | - | Whether the file is currently selected. |
fileIcon | React.ReactNode | - | Custom icon for the file. |
CollapseButton
| Prop | Type | Default | Description |
|---|---|---|---|
elements | TreeViewElement[] | - | An array of tree view elements to control. |
expandAll | boolean | false | Whether to expand all elements initially. |
Complete example installation
The component command installs the component. Each example command also includes its actual imported components, native support UI, utilities and pinned dependencies. Helpers are not additional catalog components.
File Tree Demo
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/file-tree/file-tree-demo.json
Default import: @/components/magicui/examples/file-tree-demo
Dependencies: @radix-ui/react-accordion@1.2.16, @radix-ui/react-scroll-area@1.2.14, @radix-ui/react-slot@1.3.0, class-variance-authority@0.7.1, clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Minimal usage and theme
import Demo from '@/components/magicui/examples/file-tree-demo';
import { ThemeProvider } from 'next-themes';
export default function Example() {
return <ThemeProvider attribute="class" defaultTheme="system" enableSystem><Demo /></ThemeProvider>;
}
Initialize Tailwind CSS v4, semantic color variables and the dark variant first. Reuse an existing next-themes provider rather than nesting another. Next Link examples require a Next.js environment; in static Vite projects adapt the demo links to your router without changing the native component API. Some media and tweets require network access. Examples do not supply authentication or backend services.
Source-backed parameter reference
Implementation signatures and types are generated from the pinned local source. Optional props, native inherited types, union branches and never exclusions are preserved. A forwardRef implementation's ref is a React reference, not an additional business prop.
Tree({
className,
elements,
initialSelectedId,
initialExpandedItems,
children,
indicator = true,
openIcon,
closeIcon,
sort = "default",
dir,
...props
}, ref)
Folder({
className,
element,
value,
isSelectable = true,
isSelect,
children,
...props
}, ref)
File({
value,
className,
handleSelect,
onClick,
isSelectable = true,
isSelect,
fileIcon,
children,
...props
}, ref)
CollapseButton({ className, elements, expandAll = false, children, ...props }, ref)
type TreeViewElement = {
id: string
name: string
type?: "file" | "folder"
isSelectable?: boolean
children?: TreeViewElement[]
}
type TreeSortMode =
"default" | "none" | ((a: TreeViewElement, b: TreeViewElement) => number)
type TreeViewProps = {
initialSelectedId?: string
indicator?: boolean
elements?: TreeViewElement[]
initialExpandedItems?: string[]
openIcon?: React.ReactNode
closeIcon?: React.ReactNode
sort?: TreeSortMode
} & Omit<
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Root>,
"defaultValue" | "onValueChange" | "type" | "value"
>
type FolderProps = {
expandedItems?: string[]
element: string
isSelectable?: boolean
isSelect?: boolean
} & React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>
Destructuring defaults in source
Only defaults written directly in parameter/props destructuring are listed. Conditional expressions, runtime theme and computed values require source inspection; this is not an exhaustive behavior-default list.
// Tree
indicator = true
// Tree
sort = "default"
// Folder
isSelectable = true
// File
isSelectable = true
// CollapseButton
expandAll = false
Native and external types
External types remain defined by React or native dependencies, not manually expanded into guessed props. The installed version's TypeScript definitions are authoritative.
HTMLDivElement
React.ReactNode
Omit<
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Root>,
"defaultValue" | "onValueChange" | "type" | "value"
>
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Root>
React.ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>
React.HTMLAttributes<HTMLDivElement>
HTMLButtonElement
React.ButtonHTMLAttributes<HTMLButtonElement>
React.HTMLAttributes<HTMLButtonElement>