Skip to content
Tutorials Center
My learning

Topics

Lesson 76 of 87 ยท Projects

Project: Kanban Board

Build a Kanban board in Vue 3: normalized card state, computed columns, a pure moveCard function you can test alone, then native HTML drag and drop with keyboard buttons.

A Kanban board is a set of columns, usually "To do", "In progress" and "Done", with cards that travel from left to right as work gets finished. It is a great practice project because it looks simple and hides a real design question: how do you store data that appears in several places at once? A card belongs to exactly one column, a column shows many cards, and every action (add, move, delete) touches the same shared list.

You will build the board in three stages. First you render columns from one flat list and move cards with buttons. Then you extract the trickiest rule, "move this card before that one", into a pure function and run it on its own. Finally you add drag and drop with the browser's built-in events, keeping the buttons so the board still works from a keyboard. The building blocks are list rendering, computed properties, events, components and a shared store.

Choosing the data shape

There are two natural ways to store a board.

  1. Nested: each column owns an array of cards. Rendering is trivial, but moving a card means removing it from one nested array and inserting it into another, and finding a card by id means searching every column.
  2. Flat (normalized): one array of cards, and each card says which column it is in. Finding and moving a card only touches one object. The order inside a column is simply the order of its cards in the array.

We choose the flat shape and derive the nested view with a computed property. This is the same advice as in the todo and cart projects: store the minimum, calculate the rest.

flowchart LR
    C["cards (state)<br/>id, title, column"] --> V["columns (computed)<br/>title, its cards, over limit?"]
    V --> T["Template<br/>one list per column"]
    T -- "move, add, remove" --> A["Actions"]
    A --> C

The flat list is the only state. The columns that the template shows are filtered views of it.

Stage 1: columns and button moves

Start without any dragging. The page needs the cards, a computed list of columns, an "add card" form per column and two buttons per card (move left and move right). The buttons are not a stop-gap: drag and drop alone is not usable with a keyboard or a screen reader, so a good board always keeps a button alternative.

Stage 1: columns and button moves

Move a card from "To do" into "In progress" twice and the column heading turns red: the work-in-progress limit of 2 is exceeded. That warning is the computed property overLimit, derived from the card list.

Two things to notice. moveBy only changes card.column; no array is touched, and the card appears in the other list because columns is recomputed. And the add form is a plain <form @submit.prevent> with one input, so pressing Enter adds the card.

The limitation is ordering. A moved card always lands wherever it was in the flat list, which is rarely where the user wants it. To place a card before another card, we need a real move operation.

Stage 2: a pure moveCard function

Dragging will need a rule such as "drop this card into that column, just before that other card (or at the end if none is given)". Rules like this are easiest to get right when they are pure functions: they take the old list and return a new list, with no Vue, DOM or events involved. Then you can test every case on its own.

The algorithm works on the flat list:

  1. Find the card. If it does not exist, or the user dropped it onto itself, return the list unchanged.
  2. Remove it from the list.
  3. Give it the new column.
  4. Insert it before the target card, or at the end if there is no target (or the target is not found).

Because the list order is the order inside each column, inserting before a target card in the flat list puts the card right above it in that column.

Stage 2: testing moveCard on its own

Each block shows the whole board after one operation, all computed from the same starting list, which also proves that moveCard does not modify its input (the last line prints true). This is exactly the shape of a unit test: call the function, compare the result (testing Vue apps).

Note the cardId === beforeId guard. Without it, dropping a card on itself would remove it and then fail to find the target, silently moving it to the end of its column.

Stage 3: drag and drop with native events

The browser already implements drag and drop. You need to know four events and one rule:

Event Fires on What you do
dragstart the dragged card remember which card; call dataTransfer.setData(...) (Firefox refuses to drag without it)
dragover the element under the pointer call preventDefault(), which is how you say "dropping here is allowed"
drop the target read what was dragged and call the move action
dragend the dragged card clean up, whether or not the drop succeeded

