Sortable list in React
If you're looking to incorporate draggable and sortable components into your application, dnd-kit is the preferred React library. It is well-suited for developing applications similar to JIRA and Trello.
Create a basic sortable list by following the steps outlined below.
Create react app and install following packages using npm
or yarn
Create a main SortableList
component
This component will need following things:
- DndContext : This is needed to using drag and drop functionality in an application. This establishes the drag-and-drop context for the specified components. It accepts various props to configure the behavior of the drag-and-drop context. Some common props include
onDragEnd
andcollisionDetection
.- onDragEnd : This is utilized to manage the conclusion of the drag operation. Within this context, we can define the logic for sorting items.
- collisionDetection : This is used to specify how collision detection is performed, enabling dnd-kit to identify which element should be sorted with. Here we will be providing
closestCorners
algorithm. This algorithm calculates the distance between the dragged item and the colliding item, helping determine which item should be replaced during the sorting process.
For this component, we will supply dummy data for experimentation. Additionally, we will furnish the SortableContainer
component, which will be created in the next step.
The SortableList
component will look like this.
and this is its css
Create SortableContainer
component
This component needs following things:
- SortableContext : This establishes a sorting context for the designated component, necessary for utilizing sorting functionality. For this we will provided
items
and sortingstrategy
.- item : We must furnish the items that are intended to be sortable, and the
SortableContext
ensures handling of the sorting logic. - strategy : There are four distinct strategies that can be supplied to this, namely
rectSortingStrategy
,verticalListSortingStrategy
,horizontalListSortingStrategy
, andrectSwappingStrategy
. In this case, we will utilize theverticalListSortingStrategy
since we are constructing a sortable list in a vertical orientation.
- item : We must furnish the items that are intended to be sortable, and the
Also, we will provide the SortableItem
component, which will be created in the next step.
The SortableContainer
component will look like this.
and its css
Creating SortableItem
component
For SortableItem
we will be using useSortable
hook.
- useSortable : This hooks provides few attribues for dragable elements which are namely
attributes
,listeners
,setNodeRef
,transform
andtransition
. Out of this,transform
andtransition
properties are related tocss
style and we will provide them via style object. For theuseSortable
hook to operate correctly, it requires the association of thesetNodeRef
property with the HTML element which will be a sortable element.
The SortableItem
component will look like this
and its css
Sensors
in DndContext
The DndContext
comes with a Pointer Sensor by default. If we wish to enable our component for keyboard and touch devices, we should supply the DndContext
for our component, along with associated sensors, using the useSensors
and useSensor
hooks.
Final implementation of SortableList
component will look like this
Demo
Sortable List
Conclusion
In this, we've explored creating a sortable list with a single droppable target. In future blog posts, we'll delve into building a drag-and-drop application with multiple droppable targets.