Drag and Drop
Reflex Enterprise provides comprehensive drag and drop functionality for creating interactive UI elements using the rxe.dnd module. Built on top of react-dnd, it offers both high-level components for common use cases and low-level hooks for advanced scenarios.
rxe.dnd.draggable components with @rx.memo to avoid compilation errors.Basic Usage
Simple Drag and Drop
Here's a basic example showing how to create a draggable item and drop target:
Items dropped: 0
Drag me!
I can be moved around
ExpandCollapse
Multi-Position Drag and Drop
Create a draggable item that can be moved between multiple drop targets:
Drag the card between positions
Movable Card
Position: 0
ExpandCollapse
Advanced Features
State Tracking with Collected Parameters
Access drag and drop state information using collected parameters:
Status: No drag activity
Tracked Draggable
Ready to drag
Smart Drop Zone
Waiting...
ExpandCollapse
Dynamic Lists with Drag and Drop
Create dynamic draggable lists using rx.foreach. Always pass a stable item
identifier as the key prop to the outermost component rendered by the foreach
to ensure item identity is trackable as the item in the underlying list is
rearranged.
List A
Item 1
From List A
Item 2
From List A
Item 3
From List A
List B
Item 4
From List B
Item 5
From List B
ExpandCollapse
Core Components
Draggable
The rxe.dnd.draggable component makes any element draggable:
Key Properties:
type: String identifier for drag type matchingitem: Data object passed to drop handlerson_end: Called when drag operation ends
Drop Target
The rxe.dnd.drop_target component creates areas that accept draggable items:
Key Properties:
accept: List of drag types this target acceptson_drop: Called when item is droppedon_hover: Called when item hovers over target
Collected Parameters
Access real-time drag/drop state:
Draggable Parameters (rxe.dnd.Draggable.collected_params):
is_dragging: Boolean indicating if item is being dragged
Drop Target Parameters (rxe.dnd.DropTarget.collected_params):
is_over: Boolean indicating if draggable is hoveringcan_drop: Boolean indicating if drop is allowed
API Reference
rxe.dnd.draggable
Creates a draggable component that can be moved around the interface.
Parameters:
type(str, required): String identifier that must match theacceptlist of drop targetsitem(dict | Callable): Data object passed to drop handlers. Can be a static dictionary or a function that receives aDragSourceMonitorand returns datapreview_options(dict): Configuration for the drag preview appearanceoptions(dict): Additional drag source options likedropEffecton_end(EventHandler): Event handler called when drag operation completescan_drag(Callable): Function that determines if the item can be draggedis_dragging(Callable): Function to override the default dragging state detectioncollect(Callable): Function to collect custom properties from the drag monitor
rxe.dnd.drop_target
Creates a drop target that can receive draggable items.
Parameters:
accept(str | list[str], required): Drag type(s) this target acceptsoptions(dict): Additional drop target configuration optionson_drop(EventHandler): Event handler called when an item is dropped, receives theitemdataon_hover(EventHandler): Event handler called when an item hovers over the targetcan_drop(Callable): Function that determines if a specific item can be droppedcollect(Callable): Function to collect custom properties from the drop monitor
Monitor Classes
DragSourceMonitor
Provides information about the drag operation state:
is_dragging(): ReturnsTrueif this item is currently being draggedcan_drag(): ReturnsTrueif the item can be draggedget_item(): Returns the item data being draggedget_item_type(): Returns the drag type stringget_drop_result(): Returns the drop result (available inon_end)did_drop(): ReturnsTrueif the item was successfully dropped
DropTargetMonitor
Provides information about the drop target state:
is_over(): ReturnsTrueif a draggable item is hovering over this targetcan_drop(): ReturnsTrueif the hovering item can be droppedget_item(): Returns the item data of the hovering draggableget_item_type(): Returns the drag type of the hovering item
Default Collected Parameters
Draggable.collected_params
DropTarget.collected_params
Advanced Usage Examples
Data Passing with Item Parameter
The item parameter allows you to pass data from draggable components to drop handlers:
No items dropped yet
Custom Collect Functions
The collect parameter allows you to access drag and drop state information in real-time:
Real-time Monitor State
Drag me!
Dragging: false
Can drag: false
Drop Zone
Is over: false
Can drop: false
No item hovering
No drop activity
Provider
Drag and drop functionality requires the rxe.dnd.provider component to wrap your app. The provider is automatically added when using draggable or drop_target components.
For manual control:
Best Practices
- Always use
@rx.memoon functions containing draggable components - Use descriptive type names for better debugging
- Handle edge cases in drop handlers (invalid items, etc.)
- Provide visual feedback using collected parameters
- Test on mobile devices with touch backend
- Keep item data lightweight for better performance