# Row Drag-Drop Plugin

> Drag rows within a single grid (reorder) and across grids that share a drop zone.

The Row Drag-Drop plugin lets users rearrange rows by dragging a grip handle
or using keyboard shortcuts, and — when opted in via `dropZone` — drag rows
between separate grids. It replaces the pre-v3 `RowReorderPlugin`: every
intra-grid behaviour that plugin had is preserved.

- **Intra-grid** — drag a row up or down to reorder, or use `Alt + ↑/↓`.
- **Drag origin** — choose whether drags start from the dedicated handle
  column (`dragFrom: 'handle'`, default), from anywhere on the row
  (`dragFrom: 'row'` — handle column hidden), or from both
  (`dragFrom: 'both'`). Interactive descendants (buttons, inputs, links,
  `[contenteditable]`) inside the row are still respected and never start
  a drag.
- **Cross-grid** — set a `dropZone` to allow dragging rows to another grid
  with the same `dropZone`. Supports `move` and `copy` operations.
- **Multi-row** — when the [Selection](../selection/) plugin is loaded and
  the dragged row is part of a multi-row selection, all selected rows are
  dragged together and a count badge appears on the drag image. There is
  no `selection` config option — the behaviour is automatic.
- **Cross-window** — drag rows between separate browser windows or tabs
  on the same origin. The payload travels via `dataTransfer`, and a
  same-origin `BroadcastChannel` coordinates the source-side row removal
  for `operation: 'move'` and the source-side `row-transfer` event.

## Installation

```ts
import '@toolbox-web/grid/features/row-drag-drop';
```

## Basic Usage

Enable the feature and a drag handle column appears automatically. Users can
drag rows to new positions or use `Alt + ↑/↓` to move the focused row.

#### TypeScript

```ts
import { queryGrid } from '@toolbox-web/grid';
import '@toolbox-web/grid/features/row-drag-drop';

const grid = queryGrid('tbw-grid');
grid.gridConfig = {
  columns: [
    { field: 'priority', header: '#', type: 'number', width: 50 },
    { field: 'task', header: 'Task' },
    { field: 'status', header: 'Status' },
  ],
  features: {
    rowDragDrop: {
      dragHandlePosition: 'left',
      enableKeyboard: true,
      animation: 'flip',
    },
  },
};

// Listen for row moves (intra-grid)
grid.on('row-move', ({ fromIndex, toIndex }) => {
  console.log(`Moved row from ${fromIndex} to ${toIndex}`);
});
```

#### React

```tsx
import '@toolbox-web/grid-react/features/row-drag-drop';
import { DataGrid } from '@toolbox-web/grid-react';
import type { GridConfig } from '@toolbox-web/grid-react';

const gridConfig: GridConfig = {
  columns: [
    { field: 'priority', header: '#', type: 'number', width: 50 },
    { field: 'task', header: 'Task' },
    { field: 'status', header: 'Status' },
  ],
  features: { rowDragDrop: true },
};

function ReorderableTaskList({ tasks }) {
  return (
    <DataGrid
      rows={tasks}
      gridConfig={gridConfig}
      onRowMove={(detail) => console.log('Row moved:', detail)}
      style={{ height: '400px' }}
    />
  );
}
```

:::note[Alternative: feature prop]
You can instead pass `columns={[ … ]}` and `rowDragDrop` as individual props on `<DataGrid>`. That form is idiomatic React, but `gridConfig` keeps the configuration identical across every framework.
:::

#### Vue

```html
<script setup>
import '@toolbox-web/grid-vue/features/row-drag-drop';
import { TbwGrid } from '@toolbox-web/grid-vue';
import type { GridConfig } from '@toolbox-web/grid-vue';

const tasks = [
  { priority: 1, task: 'Review PR', status: 'Pending' },
  { priority: 2, task: 'Deploy staging', status: 'In Progress' },
];

const gridConfig: GridConfig = {
  columns: [
    { field: 'priority', header: '#', type: 'number', width: 50 },
    { field: 'task', header: 'Task' },
    { field: 'status', header: 'Status' },
  ],
  features: { rowDragDrop: true },
};
</script>

<template>
  <TbwGrid
    :rows="tasks"
    :grid-config="gridConfig"
    style="height: 400px"
    @row-move="(e) => console.log('Row moved:', e.detail)"
  />
</template>
```

