# Context menu

> The built-in cell context menu, how to add, replace or remove items, and how modules contribute their own.

Right-click a cell, or press <kbd>Shift</kbd>+<kbd>F10</kbd> or the context menu key on a focused cell, to open the grid's cell menu. By default it offers:

| Item | Result |
| --- | --- |
| **Copy cell** | Copies the cell's formatted value |
| **Copy row** | Copies the row as tab-separated text; when the row is part of a multi-row selection, every selected row is copied (**Copy selected rows**) |
| **Copy with headers** | The same, with a header line first |
| **Export CSV** | Calls `downloadCsv()` |

Copied text is tab-separated, so it pastes cleanly into Excel, Google Sheets or the [Birch UI Spreadsheet](https://docs.birchui.com/spreadsheet/overview.md). Grid Pro modules add their own items, such as copying a range.

## Changing the items

`contextMenu` takes `true` (the default), `false`, or a function that receives the cell and the default items and returns the items to show:

```ts title="orders-grid.ts"
import { createGrid, type MenuItem } from '@birchui/core';

const grid = createGrid<Order>(el, {
  columns,
  rows: orders,
  getRowId: (order) => order.id,
  contextMenu: (ctx, defaults) => {
    const order = ctx.row;
    if (!order) return defaults;
    const items: MenuItem[] = [
      { label: 'Open order', action: () => openOrder(order.id) },
      { label: 'Mark as shipped', disabled: order.status !== 'Pending', action: () => markShipped(order.id) },
      { label: 'Flag for review', checked: order.flagged, action: () => toggleFlag(order.id) },
    ];
    return [...items, { ...defaults[0], separatorBefore: true }, ...defaults.slice(1)];
  },
});
```

Return an empty array to show no menu for that cell; the browser's own menu then stays out of the way only when you call `preventDefault` yourself, so prefer `contextMenu: false` to restore the native menu everywhere.

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

```ts
interface MenuItem
```

| Property | Type | Description |
| --- | --- | --- |
| `label` | `string` |  |
| `action` | `() => void` |  |
| `checked?` | `boolean` |  |
| `disabled?` | `boolean` |  |
| `separatorBefore?` | `boolean` |  |

## Behaviour

- The menu does not open on full-width rows (group rows rendered full width, master/detail panels) or while a cell is being edited; the editor keeps the browser's menu for spelling suggestions and paste.
- Opening the menu focuses the cell it was opened on, and closing it returns focus there.
- Items are buttons in a `menu` with arrow-key navigation. <kbd>Escape</kbd> closes the menu.
- User callbacks run inside a try/catch, so an error in an `action` is reported with `console.error` and the grid keeps working.

## The column menu

Every header has a separate column menu (the button at the right of the header, or <kbd>Alt</kbd>+<kbd>Arrow Down</kbd> on a focused header). It holds sort, pin, autosize, hide and show items, and **Filter…** for filterable columns, which opens the [filter popup](https://docs.birchui.com/grid/filtering.md#filter-popup).

## From a module

Modules contribute cell menu items with `internals.addContextMenuItems((ctx) => items)` and column menu items with `internals.addColumnMenuItems((column) => items)`. Both return a function that removes the provider. See [Modules](https://docs.birchui.com/grid/modules.md).

## Related

- [Keyboard and accessibility](https://docs.birchui.com/grid/accessibility.md)
- [CSV export](https://docs.birchui.com/grid/csv-export.md)
- [Clipboard](https://docs.birchui.com/grid-pro/clipboard.md) in Grid Pro
