@fragiola/dnd-react · React
Drag and drop that wraps what you already render.
Wrap any element in a Draggable or a Droppable and it drags and drops: no provider, no HTML of its own, no CSS. Mouse, touch, pen and the keyboard go through one path, and each capability is its own import.
No HTML of its own
A wrapper merges its props, ref and handlers into the element it wraps, a plain one or any Fragiola primitive, and renders nothing else.
No provider
A draggable in one corner of the tree and a droppable in another find each other through a drag space kept per document.
Pay for what you import
Draggable, droppable, sortable and swappable are separate entries: an app ships only the ones it imports.
Pointer and keyboard
Mouse, touch and pen through Pointer Events, and a keyboard path that grabs, moves, drops and cancels with the same events.
Quick start
Two wrappers, anywhere in the tree
import { Draggable } from "@fragiola/dnd-react/draggable";
import { Droppable } from "@fragiola/dnd-react/droppable";
<Draggable id="card" type="task">
<article className="card">Drag me</article>
</Draggable>;
<Droppable id="done" accepts={["task"]} onDrop={(event) => move(event.operation.source.id)}>
<section className="column">Done</section>
</Droppable>;Neither wrapper renders an element, and nothing wraps them both. The state arrives on your elements
as data-dnd-* attributes.
Sortable and swappable
Sort and swap, with your state
import { arrayMove, Sortable } from "@fragiola/dnd-react/sortable";
{tasks.map((id, index) => (
<Sortable key={id} id={id} index={index} group="tasks" onSortEnd={(event) => setTasks((now) => arrayMove(now, event.from.index, event.to.index))}>
<li>{id}</li>
</Sortable>
))}No list wrapper: items of one group find each other. While an item is dragged, the core shifts
its neighbours by their translate, without a DOM move or a React render; on the drop your state
applies the move with a pure helper. Lists, rows, grids, kanban columns that accept each other,
and swaps within and across groups.
Made for the Fragiola family
With Dockable
Drop a card on a tabset to open a tab, drag inside panels and popouts.
With Grid Layout
Drag inside a grid item without moving it, add items with item.add.
A kanban board
Columns that sort and accept each other's cards, an empty column, a column that refuses.
Bundle size
What each entry costs, measured on a production build.
What it never ships
CSSIconsTextA providerHTML5 drag and drop
The state is exposed as data-dnd-* attributes and ARIA, so any styling solution works: Tailwind, plain CSS, CSS modules, CSS-in-JS. The examples use Fragiola UI and its palettes.