Kanban
Kanban is a task board. Cards are dragged within a column and from a column to another one, with the mouse, a finger or the keyboard, with work in progress limits, drop rules, collapsible and reorderable columns, and every move announced to screen readers.
Import #
import { Kanban } from '@exhaustive-ui/core/kanban';Basic #
Kanban displays columns, each with an id, a title and its items. Drag a card to reorder it in its column or to move it to another column, with the mouse, a finger or the keyboard. The board updates columns with new arrays after every move, so bind it two-way with [(columns)]. dataKey names the field that identifies a card, and itemLabel the field displayed by the default card, title by default.
Template #
The card template replaces the content of a card and receives the card as the implicit variable, with its column, its index and a grabbed flag, true while the card is picked up with the keyboard. The columnHeader template replaces the title and the count of a column, columnFooter adds content at its bottom and empty replaces the message of an empty column; they receive the column, its count, its limit and an overLimit flag.
A card is a button that picks up the card: keep the content of the template static, without links or buttons, and open the details of a card with onCardClick.
Limits and Rules #
The limit of a column is its work in progress limit: the header shows the count as "2 / 3", the count turns orange when the column is full and red when it holds more cards than its limit, and the column refuses the cards dropped beyond it. While a card is dragged over a column that refuses it, the column gets a dashed red border and screen readers hear the reason. A disabled column neither gives nor takes cards.
canDrop decides any other rule. It receives the card, the column it comes from and the column it would go to, and returns false to refuse the move; moves within a column are always allowed. Here a card can only reach Done from Review, and the Archived column is disabled.
Add Card #
showAddButton adds an "Add card" button at the bottom of every column. It emits onAddCard with the column, and the application creates the card, often after a dialog; set the new columns to display it. The button is disabled in a disabled column and in a column that reached its limit.
Collapsible Columns #
collapsible adds a button to the header of every column that folds it into a narrow strip, as tall as the board, with its count and its title written vertically. A click anywhere on the strip unfolds it, and its button stays the way to do it with the keyboard. The board sets the collapsed property of the column and emits onColumnToggle; set collapsed in the columns to start with a folded column. A collapsed column does not take dropped cards, and the keyboard moves skip it.
Column Reorder #
reorderColumns adds a drag handle to the header of every column. Drag it to move the column, or focus it and use Space or Enter, the left and right arrow keys, and Space or Enter again. The board emits onColumnReorder with the column, its previous and its new index once the columns are updated.
Order: To do, In progress, Blocked, Done
Scroll #
When the columns do not fit, they shrink to their minimum width, then the board scrolls horizontally, with a thin scrollbar, and fades out the edges that hide columns. scrollHeight gives the board a height: the cards of each column then scroll under its header, which stays visible. While a card is dragged near an edge, the board and the column scroll. On small screens a column takes most of the width and the board snaps from a column to the next one.
Saving Moves #
onCardMove is emitted once a card is dropped, with the pointer or the keyboard, after the columns are updated. It holds the item, the fromColumn and toColumn as they are after the move, the fromIndex and toIndex, and all the columns: send the change to the server from there. A keyboard move only emits when the card is dropped, not at every arrow key, and Escape emits nothing.
- Move a card to see the request.
With a server, keep the previous columns to put the card back when the request fails.
private http = inject(HttpClient);
columns = signal<KanbanColumn<Task>[]>(columnsFromTheServer);
private saved = this.columns();
onCardMove({ item, toColumn, toIndex, columns }: KanbanCardMoveEvent<Task>) {
const previous = this.saved;
this.http.patch(`/api/tasks/${item.id}`, { status: toColumn.id, position: toIndex }).subscribe({
next: () => (this.saved = columns),
error: () => this.columns.set(previous)
});
}Accessibility #
Screen Reader
Every column is a group named by its title, rather than a landmark, so that two boards on a page never repeat landmarks such as "To do". It holds a list whose label also gives the count, such as "In progress, 2 of 3 cards", or a group with the same label when the column is empty. Each card is a list item holding a focusable element with the button role, described with aria-describedby by the instructions to move it, and with aria-pressed true while it is picked up. The count of a column reads "2 of 3 cards" instead of "2 / 3", and "over the limit" when it is exceeded.
A card can be moved with the keyboard alone, and an assertive live region announces each step: "Picked up Task A, position 2 of 5 in To do", "Moved to In progress, position 1 of 3", "Dropped Task A in In progress, position 1 of 3" or "Cancelled, Task A returned to To do, position 2 of 5". A refused move is announced with its reason, such as "Review is full, 1 of 1 cards", and so is a full column a card is dragged over with the pointer. The column drag handles, the collapse buttons and the add card buttons name their column, for example "Add card to Done". Name the board with ariaLabel or ariaLabelledBy, and translate every text with labels.
<h2 id="sprint">Sprint 42</h2>
<p-kanban [(columns)]="columns" ariaLabelledBy="sprint" [labels]="{ addCard: 'Ajouter une carte', empty: 'Aucune carte' }" />Card Keyboard Support
| Key | Function |
|---|---|
| tab | Moves focus to the next card, column button or add card button. Leaving a picked up card cancels its move. |
| space / enter | Picks up the focused card, or drops the picked up card where it is. |
| up arrow / down arrow | Moves the picked up card one position up or down in its column. Without a picked up card, moves focus to the previous or next card. |
| left arrow / right arrow | Moves the picked up card to the previous or next column, skipping collapsed columns, unless the column refuses it. Without a picked up card, moves focus to the card at the same position in the previous or next column. Reversed in right-to-left layouts. |
| home / end | Moves the picked up card to the top or the bottom of its column. |
| escape | Cancels the move and puts the card back where it was. |
Column Handle Keyboard Support
| Key | Function |
|---|---|
| space / enter | Picks up the column, or drops the picked up column where it is. |
| left arrow / right arrow | Moves the picked up column one position to the left or the right. |
| home / end | Moves the picked up column to the first or the last position. |
| escape | Cancels the move and puts the column back where it was. |
The changes are only emitted when the card or the column is dropped, so a cancelled keyboard move never reaches onCardMove or onColumnReorder.