Skip to content

Commit 5b13479

Browse files
Add comprehensive API Reference and enhanced examples for drag-and-drop documentation
- Added detailed API Reference section for rxe.dnd.draggable and rxe.dnd.drop_target components - Documented all parameters including item, collect, type, accept, on_drop, on_hover, on_end - Added Monitor Classes section covering DragSourceMonitor and DropTargetMonitor methods - Added Default Collected Parameters section showing structure for both components - Enhanced examples demonstrating item parameter usage for data passing - Enhanced examples showing collect parameter usage for state tracking - All examples tested and verified to compile correctly in browser - Documentation follows existing reflex-web patterns and styling Co-Authored-By: [email protected] <[email protected]>
1 parent 8132759 commit 5b13479

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

docs/enterprise/drag-and-drop.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,199 @@ Access real-time drag/drop state:
317317
- `is_over`: Boolean indicating if draggable is hovering
318318
- `can_drop`: Boolean indicating if drop is allowed
319319

320+
## API Reference
321+
322+
### rxe.dnd.draggable
323+
324+
Creates a draggable component that can be moved around the interface.
325+
326+
```python
327+
rxe.dnd.draggable(
328+
*children,
329+
type: str, # Required: drag type identifier
330+
item: dict | Callable[[DragSourceMonitor], dict] = None, # Data passed to drop handlers
331+
preview_options: dict = None, # Options for drag preview
332+
options: dict = None, # Drag source options
333+
on_end: EventHandler = None, # Called when drag ends
334+
can_drag: Callable[[DragSourceMonitor], bool] = None, # Predicate for drag ability
335+
is_dragging: Callable[[DragSourceMonitor], bool] = None, # Override drag state
336+
collect: Callable[[DragSourceMonitor], dict] = None, # Custom state collection
337+
**props # Standard component props
338+
)
339+
```
340+
341+
**Parameters:**
342+
343+
- **`type`** (str, required): String identifier that must match the `accept` list of drop targets
344+
- **`item`** (dict | Callable): Data object passed to drop handlers. Can be a static dictionary or a function that receives a `DragSourceMonitor` and returns data
345+
- **`preview_options`** (dict): Configuration for the drag preview appearance
346+
- **`options`** (dict): Additional drag source options like `dropEffect`
347+
- **`on_end`** (EventHandler): Event handler called when drag operation completes
348+
- **`can_drag`** (Callable): Function that determines if the item can be dragged
349+
- **`is_dragging`** (Callable): Function to override the default dragging state detection
350+
- **`collect`** (Callable): Function to collect custom properties from the drag monitor
351+
352+
### rxe.dnd.drop_target
353+
354+
Creates a drop target that can receive draggable items.
355+
356+
```python
357+
rxe.dnd.drop_target(
358+
*children,
359+
accept: str | list[str], # Required: accepted drag types
360+
options: dict = None, # Drop target options
361+
on_drop: EventHandler = None, # Called when item is dropped
362+
on_hover: EventHandler = None, # Called when item hovers
363+
can_drop: Callable[[dict, DropTargetMonitor], bool] = None, # Drop validation
364+
collect: Callable[[DropTargetMonitor], dict] = None, # Custom state collection
365+
**props # Standard component props
366+
)
367+
```
368+
369+
**Parameters:**
370+
371+
- **`accept`** (str | list[str], required): Drag type(s) this target accepts
372+
- **`options`** (dict): Additional drop target configuration options
373+
- **`on_drop`** (EventHandler): Event handler called when an item is dropped, receives the `item` data
374+
- **`on_hover`** (EventHandler): Event handler called when an item hovers over the target
375+
- **`can_drop`** (Callable): Function that determines if a specific item can be dropped
376+
- **`collect`** (Callable): Function to collect custom properties from the drop monitor
377+
378+
### Monitor Classes
379+
380+
#### DragSourceMonitor
381+
382+
Provides information about the drag operation state:
383+
384+
- **`is_dragging()`**: Returns `True` if this item is currently being dragged
385+
- **`can_drag()`**: Returns `True` if the item can be dragged
386+
- **`get_item()`**: Returns the item data being dragged
387+
- **`get_item_type()`**: Returns the drag type string
388+
- **`get_drop_result()`**: Returns the drop result (available in `on_end`)
389+
- **`did_drop()`**: Returns `True` if the item was successfully dropped
390+
391+
#### DropTargetMonitor
392+
393+
Provides information about the drop target state:
394+
395+
- **`is_over()`**: Returns `True` if a draggable item is hovering over this target
396+
- **`can_drop()`**: Returns `True` if the hovering item can be dropped
397+
- **`get_item()`**: Returns the item data of the hovering draggable
398+
- **`get_item_type()`**: Returns the drag type of the hovering item
399+
400+
### Default Collected Parameters
401+
402+
#### Draggable.collected_params
403+
404+
```python
405+
{
406+
"is_dragging": bool, # True when this item is being dragged
407+
"can_drag": bool # True when this item can be dragged
408+
}
409+
```
410+
411+
#### DropTarget.collected_params
412+
413+
```python
414+
{
415+
"is_over": bool, # True when a draggable is hovering
416+
"can_drop": bool, # True when the hovering item can be dropped
417+
"item": dict | None # Data from the hovering draggable item
418+
}
419+
```
420+
421+
## Advanced Usage Examples
422+
423+
### Data Passing with Item Parameter
424+
425+
The `item` parameter allows you to pass data from draggable components to drop handlers:
426+
427+
```python demo exec toggle
428+
import reflex as rx
429+
import reflex_enterprise as rxe
430+
431+
class SimpleState(rx.State):
432+
message: str = "No items dropped yet"
433+
434+
def simple_draggable():
435+
return rxe.dnd.draggable(
436+
rx.box(
437+
"Drag me!",
438+
p=4,
439+
bg="blue.100",
440+
border="1px solid blue",
441+
cursor="grab"
442+
),
443+
type="simple",
444+
item={"name": "test_item", "value": 42}
445+
)
446+
447+
def simple_drop_target():
448+
return rxe.dnd.drop_target(
449+
rx.box(
450+
rx.text(SimpleState.message),
451+
p=4,
452+
bg="gray.100",
453+
border="2px dashed gray",
454+
min_height="100px"
455+
),
456+
accept=["simple"],
457+
on_drop=lambda item: SimpleState.setvar("message", f"Dropped: {item['name']}")
458+
)
459+
460+
def item_data_example():
461+
return rx.vstack(
462+
simple_draggable(),
463+
simple_drop_target(),
464+
spacing="4"
465+
)
466+
```
467+
468+
### Custom Collect Functions
469+
470+
The `collect` parameter allows you to access drag and drop state information:
471+
472+
```python demo exec toggle
473+
import reflex as rx
474+
import reflex_enterprise as rxe
475+
476+
class CollectState(rx.State):
477+
status: str = "Ready to drag"
478+
479+
def collect_draggable():
480+
return rxe.dnd.draggable(
481+
rx.box(
482+
"Drag with collect",
483+
p=4,
484+
bg="green.100",
485+
border="1px solid green",
486+
cursor="grab"
487+
),
488+
type="collect_item",
489+
item={"id": "collect_test"}
490+
)
491+
492+
def collect_drop_target():
493+
return rxe.dnd.drop_target(
494+
rx.box(
495+
rx.text(CollectState.status),
496+
p=4,
497+
bg="yellow.100",
498+
border="2px dashed orange",
499+
min_height="100px"
500+
),
501+
accept=["collect_item"],
502+
on_drop=lambda item: CollectState.setvar("status", "Item dropped successfully!")
503+
)
504+
505+
def custom_collect_example():
506+
return rx.vstack(
507+
collect_draggable(),
508+
collect_drop_target(),
509+
spacing="4"
510+
)
511+
```
512+
320513
## Provider
321514

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

0 commit comments

Comments
 (0)