# File tree

> Native folder disclosures with optional nested folders and ordinary file links.

- Kind: ui · Category: Navigation · Interactivity: native
- Install: `npx shadcn@latest add @haze/file-tree`
- File: `src/components/haze/ui/file-tree.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/file-tree

## Use when
- Docs show a project structure with expandable directories
- File entries are either descriptive names or regular links

## Avoid when
- The feature needs file editing or a full tree widget with application keyboard shortcuts.
- A flat list is easier to read. Use item.

## Alternatives
- [collapsible](https://hazeui.com/ui/collapsible.md)
- [item](https://hazeui.com/ui/item.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| name | `string` | "Project files" | no | Visible folder name. |
| files | `{ name: string; href?: string; current?: boolean }[]` |  | no | Files in the folder. An href makes a file a link. |
| open | `boolean` | false | no | Opens the folder initially. |
| default slot | `slot` |  | no | Place child FileTree components here to nest folders. |
| class | `string` |  | no | Additional classes on the folder disclosure. |

## Accessibility
- Folders use native summaries and expose their expanded state. File links behave like links.
- Use current only on the file whose link points to the current page.
- Key Tab: Move among visible folders and file links.
- Key Enter or Space: Toggle the focused folder.

## Agent prompts

### Install and use

Install the Haze UI component "File tree" with `npx shadcn@latest add @haze/file-tree`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import FileTree from "@/components/haze/ui/file-tree.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/file-tree.md

### Customize with real content

Adapt the Haze file-tree ui for our product. Replace the demo values of these props with our real content: name, files, open, default slot, class. Keep the layout, the heading level rules and these accessibility details: Folders use native summaries and expose their expanded state. File links behave like links. Use current only on the file whose link points to the current page. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using file-tree, check it fits: use it when docs show a project structure with expandable directories; file entries are either descriptive names or regular links. Avoid it when the feature needs file editing or a full tree widget with application keyboard shortcuts.; a flat list is easier to read. use item.. Alternatives: @haze/collapsible, @haze/item.

## Example requests that should lead here
- Show a project file tree with two nested folders and links to the source files.

## Source

```astro
---
import { ChevronRight, File, Folder } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  name?: string;
  files?: { name: string; href?: string; current?: boolean }[];
  open?: boolean;
  class?: string;
}

const { name = "Project files", files = [{ name: "README.md" }, { name: "config.ts" }], open = false, class: className } = Astro.props;
const fileRow = "flex min-h-9 min-w-0 items-center gap-2 rounded-lg px-2.5 py-1 text-[0.8125rem]/5";
---

<details open={open} class={cn("min-w-0 [&[open]>summary>[data-tree-chevron]]:rotate-90", className)}>
  <summary class="flex min-h-9 min-w-0 cursor-pointer list-none items-center gap-2 rounded-lg px-2.5 py-1 text-[0.8125rem]/5 text-foreground transition-colors duration-150 ease-snappy hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">
    <ChevronRight data-tree-chevron aria-hidden="true" class="size-3 shrink-0 text-muted-foreground transition-[rotate] duration-200 ease-snappy rtl:rotate-180" />
    <Folder aria-hidden="true" class="size-4 shrink-0 text-muted-foreground" />
    <span class="min-w-0 wrap-anywhere font-medium">{name}</span>
  </summary>
  <div class="ms-4 grid min-w-0 gap-0.5 border-s border-border ps-2 pt-0.5">
    <slot />
    {files.length > 0 && (
      <ul class="grid min-w-0 gap-0.5">
        {files.map(({ name: fileName, href, current }) => (
          <li class="min-w-0">
            {href ? (
              <a href={href} aria-current={current ? "page" : undefined} class={cn(
                fileRow,
                "text-muted-foreground transition-colors duration-150 ease-snappy hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring",
                current && "bg-muted text-foreground",
              )}>
                <File aria-hidden="true" class="size-4 shrink-0" /><span class="min-w-0 wrap-anywhere">{fileName}</span>
              </a>
            ) : (
              <span class={cn(fileRow, "text-muted-foreground")}>
                <File aria-hidden="true" class="size-4 shrink-0" /><span class="min-w-0 wrap-anywhere">{fileName}</span>
              </span>
            )}
          </li>
        ))}
      </ul>
    )}
  </div>
</details>
```
