|
2 | 2 |
|
3 | 3 | [](https://github.com/eliottvincent/vue3-sortablejs/actions) [](https://www.npmjs.com/package/vue3-sortablejs) [](https://www.npmjs.com/package/vue3-sortablejs) |
4 | 4 |
|
| 5 | +Re-orderable drag-and-drop lists, via a **Vue directive**. Based on and offering all features of [Sortable](https://github.com/SortableJS/Sortable). |
| 6 | + |
| 7 | +[[view demo]](https://sortablejs.github.io/Sortable/) |
| 8 | + |
| 9 | + |
| 10 | +#### Yet another Sortable wrapper |
| 11 | + |
| 12 | +Several Vue wrappers for Sortable exist out there, yet I decided to build another one. |
| 13 | + |
| 14 | +The goal was to have a wrapper that: |
| 15 | +* supports Vue 3 |
| 16 | +* is **light** and easy to maintain |
| 17 | +* works as a **directive**, for example to conditionally enable / disable the drag-and-drop feature without having to change the whole component |
| 18 | +* doesn't update the underlying data model (more on that later) |
| 19 | + |
| 20 | +As a reference, here are the wrappers that I tested: |
| 21 | +* `[vuedraggable](https://www.npmjs.com/package/vuedraggable)` only supports Vue 2 |
| 22 | +* `[vuedraggable@next](https://www.npmjs.com/package/vuedraggable)` supports Vue 3, but adds a lot of overhead on top of Sortable |
| 23 | +* `[vue-sortable](https://www.npmjs.com/package/vue-sortable)` is totally outdated (last update is from 2016) |
| 24 | +* `[sortablejs-vue3](https://www.npmjs.com/package/sortablejs-vue3)` is the best wrapper I found, but only works as a component |
| 25 | + |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +Get Vue 3 Sortable from [jsDelivr](https://cdn.jsdelivr.net/npm/vue3-sortablejs/dist/vue3-sortablejs.global.js) or [UNPKG](https://unpkg.com/vue3-sortablejs/dist/vue3-sortablejs.global.js) and use it like this: |
| 30 | + |
| 31 | +```html |
| 32 | +<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> |
| 33 | +<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script> |
| 34 | +<script src="https://unpkg.com/vue3-sortablejs/dist/vue3-sortablejs.global.js"></script> |
| 35 | + |
| 36 | +<div id="app"> |
| 37 | + <div v-sortable> |
| 38 | + <div>a</div> |
| 39 | + <div>b</div> |
| 40 | + <div>c</div> |
| 41 | + </div> |
| 42 | +</div> |
| 43 | + |
| 44 | +<script> |
| 45 | + const { createApp } = Vue; |
| 46 | +
|
| 47 | + const app = createApp(); |
| 48 | +
|
| 49 | + app.use(sortablejs); |
| 50 | +
|
| 51 | + app.mount("#app"); |
| 52 | +</script> |
| 53 | +``` |
| 54 | + |
| 55 | +Vue 3 Sortable is also available through npm as the `[vue3-sortablejs](https://www.npmjs.com/package/vue3-sortablejs)` package. |
| 56 | + |
| 57 | +Install the package: |
| 58 | +```sh |
| 59 | +npm install --save vue3-sortablejs |
| 60 | +``` |
| 61 | + |
| 62 | +Register the plugin in your `App.vue` file: |
| 63 | +```js |
| 64 | +import VueSortable from "vue3-sortablejs"; |
| 65 | + |
| 66 | +app.use(VueSortable); |
| 67 | +``` |
| 68 | + |
| 69 | +And then use it like this in `MyComponent.vue`: |
| 70 | +```html |
| 71 | +<template> |
| 72 | + <h1>My Component</h1> |
| 73 | + |
| 74 | + <div v-sortable> |
| 75 | + <div>a</div> |
| 76 | + <div>b</div> |
| 77 | + <div>c</div> |
| 78 | + </div> |
| 79 | +</template> |
| 80 | +``` |
0 commit comments