File tree
Present a project structure as an accessible, collapsible hierarchy of folders and files.
Usage
Use FileTree to explain where files belong or help readers understand a project before they begin editing it. Each item requires a stable id and a visible name.
Items with children are rendered as folders and can be expanded or collapsed. Items without children are rendered as files.
Expanded folders
Pass folder IDs to defaultExpandedKeys when their contents should be visible on first render. Expand only the branches needed to explain the surrounding instructions.
Use expandedKeys with onExpandedChange when the expansion state needs to be controlled by a React component. For normal MDX content, defaultExpandedKeys is simpler and keeps the example interactive.
Highlight a file
Enable single selection when a guide refers to one specific file. The selected row gives readers a clear visual target without changing the file tree into a navigation menu.
Custom label
Override aria-label when more than one tree appears on the same page or when the structure represents something more specific than generic project files.
Keyboard interaction
The file tree uses the React Aria tree pattern and supports keyboard navigation automatically.
| Key | Action |
|---|---|
| Arrow down | Move focus to the next visible item. |
| Arrow up | Move focus to the previous visible item. |
| Arrow right | Expand a closed folder or move into its first child. |
| Arrow left | Collapse an open folder or move to its parent. |
| Enter or Space | Select an item when selection is enabled. |
Writing effective trees
- Keep every
idunique within the tree, even when two files share the same name. - Show only the branches relevant to the current explanation.
- Preserve the real casing and extension of each file.
- Place the tree near the instructions that reference it.
- Use a fenced
txtcode block instead when the structure does not need to be interactive.