File tree
Native folder disclosures with optional nested folders and ordinary file links.
pnpm dlx shadcn@latest add @haze/file-tree---
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>When to use
- Docs show a project structure with expandable directories
- File entries are either descriptive names or regular links
When not to use
- The feature needs file editing or a full tree widget with application keyboard shortcuts.
- A flat list is easier to read. Use item.
Installation
Add the Haze registry to components.json once, then install File tree with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/file-treenpx shadcn@latest add @haze/file-treeyarn dlx shadcn@latest add @haze/file-treebunx --bun shadcn@latest add @haze/file-treeFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | "Project files" | Visible folder name. |
| files | { name: string; href?: string; current?: boolean }[] | – | Files in the folder. An href makes a file a link. |
| open | boolean | false | Opens the folder initially. |
| default slot | slot | – | Place child FileTree components here to nest folders. |
| class | string | – | Additional classes on the folder disclosure. |
Keyboard interactions
- Tab
- Move among visible folders and file links.
- Enter or Space
- Toggle the focused folder.
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.