Build a Drag and Drop Kanban Board in React with Framer Motion
2026-11-17
A kanban board shows work as cards in columns such as Not started, Processing, Success and Failed. The hard part is not the layout. It is making drag and drop feel smooth, work with a keyboard and stay in sync with your data. This guide explains the approach used by the Depth UI Kanban board, with the key code, and needs no drag-and-drop library.
Model the data as one flat list
Keep every card in a single array, each with a columnId. A card's position in the array is its position in its column. This makes moves a pure function, easy to test and easy to hand to a server.
type Card = { id: string; columnId: string; title: string };
function moveCard(cards: Card[], id: string, to: string, index: number): Card[] {
const card = cards.find((c) => c.id === id);
if (!card) return cards;
const rest = cards.filter((c) => c.id !== id);
const col = rest.filter((c) => c.columnId === to);
const moved = { ...card, columnId: to };
const at =
index >= col.length
? col.length ? rest.indexOf(col[col.length - 1]) + 1 : rest.length
: rest.indexOf(col[index]);
return [...rest.slice(0, at), moved, ...rest.slice(at)];
}Drag with Framer Motion
Give each card drag, dragSnapToOrigin and dragMomentum={false}, and a layoutId so it animates smoothly when it lands in a new column. Use useDragControls with dragListener={false} so you decide what starts a drag: the whole card for a mouse, but only a grip handle on touch, which lets the page keep scrolling.
const controls = useDragControls();
<motion.li
layoutId={card.id}
drag
dragControls={controls}
dragListener={false}
dragSnapToOrigin
dragMomentum={false}
whileDrag={{ scale: 1.04, rotate: 2.5, zIndex: 50 }}
onPointerDown={(e) => e.pointerType !== "touch" && controls.start(e)}
onDrag={(_, info) => hover(info.point.x - scrollX, info.point.y - scrollY)}
onDragEnd={drop}
/>Find the drop target
On every drag event, ask the browser what is under the pointer. The dragged card is itself under the pointer, so filter it out, then find the nearest column and count how many cards sit above the pointer to get the insert position.
const els = document
.elementsFromPoint(x, y)
.filter((e) => !e.closest(`[data-card-id="${draggedId}"]`));
const column = els.map((e) => e.closest<HTMLElement>("[data-column]")).find(Boolean);
let index = 0;
for (const row of column.querySelectorAll<HTMLElement>("[data-card-id]")) {
if (row.dataset.cardId === draggedId) continue;
const b = row.getBoundingClientRect();
if (y > b.top + b.height / 2) index++;
}Only update React state when the target column or index actually changes, and highlight the column with a thin insertion line. Re-rendering on every pixel is what makes naive boards laggy.
Make it work without a mouse
Drag and drop alone fails keyboard and screen reader users, and it is easy to fix. Give each card a focusable grip button. Space picks the card up, the arrow keys move it between columns or up and down, Space drops it and Escape puts it back. Announce each step in an aria-live region, for example "Moved Design the pricing page to Processing, position 2 of 4".
Pitfalls
- A card that moves to another column is a new element. Move focus back to its grip after a keyboard move or the keyboard user loses their place.
- Do not put
overflow: hiddenon columns. It clips the card while it is dragged across them. - Let the board scroll sideways on small screens and nudge it when the pointer nears an edge during a drag.
Use it as is
If you want a finished version, the Kanban board is a single copy-paste file with all of the above, controlled or uncontrolled state and a props table. The Reorder list covers simple one-column sorting, and the Dropzone handles dragging files in.