:::note[Alternative: prop + light-dom columns]
You can instead bind `row-drag-drop` and declare columns with `<TbwGridColumn>` children. That form is idiomatic Vue, but `gridConfig` keeps the configuration identical across every framework.
:::

#### Angular

```typescript
import '@toolbox-web/grid-angular/features/row-drag-drop';
import { Component } from '@angular/core';
import { Grid } from '@toolbox-web/grid-angular';
import type { GridConfig } from '@toolbox-web/grid-angular';

@Component({
  selector: 'app-task-list',
  imports: [Grid],
  template: `
    <tbw-grid
      [rows]="rows"
      [gridConfig]="gridConfig"
      (rowMove)="onRowMove($event)"
      style="height: 400px; display: block;">
    </tbw-grid>
  `,
})
export class TaskListComponent {
  rows = [
    { priority: 1, task: 'Review PR', status: 'Pending' },
    { priority: 2, task: 'Deploy staging', status: 'In Progress' },
  ];

  gridConfig: GridConfig = {
    columns: [
      { field: 'priority', header: '#', type: 'number', width: 50 },
      { field: 'task', header: 'Task' },
      { field: 'status', header: 'Status' },
    ],
    features: { rowDragDrop: true },
  };

  onRowMove(detail: any) {
    console.log('Row moved:', detail);
  }
}
```

:::note[Alternative: per-feature directive]
You can instead import `GridRowDragDropDirective` and bind `[rowDragDrop]` directly on `<tbw-grid>`. That form gives a compile-time check that the feature is imported, but `gridConfig.features` above is the recommended, framework-consistent approach.
:::

## Demos

### Default Drag-Drop

```ts
// RowDragDropDefaultDemo.astro
import '@toolbox-web/grid';
import { queryGrid } from '@toolbox-web/grid';
import '@toolbox-web/grid/features/row-drag-drop';

const container = document.getElementById('row-drag-drop-default-demo');
if (container) {
  const sampleData = [
    { id: 1, name: 'Alice', department: 'Engineering', email: 'alice@example.com', priority: 1 },
    { id: 2, name: 'Bob', department: 'Marketing', email: 'bob@example.com', priority: 2 },
    { id: 3, name: 'Carol', department: 'Engineering', email: 'carol@example.com', priority: 3 },
    { id: 4, name: 'David', department: 'Sales', email: 'david@example.com', priority: 4 },
    { id: 5, name: 'Eve', department: 'Engineering', email: 'eve@example.com', priority: 5 },
  ];
  const columns = [
    { field: 'priority', header: '#', type: 'number', width: 50 },
    { field: 'name', header: 'Name' },
    { field: 'department', header: 'Department' },
    { field: 'email', header: 'Email' },
  ];

  const grid = queryGrid('tbw-grid', container)!;

  function rebuild(dragFrom = 'handle', dragHandlePosition = 'left', enableKeyboard = true, animation = 'flip') {
    grid.gridConfig = {
      columns,
      features: { rowDragDrop: { dragFrom, dragHandlePosition, enableKeyboard, animation: animation === 'false' ? false : animation } as any },
    };
    grid.rows = sampleData;
  }

  rebuild();

  container.addEventListener('control-change', ((e: CustomEvent) => {
    const v = e.detail.allValues;
    rebuild(v.dragFrom as string, v.dragHandlePosition as string, v.enableKeyboard as boolean, v.animation as string);
  }) as EventListener);
}
```

Drag the grip handle (☰) to reorder rows. Use `Alt + ↑/↓` to move the
focused row with the keyboard.

