Kanban
A recipe, not a package namespace. A board is a row of columns, each an ordered list of Cards. A card's grip drags it within its column or to another one through useReorder and its useReorderGroup, and its Menu moves it up, down or to another column, for the keyboard, screen readers and anyone who can't drag. Every move is announced with useAnnouncer. What the columns are and where the board is saved are up to your product. There is no Kanban.* export.
On this page
Drag a card by its grip within its column or into another one, or open its menu to move it.
import {
Badge,
Button,
Card,
categoryTone,
DensityProvider,
Menu,
ScrollArea,
Stack,
Text,
useAnnouncer,
useFocusHandoff,
useReorder,
useReorderGroup,
type ReorderGroup,
type UseReorderHandleProps,
type UseReorderItemProps,
} from '@tounsoo/todae';
import { EllipsisVertical, GripVertical } from 'lucide-react';
import { memo, useCallback, useId, useRef, useState, type RefObject } from 'react';
import styles from './Kanban.module.css';
type Task = { id: string; title: string; label: string };
type Column = { id: string; name: string; tasks: Task[] };
type Move = (fromColumn: number, from: number, toColumn: number, to: number) => void;
function moveTask(columns: readonly Column[], fromColumn: number, from: number, toColumn: number, to: number): Column[] {
const next = columns.map((column) => ({ ...column, tasks: [...column.tasks] }));
const [task] = next[fromColumn]!.tasks.splice(from, 1);
next[toColumn]!.tasks.splice(to, 0, task!);
return next;
}
type BoardCardProps = UseReorderItemProps & {
task: Task;
index: number;
columnIndex: number;
columns: readonly Column[];
grip: UseReorderHandleProps;
move: Move;
landing: RefObject<HTMLButtonElement | null>;
landed: string | null;
};
// A card takes its item props spread, so a drag step renders only the cards
// whose own drag state changed.
const BoardCard = memo(function BoardCard({
task,
index,
columnIndex,
columns,
grip,
move,
landing,
landed,
...item
}: BoardCardProps) {
// A card moved to another column leaves its list while its menu holds
// focus. Focus goes on to the card's trigger in its new column.
const handoff = useFocusHandoff({ to: landing });
const last = columns[columnIndex]!.tasks.length - 1;
return (
<li ref={handoff} {...item} className={styles.card}>
<Card.Root>
{/* The grip runs down the card's leading edge and the menu sits at its trailing edge,
both with their icons on the title's first line. The label sits under the title. */}
<Stack.Root direction="row" gap="condensed">
<span {...grip} aria-hidden className={styles.grip}>
<GripVertical size="1em" />
</span>
<Stack.Root style={{ flex: 1, minInlineSize: 0 }}>
<Stack.Root direction="row" align="start" gap="condensed">
<Text.Root style={{ flex: 1, minInlineSize: 0, overflowWrap: 'anywhere', hyphens: 'auto' }}>
{task.title}
</Text.Root>
<Menu.Root>
<Menu.Trigger
ref={task.id === landed ? landing : undefined}
variant="ghost"
className={styles.menu}
aria-label={`Move ${task.title}`}
>
<Button.Icon>
<EllipsisVertical />
</Button.Icon>
</Menu.Trigger>
<Menu.Content>
<Menu.Item
disabled={index === 0}
onSelect={() => move(columnIndex, index, columnIndex, index - 1)}
>
Move up
</Menu.Item>
<Menu.Item
disabled={index === last}
onSelect={() => move(columnIndex, index, columnIndex, index + 1)}
>
Move down
</Menu.Item>
<Menu.Separator />
{columns.map((column, target) => (
<Menu.Item
key={column.id}
disabled={target === columnIndex}
onSelect={() => move(columnIndex, index, target, column.tasks.length)}
>
Move to {column.name}
</Menu.Item>
))}
</Menu.Content>
</Menu.Root>
</Stack.Root>
{/* A row keeps the label at its own width, one stack gap under the title. */}
<Stack.Root direction="row">
<Badge.Root tone={categoryTone(task.label)}>{task.label}</Badge.Root>
</Stack.Root>
</Stack.Root>
</Stack.Root>
</Card.Root>
</li>
);
});
function BoardColumn({
group,
columnIndex,
columns,
move,
landing,
landed,
}: {
group: ReorderGroup<number>;
columnIndex: number;
columns: readonly Column[];
move: Move;
landing: RefObject<HTMLButtonElement | null>;
landed: string | null;
}) {
const column = columns[columnIndex]!;
const headingId = useId();
// Each column is a list of the board's group, keyed by its index. A drop
// inside the column calls its own onMove, and a drop from another column
// calls the group's.
const reorder = useReorder({
group,
list: columnIndex,
onMove: (from, to) => move(columnIndex, from, columnIndex, to),
});
// The list props go on the whole column, so a drop under the last card, on
// the heading or in an empty column lands in it.
return (
<section aria-labelledby={headingId} {...reorder.getListProps()} className={styles.column}>
<Stack.Root direction="row" align="center" justify="between" gap="condensed">
<Text.Root as="h3" id={headingId}>
{column.name}
</Text.Root>
<Badge.Root>{column.tasks.length}</Badge.Root>
</Stack.Root>
{column.tasks.length > 0 ? (
<ol aria-labelledby={headingId} className={styles.list}>
{column.tasks.map((task, index) => (
<BoardCard
key={task.id}
{...reorder.getItemProps(index)}
task={task}
index={index}
columnIndex={columnIndex}
columns={columns}
grip={reorder.getHandleProps(index)}
move={move}
landing={landing}
landed={landed}
/>
))}
</ol>
) : (
<Text.Root tone="muted">No tasks</Text.Root>
)}
</section>
);
}
export function KanbanBoard({
columns,
onChange,
}: {
columns: readonly Column[];
onChange: (columns: Column[]) => void;
}) {
const announce = useAnnouncer();
// The trigger of the card that last moved to another column, which takes
// focus from the card's old element.
const landing = useRef<HTMLButtonElement | null>(null);
const [landed, setLanded] = useState<string | null>(null);
// One move for every card, kept the same until the columns change, so a memoized card can skip a render.
const move: Move = useCallback(
(fromColumn, from, toColumn, to) => {
const task = columns[fromColumn]!.tasks[from]!;
const next = moveTask(columns, fromColumn, from, toColumn, to);
if (toColumn !== fromColumn) setLanded(task.id);
onChange(next);
const column = next[toColumn]!;
announce(`${task.title} moved to ${column.name}, position ${to + 1} of ${column.tasks.length}`);
},
[columns, onChange, announce],
);
// A drag between columns calls the same move as the menu.
const group = useReorderGroup<number>({
onMove: (from, to) => move(from.list, from.index, to.list, to.index),
});
// Cards on a board are dense, so the board uses the compact density.
return (
<DensityProvider density="compact">
<ScrollArea.Root axis="inline">
<div className={styles.board}>
{columns.map((column, columnIndex) => (
<BoardColumn
key={column.id}
group={group}
columnIndex={columnIndex}
columns={columns}
move={move}
landing={landing}
landed={landed}
/>
))}
</div>
</ScrollArea.Root>
</DensityProvider>
);
}
const COLUMNS: Column[] = [
{
id: 'todo',
name: 'To do',
tasks: [
{ id: 'export', title: 'Export invoices as CSV', label: 'Feature' },
{ id: 'chart', title: 'Fix chart colors in dark mode', label: 'Bug' },
{ id: 'guide', title: 'Write the onboarding guide', label: 'Docs' },
],
},
{
id: 'doing',
name: 'In progress',
tasks: [
{ id: 'search', title: 'Search across projects', label: 'Feature' },
{ id: 'due', title: 'Show due dates in local time', label: 'Bug' },
],
},
{
id: 'done',
name: 'Done',
tasks: [{ id: 'sso', title: 'Sign in with SSO', label: 'Feature' }],
},
];
export default function KanbanExample() {
const [columns, setColumns] = useState(COLUMNS);
return <KanbanBoard columns={columns} onChange={setColumns} />;
}
/* Columns sit side by side at 11rem or wider, and the board scrolls sideways
when they don't fit. */
.board {
display: grid;
grid-auto-columns: minmax(11rem, 1fr);
grid-auto-flow: column;
gap: var(--todae-space-3);
align-items: start;
}
.column {
display: flex;
flex-direction: column;
gap: var(--todae-space-3);
padding: var(--todae-space-3);
border-radius: var(--todae-radius-md);
background: var(--todae-color-bg-subtle);
}
.list {
display: flex;
flex-direction: column;
gap: var(--todae-space-2);
margin: 0;
padding: 0;
list-style: none;
}
.card {
position: relative;
}
.card[data-dragging] {
opacity: 0.5;
}
/* A line in the gap on the side the dragged card would land on. */
.card[data-drop]::before {
position: absolute;
inset-inline: 0;
block-size: var(--todae-focus-ring-width);
background: var(--todae-color-accent-default);
content: '';
}
.card[data-drop='before']::before {
inset-block-start: calc(-0.5 * (var(--todae-space-2) + var(--todae-focus-ring-width)));
}
.card[data-drop='after']::before {
inset-block-end: calc(-0.5 * (var(--todae-space-2) + var(--todae-focus-ring-width)));
}
/* The grip and the menu button mirror each other: both are as wide as an
icon-only button and sit the button's own padding in from the card's inner
edges, so their icons are the same distance from each side, the menu's
hover fill and focus ring stay inside the card, and the title and label sit
one gap inside both. Both icons are centered on the title's first line.
The card sets the title's font size and line height, so the button and the
grip measure the same line as the title, whatever line height the page
has. */
.card {
font-size: var(--todae-text-font-size);
line-height: var(--todae-text-leading-default);
--kanban-control: calc(1lh + 2 * (var(--todae-button-padding-y) + var(--todae-button-border-width)));
}
/* The grip runs the card's height over its leading padding, so it is easy to
hit (WCAG 2.2 SC 2.5.8). Its one row is a line of the title tall, which puts
its icon on the title's first line. It rests muted and turns full contrast
while the card is hovered or holds focus, and stays visible, since touch has
no hover. */
.grip {
display: grid;
grid-template-rows: 1lh;
place-items: center;
inline-size: var(--kanban-control);
margin-block: calc(-1 * var(--todae-card-padding));
margin-inline-start: calc(var(--todae-button-padding-y) - var(--todae-card-padding));
padding-block: var(--todae-card-padding);
color: var(--todae-color-fg-muted);
cursor: grab;
touch-action: none;
}
.card:is(:hover, :focus-within) .grip {
color: var(--todae-color-fg-default);
}
/* The menu button reaches into the card's trailing padding, and past the
title's line above and below, so its icon is centered on the title's first
line. */
.menu {
margin-block: calc(-1 * (var(--todae-button-padding-y) + var(--todae-button-border-width)));
margin-inline-end: calc(var(--todae-button-padding-y) - var(--todae-card-padding));
}
/* A column with no cards has no card to draw the drop line on, so the column
itself takes a dashed outline while a card would land in it. */
.column[data-drop-target]:not(:has(.list)) {
outline: var(--todae-focus-ring-width) dashed var(--todae-color-accent-default);
outline-offset: calc(-1 * var(--todae-focus-ring-width));
}
/* Forced colors replace backgrounds, so the drop line and the empty column's
outline take the system's highlight color. */
@media (forced-colors: active) {
.card[data-drop]::before {
background: Highlight;
forced-color-adjust: none;
}
.column[data-drop-target]:not(:has(.list)) {
outline-color: Highlight;
}
}
To do
3Export invoices as CSV
FeatureFix chart colors in dark mode
BugWrite the onboarding guide
Docs
In progress
2Search across projects
FeatureShow due dates in local time
Bug
Done
1Sign in with SSO
Feature
Columns and cards
Each column is a section named by its heading, with its cards in an ol that the same heading names, since a card's place in the column is its priority. The count beside the heading is a Badge. A column with no cards says so in text instead of showing an empty list. The board is a grid of columns at least 11rem wide in a ScrollArea with axis="inline", so on a narrow screen the board scrolls sideways and the cards keep their width.
Each card leads with its title, as most boards do, with its label under the title. The grip runs down the card's leading edge and the menu button sits at its trailing edge. Both are as wide as an icon-only button and sit the same small distance inside the card's edges, so their icons mirror each other and the menu's hover fill and focus ring stay inside the card, and both icons are centered on the title's first line. A word too long for the card's narrow title breaks with a hyphen when its lang has hyphenation rules, and a string with no break point, such as a URL, still wraps, so neither pushes the menu out. The board sits in a DensityProvider with density="compact", since cards on a board are dense.
Key each card by its task's id, not its index, so a card's menu moves with it and focus follows it.
The move menu
Each card has a Menu.Trigger named after the card. Move up and Move down change its place in its column, and a Move to item for each column, after a Menu.Separator, sends it to the end of that column. Moves the card can't make, such as Move up on the first card or Move to on its own column, take disabled, which a menu item draws as aria-disabled: the item stays in the arrow-key order and is read as unavailable, so every card's menu has the same items.
A move inside a column moves the card's element, and the menu hands focus back to its trigger there. A move to another column takes the card out of one list and puts a new one in the other, so its old element goes away holding focus. useFocusHandoff on each card sends that focus to the moved card's trigger in its new column, through a ref that only that trigger holds. The announcement names the card, its column and its position, which the person would otherwise have to find by reading the board again.
Dragging
The board calls useReorderGroup once, and each column calls useReorder with that group and its own index as list. Spread getListProps() on each column's section, getItemProps(index) on each card's li and getHandleProps(index) on its grip: a press on the grip makes that card draggable, so the text on the cards stays selectable and a card drags only by its grip. The hook puts data-dragging on the dragged card and data-drop on the card it would land beside, and the example's stylesheet draws the drop line in the gap from those. Each card is a memoized component that takes its item props spread, so a drag step renders only the cards whose own state changed, as Long lists says. The list props are on the whole column, so a drop under the last card or on the heading lands in that column too. A column with no cards takes a dropped card as its first, and while a card would land there the column carries data-drop-target, which the stylesheet draws as a dashed outline. A drop inside a column calls that column's onMove, and a drop in another column calls the group's onMove with { list, index } for where the card leaves and where it lands. Both call the same move function as the menu, so a drag is announced too.
The grip is aria-hidden and not a tab stop, since dragging has no keyboard path: the menu is the way to move a card from the keyboard (WCAG 2.2 SC 2.1.1) and with a single pointer and no dragging (SC 2.5.7). The grip runs the card's height over its leading padding, so it is easy to hit and more than 24 by 24 CSS pixels (SC 2.5.8). It stays visible, since touch has no hover, in a muted color that turns full contrast while the card is hovered or holds focus. It has touch-action: none, so a finger on it drags the card instead of scrolling the page. Escape cancels a drag and leaves the board as it was.
Saving the board
The example changes the board as soon as a move is chosen. If your product saves the move and the save fails, put the card back and say so with a Toast or an Alert, since the announcement already said the card moved.
Accessible composition
Todae handles: Menu gives the trigger aria-haspopup and aria-expanded, moves focus into the menu and through it with arrow keys, reads a disabled item as aria-disabled, and returns focus to the trigger when an item is chosen. useReorder drags a card only by its grip, works out the column and place a drop lands on, and ignores drags from outside the board, such as files. useFocusHandoff moves focus from a card that leaves its column. useAnnouncer writes the message to a live region.
You supply: A heading for each column that names its section and its ol. A name for each card's trigger that says which card it moves. disabled on the moves a card can't make. Keys from the task's id. A ref on the trigger of the card that last changed columns, for the focus hand-off. A grip hidden from screen readers, and a look for the drop line and the dragged card. The announcement, with the card's name, its column and its position, in your product's language.
Watch for: Keying cards by index, which hands focus to whichever card now sits where the moved one was. A move to another column without the focus hand-off, which drops focus to the page. Removing a move a card can't make instead of marking it unavailable, which changes the menu from card to card. Drag as the only way to move a card. A grip that is a button or a tab stop, which adds a control that does nothing from the keyboard.
When to use
When: Work that moves through a few stages, such as tasks, issues or hiring candidates, where people see every stage at once and move items between them.
When not: One ordered list → the Reorder recipe. Many items to sort, filter or compare by their fields → a Data table. A sequence the person goes through in order → Steps.