# Mail Folders

> The folder tree of a mailbox: standard folders with their icons (inbox, drafts, sent, scheduled, starred, archive, junk, trash) and your own folders, nested, with unread counts.

Component · Data · Halo kit · [Live page](https://ui.twentyfirst.media/halo/components/MailFolders/)

The folder tree of a mailbox: standard folders with their icons (inbox, drafts, sent, scheduled, starred, archive, junk, trash) and your own folders, nested, with unread counts.

Each folder has a menu, from its "…" button, a right click or Shift+F10: a new folder inside, rename and delete for own folders, and "Empty folder" for trash and junk. Deleting and emptying ask first. Renaming and new folders are edited in place (Enter saves, Esc cancels). Folders with `href` render as links (`linkAs`).

## Example

```tsx
import * as React from "react";
import { MailFolders } from "@twentyfirstmedia/halo";
import type { MailFolder } from "@twentyfirstmedia/halo";
import { FOLDERS } from "./_mail";

const rename = (list: MailFolder[], id: string, label: string): MailFolder[] =>
  list.map((f) =>
    f.id === id ? { ...f, label } : { ...f, children: f.children && rename(f.children, id, label) },
  );
const remove = (list: MailFolder[], id: string): MailFolder[] =>
  list
    .filter((f) => f.id !== id)
    .map((f) => ({ ...f, children: f.children && remove(f.children, id) }));

export default function Preview() {
  const [folders, setFolders] = React.useState(FOLDERS);
  return (
    <div className="tf-panel" style={{ maxWidth: 300 }}>
      <MailFolders
        folders={folders}
        defaultSelected="inbox"
        defaultOpen={["clients"]}
        onRename={(id, label) => setFolders((l) => rename(l, id, label))}
        onDelete={(id) => setFolders((l) => remove(l, id))}
        onEmpty={() => {}}
        onCreate={(label, parent) =>
          setFolders((l) => {
            const folder = { id: "f" + Date.now(), label };
            if (!parent) return l.concat([folder]);
            const add = (list: MailFolder[]): MailFolder[] =>
              list.map((f) =>
                f.id === parent
                  ? { ...f, children: (f.children || []).concat([folder]) }
                  : { ...f, children: f.children && add(f.children) },
              );
            return add(l);
          })
        }
      />
    </div>
  );
}
```

## Props

### MailFolders

| Prop | Type | Notes |
| --- | --- | --- |
| `folders` | `MailFolder[]` | Required |
| `selected?` | `string \| null` | The open folder (controlled) / initially / on pick. |
| `defaultSelected?` | `string \| null` |  |
| `onSelect?` | `(id: string \| null) => void` |  |
| `defaultOpen?` | `string[]` | Folders with children open at first. |
| `onCreate?` | `(name: string, parent: string \| null) => void` | New folder (at the top level or inside `parent`). |
| `onRename?` | `(id: string, name: string) => void` |  |
| `onDelete?` | `(id: string) => void` | Delete an own folder (after a confirmation). |
| `onEmpty?` | `(id: string) => void` | Empty trash or junk (after a confirmation). |
| `linkAs?` | `ElementType` |  |
| `label?` | `string` | Accessible name of the navigation (default "Folders"). |
| `className?` | `string` | Extra class on the root element. |
| `style?` | `CSSProperties` | Inline style on the root element. |
