# Master/detail

> Expand a row into a full-width detail panel that renders anything, from a nested grid of order lines to a form or a chart.

Master/detail turns each row into an expandable record. A toggle at the start of the row opens a full-width panel below it, and you render whatever belongs there: the order lines of an order, a technician's upcoming jobs, a customer's contact history. The panel is plain DOM, so a nested Birch UI grid, a chart, a form or a framework component all work.

> **Grid Pro: Paid feature. Fully functional without a key; a license removes the watermark. Pricing: https://docs.birchui.com/getting-started/pricing.md**
>
> Master/detail requires `MasterDetailModule` from `@birchui/grid-pro`.

## A detail grid of order lines

```ts title="orders-with-lines.ts"
import { createGrid, type ColumnDef } from '@birchui/core';
import { MasterDetailModule } from '@birchui/grid-pro';

interface OrderLine {
  sku: string;
  product: string;
  quantity: number;
  unitPrice: number;
}

interface Order {
  id: string;
  customer: string;
  orderedOn: string;
  status: 'Open' | 'Shipped' | 'Invoiced';
  total: number;
  lines: OrderLine[];
}

const columns: ColumnDef<Order>[] = [
  { field: 'id', header: 'Order', width: 130 },
  { field: 'customer', flex: 1 },
  { field: 'orderedOn', header: 'Ordered', type: 'date' },
  { field: 'status' },
  { field: 'total', type: 'number' },
];

const lineColumns: ColumnDef<OrderLine>[] = [
  { field: 'sku', header: 'SKU', width: 120 },
  { field: 'product', flex: 1 },
  { field: 'quantity', type: 'number' },
  { field: 'unitPrice', header: 'Unit price', type: 'number' },
];

const api = createGrid(container, {
  columns,
  rows: orders,
  getRowId: (row) => row.id,
  modules: [MasterDetailModule],
  masterDetail: {
    height: (order) => Math.min(80 + order.lines.length * 36, 320),
    isExpandable: (order) => order.lines.length > 0,
    render(order, panel) {
      const lines = createGrid(panel, {
        columns: lineColumns,
        rows: order.lines,
        getRowId: (line) => line.sku,
        density: 'compact',
      });
      return () => lines.destroy();
    },
  },
});
```

The grid adds a narrow toggle column pinned to the left. Clicking the toggle, or pressing `Enter` or `Space` while it is focused, opens or closes the panel.

## Options

**`MasterDetailOptions`** (interface from `@birchui/core`, full reference: https://docs.birchui.com/api/core.md)

```ts
interface MasterDetailOptions<TRow = RowData>
```

| Property | Type | Description |
| --- | --- | --- |
| `render` | `(row: TRow, container: HTMLElement) => void \| (() => void)` |  |
| `height?` | `number \| ((row: TRow) => number)` |  |
| `isExpandable?` | `(row: TRow) => boolean` |  |

### `render(row, container)`

Called when a detail panel is rendered, with the row's data and an empty element to render into. Return a function to clean up, and the grid calls it when the panel is removed from the page: when the row collapses, when the panel leaves the rendered window, when the module is removed and when the grid is destroyed.

Because the rows are virtualized, a panel can be rendered more than once during a session. Keep anything that must survive, such as unsaved form input, in your own state keyed by row id, not only in the panel's DOM.

If `render` throws, the error is logged with `console.error` and the panel stays empty; the rest of the grid keeps working.

### `height`

The panel height in pixels, as a number or a function of the row. The default is 240. Detail rows take part in [variable row height](https://docs.birchui.com/grid/virtualization.md) layout, so a mix of heights scrolls smoothly. The panel does not grow with its content; choose a height and let content scroll inside it if needed.

### `isExpandable(row)`

Return `false` for rows that have no details. They get no toggle and cannot be expanded. When omitted, every row is expandable.

## Open and close panels in code

The module adds two methods to the grid API:

| Method | Purpose |
| --- | --- |
| `setDetailExpanded(rowId, expanded)` | Open or close one row's panel. |
| `isDetailExpanded(rowId)` | Whether a row's panel is open. |

```ts
api.setDetailExpanded?.('SO-10422', true);

api.on('rowClick', ({ displayRow }) => {
  if (displayRow.kind === 'data') {
    api.setDetailExpanded?.(displayRow.id, !api.isDetailExpanded?.(displayRow.id));
  }
});
```

`expandAll()` never opens detail panels; it only affects group and tree rows. Both `expandAll()` and `collapseAll()` reset all expansion state, which closes any open panels.

Detail state is stored with the grid's expansion state under a separate key per row, so opening a panel fires `rowExpanded` with a `rowId` of `detail:` followed by the row id. Filter on that prefix if you also use [row grouping](https://docs.birchui.com/grid-pro/row-grouping.md) or [tree data](https://docs.birchui.com/grid-pro/tree-data.md):

```ts
api.on('rowExpanded', ({ rowId, expanded }) => {
  if (rowId.startsWith('detail:')) analytics.track('order-details', { order: rowId.slice(7), expanded });
});
```

## Framework components in the panel

`render` hands you a plain element, so mount a component from your framework into it and unmount it in the cleanup function.

**React**

```tsx
import { createRoot } from 'react-dom/client';

const masterDetail = {
  height: 260,
  render(order: Order, panel: HTMLElement) {
    const root = createRoot(panel);
    root.render(<OrderLines order={order} />);
    return () => root.unmount();
  },
};
```

**Vue**

```ts
import { createApp } from 'vue';
import OrderLines from './OrderLines.vue';

const masterDetail = {
  height: 260,
  render(order: Order, panel: HTMLElement) {
    const app = createApp(OrderLines, { order });
    app.mount(panel);
    return () => app.unmount();
  },
};
```

**Svelte**

```ts
import { mount, unmount } from 'svelte';
import OrderLines from './OrderLines.svelte';

const masterDetail = {
  height: 260,
  render(order: Order, panel: HTMLElement) {
    const component = mount(OrderLines, { target: panel, props: { order } });
    return () => unmount(component);
  },
};
```

Keyboard input inside the panel belongs to the panel. Typing in an input or text area there does not trigger grid shortcuts such as copy, paste or range selection.

## Interaction with other features

- **Grouping and tree data.** Data rows inside groups or trees can be expanded too. Detail rows are inserted after grouping, so they appear directly under their row.
- **Sorting and filtering.** A panel stays open when its row moves; it is hidden while the row is filtered out and comes back when the row reappears.
- **Range selection, clipboard and status bar.** Detail rows are skipped, and the toggle column is never part of copied or exported data.
- **Excel export.** Detail panels are not exported; the master rows are.

## Accessibility

The toggle is a button with `aria-expanded`, `aria-controls` pointing at the panel, and an accessible name of "Show details" or "Hide details". The panel is labelled "Details". Give your panel content its own headings or labels so screen reader users know which record they are in, for example "Order SO-10422 lines".
