sortablejs-vue3
sortablejs-vue3 copied to clipboard
A thin wrapper around Sortable.js for Vue 3
SortableJS-vue3

This is a thin wrapper around the great SortableJS library. I had many issues migrating from Vue.Draggable to vue.draggable.next, and after briefly investigating I decided that it was too complicated and a smaller solution was the answer. This wrapper attempts to keep you as close to Sortable as possible.
Why not use <other library>?
Vue.Draggableonly supports Vue 2vue.draggable.nextuses the Options API, has multiple open (and afaict useful) pull requests, and had weird bugs/side-effects when I tried and used itshopify/draggableandvue-shopify-dragableseemed promising but they don't supported nested components
Usage
You can see a demo with more complete code at https://sortablejs-vue3.maxleiter.com.
- Install the package:
yarn add sortablejs-vue3 sortablejs
or
npm install sortablejs-vue3 sortablejs
- Import the component in your
<script setup>(or<script>):
import { Sortable } from "sortablejs-vue3";
- Use the component:
<template>
<main>
<Sortable
:list="elements"
item-key="id"
tag="div"
:options="options"
>
<template #item="{element, index}">
<div class="draggable" :key="element.id">
{{ element.name }}
</div>
</template>
</Sortable>
</template>
-
The
listanditem-keyprops are necessary. Theoptionsprop is an object that can contain any SortableJS option. You can find a full list of them here: https://github.com/SortableJS/Sortable#options -
The
tagprop is an optional prop, it's the HTML node type of the element that creates an outer element for the included slot. the default value isdiv
Props
list(Array<any>, required): your data to listitemKey(string|(item) => (string | number | Symbol), required): The name of the key present in each item in the list that corresponds to a unique value (to use as thekey)tag(string, optional, default ="div"): The element type to render asoptions(Object, false): the SortableJS options minus event handlers (see below)
Events
You can listen to Sortable events by adding the listeners to the Sortable component. For example:
<Sortable
:list="elements"
item-key="id"
@change="(event: Sortable.SortableEvent) => void"
@choose="(event: Sortable.SortableEvent) => void"
@unchoose="(event: Sortable.SortableEvent) => void"
@start="(event: Sortable.SortableEvent) => void"
@end="(event: Sortable.SortableEvent) => void"
@add="(event: Sortable.SortableEvent) => void"
@update="(event: Sortable.SortableEvent) => void"
@sort="(event: Sortable.SortableEvent) => void"
@remove="(event: Sortable.SortableEvent) => void"
@filter="(event: Sortable.SortableEvent) => void"
@move="(event: Sortable.MoveEvent, event2: Event) => void"
@clone="(event: Sortable.SortableEvent) => void"
>
Vuex
No changes are necessary to work with Vuex. Just pass store.state.items as your list. To modify your data you need to manually listen to the events and calculate the new position with event.oldIndex and event.newIndex with something like the following:
const moveItemInArray = <T>(array: T[], from: number, to: number) => {
const item = array.splice(from, 1)[0];
array.splice(to, 0, item);
};
onEnd(event) { moveItemInArray(store.state.items, event.oldIndex, event.newIndex) }
Development
- Run
yarnto install dependencies yarn devwill start a web server with live reloadingyarn buildwill build the production library filesyarn build:sitewill build the demo website