useDraggableList is a custom React hook that enables drag-and-drop functionality for lists.
- Install dependencies.
npm installor
yarn install- Run
yarn run devimport React from 'react';
import useDraggableList from './useDraggableList/useDraggableList';;
const MyComponent = () => {
const { items, handleDragStart, handleDragOver, handleDrop } = useDraggableList(initialItems);
return (
<ul>
{items.map((item, index) => (
<li
key={item.id}
draggable
onDragStart={(e) => handleDragStart(e, index)}
onDragOver={(e) => handleDragOver(e, index)}
onDrop={(e) => handleDrop(e, index)}
>
{item.content}
</li>
))}
</ul>
);
};initialItems(Array): The initial list of items.
listRef(Object): A ref object to be attached to the list container.listAttributes(Object): Attributes to be spread onto the list container.items(Array): The current list of items, updated after drag-and-drop operations.listRootClassNames(String): Class names to be applied to the list container.getListEventHandlers(Function): A function that returns event handlers for the list container.getItemButtonEventHandlers(Function): A function that returns event handlers for the draggable items.
This project is licensed under the MIT License.