vue-slicksort
A set of vue mixins to turn any list into an animated, touch-friendly, sortable list ✌️
Top Related Projects
Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️
Beautiful and accessible drag and drop for lists with React
:ok_hand: Drag and drop so simple it hurts
VanillaJS sortable lists and grids using native HTML5 drag and drop API.
Quick Overview
Vue-slicksort is a lightweight, dependency-free Vue.js component for creating sortable and reorderable drag-and-drop lists. It provides a simple and flexible way to implement sortable lists in Vue applications, with support for both vertical and horizontal sorting.
Pros
- Easy to integrate with existing Vue projects
- Lightweight and dependency-free
- Supports both vertical and horizontal sorting
- Customizable with various props and events
Cons
- Limited documentation and examples
- May require additional styling for optimal appearance
- Not as feature-rich as some other drag-and-drop libraries
- Relatively small community and fewer updates
Code Examples
- Basic usage:
<template>
<SlickList v-model="items">
<SlickItem v-for="item in items" :key="item.id">
{{ item.text }}
</SlickItem>
</SlickList>
</template>
<script>
import { SlickList, SlickItem } from 'vue-slicksort'
export default {
components: { SlickList, SlickItem },
data() {
return {
items: [
{ id: 1, text: 'Item 1' },
{ id: 2, text: 'Item 2' },
{ id: 3, text: 'Item 3' },
]
}
}
}
</script>
- Horizontal sorting:
<template>
<SlickList v-model="items" axis="x">
<SlickItem v-for="item in items" :key="item.id">
{{ item.text }}
</SlickItem>
</SlickList>
</template>
- Using events:
<template>
<SlickList
v-model="items"
@sort-start="onSortStart"
@sort-end="onSortEnd"
>
<SlickItem v-for="item in items" :key="item.id">
{{ item.text }}
</SlickItem>
</SlickList>
</template>
<script>
export default {
// ...
methods: {
onSortStart(event) {
console.log('Sort started', event)
},
onSortEnd(event) {
console.log('Sort ended', event)
}
}
}
</script>
Getting Started
-
Install the package:
npm install vue-slicksort
-
Import and use the components in your Vue file:
<template> <SlickList v-model="items"> <SlickItem v-for="item in items" :key="item.id"> {{ item.text }} </SlickItem> </SlickList> </template> <script> import { SlickList, SlickItem } from 'vue-slicksort' export default { components: { SlickList, SlickItem }, data() { return { items: [ { id: 1, text: 'Item 1' }, { id: 2, text: 'Item 2' }, { id: 3, text: 'Item 3' }, ] } } } </script>
-
Style your components as needed and customize with available props and events.
Competitor Comparisons
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 project
- More feature-rich, including multi-list sorting and nested lists
- Larger community and more frequent updates
Cons of Sortable
- Steeper learning curve due to more complex API
- Larger file size, which may impact performance for smaller projects
Code Comparison
vue-slicksort:
<template>
<SlickList v-model="items">
<SlickItem v-for="item in items" :key="item.id">
{{ item.text }}
</SlickItem>
</SlickList>
</template>
Sortable:
new Sortable(document.getElementById('list'), {
animation: 150,
ghostClass: 'blue-background-class'
});
The vue-slicksort example shows a more Vue-specific implementation, while Sortable demonstrates its framework-agnostic nature. vue-slicksort provides a more declarative approach, whereas Sortable offers a more imperative style with greater flexibility.
Both libraries serve the purpose of creating sortable lists, but they cater to different use cases. vue-slicksort is ideal for Vue.js projects seeking a simple, lightweight solution, while Sortable is better suited for complex sorting requirements across various frameworks or vanilla JavaScript applications.
A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️
Pros of react-sortable-hoc
- More comprehensive documentation and examples
- Supports both vertical and horizontal sorting out of the box
- Offers a wider range of customization options and props
Cons of react-sortable-hoc
- Larger bundle size due to more features and dependencies
- Steeper learning curve for beginners due to its extensive API
- May require more setup and configuration for simple use cases
Code Comparison
react-sortable-hoc:
import { SortableContainer, SortableElement } from 'react-sortable-hoc';
const SortableItem = SortableElement(({value}) => <li>{value}</li>);
const SortableList = SortableContainer(({items}) => {
return (
<ul>
{items.map((value, index) => (
<SortableItem key={`item-${index}`} index={index} value={value} />
))}
</ul>
);
});
vue-slicksort:
<template>
<SlickList v-model="items">
<SlickItem v-for="(item, index) in items" :key="index" :index="index">
{{ item }}
</SlickItem>
</SlickList>
</template>
<script>
import { SlickList, SlickItem } from 'vue-slicksort'
</script>
The code comparison shows that vue-slicksort has a simpler implementation, requiring less boilerplate code. react-sortable-hoc, on the other hand, offers more flexibility but requires more setup.
Beautiful and accessible drag and drop for lists with React
Pros of react-beautiful-dnd
- Robust and well-maintained by a large company (Atlassian)
- Extensive documentation and community support
- Supports both horizontal and vertical dragging
Cons of react-beautiful-dnd
- Larger bundle size due to more features
- Steeper learning curve for beginners
- Limited to React applications only
Code Comparison
react-beautiful-dnd:
import { DragDropContext, Droppable, Draggable } from '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>
vue-slicksort:
<template>
<SlickList v-model="items" :axis="axis" @input="onInput">
<SlickItem v-for="(item, index) in items" :key="index" :index="index">
{{ item }}
</SlickItem>
</SlickList>
</template>
<script>
import { SlickList, SlickItem } from 'vue-slicksort'
export default {
components: { SlickList, SlickItem },
// ... rest of the component logic
}
</script>
:ok_hand: Drag and drop so simple it hurts
Pros of dragula
- Framework-agnostic, can be used with any JavaScript project
- Extensive documentation and examples
- Supports touch events for mobile devices
Cons of dragula
- Not specifically optimized for Vue.js
- May require more setup and configuration for Vue.js projects
- Less performant for large lists compared to vue-slicksort
Code Comparison
dragula:
dragula([document.getElementById('left'), document.getElementById('right')])
.on('drag', function (el) {
el.className += ' is-moving';
})
.on('drop', function (el) {
el.className = el.className.replace('is-moving', '');
});
vue-slicksort:
<template>
<SlickList v-model="items" :axis="'y'" :lockAxis="true">
<SlickItem v-for="(item, index) in items" :key="index" :index="index">
{{ item }}
</SlickItem>
</SlickList>
</template>
The code comparison shows that dragula requires more JavaScript setup, while vue-slicksort integrates seamlessly with Vue.js components and directives. vue-slicksort's approach is more declarative and Vue-friendly, making it easier to implement in Vue.js projects.
VanillaJS sortable lists and grids using native HTML5 drag and drop API.
Pros of html5sortable
- Framework-agnostic, can be used with any JavaScript project
- Supports native HTML5 drag and drop API
- Lightweight and has minimal dependencies
Cons of html5sortable
- Lacks Vue.js-specific optimizations and integration
- May require more manual setup for complex Vue applications
- Less tailored for Vue.js reactivity system
Code Comparison
html5sortable:
sortable('.sortable', {
forcePlaceholderSize: true,
placeholderClass: 'my-placeholder'
});
vue-slicksort:
<template>
<SlickList v-model="items">
<SlickItem v-for="item in items" :key="item.id">
{{ item.text }}
</SlickItem>
</SlickList>
</template>
Summary
html5sortable is a versatile, framework-agnostic sorting library that leverages the HTML5 drag and drop API. It's lightweight and can be used in various JavaScript projects. However, it may require more setup for complex Vue.js applications and lacks Vue-specific optimizations.
vue-slicksort, on the other hand, is specifically designed for Vue.js, offering seamless integration with Vue's reactivity system and component structure. It provides a more Vue-centric approach but is limited to Vue.js projects.
Choose html5sortable for flexibility across different frameworks or vue-slicksort for a more tailored Vue.js experience.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Vue Slicksort ð
A set of component mixins to turn any list into an animated, touch-friendly, sortable list. Based on react-sortable-hoc by [@clauderic]
Examples available here: vue-slicksort.netlify.app/
ä¸æææ¡£
Features
v-model
Compatible â Make any array editable with thev-model
standard- Mixin Components â Integrates with your existing components
- Standalone Components â Easy to use components for slick lists
- Drag handle, auto-scrolling, locked axis, events, and more!
- Suuuper smooth animations â Chasing the 60FPS dream ð
- Horizontal lists, vertical lists, or a grid â â ⤡
- Touch support ð
- Oh yeah, and it's DEPENDENCY FREE! ð
Installation
Using npm:
$ npm install vue-slicksort --save
Using yarn:
$ yarn add vue-slicksort
Using a CDN:
<script src="https://unpkg.com/vue-slicksort@latest/dist/vue-slicksort.min.js"></script>
Then, using a module bundler that supports either CommonJS or ES2015 modules, such as webpack:
// Using an ES6 transpiler like Babel
import { ContainerMixin, ElementMixin } from 'vue-slicksort';
// Not using an ES6 transpiler
var slicksort = require('vue-slicksort');
var ContainerMixin = slicksort.ContainerMixin;
var ElementMixin = slicksort.ElementMixin;
If you are loading the package via <script>
tag:
<script>
var { ContainerMixin, ElementMixin, HandleDirective } = window.VueSlicksort;
</script>
Usage
Check out the docs: vue-slicksort.netlify.app
Why should I use this?
There are already a number of great Drag & Drop libraries out there (for instance, vuedraggable is fantastic). If those libraries fit your needs, you should definitely give them a try first. However, most of those libraries rely on the HTML5 Drag & Drop API, which has some severe limitations. For instance, things rapidly become tricky if you need to support touch devices, if you need to lock dragging to an axis, or want to animate the nodes as they're being sorted. Vue Slicksort aims to provide a simple set of component mixins to fill those gaps. If you're looking for a dead-simple, mobile-friendly way to add sortable functionality to your lists, then you're in the right place.
FAQ
Upgrade from v1.x
There are a few changes in v2, mainly support for Vue 3 and dragging between groups. Read more about migrating here: vue-slicksort.netlify.app/migrating-1x
Upgrade from v0.x
The event names have all changed from camelCase to dash-case to accommodate for inline HTML templates.
Grid support?
Need to sort items in a grid? We've got you covered! Just set the axis
prop to xy
. Grid support is currently limited to a setup where all the cells in the grid have the same width and height, though we're working hard to get variable width support in the near future.
Item disappearing when sorting / CSS issues
Upon sorting, vue-slicksort
creates a clone of the element you are sorting (the sortable-helper) and appends it to the end of the appendTo
tag. The original element will still be in-place to preserve its position in the DOM until the end of the drag (with inline-styling to make it invisible). If the sortable-helper gets messed up from a CSS standpoint, consider that maybe your selectors to the draggable item are dependent on a parent element which isn't present anymore (again, since the sortable-helper is at the end of the appendTo
prop). This can also be a z-index
issue, for example, when using vue-slicksort
within a Bootstrap modal, you'll need to increase the z-index
of the SortableHelper so it is displayed on top of the modal.
Click events being swallowed
By default, vue-slicksort
is triggered immediately on mousedown
. If you'd like to prevent this behaviour, there are a number of strategies readily available. You can use the distance
prop to set a minimum distance (in pixels) to be dragged before sorting is enabled. You can also use the pressDelay
prop to add a delay before sorting is enabled. Alternatively, you can also use the HandleDirective.
Scoped styles
If you are using scoped styles on the sortable list, you can use appendTo
prop.
Dependencies
Slicksort has no dependencies.
vue
is the only peerDependency
Reporting Issues
If believe you've found an issue, please report it along with any relevant details to reproduce it. The easiest way to do so is to fork this jsfiddle.
Asking for help
Please file an issue for personal support requests. Tag them with question
.
Contributions
Yes please! Feature requests / pull requests are welcome.
Thanks
This library is heavily based on react-sortable-hoc by Claudéric Demers (@clauderic). A very simple and low overhead implementation of drag and drop that looks and performs great!
Top Related Projects
Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️
Beautiful and accessible drag and drop for lists with React
:ok_hand: Drag and drop so simple it hurts
VanillaJS sortable lists and grids using native HTML5 drag and drop API.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot