# Kanban drag and drop

> Pointer, touch and keyboard card drag within and between columns and swimlanes, per-column drag rules, column reorder, drops between boards of one drag group and drops on external elements.

Canonical: https://salmonui.dev/docs/data-visualization/kanban-drag-drop

[Kanban](https://salmonui.dev/docs/data-visualization/kanban) drag and drop is one lazy chunk,
loaded after hydration when `cardsDraggable`, `columnsReorderable`,
`dragGroup` or `dropTargets` is set.
During a drag the ghost, the drop indicator and edge auto-scroll are DOM
transients: React state changes once, on the drop.

### Live demo: Kanban swimlanes and column reorder

Live demo: https://salmonui.dev/demos/data-visualization/kanban-swimlanes

Issues in one swimlane per owner with an empty declared lane and an unassigned lane, per-lane WIP limits, drag across lanes, collapsible and reorderable columns, and an external archive drop target.

`Demo.tsx`:

```tsx
'use client';
import { useRef, useState } from 'react';
import { Kanban, KanbanColumn, type KanbanDropTarget, type KanbanKey, type KanbanSwimlanes } from '@salmonui/kanban';

interface Issue {
  id: number;
  title: string;
  state: string;
  owner?: string;
  ownerName?: string;
}

const ISSUES: readonly Issue[] = [
  {
    id: 1,
    title: 'Login rate limit',
    state: 'open',
    owner: 'ana',
    ownerName: 'Ana',
  },
  {
    id: 2,
    title: 'Export to CSV',
    state: 'doing',
    owner: 'ana',
    ownerName: 'Ana',
  },
  {
    id: 3,
    title: 'Session timeout banner',
    state: 'doing',
    owner: 'ana',
    ownerName: 'Ana',
  },
  {
    id: 4,
    title: 'Dark mode chart colors',
    state: 'open',
    owner: 'ben',
    ownerName: 'Ben',
  },
  {
    id: 5,
    title: 'Upgrade build tooling',
    state: 'doing',
    owner: 'ben',
    ownerName: 'Ben',
  },
  {
    id: 6,
    title: 'Invoice PDF layout',
    state: 'done',
    owner: 'ben',
    ownerName: 'Ben',
  },
  { id: 7, title: 'Triage new reports', state: 'open' },
];

const LANES: KanbanSwimlanes = {
  field: 'owner',
  labelField: 'ownerName',
  lanes: [
    { key: 'ana', label: 'Ana' },
    { key: 'ben', label: 'Ben' },
    { key: 'chen', label: 'Chen' },
  ],
  showEmpty: true,
  sticky: true,
  draggable: true,
};

export default function KanbanSwimlanesDemo() {
  const archive = useRef<HTMLDivElement>(null);
  const [archived, setArchived] = useState<readonly string[]>([]);
  const [order, setOrder] = useState<readonly KanbanKey[]>(['open', 'doing', 'done']);
  const [collapsed, setCollapsed] = useState<readonly KanbanKey[]>([]);

  const targets: readonly KanbanDropTarget<Issue>[] = [
    {
      ref: archive,
      accept: (cards) => cards.every((c) => c.state === 'done'),
      onDrop: (e) => setArchived((list) => [...list, ...e.cards.map((c) => c.title)]),
    },
  ];

  return (
    <div style={{ display: 'grid', gap: 12 }}>
      <Kanban
        aria-label="Issues by owner"
        data={ISSUES}
        getCardId={(i) => i.id}
        columnField="state"
        card={{ headerField: 'title' }}
        swimlanes={LANES}
        limitScope="swimlane"
        cardsDraggable
        dropTargets={targets}
        columnsReorderable
        columnOrder={order}
        onColumnOrderChange={(value) => setOrder(value)}
        collapsedColumns={collapsed}
        onCollapsedColumnsChange={(value) => setCollapsed(value)}
        height={460}
      >
        <KanbanColumn columnKey="open" header="Open" collapsible />
        <KanbanColumn columnKey="doing" header="Doing" max={1} collapsible />
        <KanbanColumn columnKey="done" header="Done" collapsible allowedTargets={['done']} />
      </Kanban>
      <div
        ref={archive}
        style={{
          padding: 12,
          border: '1px dashed var(--salmon-border-default)',
          borderRadius: 'var(--salmon-radius-lg)',
        }}
        aria-label="Archive"
        role="region"
      >
        <strong>Archive</strong> (drop Done cards here): <output>{archived.length ? archived.join(', ') : 'empty'}</output>
      </div>
      <p>
        Column order: <output>{order.join(' → ')}</output>
      </p>
    </div>
  );
}
```

## Card drag

`cardsDraggable` lets cards move
within a column and between columns with the mouse, a pen or touch. The
drag starts after a few pixels of movement, so a click still selects. Near
the top or bottom of a column, or the start or end of the board, the view
scrolls; Escape cancels. Dragging a selected card moves every selected card.

Every drop calls `onCardMove(event)` before the board changes:

| Field | Meaning |
| --- | --- |
| `cardId`, `cardIds`, `cards` | The moved cards (before the move) |
| `from`, `to` | `{ columnKey, swimlaneKey }` of the source and target cell |
| `index` | Insertion index among the other cards of the target cell |
| `source` | `'pointer'`, `'touch'` or `'keyboard'` |
| `external` | `'in'` or `'out'` for a drop between boards, else `null` |

`event.preventDefault()` cancels the move. Otherwise the board writes it:
the moved cards get the target column's value in `columnField` (the first
of its `keys`) and the target lane in `swimlanes.field`, and the board
reports a `move` change through `onCardChange`
([editing](https://salmonui.dev/docs/data-visualization/kanban-editing)). With a plain array the
board updates its own copy; with a data source the move is written through
the source, carrying the placement. To keep the cards in your own state,
cancel the move and apply it yourself:

```tsx
<Kanban
  cardsDraggable
  onCardMove={(e) => {
    e.preventDefault();
    setTasks((list) => list.map((t) => (e.cardIds.includes(t.id) ? { ...t, status: String(e.to.columnKey) } : t)));
  }}
  /* ... */
/>
```

## Keyboard drag

With `cardsDraggable`, every card is described by the drag instructions
(`dragInstructions` in `localeText`). On a focused card:

| Key | Action |
| --- | --- |
| Space | Lift the card (or the selection) |
| ArrowUp / ArrowDown | Move up or down within the cell |
| ArrowLeft / ArrowRight | Move to the previous or next column (mirrored in RTL) |
| PageUp / PageDown | Move to the previous or next swimlane (with `swimlanes.draggable`) |
| Space or Enter | Drop |
| Escape | Cancel; the card returns |
| Tab | Cancel and leave the board |

Each step is announced in the board's live region ("Picked up …", the
column, lane and position, "… dropped", "Move cancelled"). Steps skip
columns that refuse the card and collapsed columns.

## Drag rules per column

Column options restrict moves:

| Option | Default | Effect |
| --- | --- | --- |
| `draggable` | true | Cards cannot be dragged out of the column when false |
| `droppable` | true | Nothing can be dropped into the column when false |
| `allowedTargets` | every droppable column | Columns that cards of this column may move to; reordering within the column stays allowed |

A refused cell gets `data-drop-denied` instead of a drop indicator.
Collapsed columns accept no drops.

## Swimlanes

Cards move between columns of their own lane. `swimlanes.draggable`
 allows drops in
other lanes; the move then also writes the lane field. With
`limitScope="swimlane"` WIP limits are checked per lane.

## Column reorder

`columnsReorderable` adds a grip button to each column
header. Drag the grip, or focus it and press Alt+ArrowLeft / Alt+ArrowRight
(mirrored in RTL), to move the column; keyboard moves are announced
(`columnMoved`). The order is a triad of column keys:

```tsx
const [order, setOrder] = useState(['open', 'doing', 'done']);
<Kanban columnsReorderable columnOrder={order} onColumnOrderChange={(keys) => setOrder(keys)} /* ... */ />
```

Keys missing from `columnOrder` follow in definition order.

## Between boards

Boards with the same `dragGroup` accept each other's cards.
A drop calls `onCardMove` on
the receiving board with `external: 'in'` and on the source board with
`external: 'out'`; either can cancel. The source board then deletes the
cards and the receiving board adds them (a `delete` and an `add` change), so
each board's data or source stays consistent. Drops between boards use the
pointer; the keyboard drag stays within one board.

```tsx
<Kanban dragGroup="tasks" cardsDraggable data={teamA} /* ... */ />
<Kanban dragGroup="tasks" cardsDraggable data={teamB} /* ... */ />
```

## External drop targets

`dropTargets` turns elements outside the board into drop targets,
for
example a `TreeView` container, a trash area or a calendar:

```tsx
const archive = useRef<HTMLDivElement>(null);

<Kanban
  cardsDraggable
  dropTargets={[
    {
      ref: archive,
      accept: (cards) => cards.every((c) => c.status === 'done'),
      onDrop: (e) => archiveCards(e.cards, e.target),
    },
  ]}
  /* ... */
/>
<div ref={archive}>Archive</div>
```

`accept(cards)` refuses a drag (default: accepts); while an accepted drag
is over the element it carries `data-drop-target` for styling. `onDrop`
receives the cards, the element under the pointer (`target`) and the pointer position;
unless it calls `preventDefault()` the board removes the cards (a `delete`
change). A TreeView does not yet accept cards as tree items by itself; use
its container as a drop target and insert the items in your tree data.
