dioxus-dnd
Modular, accessible drag-and-drop for Dioxus. One small core, one module per drop pattern: use only what you need. Keyboard accessible by default, with touch support, live drop previews, and auto-scroll built in.
Compatibility
| dioxus-dnd | Dioxus | Rust |
|---|---|---|
| 0.5 | 0.8 (verified against 0.8.0-alpha.0; also compiles unchanged on 0.7.9) |
1.85+ |
The crate depends on dioxus with default-features = false, features = ["minimal"], so it adds no renderer, no JavaScript, and no extra
dependencies of its own.
Modules
| Module | Pattern | Payload transport |
|---|---|---|
core |
Draggable to DropZone with any Clone payload |
Rust Store context |
files |
OS file drops (FileDropZone, FileFilter) |
native event (evt.files()) |
sortable |
reorder within one list, with live preview (SortableList) |
self-contained (indices) |
board |
kanban and cross-container moves (BoardColumn, BoardItem, BoardSlot) |
context (BoardPayload<T>) |
tree |
nested drops with before/after/into intent (TreeNodeTarget) |
context |
canvas |
free-position drops with snap and bounds (CanvasDropZone) |
context |
grid |
2D tile reorder or swap (SortableGrid) |
self-contained (indices) |
multiselect |
drag N selected items as one (SelectableDraggable) |
context (Vec<K>) |
external |
text, URLs and HTML dropped in from other apps | native DataTransfer |
dragout |
drag text, links and HTML out to other apps (ExternalDragSource) |
native DataTransfer |
pointer |
touch and pen drags (PointerDraggable) |
context + pointer events |
autoscroll |
edge-scrolling containers (AutoScroll) |
n/a |
a11y |
screen-reader announcements (LiveRegion), no-drag reordering (ReorderButtons) |
n/a |
animate |
FLIP reorder transitions (FlipItem, experimental) |
n/a |
Design
In-app payloads travel through a shared Store<DragState<T>> in Dioxus
context: any Clone type, zero serialization, no DataTransfer string
round-trip. Stores (Dioxus 0.8's fine-grained reactivity) give each state
field its own lazy subscription, so a component that reads dnd.over() in
render to highlight a zone reruns only when the hovered zone changes, not
on every pointer move.
The native events are used for what only they can do: gestures,
coordinates, OS files, and content crossing the app boundary. Firefox's
requirement that something is set on DataTransfer for a drag to start is
handled for you.
The provider also carries a zone registry. Every mounted DropZone
records its id, label, drop callback, acceptance filter and DOM handle
there; that registry powers keyboard navigation and touch hit-testing, and
it is public (use_zone_registry) if you want to build your own
interaction on top. The pointer gesture lifecycle itself is a formal,
exhaustively tested state machine (core::machine), also public.
Quick start
use *;
use *;
Both components forward arbitrary attributes (class, style, and so on)
to their wrapper div.
Accessibility (built in, not opt-in)
Every core Draggable (and PointerDraggable) is focusable and keyboard
operable:
- Space / Enter picks the item up
- Up / Down cycles drop zones at the current level (spatial order)
- Right / Left descends into a zone's nested zones or ascends to the
parent (nesting is detected automatically when
DropZones containDropZones; in flat apps these fall back to next/previous) - Space / Enter drops into the selected zone
- Escape cancels
Render one LiveRegion::<T> per provider to voice announcements to screen
readers, and give Draggable and DropZone a label:
// "Picked up Ship it. Use arrow keys to choose a drop target, ..."
// "Over Done." then "Dropped in Done."
For a no-drag fallback, a11y::ReorderButtons renders headless move-up and
move-down buttons that emit the same SortEvent as dragging, so one
on_sort serves both inputs. Custom flows can push their own messages
with dnd.announce(...).
Touch
Every pattern in this crate works with touch and pen instantly, in every browser. You do not need to do anything to get it.
Native HTML5 drag from a touch long-press exists in some browsers (Safari on iOS/iPadOS 15+; Android support is inconsistent), so relying on it means your app works on some phones after a hold and not at all on others. This crate instead runs a pointer-event gesture path alongside the native one: mouse input takes the native HTML5 path, while touch and pen input is recognized by a small movement threshold (no hold delay) and hit-tested against measured client rects. Both paths feed the same state and drop callbacks, so your code cannot tell which one fired.
PointerDraggableis the touch-capable drag source forDropZonetargets.BoardItem,SelectableDraggableandTreeNodeTargetalready build on this machinery, so boards, multi-select and trees are touch-ready as-is. Missed finger drops re-measure the zones and retry with a closest-center fallback, so drops in the gutter between zones still land.SortableListandSortableGridcarry their own built-in pointer path. Rows and tiles respond to touch with the same live displacement preview as mouse drags.
The one tradeoff to know about: a touch drag surface must set
touch-action: none, which stops the browser from scrolling when a finger
moves on it. For a SortableList inside a scrollable container, set
touch_handle: true so only a leading grip (style it via
[data-sort-handle]) claims the finger and the rows themselves keep
scrolling:
SortableList
There is deliberately no long-press activation option; a movement threshold plus an explicit handle is more predictable than a timer, and works the same for pens.
Sortable lists with live preview
let mut items = use_signal;
rsx!
While you drag, the row slides toward its landing slot and its neighbors
translate out of the way: a live preview of the final order (disable with
live_preview: false). Style the hover target via
[data-drop-target="true"] and the dragged row via
[data-dragging="true"].
Auto-scroll
Wrap any scrollable container in AutoScroll and drags hovering within
threshold px of an edge (default 48) scroll it by up to speed px per
event (default 24), ramped by proximity. Works for native mouse drags and
PointerDraggable touch drags. Pure MountedData, no JavaScript eval.
AutoScroll
Modifier keys
The file-manager convention works out of the box: holding Ctrl/Cmd
during a drag forces a Copy effect, Alt forces Link. Both are
reflected in the browser's cursor feedback and in DropOutcome::effect,
so your on_drop can branch on move-vs-copy. effective_effect is public
if you need the same resolution in custom handlers. For positional
constraints there is a composable modifier chain (core::modifiers):
LockAxis, Snap and KeepInside, applied with apply_modifiers.
For simple zone models, apply_clone_or_move applies that convention to a
HashMap<ZoneId, Vec<T>>. Give it an identity function so moves can remove
the source item, and a clone hook for assigning a fresh id on copy:
For two plain lists, use apply_list_clone_or_move and pass the source list
directly:
Multi-select
let selection = ;
rsx!
Click selects one, Ctrl/Cmd+click toggles. Dragging a selected item carries the whole selection; dragging an unselected one carries just itself.
File drops
FileDropZone
Dragging out
ExternalDragSource
OutboundContent covers text, links (written as text/uri-list plus
text/plain plus text/html), rich HTML with a plain-text fallback, and
raw custom (format, data) pairs.
Nesting
Sortables inside sortables, boards inside boards: inner drag scopes stop
propagation on drag start and drop, so each level owns its own gestures.
Nested DropZones discover their parents automatically through context,
which is what powers hierarchical keyboard traversal. No configuration
needed.
Live showcase
examples/showcase.rs is a full landing page whose centerpiece is a live
playground: one interactive demo per pattern, with an "outcome tape" that
prints every DropOutcome the library delivers. It is designed to deploy
as-is as the project website:
There is also a focused board example:
Feature flags
serde: enablesexternal::typed::{store, retrieve}, JSON-typed payloads over the nativeDataTransfer(wire-compatible with dioxus-html's ownstore/retrieve) for drags that must cross app or window boundaries.
Platform notes
- Firefox: handled. Drags set
text/plaindata automatically so the gesture starts. DragOverlay: pointer tracking uses thedragevent, whose coordinates some webviews report as(0, 0). Those samples are ignored, so treat the overlay as progressive enhancement on desktop.- Windows desktop file drops have a history of platform quirks in wry-based webviews. Test on your target and consider a file input fallback.
animate::FlipItemis experimental: it is the one module whose behavior depends on browser paint timing rather than pure logic.
Prior art
The Dioxus ecosystem has several dnd crates with different philosophies:
dioxus-dnd-kit (mouse-synthesized, layout-stability focused), taino-dnd
(framework-agnostic core, pointer-events) and dioxus-nox-dnd (headless
sortable primitives). This crate's live-preview displacement, collision
fallback, modifier chain and gesture state machine were informed by
reading them, and by dnd-kit and react-beautiful-dnd before that. What it
does that the others do not: the native HTML5 path (OS file drops,
drag-out to other apps, copy/move cursor effects) alongside touch and
keyboard, across twelve patterns.
License
Licensed under the MIT license.