Vue.Draggable vs React-sortable-hoc
Detailed comparison of features, pros, cons, and usage
Vue.Draggable is a lightweight drag-and-drop component for Vue.js with a simple API, while react-sortable-hoc is a more feature-rich and flexible sorting library for React that offers higher-order components, but may have a steeper learning curve.
A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️
Vue.Draggable Pros and Cons
Pros
- Easy integration with Vue.js applications
- Supports both Vue 2 and Vue 3
- Highly customizable with numerous configuration options
- Excellent performance for smooth drag-and-drop interactions
Cons
- Learning curve for advanced features and customizations
- Limited built-in styling options, requiring additional CSS work
- Potential conflicts with other Vue libraries or components
- Documentation could be more comprehensive for complex use cases
React-sortable-hoc Pros and Cons
Pros
-
Easy to implement: The library provides a simple API and higher-order components that make it easy to add drag-and-drop functionality to existing React components.
-
Performance optimized: Built with performance in mind, it uses virtualization techniques to handle large lists efficiently.
-
Customizable: Offers a wide range of customization options, including custom drag handles, auto-scrolling, and animation settings.
-
Cross-platform support: Works well across different devices and platforms, including touch devices.
Cons
-
Learning curve: While easy to implement basic functionality, mastering advanced features and optimizations may require some time and effort.
-
Dependency on React: As a React-specific library, it's not suitable for projects using other frameworks or vanilla JavaScript.
-
Limited built-in styling: The library focuses on functionality rather than appearance, so developers may need to invest additional time in styling and theming.
-
Potential performance issues: In some cases, particularly with very large lists or complex layouts, performance can degrade if not properly optimized.
Vue.Draggable Code Examples
Basic Usage
This snippet demonstrates how to create a simple draggable list using Vue.Draggable:
<template>
<draggable v-model="myArray" group="people" @start="drag=true" @end="drag=false">
<div v-for="element in myArray" :key="element.id">
{{ element.name }}
</div>
</draggable>
</template>
<script>
import draggable from 'vuedraggable'
</script>
Two-Lists Example
Here's an example of how to create two interconnected draggable lists:
<template>
<div class="row">
<draggable class="list-group" :list="list1" group="people">
<div class="list-group-item" v-for="element in list1" :key="element.name">
{{ element.name }}
</div>
</draggable>
<draggable class="list-group" :list="list2" group="people">
<div class="list-group-item" v-for="element in list2" :key="element.name">
{{ element.name }}
</div>
</draggable>
</div>
</template>
React-sortable-hoc Code Examples
Basic Usage
This snippet demonstrates how to create a basic sortable list using react-sortable-hoc
:
import { SortableContainer, SortableElement } from 'react-sortable-hoc';
import { arrayMoveImmutable } from 'array-move';
const SortableItem = SortableElement(({value}) => <li>{value}</li>);
const SortableList = SortableContainer(({items}) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${value}`} index={index} value={value} />
))}
</ul>
);
});
Handling Sort End
This snippet shows how to handle the onSortEnd
event to update the state:
const [items, setItems] = useState(['Item 1', 'Item 2', 'Item 3']);
const onSortEnd = ({oldIndex, newIndex}) => {
setItems(arrayMoveImmutable(items, oldIndex, newIndex));
};
return (
<SortableList items={items} onSortEnd={onSortEnd} />
);
Custom Drag Handle
This example demonstrates how to use a custom drag handle:
const DragHandle = SortableHandle(() => <span>::</span>);
const SortableItem = SortableElement(({value}) => (
<li>
<DragHandle />
{value}
</li>
));
const SortableList = SortableContainer(({items}) => (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${value}`} index={index} value={value} />
))}
</ul>
));
Vue.Draggable Quick Start
Installation
- Install the package using npm or yarn:
npm install vuedraggable
yarn add vuedraggable
- Import the component in your Vue.js project:
import draggable from 'vuedraggable'
Basic Usage
- Register the component in your Vue instance or component:
export default {
components: {
draggable,
},
// ...
}
- Use the component in your template:
<template>
<div>
<draggable v-model="myArray" group="people" @start="drag=true" @end="drag=false">
<div v-for="element in myArray" :key="element.id">
{{ element.name }}
</div>
</draggable>
</div>
</template>
<script>
export default {
data() {
return {
myArray: [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Bob' },
],
drag: false,
}
},
}
</script>
- Customize the draggable component by adding props and event handlers as needed. Refer to the official documentation for more advanced usage and configuration options.
React-sortable-hoc Quick Start
Installation
To get started with react-sortable-hoc, follow these steps:
- Install the package using npm or yarn:
npm install react-sortable-hoc
yarn add react-sortable-hoc
- Import the necessary components in your React project:
import { SortableContainer, SortableElement } from 'react-sortable-hoc';
Basic Usage
Here's a simple example to demonstrate how to use react-sortable-hoc:
- Create a sortable item component:
const SortableItem = SortableElement(({ value }) => <li>{value}</li>);
- Create a sortable container component:
const SortableList = SortableContainer(({ items }) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
);
});
- Use the sortable list in your main component:
import React, { useState } from 'react';
import { arrayMoveImmutable } from 'array-move';
const MyComponent = () => {
const [items, setItems] = useState(['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']);
const onSortEnd = ({ oldIndex, newIndex }) => {
setItems(arrayMoveImmutable(items, oldIndex, newIndex));
};
return <SortableList items={items} onSortEnd={onSortEnd} />;
};
This example creates a simple sortable list where items can be dragged and reordered. The onSortEnd
function updates the state with the new order of items after each drag operation.
Top Related Projects
Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
Pros of Sortable
- Framework-agnostic: Can be used with any JavaScript framework or vanilla JS
- Lightweight: Smaller bundle size compared to Vue.Draggable and react-sortable-hoc
- Extensive features: Supports nested lists, multi-drag, and animations out of the box
Cons of Sortable
- Less integration with specific frameworks: Requires more setup for Vue or React projects
- Learning curve: May be more complex to use initially compared to framework-specific solutions
Code Comparison
Sortable:
Sortable.create(el, {
animation: 150,
ghostClass: 'blue-background-class'
});
Vue.Draggable:
<draggable v-model="myArray" :options="{animation:150}">
<div v-for="element in myArray">{{element.name}}</div>
</draggable>
react-sortable-hoc:
const SortableList = SortableContainer(({items}) => (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
));
Summary
Sortable offers a versatile, framework-agnostic solution with extensive features, while Vue.Draggable and react-sortable-hoc provide more seamless integration with their respective frameworks. The choice depends on project requirements, preferred framework, and desired level of customization.
Beautiful and accessible drag and drop for lists with React
Pros of react-beautiful-dnd
- Excellent accessibility features, including screen reader support
- Smooth animations and intuitive drag-and-drop experience
- Comprehensive documentation and examples
Cons of react-beautiful-dnd
- Limited to vertical and horizontal lists, no support for grid layouts
- Larger bundle size compared to other options
- React-specific, not suitable for Vue.js projects
Code Comparison
Vue.Draggable:
<draggable v-model="myArray">
<div v-for="element in myArray" :key="element.id">
{{ element.name }}
</div>
</draggable>
react-sortable-hoc:
const SortableList = SortableContainer(({items}) => (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
));
react-beautiful-dnd:
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="list">
{(provided) => (
<ul {...provided.droppableProps} ref={provided.innerRef}>
{items.map((item, index) => (
<Draggable key={item.id} draggableId={item.id} index={index}>
{(provided) => (
<li ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps}>
{item.content}
</li>
)}
</Draggable>
))}
{provided.placeholder}
</ul>
)}
</Droppable>
</DragDropContext>
Infinite responsive, sortable, filterable and draggable layouts
Pros of Muuri
- Framework-agnostic: Works with any JavaScript framework or vanilla JS
- Highly customizable with extensive API and options
- Supports both grid and list layouts with responsive capabilities
Cons of Muuri
- Steeper learning curve compared to Vue.Draggable and react-sortable-hoc
- Less specific integration with Vue or React ecosystems
- May require more setup and configuration for basic use cases
Code Comparison
Vue.Draggable:
<draggable v-model="myArray">
<div v-for="element in myArray" :key="element.id">
{{ element.name }}
</div>
</draggable>
react-sortable-hoc:
const SortableList = SortableContainer(({items}) => (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
));
Muuri:
const grid = new Muuri('.grid', {
dragEnabled: true,
layout: {
fillGaps: true
}
});
Vue.Draggable and react-sortable-hoc offer simpler integration with their respective frameworks, while Muuri provides more flexibility and control at the cost of additional setup. Vue.Draggable and react-sortable-hoc are better suited for quick implementations in Vue and React projects, whereas Muuri excels in complex, framework-agnostic scenarios requiring advanced customization.
:ok_hand: Drag and drop so simple it hurts
Pros of Dragula
- Framework-agnostic: Works with any JavaScript framework or vanilla JS
- Lightweight: Smaller bundle size compared to Vue.Draggable and react-sortable-hoc
- Simple API: Easy to set up and use with minimal configuration
Cons of Dragula
- Less feature-rich: Fewer advanced features compared to Vue.Draggable and react-sortable-hoc
- Manual integration: Requires more manual work to integrate with React or Vue components
- Limited animation options: Fewer built-in animation capabilities
Code Comparison
Vue.Draggable:
<draggable v-model="myArray">
<div v-for="element in myArray" :key="element.id">{{ element.name }}</div>
</draggable>
react-sortable-hoc:
const SortableList = SortableContainer(({ items }) => (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
));
Dragula:
dragula([document.querySelector('#left'), document.querySelector('#right')])
.on('drop', function (el) {
console.log('Dropped: ' + el.textContent);
});
Drag and Drop for React
Pros of react-dnd
- Highly flexible and customizable, allowing for complex drag-and-drop interactions
- Supports touch events and works well on mobile devices
- Provides a low-level API for fine-grained control over drag-and-drop behavior
Cons of react-dnd
- Steeper learning curve compared to Vue.Draggable and react-sortable-hoc
- Requires more boilerplate code to set up basic drag-and-drop functionality
- Less performant for simple list reordering tasks compared to specialized libraries
Code Comparison
Vue.Draggable:
<draggable v-model="myArray">
<div v-for="element in myArray" :key="element.id">{{ element.name }}</div>
</draggable>
react-sortable-hoc:
const SortableList = SortableContainer(({ items }) => (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
));
react-dnd:
const [{ isDragging }, drag] = useDrag(() => ({
type: 'ITEM',
item: { id, index },
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
}))
While Vue.Draggable and react-sortable-hoc provide simpler APIs for basic list reordering, react-dnd offers more flexibility for complex drag-and-drop interactions at the cost of increased complexity.