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
- (marketing)
- dashboard
- layout.tsx
- page.tsx
- loading.tsx
- globals.css
- layout.tsx
- components
- ui
- site-header.tsx
- public
- scripts
- .env.local
- next.config.ts
- package.json
- package-lock.json
- README.md
"use client";
import * as React from "react";
import { FileTree } from "@/components/dusk/file-tree/file-tree";
import { projectFiles } from "@/components/demos/file-tree/project-files";
export default function FileTreeDemo() {
const [selected, setSelected] = React.useState<string | null>("app/dashboard/page.tsx");
return (
<div className="w-full max-w-sm overflow-hidden rounded-xl border border-white/10 bg-background">
<div className="flex items-center gap-1.5 border-b border-white/10 px-3 py-2">
<span className="size-2.5 rounded-full bg-white/15" />
<span className="size-2.5 rounded-full bg-white/15" />
<span className="size-2.5 rounded-full bg-white/15" />
<span className="ml-2 truncate font-mono text-xs text-white/50">{selected ?? "Explorer"}</span>
</div>
<FileTree
aria-label="Project files"
items={projectFiles}
defaultExpanded={["app", "app/dashboard", "components"]}
selected={selected}
onSelectedChange={(id) => setSelected(id)}
className="p-2"
/>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/file-tree.json1. Install the dependencies.
npm install motion lucide-react2. Copy the source into
components/dusk/file-tree/file-tree.tsx.file-tree.tsx "use client"; import * as React from "react"; import { AnimatePresence, LayoutGroup, motion, useReducedMotion } from "motion/react"; import { ChevronRight, File, FileArchive, FileBraces, FileCode, FileCog, FileImage, FileLock, FileMusic, FileSpreadsheet, FileTerminal, FileText, FileType, FileVideoCamera, Folder, FolderOpen, type LucideIcon, } from "lucide-react"; import { cn } from "@/lib/utils"; export interface FileTreeItem { /** Unique, stable id (a path works well). */ id: string; name: string; /** Present (even empty) for folders. */ children?: FileTreeItem[]; /** Replaces the icon picked from the extension. */ icon?: React.ReactNode; /** Small trailing content, e.g. a git status letter. */ badge?: React.ReactNode; /** Focusable but cannot be selected. */ disabled?: boolean; } export interface FileTreeProps extends Omit<React.ComponentProps<"ul">, "onSelect" | "children" | "defaultValue"> { items: FileTreeItem[]; /** Open folder ids (controlled). */ expanded?: string[]; defaultExpanded?: string[]; onExpandedChange?: (expanded: string[]) => void; /** Selected item id (controlled). */ selected?: string | null; defaultSelected?: string | null; onSelectedChange?: (id: string, item: FileTreeItem) => void; /** Draw vertical guides for each nesting level. The selected item's branch is highlighted. */ indentGuides?: boolean; /** Indent per level in px. */ indent?: number; } type Row = { item: FileTreeItem; depth: number; parentId: string | null; posinset: number; setsize: number; }; const isFolder = (item: FileTreeItem) => Array.isArray(item.children); const ICONS: Array<[RegExp, LucideIcon, string]> = [ // Lock and config files first, so `package-lock.json` or `next.config.ts` don't read as plain code. [/(\.lock|-lock\.json|-lock\.ya?ml)$/i, FileLock, "text-white/55"], [/(\.config\.[a-z]+|^\.[\w.-]+)$/i, FileCog, "text-white/55"], [/\.(tsx?|jsx?|mjs|cjs|vue|svelte|astro|py|rb|go|rs|java|kt|swift|c|cpp|h|php)$/i, FileCode, "text-white/55"], [/\.(json|jsonc|ya?ml|toml)$/i, FileBraces, "text-white/55"], [/\.(css|scss|sass|less)$/i, FileType, "text-white/55"], [/\.(mdx?|txt|rst)$/i, FileText, "text-white/55"], [/\.(png|jpe?g|gif|webp|avif|svg|ico)$/i, FileImage, "text-white/55"], [/\.(mp4|mov|webm|mkv)$/i, FileVideoCamera, "text-white/55"], [/\.(mp3|wav|ogg|flac)$/i, FileMusic, "text-white/55"], [/\.(zip|tar|gz|tgz|rar|7z)$/i, FileArchive, "text-white/55"], [/\.(csv|tsv|xlsx?)$/i, FileSpreadsheet, "text-white/55"], [/\.(sh|bash|zsh|ps1)$/i, FileTerminal, "text-white/55"], ]; /** The lucide icon and color class used for a file name. */ export function fileIcon(name: string): { Icon: LucideIcon; className: string } { for (const [pattern, Icon, className] of ICONS) { if (pattern.test(name)) return { Icon, className }; } return { Icon: File, className: "text-white/55" }; } function flatten(items: FileTreeItem[], open: Set<string>, depth = 0, parentId: string | null = null, out: Row[] = []) { items.forEach((item, index) => { out.push({ item, depth, parentId, posinset: index + 1, setsize: items.length }); if (isFolder(item) && open.has(item.id)) flatten(item.children!, open, depth + 1, item.id, out); }); return out; } function findPath(items: FileTreeItem[], id: string, trail: string[] = []): string[] | null { for (const item of items) { if (item.id === id) return trail; if (item.children) { const found = findPath(item.children, id, [...trail, item.id]); if (found) return found; } } return null; } type TreeContextValue = { open: Set<string>; selected: string | null; focusable: string | null; branch: Set<string>; indent: number; indentGuides: boolean; instant: boolean; layoutId: string; register: (id: string, el: HTMLLIElement | null) => void; onItemClick: (item: FileTreeItem) => void; onItemFocus: (id: string) => void; }; const TreeContext = React.createContext<TreeContextValue | null>(null); /** * An accessible file tree for React: folders and files with icons picked from * the extension, folders that spring open and closed, a selection highlight * that glides between rows, and optional indent guides. * * Follows the WAI-ARIA tree pattern: one tab stop (roving tabindex), arrow * keys to move / open / close, Home and End, Enter or Space to select (and * toggle folders), `*` to open all siblings, and type-ahead by name. Expanded * and selected state can be controlled. Rendered from data, so the initial * tree is in the server HTML; collapsed branches are not rendered. * Reduced motion turns the animations off. */ export function FileTree({ items, expanded: expandedProp, defaultExpanded = [], onExpandedChange, selected: selectedProp, defaultSelected = null, onSelectedChange, indentGuides = true, indent = 16, className, onKeyDown, "aria-label": ariaLabel = "Files", ...props }: FileTreeProps) { const reduceMotion = useReducedMotion(); const layoutId = React.useId(); const [expandedState, setExpandedState] = React.useState(defaultExpanded); const [selectedState, setSelectedState] = React.useState(defaultSelected); const [focusedId, setFocusedId] = React.useState<string | null>(null); const expanded = expandedProp ?? expandedState; const selected = selectedProp !== undefined ? selectedProp : selectedState; const open = React.useMemo(() => new Set(expanded), [expanded]); const rows = React.useMemo(() => flatten(items, open), [items, open]); const branch = React.useMemo(() => new Set(selected ? findPath(items, selected) ?? [] : []), [items, selected]); // The single tab stop: the focused row, else the selected row, else the first. // If the remembered row was hidden by collapsing an ancestor, fall back to that ancestor. const visible = React.useMemo(() => new Set(rows.map((r) => r.item.id)), [rows]); const pick = (id: string | null) => { if (!id) return null; if (visible.has(id)) return id; const path = findPath(items, id); return path?.filter((p) => visible.has(p)).pop() ?? null; }; const focusable = pick(focusedId) ?? pick(selected) ?? rows[0]?.item.id ?? null; const elements = React.useRef(new Map<string, HTMLLIElement>()); const register = React.useCallback((id: string, el: HTMLLIElement | null) => { if (el) elements.current.set(id, el); else elements.current.delete(id); }, []); const setExpanded = (next: string[]) => { if (expandedProp === undefined) setExpandedState(next); onExpandedChange?.(next); }; const toggle = (id: string, force?: boolean) => { const isOpen = open.has(id); const next = force ?? !isOpen; if (next === isOpen) return; setExpanded(next ? [...expanded, id] : expanded.filter((e) => e !== id)); }; const select = (item: FileTreeItem) => { if (item.disabled) return; if (selectedProp === undefined) setSelectedState(item.id); onSelectedChange?.(item.id, item); }; const focusRow = (id: string | undefined) => { if (!id) return; setFocusedId(id); elements.current.get(id)?.focus(); }; const typeahead = React.useRef({ buffer: "", time: 0 }); const handleKeyDown = (event: React.KeyboardEvent<HTMLUListElement>) => { onKeyDown?.(event); if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey) return; const index = rows.findIndex((r) => r.item.id === focusable); const row = rows[index]; if (!row) return; const { item } = row; const folder = isFolder(item); let handled = true; switch (event.key) { case "ArrowDown": focusRow(rows[index + 1]?.item.id); break; case "ArrowUp": focusRow(rows[index - 1]?.item.id); break; case "ArrowRight": if (folder && !open.has(item.id)) toggle(item.id, true); else if (folder && item.children!.length) focusRow(rows[index + 1]?.item.id); break; case "ArrowLeft": if (folder && open.has(item.id)) toggle(item.id, false); else focusRow(row.parentId ?? undefined); break; case "Home": focusRow(rows[0]?.item.id); break; case "End": focusRow(rows[rows.length - 1]?.item.id); break; case "Enter": case " ": select(item); if (folder) toggle(item.id); break; case "*": { const siblings = rows .filter((r) => r.parentId === row.parentId && isFolder(r.item) && !open.has(r.item.id)) .map((r) => r.item.id); if (siblings.length) setExpanded([...expanded, ...siblings]); break; } default: { if (event.key.length !== 1 || !/\S/.test(event.key)) { handled = false; break; } const now = event.timeStamp; const t = typeahead.current; t.buffer = now - t.time > 600 ? event.key : t.buffer + event.key; t.time = now; const query = t.buffer.toLowerCase(); // Repeating one letter cycles through matches; a longer string refines from the current row. const cycling = query.length > 1 && query.split("").every((c) => c === query[0]); const needle = cycling ? query[0] : query; const startAt = cycling || query.length === 1 ? index + 1 : index; for (let k = 0; k < rows.length; k++) { const candidate = rows[(startAt + k) % rows.length]; if (candidate.item.name.toLowerCase().startsWith(needle)) { focusRow(candidate.item.id); break; } } } } if (handled) event.preventDefault(); }; const context: TreeContextValue = { open, selected, focusable, branch, indent, indentGuides, instant: !!reduceMotion, layoutId, register, onItemClick: (item) => { setFocusedId(item.id); select(item); if (isFolder(item)) toggle(item.id); }, onItemFocus: setFocusedId, }; return ( <TreeContext.Provider value={context}> <LayoutGroup id={layoutId}> <ul role="tree" aria-label={ariaLabel} data-slot="file-tree" className={cn("relative select-none text-sm", className)} onKeyDown={handleKeyDown} {...props} > {items.map((item, index) => ( <TreeNode key={item.id} item={item} depth={0} posinset={index + 1} setsize={items.length} /> ))} </ul> </LayoutGroup> </TreeContext.Provider> ); } function TreeNode({ item, depth, posinset, setsize, }: { item: FileTreeItem; depth: number; posinset: number; setsize: number; }) { const ctx = React.useContext(TreeContext)!; const folder = isFolder(item); const isOpen = folder && ctx.open.has(item.id); const isSelected = ctx.selected === item.id; const padding = 8 + depth * ctx.indent; const { Icon, className: iconColor } = fileIcon(item.name); const spring = ctx.instant ? { duration: 0 } : { type: "spring" as const, bounce: 0.1, duration: 0.35 }; return ( <li ref={(el) => ctx.register(item.id, el)} role="treeitem" aria-level={depth + 1} aria-posinset={posinset} aria-setsize={setsize} aria-expanded={folder ? isOpen : undefined} aria-selected={item.disabled ? undefined : isSelected} aria-disabled={item.disabled || undefined} tabIndex={ctx.focusable === item.id ? 0 : -1} onFocus={(event) => { if (event.target === event.currentTarget) ctx.onItemFocus(item.id); }} className="outline-none" > <div onClick={(event) => { event.stopPropagation(); ctx.onItemClick(item); }} className={cn( "relative flex h-8 cursor-pointer items-center gap-1.5 rounded-md pr-2 transition-colors duration-150", "text-white/70 hover:bg-white/[0.04] hover:text-white", "[li:focus-visible>&]:ring-2 [li:focus-visible>&]:ring-ring/60", isSelected && "text-white hover:bg-transparent", item.disabled && "cursor-not-allowed opacity-50" )} style={{ paddingLeft: padding }} > {isSelected && ( <motion.span layoutId={`${ctx.layoutId}-selection`} aria-hidden className="absolute inset-0 rounded-md bg-primary/10" transition={ctx.instant ? { duration: 0 } : { type: "spring", bounce: 0.1, duration: 0.3 }} /> )} <span aria-hidden className="relative grid size-4 shrink-0 place-items-center text-white/55"> {folder && ( <ChevronRight className={cn( "size-3.5 transition-transform duration-200 motion-reduce:transition-none", isOpen && "rotate-90" )} /> )} </span> <span aria-hidden className="relative grid size-4 shrink-0 place-items-center"> {item.icon ?? (folder ? ( isOpen ? ( <FolderOpen className="size-4 text-white/55" /> ) : ( <Folder className="size-4 text-white/55" /> ) ) : ( <Icon className={cn("size-4", iconColor)} /> ))} </span> <span className={cn("relative truncate", isSelected && "font-medium")}>{item.name}</span> {item.badge != null && <span className="relative ml-auto pl-2 text-xs text-white/55">{item.badge}</span>} </div> {folder && ( <AnimatePresence initial={false}> {isOpen && ( <motion.ul key="group" role="group" className="relative overflow-hidden" initial={{ height: 0, opacity: 0 }} animate={{ height: "auto", opacity: 1 }} exit={{ height: 0, opacity: 0 }} transition={{ height: spring, opacity: { duration: ctx.instant ? 0 : 0.2 } }} > {ctx.indentGuides && ( <span aria-hidden className={cn( "pointer-events-none absolute inset-y-1 w-px transition-colors duration-200", ctx.branch.has(item.id) ? "bg-white/25" : "bg-white/10" )} style={{ left: padding + 7.5 }} /> )} {item.children!.map((child, index) => ( <TreeNode key={child.id} item={child} depth={depth + 1} posinset={index + 1} setsize={item.children!.length} /> ))} </motion.ul> )} </AnimatePresence> )} </li> ); }
Usage
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
- app
- components
- ui
- site-header.tsx
- public
- scripts
- .env.local
- next.config.ts
- package.json
- package-lock.json
- README.md
"use client";
import * as React from "react";
import { ChevronsDownUp, ChevronsUpDown } from "lucide-react";
import { FileTree, type FileTreeItem } from "@/components/dusk/file-tree/file-tree";
import { projectFiles } from "@/components/demos/file-tree/project-files";
import { Button } from "@/components/ui/button";
function folderIds(items: FileTreeItem[]): string[] {
return items.flatMap((item) => (item.children ? [item.id, ...folderIds(item.children)] : []));
}
const allFolders = folderIds(projectFiles);
export default function FileTreeControlled() {
const [expanded, setExpanded] = React.useState<string[]>(["components"]);
return (
<div className="w-full max-w-sm">
<div className="mb-2 flex items-center justify-between">
<p className="font-mono text-xs text-white/55">{expanded.length} open</p>
<div className="flex gap-1">
<Button variant="ghost" size="sm" onClick={() => setExpanded(allFolders)}>
<ChevronsUpDown aria-hidden /> Expand all
</Button>
<Button variant="ghost" size="sm" onClick={() => setExpanded([])}>
<ChevronsDownUp aria-hidden /> Collapse all
</Button>
</div>
</div>
<FileTree
aria-label="Project files"
items={projectFiles}
expanded={expanded}
onExpandedChange={setExpanded}
className="max-h-80 overflow-y-auto rounded-xl border border-white/10 bg-background p-2"
/>
</div>
);
}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
import { Database, Lock } from "lucide-react";
import { FileTree, type FileTreeItem } from "@/components/dusk/file-tree/file-tree";
const labels = { M: "Modified", A: "Added", U: "Untracked" } as const;
const status = (letter: keyof typeof labels, className: string) => (
<>
<span aria-hidden className={`font-mono font-semibold ${className}`}>
{letter}
</span>
<span className="sr-only">, {labels[letter]}</span>
</>
);
const items: FileTreeItem[] = [
{
id: "src",
name: "src",
children: [
{ id: "src/auth.ts", name: "auth.ts", badge: status("M", "text-amber-300") },
{ id: "src/session.ts", name: "session.ts", badge: status("A", "text-emerald-400") },
{
id: "src/db",
name: "db",
icon: <Database className="size-4 text-white/55" />,
children: [
{ id: "src/db/schema.sql", name: "schema.sql", badge: status("U", "text-white/50") },
{ id: "src/db/seed.ts", name: "seed.ts" },
],
},
],
},
{ id: "secrets", name: "secrets.enc", icon: <Lock className="size-4 text-white/55" />, disabled: true },
{ id: "tsconfig.json", name: "tsconfig.json" },
];
export default function FileTreeBadges() {
return (
<FileTree
aria-label="Changed files"
items={items}
defaultExpanded={["src", "src/db"]}
defaultSelected="src/auth.ts"
indentGuides={false}
indent={20}
className="w-full max-w-xs rounded-xl border border-white/10 bg-background p-2"
/>
);
}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.
| Prop | Type | Default | Description |
|---|---|---|---|
| items | FileTreeItem[] | — | The tree data. |
| expanded | string[] | — | Open folder ids (controlled). |
| defaultExpanded | string[] | [] | Open folder ids on first render. |
| onExpandedChange | (expanded: string[]) => void | — | Called when a folder opens or closes. |
| selected | string | null | — | Selected item id (controlled). |
| defaultSelected | string | null | null | Selected item id on first render. |
| onSelectedChange | (id: string, item: FileTreeItem) => void | — | Called when an item is selected by click, Enter or Space. |
| indentGuides | boolean | true | Vertical guides per level; the selected item's branch is highlighted. |
| indent | number | 16 | Indent per level in px. |
| aria-label | string | "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.