The rule: an element is draggable only with the attribute draggable="true", and it becomes a valid drop target only if its dragover handler cancels the event. Vue's .prevent modifier does that in the template (event handling).

sequenceDiagram
    participant Card
    participant Target as Column or card
    participant Store
    Card->>Store: dragstart: startDrag(id)
    Target->>Target: dragover: preventDefault, show highlight
    Target->>Store: drop: move(id, column, beforeId)
    Card->>Store: dragend: endDrag()

The store remembers the dragged card between dragstart and drop. The target only needs to say where the card was dropped.

Design decisions for the finished board:

  • The store holds the cards, the id being dragged and a short status message. A visually hidden live region reads it out after each move, so assistive technology users hear what the buttons did too.
  • A column accepts drops on its whole body (append at the end), and each card accepts drops too (insert before that card). The card's drop handler uses .stop so the column does not also receive the event.
  • The dragleave event fires whenever the pointer moves onto a child element, which would make the highlight flicker. A small helper ignores it unless the pointer really left the element.
  • The buttons from stage 1 stay, calling the same move action.
Stage 3: the complete board with drag and drop

Drag "Add dragging" into "In progress" and drop it between the two cards: the blue line above a card shows where it will land. Drop it on the empty space of a column and it goes to the end. Try the arrow buttons too; they use the same move action. Dragging a third card into "In progress" makes the red limit warning appear.

Review what each part owns:

  • The store owns all board state and all rules (moveCard, status messages, the drag id). It does not know about the DOM.
  • BoardColumn owns only "is something being dragged over me?" and the add-card draft. Neither matters to anyone else, so neither is in the store.
  • BoardCard owns only its own insertion marker.

Native drag and drop has limits: it is awkward on touch screens and gives little control over the drag preview. Libraries add pointer-based dragging for those cases. They are worth it for a production board, but the native API is enough to learn the concepts, and the state design you built here (flat list, pure moveCard, derived columns) would carry over unchanged.

Keep the buttons. A drag-only interface excludes people who use a keyboard, a switch device or a screen reader, and it is also the fallback on browsers where touch dragging misbehaves.

Ideas to extend it

  • Edit a card's title in place with a double-click, as in the todo project.
  • Add a priority field and a computed sort inside each column (sort a copy of the array).
  • Make the columns configurable: let the user add and rename columns, which turns COLUMNS into state.
  • Persist the board with a deep watch (read-only in the sandbox, which has no storage).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is the flat list of cards (each with a column field) a good state shape for a Kanban board?

    With a flat list there is one place to find or change a card. The nested per-column view is derived, so it can never disagree with the data.
  2. 2. What must a dragover handler do for an element to be a valid drop target?

    By default the browser treats most elements as not accepting drops. Cancelling dragover (in Vue, @dragover.prevent) signals that dropping here is allowed.
  3. 3. Why does the card's drop handler use @drop.prevent.stop?

    Drop events bubble. Without .stop, the drop on a card would reach the column handler as well, which would move the card to the end of the column and undo the insertion before the target card.
  4. 4. Why is moveCard written as a pure function that returns a new list?

    A pure function depends only on its arguments and has no side effects, so every case can be checked by calling it. The store then assigns the returned list to the ref.
  5. 5. Why keep arrow buttons next to drag and drop?

    Native dragging is a pointer interaction. Buttons that call the same action keep the board usable without a mouse, and they also help on touch devices.

Summary

  • Store related items in one flat list and derive per-group views with computed; the order of the list is the order inside each group.
  • Put tricky rules in pure functions (moveCard) that return new data; they are easy to test and to reuse.
  • Native drag and drop uses draggable="true", dragstart, a cancelled dragover, drop and dragend; Vue's .prevent and .stop modifiers keep the handlers short.
  • Keep drag-specific UI state (hover highlight, insertion marker) inside the component; keep shared state and rules in the store.
  • Always provide buttons and a live status message so the board works without a mouse.

Next: Project: Notes App with Routing.