A sortable list in one hook. Keyboard users included.
Reorder lists, build kanban boards and take file drops on Atlassian's pragmatic-drag-and-drop. Space picks an item up, the arrows move it, a screen reader hears every step, and every drag state is a signal your JSX reads directly.
bun add @pyreon/dnd - engine
- pragmatic-drag-and-dropAtlassian's engine: native drag events, hit-testing, edges.
- keyboard
- Space · arrows · EscAlt+Arrow moves directly. Each step is announced.
- hooks
- 5Sortable, draggable, droppable, file drop, monitor.
- iOS and Android
- sortable, draggable, droppableExperimental. SwiftUI
.onDrag/.onDrop, Compose drag-and-drop. File drop and the monitor stay web.
- 20× faster hover across 1,000 rows:
isOverKeywakes the two rows involved, a hand-writtenoverId() === keywakes every rowBENCHMARKS.md §10, dnd row · overnight run, one run, bun, happy-dom (relative only)
synthetic · author-run · M3 Max · record of 2026-11-05
Every result, including where we lose →The parts of drag and drop nobody finishes.
Keyboard reordering, on by default
Focus a row, Space or Enter picks it up, the arrows move it, Space drops it and Escape puts it back. Alt+Arrow moves in one step. No sensors to configure.
Screen readers hear every move
"Picked up Fix Safari bug. Current position 4 of 4." then "Moved Fix Safari bug to position 3 of 4." Items get
aria-roledescription, plusrole="listitem"and keyboard instructions unless you set your own.State you read, not render props
activeId(),overId()andoverEdge()are signals, andisOverKey(id)answers per row. A drop indicator isclass={() => s.isOverKey(t.id) ? s.overEdge() : ''}: when the hover moves, only the row it left and the row it entered update.Files with reasons
useFileDrop({ accept: ['image/*'], maxFiles: 5 })gives you the accepted files, andonReject(files, 'accept' | 'maxFiles')tells you why the rest were refused.
Delete the sensors, the contexts and arrayMove.
Two providers, two sensors, a per-row hook with a style transform, and the reorder written by hand.
function Row({ task }: { task: Task }) {
const { attributes, listeners, setNodeRef, transform, transition } =
useSortable({ id: task.id })
const style = { transform: CSS.Transform.toString(transform), transition }
return (
<li ref={setNodeRef} style={style} {...attributes} {...listeners}>
{task.title}
</li>
)
}
function Tasks() {
const [tasks, setTasks] = useState(initial)
const sensors = useSensors(
useSensor(PointerSensor),
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }),
)
const onDragEnd = ({ active, over }: DragEndEvent) => {
if (!over || active.id === over.id) return
setTasks((ts) => {
const from = ts.findIndex((t) => t.id === active.id)
const to = ts.findIndex((t) => t.id === over.id)
return arrayMove(ts, from, to)
})
}
return (
<DndContext sensors={sensors} collisionDetection={closestCenter} onDragEnd={onDragEnd}>
<SortableContext items={tasks} strategy={verticalListSortingStrategy}>
<ul>{tasks.map((t) => <Row key={t.id} task={t} />)}</ul>
</SortableContext>
</DndContext>
)
}One hook. Pointer, keyboard, announcements and auto-scroll included; you get the reordered array.
import { For } from '@pyreon/core'
import { useSortable } from '@pyreon/dnd'
import { signal } from '@pyreon/reactivity'
const tasks = signal<Task[]>(initial)
const Tasks = () => {
const { containerRef, itemRef } = useSortable({
items: tasks,
by: (t) => t.id,
label: (t) => t.title,
onReorder: (next) => tasks.set(next),
})
return (
<ul ref={containerRef}>
<For each={tasks()} by={(t) => t.id}>
{(t) => <li ref={itemRef(t.id)}>{t.title}</li>}
</For>
</ul>
)
}Coming from react-beautiful-dnd?
Same company's engine, without the render props.
<DragDropContext
onDragEnd={(r) => {
if (!r.destination) return
setTasks((ts) => reorder(ts, r.source.index, r.destination!.index))
}}
>
<Droppable droppableId="tasks">
{(provided) => (
<ul ref={provided.innerRef} {...provided.droppableProps}>
{tasks.map((t, i) => (
<Draggable key={t.id} draggableId={t.id} index={i}>
{(p) => (
<li ref={p.innerRef} {...p.draggableProps} {...p.dragHandleProps}>
{t.title}
</li>
)}
</Draggable>
))}
{provided.placeholder}
</ul>
)}
</Droppable>
</DragDropContext>react-beautiful-dnd is no longer maintained, and Atlassian points its users to Pragmatic drag and drop, the engine under @pyreon/dnd. Boards across lists: give each list the same groupId and handle onCrossListDrop / onCrossListReceive.
const { containerRef, itemRef } = useSortable({
items: tasks,
by: (t) => t.id,
label: (t) => t.title,
onReorder: (next) => tasks.set(next),
})
<ul ref={containerRef}>
<For each={tasks()} by={(t) => t.id}>
{(t) => <li ref={itemRef(t.id)}>{t.title}</li>}
</For>
</ul>Reorder it. Then try the keyboard.
Drag a row, or focus one and press Space, then the arrow keys, then Space again.
- ⋮⋮Design review#1
- ⋮⋮Ship onboarding#2
- ⋮⋮Write release notes#3
- ⋮⋮Fix Safari bug#4
Escape cancels a keyboard move and puts the row back.
[
{ id: 1, title: 'Design review' },
{ id: 2, title: 'Ship onboarding' },
{ id: 3, title: 'Write release notes' },
{ id: 4, title: 'Fix Safari bug' }
]The line under the list mirrors the live region the package announces into: that is what a screen reader hears.
Hover across 1,000 rows: 20× faster.
The baseline is what you would write without the wrapper: the same engine calls, the same signals, the same cleanup. Mounting costs the same, each event and each page monitor adds a few nanoseconds, and a hover that moves across a long list is where the hooks pull ahead. happy-dom is a JavaScript DOM, so read the ratios, not the times.
- hover fan-out, 1,000 rows (
isOverKeyvsoverId() === key)win20× - draggable, droppable, sortable-row mount + unmounttiecould not distinguish
- per-event dispatch (drag start + drop)loss+5 ns
- page monitor mount + unmountloss+82 ns
Five hooks, every drag surface.
useSortableReorder with edge detection and auto-scroll;axis: 'horizontal'for rows of cards.- Boards
groupIdlinks lists,onCrossListDropandonCrossListReceive(item, index)move cards between them. itemHandleRefDrag only by the grip, keep the rest of the row clickable.useDraggableTypeddata, ahandle, a custompreviewandisDragging().useDroppablecanDrop(source)filters,edgesturns onoverEdge()for which side you are on,isOver()lights the target.useFileDropReal OS files only, withaccept,maxFilesandisDraggingFiles()for a page-wide hint.useDragMonitorWatch every element drag on the page, withcanMonitorto filter, for an overlay or analytics.- Keyboard and live region Pick up, move, drop and cancel with announcements through
@pyreon/a11y. disabledA boolean or a signal: lock a list while it saves.- Cleanup by design Every engine registration is torn down on unmount, and per item when its ref goes
null.
Things to build with it.
isOverKey(id) and overEdge() are signals. The line appears above or below the hovered card, and a hover move updates only the row it left and the row it entered.
const s = useSortable({
items: tasks,
by: (t) => t.id,
label: (t) => t.title,
onReorder: (next) => tasks.set(next),
})
<ul ref={s.containerRef}>
<For each={tasks()} by={(t) => t.id}>
{(t) => (
<li
ref={s.itemRef(t.id)}
class={() => (s.isOverKey(t.id) ? `drop-${s.overEdge()}` : '')}
data-active={() => s.isActive(t.id)}
>
{t.title}
</li>
)}
</For>
</ul>One image at a time. Rejections come with a reason you can show.
import { signal } from '@pyreon/reactivity'
import { useFileDrop } from '@pyreon/dnd'
const Avatar = () => {
const error = signal('')
const drop = useFileDrop({
accept: ['image/*'],
maxFiles: 1,
onDrop: (files) => upload(files[0]!),
onReject: (_files, reason) =>
error.set(reason === 'accept' ? 'Images only' : 'One file at a time'),
})
return (
<div ref={drop.ref} class={() => (drop.isOver() ? 'zone over' : 'zone')}>
Drop a photo <span role="alert">{error()}</span>
</div>
)
}Five hooks.
Every export has a reference page with its signature, an example and the mistakes people make with it.
- useSortable
- useDraggable
- useDroppable
- useFileDrop
- useDragMonitor
- containerRef
- itemRef
- itemHandleRef
- activeId
- overId
- overEdge
- isActive
- isOverKey
- UseSortableOptions
- UseDraggableOptions
- UseDroppableOptions
- UseFileDropOptions
- UseDragMonitorOptions
- DragData
- DropEdge
- DropLocation
- DragPreviewOptions
- FileRejectReason
Good to know before you start.
Give items as a signal or getter
useSortable reads items() when a drop lands. A captured array goes stale after the first reorder.
Match by with <For by>
Use the same key in the hook's by, in itemRef(key) and in <For by>: the hook finds the dragged row by that key, and <For> moves the existing DOM node instead of rebuilding it.
File drop is for OS files
useFileDrop reacts to files dragged in from the desktop, not to in-app drags. Use useDroppable for those.
On native, the gesture hooks only
iOS and Android lower useSortable, useDraggable and useDroppable to the platform drag engines. The page-wide monitor and OS file drop stay web. Native targets are experimental.
What people ask first.
Is it accessible without extra work?
Sortable lists are. Items become focusable with list semantics and instructions, the keyboard flow is built in, and every pick-up, move, drop and cancel is announced. label(item) makes the announcement say "Fix Safari bug" instead of an id.
Why pragmatic-drag-and-drop?
It is built on the browser's native drag events, which is also what lets files arrive from the desktop, and it is the engine behind Trello and Jira. @pyreon/dnd adapts its state into signals and its teardown into onCleanup.
What does the wrapper cost?
Against a hand-rolled signals + pragmatic-drag-and-drop integration, measured in one run under happy-dom: mount and unmount tie, each drag start and drop adds about 5 ns, registering a page monitor about 82 ns, and a hover move across 1,000 rows is 20× cheaper with isOverKey than with a per-row overId() === key. happy-dom times are relative, not browser times.
How do I build a kanban board?
One useSortable per column with the same groupId. Reordering inside a column calls onReorder; a card dropped into another column calls onCrossListDrop(item) on the source and onCrossListReceive(item, index) on the target, where you move it between your signals.
Built to be used together.
Drag it, or press Space. Both work.
bun add @pyreon/dnd