FragiolaDnD

@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.

The same markup, five themes:
Theme
See the code →

Quick start

Two wrappers, anywhere in the tree

board.tsx
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

list.tsx
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.

KanbanColumns as sortable groups: cards sort and move where a column accepts them, into an empty column through SortableGroup.Open the live example

What it never ships

  • CSS
  • Icons
  • Text
  • A provider
  • HTML5 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.