FragiolaDnD
Documentation
Concepts

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

keydoes
Space or Entergrabs the focused draggable; while dragging, drops it
Arrow keysmove it: by a step, or to the next or previous droppable
Escapecancels: the drag ends with onDragCancel, the element goes back
Tab, or the focus leavingcancels

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

modes.tsx
// 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.

Keyboard
Gallery
Right to left
Gallery

Sortable and swappable

Sortable and swappable items are always in a targets mode of their own; there is no keyboard option.

keya sortable itema swappable item
Space or Entergrabs it; while dragging, drops it in the slot the preview showsgrabs it; while dragging, swaps it with the target
Arrow keysone slot at a time in its group; past the group's end, on into the next accepting group in reading orderthe next or previous swap target, in reading order
Escapecancels and restores every shiftcancels

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.

Sorting by keyboard
Gallery

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 describedBy at an element that says how to drag, in your words.
  • Announcements. Listen with useDragMonitor and say each step in a live region.
announcer.tsx
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.