Components

File Tree

An accessible file tree component for React and Tailwind CSS: folders and files with icons by extension, folders that expand smoothly, a selection highlight that glides between rows and optional indent guides. Data-driven, controllable and fully keyboard navigable following the WAI-ARIA tree pattern.

  • WAI-ARIA tree
  • Arrow keys & type-ahead
  • Icons by extension
  • Gliding selection
  • Controlled or uncontrolled
app/dashboard/page.tsx
  • app
    • dashboard
      • layout.tsx
      • page.tsx
      • loading.tsx
    • globals.css
    • layout.tsx
  • components
    • site-header.tsx
  • .env.local
  • next.config.ts
  • package.json
  • package-lock.json
  • README.md

Installation

$ npx shadcn@latest add https://duskui.com/r/file-tree.json

Usage

explorer.tsx
import { FileTree, type FileTreeItem } from "@/components/dusk/file-tree/file-tree"

const items: FileTreeItem[] = [
  {
    id: "app",
    name: "app",
    children: [
      { id: "app/page.tsx", name: "page.tsx" },
      { id: "app/layout.tsx", name: "layout.tsx" },
    ],
  },
  { id: "package.json", name: "package.json" },
]

export function Explorer() {
  return (
    <FileTree
      aria-label="Project files"
      items={items}
      defaultExpanded={["app"]}
      defaultSelected="app/page.tsx"
    />
  )
}

Examples

Controlled expansion

Pass expanded and onExpandedChange to drive folders from outside, e.g. expand / collapse all. selected and onSelectedChange work the same way.

1 open

  • components
    • site-header.tsx
  • .env.local
  • next.config.ts
  • package.json
  • package-lock.json
  • README.md

Badges, custom icons & disabled

Items take a trailing badge (here git status), an icon override and disabled. indentGuides={false} drops the guides; indent sets the step per level.

  • src
    • auth.ts, Modified
    • session.ts, Added
    • db
      • schema.sql, Untracked
      • seed.ts
  • secrets.enc
  • tsconfig.json

API Reference

Also accepts any HTML attribute for the ul role="tree". Each FileTreeItem is { id, name, children?, icon?, badge?, disabled? }; an item with a children array (even empty) is a folder. fileIcon(name) is exported if you need the same icons elsewhere.

PropTypeDefaultDescription
itemsFileTreeItem[]—The tree data.
expandedstring[]—Open folder ids (controlled).
defaultExpandedstring[][]Open folder ids on first render.
onExpandedChange(expanded: string[]) => void—Called when a folder opens or closes.
selectedstring | null—Selected item id (controlled).
defaultSelectedstring | nullnullSelected item id on first render.
onSelectedChange(id: string, item: FileTreeItem) => void—Called when an item is selected by click, Enter or Space.
indentGuidesbooleantrueVertical guides per level; the selected item's branch is highlighted.
indentnumber16Indent per level in px.
aria-labelstring"Files"Accessible name of the tree.

Accessibility

  • Implements the WAI-ARIA tree pattern: role="tree", nested role="group" lists and role="treeitem" with aria-expanded, aria-selected, aria-level, aria-posinset and aria-setsize.
  • One tab stop (roving tabindex). ↑/↓ move, → opens a folder or enters it, ← closes it or jumps to the parent, Home/End jump to the ends, Enter/Space select (and toggle folders), * opens all sibling folders, and typing jumps to items by name.
  • Collapsed branches are not rendered, so hidden items never receive focus or get announced.
  • Icons, chevrons and guides are aria-hidden; the item name is the accessible label.
  • With prefers-reduced-motion folders open instantly and the selection jumps instead of gliding.