# Tree View items and templates

> Icons, links with router support, tooltips and attributes per item, and renderItem templates that receive the item state, all pure and called only for rendered rows.

Canonical: https://salmonui.dev/docs/navigation/tree-view-templates

Every per-item customization is a pure function called during render, only
for the rows that are rendered (the virtual window). None of them mutates
the DOM, so no per-row render event is needed and they stay cheap
on a 100,000-item tree.

### Live demo: Tree View items and templates

Live demo: https://salmonui.dev/demos/navigation/tree-view-templates

SVG icons per item, link items that follow on a click anywhere on the row, tooltips through getItemProps and a renderItem template with unread badges.

`Demo.tsx`:

```tsx
'use client';
import { Badge } from '@salmonui/notifications/badge';
import { TreeView } from '@salmonui/navigation/tree-view';

// Placeholder links target this demo's own title, which exists on both the
// docs page and the demo page, so no link dangles in the static export.
const DEMO_ANCHOR = '#demo-tree-view-templates-title';

interface Page {
  id: string;
  title: string;
  href?: string;
  unread?: number;
  children?: Page[];
}

const pages: Page[] = [
  {
    id: 'guide',
    title: 'Guide',
    children: [
      { id: 'install', title: 'Installation', href: DEMO_ANCHOR },
      { id: 'theming', title: 'Theming', href: DEMO_ANCHOR, unread: 2 },
    ],
  },
  { id: 'inbox', title: 'Inbox', unread: 12, children: [{ id: 'today', title: 'Today', unread: 3 }] },
  { id: 'changelog', title: 'Changelog', href: DEMO_ANCHOR },
];

const folder = (
  <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
    <path d="M1 3h5l2 2h7v8H1z" />
  </svg>
);
const file = (
  <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor">
    <path d="M3 1.5h6l4 4v9H3z" />
  </svg>
);

export default function TreeViewTemplatesDemo() {
  return (
    <TreeView
      aria-label="Pages"
      data={pages}
      getItemId={(p) => p.id}
      getItemLabel={(p) => p.title}
      getChildren={(p) => p.children}
      getItemIcon={(p) => (p.children ? folder : file)}
      getItemHref={(p) => p.href}
      getItemProps={(p) => ({ title: p.href ? `Open ${p.title}` : p.title })}
      renderItem={(ctx) =>
        ctx.item.unread ? (
          <span style={{ display: 'inline-flex', gap: 8, alignItems: 'center' }}>
            {ctx.label}
            <Badge shape="pill" tone="primary" variant="solid">
              {ctx.item.unread}
            </Badge>
          </span>
        ) : undefined
      }
      expanded={['guide', 'inbox']}
      height={220}
      style={{ maxWidth: 360 }}
    />
  );
}
```

## Icons

`getItemIcon(item)` returns a `ReactNode` shown before the label, such as an
icon or an image. It is `aria-hidden`; the
label stays the accessible name.

```tsx
<TreeView getItemIcon={(f) => (f.children ? <FolderIcon /> : <FileIcon />)} /* ... */ />
```

## Links

`getItemHref(item)` renders the label as a link. A
click anywhere on the row follows it, and so does Enter. The link has
`tabindex="-1"`: the row keeps the single tab stop.

For a router link, `renderLink(props, item)` receives the props to spread
(`href`, `className`, `tabIndex`, `children`):

```tsx
<TreeView
  getItemHref={(page) => `/docs/${page.slug}`}
  renderLink={(props) => <Link {...props} to={props.href} />}
  /* ... */
/>
```

## Tooltips and attributes

`getItemProps(item, context)` returns extra props for a rendered row:
`title` for a tooltip, `className`, `style`, `data-*` and other
attributes.
ARIA attributes and handlers of the row are owned by the tree.

```tsx
<TreeView getItemProps={(f, { level }) => ({ title: f.path, 'data-kind': f.kind, className: level === 1 ? 'top' : undefined })} /* ... */ />
```

## Templates

`renderItem(context)` replaces the label content. The context is `{ item, id, level, label, expanded, selected,
checked, disabled, loading, error, retry }`; `checked` is `true`, `false` or
`'mixed'`, and `error` / `retry` describe a failed lazy load.

```tsx
<TreeView
  renderItem={({ item, label, error, retry }) =>
    error ? (
      <span>
        {label} <button onClick={retry}>Try again</button>
      </span>
    ) : (
      <span>
        {label} <Badge>{item.count}</Badge>
      </span>
    )
  }
  /* ... */
/>
```

The twisty, the checkbox, the icon and the loading status stay outside the
template, so keyboard and ARIA behavior do not depend on it.

All of these run on the server too: icons, links, attributes and templates
are part of the server-rendered first window, so they must be pure (no
`window`, no random ids) to hydrate without warnings.

## API reference

`getItemIcon`, `getItemHref`, `renderLink`, `getItemProps`, `renderItem`,
`TreeViewItemContext` and `TreeViewLinkProps` are in the
[`@salmonui/navigation/tree-view` reference](https://salmonui.dev/docs/reference/navigation/tree-view);
imports and styles are on the [Tree View](https://salmonui.dev/docs/navigation/tree-view) page.
