@bedard/vue-sortablejs
v0.4.0
Published
Sortablejs behavior for Vue 3
Downloads
21
Readme
@bedard/vue-sortablejs
A minimalist interface for Sortablejs, no components required.
Installation
Install via npm
npm install @bedard/vue-sortablejs
Install via cdn
<script src="https://unpkg.com/@bedard/vue-sortablejs" type="module"></script>
Basic use
Register useSortable
with a container, and render the array with a v-for
loop
<template>
<div ref="container" :key="sortKey">
<div v-for="item in items">
{{ item }}
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { sort, useSortable } from '@bedard/vue-sortablejs'
const container = ref()
const { sortKey } = useSortable(container, {
onSort: e => sort(items, e),
})
</script>
Here is a breakdown of what's happening
- an outer
container
ref is created - a unique
sortKey
is attached to that container - the
sort
helper syncs state whenonSort
fires
Advanced use
Reactivity
All options are supported reactivity. Sortablejs instances are refreshed when options changes.
const disabled = ref(false)
const sortable = useSortable(container, {
disabled,
})
Shared lists
Use transfer
to move items from one array to another
import { sort, transfer, useSortable } from '@bedard/vue-sortablejs'
const first = useSortable(firstContainer, {
group: 'shared',
onAdd: e => transfer(from, to, e),
onSort: e => sort(firstItems, e),
})
const second = useSortable(secondContainer, {
group: 'shared',
onAdd: e => transfer(from, to, e),
onSort: e => sort(secondItems, e),
})
Manual controls
Sortable instance can be manually created and destroyed
const { createSortableInstances, destroySortableInstances } = useSortable(container)
createSortableInstances() // refresh instances and dom keys
destroySortableInstances() // destroy instances
License
Copyright (c) 2023-present, Scott Bedard