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.
Important: Always decorate functions defining rxe.dnd.draggable
components with @rx.memo
to avoid compilation errors.
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
Create a draggable item that can be moved between multiple drop targets:
Drag the card between positions
Movable Card
Position: 0
Access drag and drop state information using collected parameters:
Status: No drag activity
Tracked Draggable
Ready to drag
Smart Drop Zone
Waiting...
Create dynamic draggable lists using rx.foreach
:
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
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
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
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
Creates a draggable component that can be moved around the interface.
Parameters:
type
(str, required): String identifier that must match theaccept
list of drop targetsitem
(dict | Callable): Data object passed to drop handlers. Can be a static dictionary or a function that receives aDragSourceMonitor
and returns datapreview_options
(dict): Configuration for the drag preview appearanceoptions
(dict): Additional drag source options likedropEffect
on_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
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 theitem
dataon_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
Provides information about the drag operation state:
is_dragging()
: ReturnsTrue
if this item is currently being draggedcan_drag()
: ReturnsTrue
if 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()
: ReturnsTrue
if the item was successfully dropped
Provides information about the drop target state:
is_over()
: ReturnsTrue
if a draggable item is hovering over this targetcan_drop()
: ReturnsTrue
if 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
The item
parameter allows you to pass data from draggable components to drop handlers:
No items dropped yet
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
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:
- Always use
@rx.memo
on 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