### Cancelable `row-move`

```ts
// RowDragDropCancelableEventDemo.astro
import '@toolbox-web/grid';
import { queryGrid } from '@toolbox-web/grid';

import '@toolbox-web/grid/features/row-drag-drop';

const container = document.getElementById('row-drag-drop-cancelable-event-demo');
if (container) {
  const sampleData = [
    { id: 1, name: 'Alice', department: 'Engineering', email: 'alice@example.com', priority: 1 },
    { id: 2, name: 'Bob', department: 'Marketing', email: 'bob@example.com', priority: 2 },
    { id: 3, name: 'Carol', department: 'Engineering', email: 'carol@example.com', priority: 3 },
    { id: 4, name: 'David', department: 'Sales', email: 'david@example.com', priority: 4 },
    { id: 5, name: 'Eve', department: 'Engineering', email: 'eve@example.com', priority: 5 },
  ];
  const columns = [
    { field: 'priority', header: '#', type: 'number', width: 50 },
    { field: 'name', header: 'Name' },
    { field: 'department', header: 'Department' },
    { field: 'email', header: 'Email' },
  ];

  const status = document.getElementById('row-drag-drop-cancelable-status')!;

  const grid = queryGrid('tbw-grid', container);

  grid.gridConfig = {
    columns,
    features: { rowDragDrop: true },
  };
  grid.rows = sampleData;

  // Prevent moving Bob
  grid.on('row-move', (detail, e) => {
    if (detail.row.name === 'Bob') {
      e.preventDefault();
      status.textContent = '❌ Cannot move Bob!';
      status.style.background = 'var(--tbw-color-danger-light)';
    } else {
      status.textContent = `✓ Moved ${detail.row.name} from index ${detail.fromIndex} to ${detail.toIndex}`;
      status.style.background = 'var(--tbw-color-success-light)';
    }
  });
}
```

Cancel row moves by calling `event.preventDefault()` in the `row-move`
handler.

### Cross-grid transfer

The grids below share `dropZone: 'employees'` with `operation: 'move'`.
Drag any row from the left grid to the right grid (or back).

```ts
// RowDragDropCrossGridDemo.astro
import '@toolbox-web/grid';
import { queryGrid, type ColumnConfig } from '@toolbox-web/grid';
import '@toolbox-web/grid/features/row-drag-drop';
import '@toolbox-web/grid/features/selection';

const container = document.getElementById('row-drag-drop-cross-grid-demo');
if (container) {
  const columns: ColumnConfig<any>[] = [
    { field: 'id', header: 'ID', type: 'number', width: 60 },
    { field: 'name', header: 'Name' },
    { field: 'department', header: 'Department' },
  ];

  const source = queryGrid('tbw-grid#rdd-source', container)!;
  const target = queryGrid('tbw-grid#rdd-target', container)!;

  // Enable Selection on the source grid so multi-row drag is automatic when
  // the dragged row is part of a multi-row selection. RowDragDrop has no
  // `selection` config — the behaviour is driven entirely by the presence of
  // a multi-row selection at dragstart.
  source.gridConfig = {
    columns,
    features: {
      rowDragDrop: { dropZone: 'employees', operation: 'move' },
      selection: 'row',
    },
  };
  source.rows = [
    { id: 1, name: 'Alice', department: 'Engineering' },
    { id: 2, name: 'Bob', department: 'Marketing' },
    { id: 3, name: 'Carol', department: 'Engineering' },
    { id: 4, name: 'David', department: 'Sales' },
    { id: 5, name: 'Eve', department: 'Engineering' },
  ];

  target.gridConfig = {
    columns,
    features: { rowDragDrop: { dropZone: 'employees', operation: 'move' } },
  };
  target.rows = [];
}
```

### Cross-window transfer

Grids in different browser windows (or tabs) on the same origin can also
exchange rows. The button in the demo below opens a popout window that
hosts a target grid joined to the same `dropZone`. Drag any row from the
left grid into the popout — the row is moved across windows, and the
source grid's `row-transfer` event fires once the popout confirms the drop.

