- Item 1
- Item 1.1
- Item 1.2
- Item 1.2.1
- Item 2
Component
tree-view-demo
not found in registry.
import { File, FolderClosed, FolderOpen } from "lucide-react"
import {
TreeView,
TreeViewBranch,
TreeViewBranchContent,
TreeViewBranchControl,
TreeViewBranchIndicator,
TreeViewBranchText,
TreeViewItem,
TreeViewItemText,
TreeViewLabel,
TreeViewTree,
} from "@/components/ui/tree-view"
export default function TreeViewDemo() {
return (
<TreeView className="w-[40%]">
<TreeViewLabel>Label</TreeViewLabel>
<TreeViewTree>
<TreeViewBranch value="1">
<TreeViewBranchControl>
<TreeViewBranchIndicator className="data-[state=open]:hidden">
<FolderClosed className="h-4 w-4 shrink-0" />
</TreeViewBranchIndicator>
<TreeViewBranchIndicator className="data-[state=closed]:hidden">
<FolderOpen className="h-4 w-4 shrink-0" />
</TreeViewBranchIndicator>
<TreeViewBranchText>Item 1</TreeViewBranchText>
</TreeViewBranchControl>
<TreeViewBranchContent className="">
<TreeViewItem value="1.1">
<File className="h-4 w-4 shrink-0" />
<TreeViewItemText>Item 1.1</TreeViewItemText>
</TreeViewItem>
<TreeViewBranch value="1.2">
<TreeViewBranchControl>
<TreeViewBranchIndicator className="data-[state=open]:hidden">
<FolderClosed className="h-4 w-4 shrink-0" />
</TreeViewBranchIndicator>
<TreeViewBranchIndicator className="data-[state=closed]:hidden">
<FolderOpen className="h-4 w-4 shrink-0" />
</TreeViewBranchIndicator>
<TreeViewBranchText>Item 1.2</TreeViewBranchText>
</TreeViewBranchControl>
<TreeViewBranchContent>
<TreeViewItem value="1.2.1">
<File className="h-4 w-4 shrink-0" />
<TreeViewItemText>Item 1.2.1</TreeViewItemText>
</TreeViewItem>
</TreeViewBranchContent>
</TreeViewBranch>
</TreeViewBranchContent>
</TreeViewBranch>
<TreeViewItem value="2">
<File className="h-4 w-4 shrink-0" />
<TreeViewItemText>Item 2</TreeViewItemText>
</TreeViewItem>
</TreeViewTree>
</TreeView>
)
}
Not found
Installation
Copy and paste the following code into your project.
"use client"
import * as React from "react"
import { TreeView as TreeViewPrimitive } from "@ark-ui/react/tree-view"
import { cn } from "@/lib/utils"
const TreeView = React.forwardRef<
React.ElementRef<typeof TreeViewPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof TreeViewPrimitive.Root>
>(({ className, ...props }, ref) => (
<TreeViewPrimitive.Root
ref={ref}
className={cn("w-full [--pl:0.5rem]", className)}
{...props}
/>
))
const TreeViewContext = TreeViewPrimitive.Context
const TreeViewBranch = TreeViewPrimitive.Branch
const TreeViewBranchIndicator = TreeViewPrimitive.BranchIndicator
const TreeViewBranchText = TreeViewPrimitive.BranchText
const TreeViewItemText = TreeViewPrimitive.ItemText
const TreeViewTree = TreeViewPrimitive.Tree
const TreeViewLabel = React.forwardRef<
React.ElementRef<typeof TreeViewPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof TreeViewPrimitive.Label>
>(({ className, ...props }, ref) => (
<TreeViewPrimitive.Label
ref={ref}
className={cn("font-medium text-sm leading-none", className)}
{...props}
/>
))
const TreeViewBranchControl = React.forwardRef<
React.ElementRef<typeof TreeViewPrimitive.BranchControl>,
React.ComponentPropsWithoutRef<typeof TreeViewPrimitive.BranchControl>
>(({ className, ...props }, ref) => (
<TreeViewPrimitive.BranchControl
ref={ref}
className={cn(
"flex w-full items-center gap-2 rounded-md px-2 py-1 pl-[calc(var(--indent)+var(--pl))] text-primary [--indent:calc((var(--depth)-1)*1rem+(var(--depth)-1)*0.5rem)] hover:bg-muted",
className
)}
{...props}
/>
))
const TreeViewBranchContent = React.forwardRef<
React.ElementRef<typeof TreeViewPrimitive.BranchContent>,
React.ComponentPropsWithoutRef<typeof TreeViewPrimitive.BranchContent>
>(({ className, ...props }, ref) => (
<TreeViewPrimitive.BranchContent
ref={ref}
className={cn(
"relative [--indent:calc(var(--depth)*1rem+(var(--depth)-1)*0.5rem)] before:absolute before:left-[var(--indent)] before:h-full before:w-0.5 before:rounded before:bg-border before:content-['']",
className
)}
{...props}
/>
))
const TreeViewItem = React.forwardRef<
React.ElementRef<typeof TreeViewPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof TreeViewPrimitive.Item>
>(({ className, ...props }, ref) => (
<TreeViewPrimitive.Item
ref={ref}
className={cn(
"ml-[var(--indent)] flex w-full items-center gap-2 rounded-md px-2 py-1 text-primary [--indent:calc((var(--depth)-1)*1rem+(var(--depth)-1)*0.5rem)] hover:bg-muted",
className
)}
{...props}
/>
))
export {
TreeView,
TreeViewBranch,
TreeViewBranchContent,
TreeViewBranchControl,
TreeViewBranchIndicator,
TreeViewBranchText,
TreeViewContext,
TreeViewItem,
TreeViewItemText,
TreeViewLabel,
TreeViewTree,
}
Not Found
Update the import paths to match your project setup.
Usage
import {
TreeView,
TreeViewBranch,
TreeViewBranchContent,
TreeViewBranchControl,
TreeViewBranchIndicator,
TreeViewBranchText,
TreeViewItem,
TreeViewItemText,
TreeViewLabel,
TreeViewTree,
} from "@/components/ui/tree-view"
<TreeView>
<TreeViewLabel>Tree View</TreeViewLabel>
<TreeViewTree>
<TreeViewBranch value="1">
<TreeViewBranchControl>
<TreeViewBranchIndicator>></TreeViewBranchIndicator>
<TreeViewBranchText>Item 1</TreeViewBranchText>
</TreeViewBranchControl>
<TreeViewBranchContent>
<TreeViewItem value="1.1">
<TreeViewItemText>Item 1.1</TreeViewItemText>
</TreeViewItem>
</TreeViewBranchContent>
</TreeViewBranch>
<TreeViewItem value="2">
<TreeViewItemText>Item 2</TreeViewItemText>
</TreeViewItem>
</TreeViewTree>
</TreeView>