Basic Usage
In This chapter we will understand the following things
This chapter will help you understand how to setup drag and drop using ReDrop and add multiple events like "dragstart", "dragenter", "drop" with different drag and drop options.
Instance of ReDrop Class ( dndManager )
ReDrop is built using a class which means invoking it will return you a instance of Redrop class with all the methods attached to it. This instance returned will be used to create droppable and draggable element and perform all the drag and drop actions like attaching events etc.
Lets see first how can we create a instance of ReDrop.
import { Redrop } from "redrop-dnd"
const dndManager = new Redrop()
I like to call the instance as dndManager as it serve as a drag and drop manager and help us create elements draggable and droppable.
Using this instance now we can make one or more then one element draggable or droppable
How to make elements draggable and droppable
ReDrop simplifies making elements draggable and droppable, offering an easier alternative to the native HTML5 drag-and-drop API. With native drag-and-drop, you need to manually set the draggable
attribute to true
, attach a dragStart
event for draggable elements, and dragOver
and drop
events for droppable elements. In ReDrop, the process is more streamlined.
Using ReDrop, elements can be made draggable or droppable with the makeDraggable()
or makeDroppable()
methods for single elements, or makeDraggables()
and makeDroppables()
for multiple elements. We’ll explore these methods shortly, but first, let’s cover some key concepts.
Now, let’s quickly see how to make elements draggable and droppable!
import { Redrop } from "redrop-dnd"
const dndManager = new Redrop()
// making a single element draggable or droppable
const draggableItem = dndManager.makeDraggable("#draggableElm", draggableOptions)
const droppableItem = dndManager.makeDroppable("#dropzone", droppableOptions)
// making multiple elements draggable and droppables
const draggables = dndManager.makeDraggables(".draggable", draggableOptions)
const droppables = dndManager.makeDroppables(".dropzone", droppableOptions)
That’s all it takes to make an element draggable or droppable! In the next section, we’ll explore how to add events to these elements, but first, let’s clarify a few more details.
The makeDraggable()
/
makeDraggables()
and makeDroppable()
/
makeDroppables()
methods accept either a string selector or a direct reference to an element. This means you don’t need to manually select an element using methods like getElementById
or querySelector
; you can simply pass a valid selector as a string, and the element will automatically be selected and made draggable or droppable.
Additionally, these methods accept a second parameter for draggable or droppable options, which allow you to customise and control the drag-and-drop behaviour. We’ll dive deeper into these options in an upcoming chapter, as understanding them is crucial for effectively using ReDrop.
When elements are made draggable or droppable, the methods return the element(s) with a special on()
method attached. This method makes it easy to add events to your draggable and droppable elements, which we’ll cover in detail in the next section.
Attaching Drag and Drop Events
As mentioned earlier, when elements are made draggable or droppable, a special on()
method is attached to them. This method allows you to easily add drag-and-drop events to those elements.
Here’s an example:
import { Redrop } from "redrop-dnd";
const dndManager = new Redrop();
const draggableItem = dndManager.makeDraggable("#draggableElm", draggableOptions);
const droppableItem = dndManager.makeDroppable("#dropzone", droppableOptions);
draggableItem.on("dragstart", (event, state, element) => {
const data = "Your custom data (string, object, etc.)";
dndManager.setData(data, element);
});
droppableItem.on("drop", (event, state, element, sortedIndex) => {
const data = state?.data;
console.log(data);
});
Understanding the on()
Method
on()
MethodThe on()
method takes two parameters:
Event Name: The name of the event to attach (e.g.,
"dragstart"
,"drop"
).Callback Function: The function to execute when the event is triggered.
Supported Events
Draggable Elements:
"drag"
,"dragstart"
,"dragend"
The callback does not include the
sortedIndex
argument.
Droppable Elements:
"dragenter"
,"dragleave"
,"dragover"
,"drop"
For drop events, the callback includes the
sortedIndex
argument if sorting is enabled.
Callback Function Type Definitions
Drag Event Callback
typescriptCopy codetype DragEventCallback = (
event: PointerEvent,
state: DndState,
element: DraggableElement,
) => void;
Drop Event Callback
typescriptCopy codetype DropEventCallback = (
event: PointerEvent,
state: DndState,
element: DroppableElement,
sortedIndex?: number | null,
) => void;
Explanation of Arguments
event
: APointerEvent
providing control over drag-and-drop operations.state
: Contains the state for both drag and drop elements. Refer to the DND State documentation for details.element
: The element theon()
method is attached to. For draggable elements, it’s the draggable item; for droppable elements, it’s the dropzone.sortedIndex
: Available only for drop events (if sorting is enabled), this indicates the index position where the element is dropped, which can be used with thetransferElm
method to move elements.
By using this simple structure, you can easily attach and manage events for your drag-and-drop elements.
In the next chapter we will understand how can we use options.
Last updated