```ts
// RowDragDropCrossWindowDemo.astro
import '@toolbox-web/grid';
import { queryGrid } from '@toolbox-web/grid';
import '@toolbox-web/grid/features/row-drag-drop';

const container = document.getElementById('row-drag-drop-cross-window-demo');
if (container) {
  const source = queryGrid('tbw-grid#rdd-cross-window-source', container)!;
  const status = container.querySelector('#rdd-cross-window-status') as HTMLElement;
  const btn = container.querySelector('#rdd-popout-btn') as HTMLButtonElement;

  source.gridConfig = {
    columns: [
      { field: 'id', header: 'ID', type: 'number', width: 60 },
      { field: 'name', header: 'Name' },
      { field: 'department', header: 'Department' },
    ],
    features: {
      rowDragDrop: { dropZone: 'cross-window-employees', operation: 'move' },
    },
  };
  source.rows = [
    { id: 1, name: 'Alice', department: 'Engineering' },
    { id: 2, name: 'Bob', department: 'Marketing' },
    { id: 3, name: 'Carol', department: 'Engineering' },
    { id: 4, name: 'David', department: 'Sales' },
    { id: 5, name: 'Eve', department: 'Engineering' },
  ];

  source.addEventListener('row-transfer', ((e: Event) => {
    const detail = (e as CustomEvent).detail as { rows: { name: string }[]; toGridId: string };
    status.textContent = `Sent ${detail.rows.length} row(s) to ${detail.toGridId}.`;
  }) as EventListener);

  btn.addEventListener('click', () => {
    const w = window.open(
      '/grid/row-drag-drop-popout/',
      'tbw-rdd-popout',
      'width=520,height=480,resizable=yes,scrollbars=yes',
    );
    if (!w) {
      status.textContent = 'Popup blocked. Please allow popups for this site and try again.';
    } else {
      status.textContent = 'Popout opened. Drag rows from above into the new window.';
    }
  });
}
```

