80Build a drag-and-drop Kanban board
mediumAbout 45 minutesCodingreactdrag-and-dropaccessibilitystate
Problem
Build a Kanban board with the native HTML Drag and Drop API.
The board state is normalised:
{
columns: [{ id: "todo", title: "To do", cardIds: ["c1", "c2"] }, …],
cards: { c1: { id: "c1", title: "Design" }, … },
}- Write a pure function
moveCard(board, cardId, toColumnId, toIndex)that returns a new board with the card removed from its column and inserted intotoColumnIdattoIndex. Moving within the same column must account for the card's own removal. - Build
<KanbanBoard initialBoard={board} onChange={fn} />:- each column is
<section data-column-id={id}>with an<h2>title and a list of cards; - each card is
<article draggable="true" data-card-id={id}>; - dropping a card on another card inserts it before that card; dropping on a column's empty area appends it;
- for keyboard and screen reader users, each card also has Move left and Move right buttons (
aria-label="Move {title} left") that move it to the end of the neighbouring column; - call
onChange(board)after every move.
- each column is
Try it yourself
Write your solution, run it, then submit to check it against the tests.
App.jsxReact + DOM
Step-by-step
Columns hold ordered card ids; cards live in a lookup object. Moving a card only edits two small id arrays, and the card objects never move.
Unlock the full solution
What's inside
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Tests
Includes 2 code listings, 5 edge cases, 4 follow-up questions.
One-time payment by UPI, card or net banking. Launch price.