Skip to main content

File Tree

2 min readStableBeginner

FileTree turns indented text into a directory listing with folder and file icons. A name ending in / becomes a folder; everything else is a file. Each example below shows the live component first, then the exact MDX that produced it.

Quick Start

Pass the tree as a single string child, with \n between rows.

index.mdx
layout.tsx
app/features/components/file-tree.mdx
MDX
<FileTree>{'app/\n  index.mdx\n  layout.tsx'}</FileTree>

Adding Options

The data prop accepts the same string and keeps deeper trees out of the component's children.

introduction.mdx
features.mdx
route.ts
CodeBlock.tsx
app/features/components/file-tree.mdx
MDX
<FileTree data="app/\n  getting-started/\n    introduction.mdx\n    features.mdx\n  api/\n    health/\n      route.ts\ncomponents/\n  CodeBlock.tsx" />

Advanced

Every folder row is a button, so readers can collapse a subtree they don't care about. Folders start expanded, and the toggle is local to that row — collapsing one branch leaves the rest alone. Two rules govern deeper trees. Each level must indent by exactly two more spaces than its parent, and a row that misses that step is dropped from the output rather than reparented. A name without a trailing slash stays a file even when it contains a slash, which is how health/route.ts collapses into one row.

index.mdx
layout.tsx
health/route.ts
verify/route.ts
logout/route.ts
FileTree.tsx
navigation-utils.ts
app/features/components/file-tree.mdx
MDX
<FileTree>
  {
    'app/\n  index.mdx\n  layout.tsx\n  api/\n    health/route.ts\n    auth/\n      verify/route.ts\n      logout/route.ts\ncomponents/\n  FileTree.tsx\nlib/\n  navigation-utils.ts'
  }
</FileTree>

Options

datastring

The indented tree text. Literal \n sequences are converted to line breaks before parsing, so a one-line attribute works.

childrenReactNode

The same text, passed as children. Text is pulled out of nested elements before parsing, and the \n conversion applies here too. Keep it a single string expression, {'app/\n index.mdx'}, because MDX strips the leading spaces off plain Markdown children and the indentation is what builds the tree. A non-empty data wins when both are set.

Neither prop is required, and neither has a default. Whitespace-only input, or no input at all, renders nothing — the component returns null before it draws a border.

Was this page helpful?