Skip to the page

Trees, comments and drag and drop · senior

Drag and drop and a Kanban board

A Kanban board stores columns as ordered lists of card ids, moves cards with one pure moveCard function, implements dragging with HTML5 drag events or pointer events (better on touch), shows where the card will drop, offers a keyboard way to move cards with announcements, and persists the result.

The notes

What you must know

  • State: { columns: { [colId]: { title, cardIds } }, cards: { [id]: card } }, plus column order.
  • moveCard(board, cardId, toCol, toIndex): remove from the source list, insert at toIndex in the target (index measured after removal), immutably.
  • HTML5 DnD: draggable='true'; dragstart → dataTransfer.setData('text/plain', id); dragover → preventDefault() (otherwise drop never fires); drop → read the id and move. Data is readable only in dragstart and drop.
  • HTML5 DnD is weak on touch screens and hard to style; pointer events (pointerdown + setPointerCapture + pointermove + pointerup) give full control and work on touch.
  • Drop position: compare the pointer's Y with each card's vertical midpoint; show a placeholder/indicator line.
  • Keyboard alternative: 'Move to…' buttons or pick up with Space, move with arrows, drop with Space, cancel with Escape; announce each step in an aria-live region.
  • Persist to localStorage (or the server with an optimistic update and rollback); fractional ranks on the server so one move updates one card.
  • Performance: memoise columns and cards so a drag doesn't re-render the whole board; virtualise huge columns.

The red pen

Common traps: mistake → fix

  • Mistake: Forget preventDefault in dragover

    Fix: drop never fires.

  • Mistake: Same-column move with the index measured before removal

    Fix: off-by-one when moving down.

  • Mistake: Mouse-only dragging

    Fix: keyboard and screen-reader users are stuck.

  • Mistake: Re-render every card on every pointermove

    Fix: jank.

Easy to hard

Questions interviewers ask, easy to hard

  1. Build a Kanban board with three columns and drag and drop.
  2. Reorder within a column. Watch the index when moving down.
  3. Make it work on touch devices.
  4. Add a keyboard alternative.
  5. Persist and sync with a server; the server rejects a move.

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview