0.4.0 • Published 1 year ago

@bedard/vue-sortablejs v0.4.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

@bedard/vue-sortablejs

Build status NPM Bundle size License

A minimalist interface for Sortablejs, no components required.

View live sandbox →

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

  1. an outer container ref is created
  2. a unique sortKey is attached to that container
  3. the sort helper syncs state when onSort 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,
})

View all available options →

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

MIT

Copyright (c) 2023-present, Scott Bedard

0.4.0

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.1.0

1 year ago

0.0.7

1 year ago

0.0.5

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago