Data display
Tree View
Nested, expandable hierarchy with folder icons, counts and keyboard navigation.
react-aria-componentslucide-react
Preview
Q4 Enterprise push3
Series B — VP Sales128
New CROs42
High intent (smart)86
EMEA expansion2
UK Revenue Leaders74
DACH0
Archive
"use client";
import { ListFilter } from "lucide-react";
import { TreeView, TreeViewItem } from "@/components/ui/tree-view";
export default function Example() {
return (
<TreeView aria-label="Lists" selectionMode="single" defaultExpandedKeys={["q4", "emea"]} defaultSelectedKeys={["series-b"]} className="w-72 rounded-lg border border-line bg-surface p-2">
<TreeViewItem id="q4" label="Q4 Enterprise push" meta={3}>
<TreeViewItem id="series-b" label="Series B — VP Sales" meta={128} />
<TreeViewItem id="cro" label="New CROs" meta={42} />
<TreeViewItem id="smart" label="High intent (smart)" icon={<ListFilter />} meta={86} />
</TreeViewItem>
<TreeViewItem id="emea" label="EMEA expansion" meta={2}>
<TreeViewItem id="uk" label="UK Revenue Leaders" meta={74} />
<TreeViewItem id="dach" label="DACH" meta={0}>
<TreeViewItem id="berlin" label="Berlin fintech" meta={19} />
</TreeViewItem>
</TreeViewItem>
<TreeViewItem id="archive" label="Archive" />
</TreeView>
);
}Installation
Components are copied into your project — no runtime package.
Install the following dependencies:
terminalnpm install react-aria-components lucide-reactCreate a
tree-view.tsxfile and paste the following code into it.src/components/ui/tree-view.tsx"use client"; import { ChevronRight, File, Folder, FolderOpen } from "lucide-react"; import type { ReactNode } from "react"; import { Button as AriaButton, Tree as AriaTree, TreeItem as AriaTreeItem, TreeItemContent, type TreeProps } from "react-aria-components"; import { cx } from "@/utils/cx"; export function TreeView<T extends object>({ className, ...props }: Omit<TreeProps<T>, "className"> & { className?: string }) { return <AriaTree {...props} className={cx("flex flex-col gap-px outline-none", className)} />; } export interface TreeViewItemProps { id: string; label: string; /** Overrides the default folder/file icon. */ icon?: ReactNode; /** Right-aligned meta, e.g. a count. */ meta?: ReactNode; children?: ReactNode; } /** Nested item with an expand chevron. Folders get open/closed icons automatically. */ export function TreeViewItem({ id, label, icon, meta, children }: TreeViewItemProps) { return ( <AriaTreeItem id={id} textValue={label} className="group cursor-default rounded-sm text-base text-fg-secondary outline-none data-hovered:bg-muted data-hovered:text-fg data-selected:bg-accent-soft data-selected:text-accent-text data-focus-visible:shadow-focus" > <TreeItemContent> {({ hasChildItems, isExpanded, level }) => ( <div className="flex h-8 items-center gap-1.5 pr-2" style={{ paddingLeft: (level - 1) * 18 + 6 }}> {hasChildItems ? ( <AriaButton slot="chevron" className="inline-flex size-5 shrink-0 items-center justify-center rounded-[4px] text-fg-muted outline-none data-hovered:bg-active"> <ChevronRight className={cx("size-3.5 transition-transform", isExpanded && "rotate-90")} aria-hidden /> </AriaButton> ) : ( <span className="size-5 shrink-0" /> )} <span className="shrink-0 text-fg-muted group-data-selected:text-accent [&_svg]:size-4"> {icon ?? (hasChildItems ? isExpanded ? <FolderOpen /> : <Folder /> : <File />)} </span> <span className="flex-1 truncate">{label}</span> {meta && <span className="text-sm tabular text-fg-muted">{meta}</span>} </div> )} </TreeItemContent> {children} </AriaTreeItem> ); }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.