Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork27
A thin wrapper around Sortable.js for Vue 3
License
MaxLeiter/sortablejs-vue3
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a thin wrapper around the greatSortableJS 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.
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
You can see a demo with more complete code athttps://sortablejs-vue3.maxleiter.com.
- Install the package:
pnpm 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" > <--TheHeaderandFootertemplatesbelowareoptional--> <template #header> <header> <h1>SortableJS Vue3 Demo</h1> </header> </template> <template #item="{element,index}"> <divclass="draggable":key="element.id"> {{ element.name }} </div> </template> <template #footer> <footerclass="draggable">A footer</footer> </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 optional and defaults todiv. It's the HTML node type for the outer element of the included template/slot.
- The
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)
You can listen to Sortable events by adding the listeners to theSortable 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" @move.capture="(event: Sortable.MoveEvent, event2: Event) => boolean | -1 | 1" @clone="(event: Sortable.SortableEvent) => void">
You need to mount any plugins you want outside ofthe default before importing this library. For example, the below is correct:
importSortableJsfrom"sortablejs";import{Swap}from"sortablejs/modular/sortable.core.esm";SortableJs.mount(newSwap());import{Sortable}from"sortablejs-vue3";
No changes are necessary to work with Vuex or another store. Just passstore.state.items as your list. To modify your data you need to manually listen to the events and calculate the new position withevent.oldIndex andevent.newIndex with something like the following:
constmoveItemInArray=<T>(array:T[],from:number,to:number)=>{constitem=array.splice(from,1)[0];nextTick(()=>array.splice(to,0,item));};onEnd(event){moveItemInArray(store.state.items,event.oldIndex,event.newIndex)}
You may also want to see the SortableJS store documentationhere.
- ./src/examples/WithStore.vue - A simple example with a store
- Run
pnpmto install dependencies pnpm devwill start a web server with live reloadingpnpm buildwill build the production library filespnpm build:sitewill build the demo website
About
A thin wrapper around Sortable.js for Vue 3
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors14
Uh oh!
There was an error while loading.Please reload this page.
