# Undo and redo

> Record cell edits, pastes and fills, undo them with Ctrl+Z, and keep your save handler in sync with undo and redo.

With `undoRedo` on, the grid records every change that fires `cellValueChanged`: inline edits, `setValue()` calls, and paste and fill from Grid Pro. Undo is part of the MIT grid in `@birchui/core`.

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

const grid = createGrid<Order>(el, {
  columns,
  rows: orders,
  getRowId: (order) => order.id,
  undoRedo: { limit: 50 },
});
```

`undoRedo: true` keeps the last 100 steps. `{ limit }` sets another positive whole number; an invalid limit logs one warning and falls back to 100. Setting `undoRedo` to `false` with `update()` clears the history.

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

```ts
interface UndoRedoOptions
```

| Property | Type | Description |
| --- | --- | --- |
| `limit?` | `number` |  |

## Keys and methods

| Keys (Windows, Linux) | macOS | Action |
| --- | --- | --- |
| <kbd>Ctrl</kbd>+<kbd>Z</kbd> | <kbd>⌘</kbd>+<kbd>Z</kbd> | Undo |
| <kbd>Ctrl</kbd>+<kbd>Y</kbd> or <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Z</kbd> | <kbd>⌘</kbd>+<kbd>Shift</kbd>+<kbd>Z</kbd> | Redo |

The keys work while the grid has focus and no cell is being edited; inside an editor they belong to the text box. After undo or redo, focus moves to the restored cell.

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

```ts
interface GridApi<TRow = RowData> extends Widget<GridOptions<TRow>, GridEvents<TRow>>, GridFeatureApi<TRow>
```

| Property | Type | Description |
| --- | --- | --- |
| `undo` | `() => boolean` |  |
| `redo` | `() => boolean` |  |
| `canUndo` | `() => boolean` |  |
| `canRedo` | `() => boolean` |  |
| `clearHistory` | `() => void` |  |

```ts
undoButton.addEventListener('click', () => grid.undo());
redoButton.addEventListener('click', () => grid.redo());

grid.on('cellValueChanged', () => {
  undoButton.disabled = !grid.canUndo();
  redoButton.disabled = !grid.canRedo();
});
```

## Steps

Writes made in the same task form one step. Pasting a 10 by 4 range, dragging the fill handle, or a listener that writes three cells in one go are each undone at once.

## Saving changes

Undo and redo write through `setValue()`, so `cellValueChanged` fires with `source: 'undo'` or `source: 'redo'`. A save handler written for edits keeps working:

```ts
grid.on('cellValueChanged', ({ row, colId, newValue, source }) => {
  void api.patchOrder(row.id, { [colId]: newValue }, { reason: source });
});
```

Writes made by undo and redo, and writes your listeners make in response to them, are not recorded again.

## Limits

- Rows are matched by `getRowId`. Give the grid one; changes to rows that have since been removed are skipped.
- `setRows()` keeps the history. Call `clearHistory()` when you load a different dataset, or after a successful save if users should not undo past it.
- Only cell values are recorded. Sorting, filtering, column moves and row transactions are not undoable; use [saved state](https://docs.birchui.com/grid/saving-state.md) for view changes.

## Related

- [Editing](https://docs.birchui.com/grid/editing.md)
- [Clipboard](https://docs.birchui.com/grid-pro/clipboard.md) and [fill handle](https://docs.birchui.com/grid-pro/fill-handle.md)
- [Events](https://docs.birchui.com/grid/events.md)
