Keyboard and accessibility
Grab, move, drop and cancel without a pointer, by steps or from target to target, and the names and announcements that stay the app's.
Every draggable, sortable and swappable item can be dragged with the keyboard, and the keyboard emits the same events as the
pointer, with input: "keyboard".
The keys
| key | does |
|---|---|
| Space or Enter | grabs the focused draggable; while dragging, drops it |
| Arrow keys | move it: by a step, or to the next or previous droppable |
| Escape | cancels: the drag ends with onDragCancel, the element goes back |
| Tab, or the focus leaving | cancels |
The draggable is a tab stop (its handle, when it has one): the wrapper sets tabIndex={0} when
the element is not focusable already. Focus returns to the activator after a drop or Escape,
unless the app moved it.
Steps or targets
// 25 px per arrow (the default mode and step)
<Draggable id="card" keyboard={{ step: 25 }}>…</Draggable>;
// droppable to droppable, in reading order
<Draggable id="card" keyboard={{ mode: "targets" }}>…</Draggable>;In "targets" mode, the arrows go from droppable to droppable in reading order: rows top to
bottom, each row from its inline start. Down and the arrow toward the inline end go forward. Each
move scrolls the droppable into view within its scroll containers, by the least that shows it
(as scrollIntoView({ block: "nearest" }) does): instantly, unless your CSS sets
scroll-behavior: smooth on the container. In "step" mode, arrows are visual: in right to left,
ArrowRight still moves right.
Sortable and swappable
Sortable and swappable items are always in a targets mode of their own; there is no keyboard
option.
| key | a sortable item | a swappable item |
|---|---|---|
| Space or Enter | grabs it; while dragging, drops it in the slot the preview shows | grabs it; while dragging, swaps it with the target |
| Arrow keys | one slot at a time in its group; past the group's end, on into the next accepting group in reading order | the next or previous swap target, in reading order |
| Escape | cancels and restores every shift | cancels |
Forward is down or toward the inline end, as in the targets mode. A refusing group is skipped. The
drag events still name the source and the target (a sort's target is the group's name), so the
live region can say where the item is, from onSortOver's to (its group and index), in your
words.
The focus returns to the item after a drop within its group. Moved into another group, the item
is a new element in the app's other list (React mounts it under its new parent), so giving it the
focus is the app's: the example below focuses it from its ref once it mounts. Each arrow
scrolls the destination slot or swap target into view within its scroll containers, as in the
targets mode.
What the wrappers never add
The wrappers set no role, no aria-label, no aria-roledescription and none of the deprecated
aria-grabbed/aria-dropeffect. They set tabIndex when the element is not focusable,
aria-disabled when disabled, and aria-describedby only when the app passes describedBy.
Every word is the app's:
- Names. Give each draggable and droppable an accessible name (
aria-label, or its text). A handle that is only an icon needs one (aria-label="Move Report"). - Instructions. Point
describedByat an element that says how to drag, in your words. - Announcements. Listen with
useDragMonitorand say each step in a live region.
import { useDragMonitor } from "@fragiola/dnd-react";
function Announcer({ nameOf }: { nameOf: (id: string | number) => string }) {
const [message, setMessage] = useState("");
useDragMonitor((event) => {
const { source, target } = event.operation;
if (event.type === "drag-start") setMessage(`Picked up ${nameOf(source.id)}.`);
if (event.type === "drag-over" && target) setMessage(`Over ${nameOf(target.id)}.`);
if (event.type === "drop")
setMessage(target ? `Dropped on ${nameOf(target.id)}.` : "Dropped nowhere.");
if (event.type === "drag-cancel") setMessage("Cancelled.");
});
return <p role="status">{message}</p>;
}drag-start already carries the droppable the drag starts over as its operation.target, and
drag-over only reports a change of target after that, so the "Picked up" sentence is not
replaced before it is read.
Groups and accepts
How sortable and swappable items form lists, which lists take items from which, how an empty list is reached, and how a drop rule sees a sort.
A kanban board
Columns of cards that sort and move between columns, an empty column through SortableGroup, a column that refuses cards, and your state applying it.