File Tree

Animated file-tree component with spring folder expand/collapse, animated hover highlight, and automatic file-type icons.

Installation

File Structure

file-tree.tsx

Usage

import {
  FileTree,
  type FileTreeElement,
} from "@/components/unlumen-ui/file-tree";

const elements: FileTreeElement[] = [
  {
    id: "src",
    name: "src",
    type: "folder",
    children: [
      {
        id: "components",
        name: "components",
        type: "folder",
        children: [
          { id: "button", name: "button.tsx", highlight: true },
          { id: "card", name: "card.tsx", highlight: true },
        ],
      },
      { id: "page", name: "page.tsx" },
    ],
  },
  { id: "package-json", name: "package.json" },
];

export default function Example() {
  return <FileTree elements={elements} defaultOpenIds={["src"]} />;
}

API Reference

FileTree

elements
FileTreeElement[]

Tree nodes rendered by the component.

className?
string

Additional classes applied to the root element.

highlightColor?
string
"#f472b6"

Text color used for nodes marked with `highlight: true`.

indentSize?
number
24

Horizontal indent in pixels for nested folder content.

showIcons?
boolean
true

Whether file and folder icons are rendered.

defaultOpenIds?
string[]
[]

Folder ids that should be open on first render.

FileTreeElement

id
string

Stable unique id used as the React key.

name
string

Label shown for the file or folder.

type?
"folder" | "file"

Node type. Use `"folder"` for collapsible branches; omit it for files.

children?
FileTreeElement[]

Nested nodes rendered inside a folder.

icon?
React.ComponentType<{ className?: string }>

Custom icon component for file nodes.

highlight?
boolean

Marks the node as visually highlighted.

defaultOpen?
boolean

Whether a folder starts expanded.

Notes

  • Folders are closed by default. Use defaultOpenIds on <FileTree /> or defaultOpen on a folder node to choose what starts expanded.
  • Opening a folder does not automatically open its nested folders.
  • File icons are resolved from common extensions like tsx, json, mdx, svg, and image formats.
  • The hover background is rendered once and moves between rows with spring animation.

Credits

Built by leo and inspired by the file tree in Animate UI.

Keep in mind

Most components on this site are inspired by or recreated from existing work across the web. I'm not here to take credit; just to learn, experiment, and sometimes push things a bit further. If something looks familiar and I forgot to mention you, reach out and I'll fix that right away.