The popout page itself is a tiny standalone host that registers the same
`dropZone` ([`pages/grid/row-drag-drop-popout.astro`](https://github.com/OysteinAmundsen/toolbox/blob/main/apps/docs/src/pages/grid/row-drag-drop-popout.astro)).

Under the hood the plugin sets a custom MIME type on `dataTransfer` and
opens a `BroadcastChannel('tbw-row-drag-drop')`. The target window decodes
the payload from `dataTransfer` and broadcasts a confirmation; the source
window's plugin instance listens on the same channel and — on `move` —
removes the transferred rows and emits `row-transfer` locally.

**Custom row shapes.** Rows are round-tripped through `JSON.parse(JSON.stringify(row))`
by default. If your rows contain non-JSON values (functions, `Date`, `Map`,
etc.), provide `serializeRow` / `deserializeRow` so the target reconstructs
an equivalent object:

```ts
features: {
  rowDragDrop: {
    dropZone: 'employees',
    serializeRow: (row) => ({ ...row, hiredAt: row.hiredAt.toISOString() }),
    deserializeRow: (raw) => ({ ...raw, hiredAt: new Date(raw.hiredAt) }),
  },
}
```

**Caveats.**

- Cross-window coordination requires `BroadcastChannel`; in environments
  without it (very old browsers, sandboxed iframes), only the target
  receives rows and the source is left untouched.
- Both windows must be on the same origin — `BroadcastChannel` is
  origin-scoped.
- The source's `row-drag-end` may fire with `accepted: false` before the
  remote confirmation arrives. Treat the `row-transfer` event as the
  authoritative success signal for cross-window transfers.

## Configuration Options

See [`RowDragDropConfig`](./interfaces/rowdragdropconfig/) for the full list
of options and defaults. Three of them decide the plugin's overall shape:

- **`dropZone`** — opt-in identifier for cross-grid drops. Grids sharing a value accept each
  other's rows; without it, dragging is confined to reordering within the grid.
- **`operation`** — whether the source grid keeps (`'copy'`) or loses (`'move'`) the rows after
  a successful cross-grid drop.
- **`dragFrom`** — where a drag can start. `'handle'` (default) keeps the handle column, `'row'`
  hides the handle and makes the whole row draggable, `'both'` allows either. Interactive
  descendants (`button`, `input`, `select`, `textarea`, `a[href]`, `[contenteditable]`) never
  start a drag.

Use `canDrag` and `canDrop` to veto individual drags and drops; both are called synchronously,
so keep them cheap.

## Events

| Event             | Detail                                                              | Cancelable | When                                                          |
| ----------------- | ------------------------------------------------------------------- | ---------- | ------------------------------------------------------------- |
| `row-move`        | [`RowMoveDetail`](./interfaces/rowmovedetail/)                      | Yes        | Intra-grid reorder committed (back-compat).                   |
| `row-drag-start`  | [`RowDragStartDetail`](./interfaces/rowdragstartdetail/)            | Yes        | A drag begins.                                                |
| `row-drag-end`    | [`RowDragEndDetail`](./interfaces/rowdragenddetail/)                | No         | The drag ends, regardless of whether a drop succeeded.        |
| `row-drop`        | [`RowDropDetail`](./interfaces/rowdropdetail/)                      | Yes        | Rows are dropped on this grid from another grid.              |
| `row-transfer`    | [`RowTransferDetail`](./interfaces/rowtransferdetail/)              | No         | Successful cross-grid transfer (fired on **both** grids).     |

## Keyboard Shortcuts

| Key       | Action                |
| --------- | --------------------- |
| `Alt + ↑` | Move focused row up   |
| `Alt + ↓` | Move focused row down |

`Ctrl + ↑/↓` still works but is **deprecated** — macOS reserves it for Mission Control, so it
never reaches the page there. It will be removed in the next major version.

While `enableKeyboard` is on, this deprecated alias usually takes priority over the core
`Ctrl/⌘ + ↑/↓` jump to the first/last row on Windows and Linux — plugins handle keys first.
When the row can't move (it is already at the top/bottom, or `canMoveRow` rejects the move), the
plugin leaves the key alone and core navigation runs instead, so focus jumps to the first/last row.
`⌘ + ↑/↓` on macOS is unaffected. Once the alias is removed, `Ctrl + ↑/↓` always falls through to
core navigation.

## Styling

The plugin adds these CSS classes you can customise:

| Class                                  | Description                                         |
| -------------------------------------- | --------------------------------------------------- |
| `.dg-row-drag-handle`                  | The drag handle element                             |
| `.dg-row-drag-handle:hover`            | Handle hover state                                  |
| `.data-grid-row.dragging`              | Row currently being dragged                         |
| `.data-grid-row.drop-target`           | Row being hovered as drop target                    |
| `.data-grid-row.drop-before`           | Drop indicator showing insertion above              |
| `.data-grid-row.drop-after`            | Drop indicator showing insertion below              |
| `.tbw-grid--drag-source`               | The source grid during a drag (whole-grid state)    |
| `.tbw-grid--drop-target-active`        | The target grid during a valid dragover             |
| `.tbw-grid--drop-target-rejected`      | The target grid during a `canDrop`-rejected drag    |
| `.tbw-grid--auto-scrolling`            | A grid currently auto-scrolling during a drag       |

### Custom Handle Icon

Override the default grip icon via CSS:

```css
.dg-row-drag-handle::before {
  content: '⋮⋮'; /* Double vertical ellipsis */
}
```

## Notes

- **Row identification** — currently uses processed/visual row indices
  (`data-row` attribute and `payload.rowIndices`), not `getRowId()`.
  Same-window cross-grid drops also recover live object references via the
  internal `WeakRef` registry; primitive row values fall through to the
  JSON payload.
- **Data mutation** — the plugin reorders `grid.rows` in place. The
  `row-move` event provides the updated array.
- **Virtualization** — works with virtualized grids; only visible rows are
  rendered but logical indices are preserved.
- **Keyboard debounce** — rapid keyboard moves are debounced (150 ms) to
  prevent excessive rerenders.
- **Drag image** — when a drag starts the plugin clones the source row and
  uses it as the HTML5 drag image so the user sees the full row content
  follow the cursor (instead of just the handle cell). The browser applies
  its standard translucency to the snapshot — this is a native effect that
  cannot be disabled.
- **Cross-window fallback** — when the live row reference cannot be
  recovered from the same-window registry (e.g. drop into a different
  window), the row payload is decoded from `dataTransfer` JSON via
  `serializeRow`/`deserializeRow`. The source window completes its half
  of the transfer (row removal on `move`, `row-transfer` emit) when it
  receives the confirmation message on the `tbw-row-drag-drop`
  `BroadcastChannel`.

## Migration from `RowReorderPlugin`

`RowReorderPlugin` and the `reorderRows` feature key were removed in v3. To
migrate:

```diff
- import { RowReorderPlugin } from '@toolbox-web/grid/plugins/reorder-rows';
+ import { RowDragDropPlugin } from '@toolbox-web/grid/plugins/row-drag-drop';

- new RowReorderPlugin(cfg);
+ new RowDragDropPlugin(cfg);
```

The legacy `canMove` callback continues to work — it is mapped internally to
`canDrop` with a synthesised intra-grid payload.

## Accessibility

The plugin satisfies [WCAG 2.2 SC 2.5.7 Dragging Movements](https://www.w3.org/WAI/WCAG22/Understanding/dragging-movements.html): every row move — within the grid **and** across grids — can also be made with a single pointer that never drags.

- **Tap the handle you would have dragged** — pressing and releasing the drag handle without moving is a tap, not a drag. HTML5 drag-and-drop only fires `dragstart` on a real drag, so a plain click on the handle means the pointer user could not (or chose not to) drag, and the plugin opens a menu of click-only actions instead.
- **Intra-grid moves** — the menu offers **Move up**, **Move down**, **Move to top** and **Move to bottom**. Entries that would run off the end of the list are rendered disabled rather than hidden.
- **Cross-grid transfers** — every other grid sharing the same `dropZone` gets its own **Send to _grid_** entry (**Copy to _grid_** when `operation: 'copy'`). The click path fires the same `row-drag-start`, `row-drop` and `row-transfer` events, and honours the same `canDrop` vetoes, as a real drag — so nothing has to be re-implemented to stay conformant.
- **No reserved chrome** — the alternative reuses the existing drag handle, so it costs no extra column width and stays out of the way until it is asked for. SC 2.5.7 requires the alternative to _exist_ and be pointer-operable, not to be permanently on screen.
- **Handle semantics** — the handle is `role="button"` with the accessible name _"Drag to reorder, or activate for move options"_, so its dual purpose is announced. The menu itself is a `role="group"` of plain buttons; focus moves to the first enabled entry on open, <kbd>Escape</kbd> closes it, and moving focus outside dismisses it.
- **Keyboard** — <kbd>Alt</kbd> + <kbd>↑</kbd> / <kbd>↓</kbd> move the focused row, satisfying [SC 2.1.1 Keyboard](https://www.w3.org/WAI/WCAG22/Understanding/keyboard.html). Per the W3C, a keyboard equivalent alone does **not** satisfy SC 2.5.7 — that is why the click path exists as well.

See [Pointer Alternatives to Dragging](https://toolboxjs.com/grid/guides/accessibility.md#pointer-alternatives-to-dragging) for how this fits the grid-wide `a11y.dragAlternatives` setting.

## See Also

- **[Column Reorder](../reorder-columns/)** — Drag-to-reorder columns
- **[Selection](../selection/)** — Row and cell selection (drives multi-row drag)
