Mark Yo

Tree View

Component

About

A keyboard-navigable WAI-ARIA tree view for file explorers and nested lists, with animated expand/collapse, a selection highlight that glides between rows, and typeahead search.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Tree View to your project and fits it to your theme.

Code

How Tree View is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

import { type TreeNode, TreeView } from "@/components/ui/tree-view";
import { useState } from "react";
import { FileCode2 } from "lucide-react";
import { FileJson } from "lucide-react";
import { FileText } from "lucide-react";
import { Folder } from "lucide-react";
import { FolderOpen } from "lucide-react";
import { Image } from "lucide-react";
import { Palette } from "lucide-react";

const folder = <Folder className="text-muted-foreground" />;
const folderOpen = <FolderOpen className="text-muted-foreground" />;
const tsx = <FileCode2 className="text-sky-500" />;
const ts = <FileCode2 className="text-blue-500" />;
const json = <FileJson className="text-amber-500" />;
const css = <Palette className="text-pink-500" />;
const md = <FileText className="text-muted-foreground" />;
const img = <Image className="text-emerald-500" />;

const nodes: TreeNode[] = [
  {
    id: "app",
    label: "app",
    icon: folder,
    expandedIcon: folderOpen,
    children: [
      { id: "app/layout", label: "layout.tsx", icon: tsx },
      { id: "app/page", label: "page.tsx", icon: tsx },
      { id: "app/globals", label: "globals.css", icon: css },
    ],
  },
  {
    id: "components",
    label: "components",
    icon: folder,
    expandedIcon: folderOpen,
    children: [
      {
        id: "components/motion",
        label: "motion",
        icon: folder,
        expandedIcon: folderOpen,
        children: [
          {
            id: "components/motion/tree-view",
            label: "tree-view.tsx",
            icon: tsx,
          },
          { id: "components/motion/tabs", label: "tabs.tsx", icon: tsx },
          { id: "components/motion/switch", label: "switch.tsx", icon: tsx },
        ],
      },
      {
        id: "components/previews",
        label: "previews",
        icon: folder,
        expandedIcon: folderOpen,
        children: [
          { id: "components/previews/index", label: "index.tsx", icon: tsx },
        ],
      },
    ],
  },
  {
    id: "lib",
    label: "lib",
    icon: folder,
    expandedIcon: folderOpen,
    children: [
      { id: "lib/ease", label: "ease.ts", icon: ts },
      { id: "lib/utils", label: "utils.ts", icon: ts },
    ],
  },
  {
    id: "public",
    label: "public",
    icon: folder,
    expandedIcon: folderOpen,
    children: [{ id: "public/og", label: "og.png", icon: img }],
  },
  { id: "package", label: "package.json", icon: json },
  { id: "readme", label: "README.md", icon: md },
];

export default function TreeViewPreview() {
  const [selected, setSelected] = useState<string | null>(
    "components/motion/tree-view",
  );

  return (
    <div className="flex h-[400px] w-full items-center justify-center">
      <div className="flex h-full max-h-[360px] w-full max-w-xs flex-col overflow-hidden rounded-2xl border border-border bg-card">
        <div className="flex items-center justify-between border-b border-border px-4 py-2.5">
          <span className="text-xs font-medium uppercase tracking-wider text-muted-foreground">
            Explorer
          </span>
          <span className="max-w-[60%] truncate font-mono text-[11px] text-muted-foreground">
            {selected ?? "—"}
          </span>
        </div>
        <div className="min-h-0 flex-1 overflow-y-auto p-2 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
          <TreeView
            aria-label="Project files"
            nodes={nodes}
            value={selected}
            onValueChange={setSelected}
            defaultExpanded={["components", "components/motion"]}
          />
        </div>
      </div>
    </div>
  );
}

Dependencies

More by Mark Yo

View all
  • Loader

    Loader

    64
  • Pricing Table

    Pricing Table

    23
  • Bento Grid

    Bento Grid

    4
  • Expandable Tabs

    Expandable Tabs

    1
  • Shared Layout Background

    Shared Layout Background

    1
  • Stateful Button

    Stateful Button

  • Animated Button

    Animated Button

  • Morphing Modal

    Morphing Modal