npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue3-sortablejs

v1.0.7

Published

A directive for Sortable on Vue 3

Downloads

2,716

Readme

Vue 3 Sortable

Build Status Version Downloads

Re-orderable drag-and-drop lists, via a Vue directive. Based on and offering all features of Sortable.

[view demo]

Yet another Sortable wrapper

Several Vue wrappers for Sortable exist out there, yet I decided to build another one.

The goal was to have a wrapper that:

  • supports Vue 3
  • is light and easy to maintain
  • works as a directive, for example to conditionally enable / disable the drag-and-drop feature without having to change the whole component
  • doesn't iterate on the data by itself
  • doesn't update the underlying data model (see Order mutation)

As a reference, here are other Sortable wrappers:

Usage

Get Vue 3 Sortable from jsDelivr or UNPKG and use it like this:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="https://unpkg.com/vue3-sortablejs/dist/vue3-sortablejs.global.js"></script>

<div id="app">
  <div v-sortable>
    <div>a</div>
    <div>b</div>
    <div>c</div>
  </div>
</div>

<script>
  const { createApp } = Vue;

  const app = createApp();

  app.use(sortablejs);

  app.mount("#app");
</script>

Vue 3 Sortable is also available through npm as the vue3-sortablejs package.

Install the package:

npm install --save vue3-sortablejs

Register the plugin in App.vue:

import VueSortable from "vue3-sortablejs";

app.use(VueSortable);

And then use it like this in MyComponent.vue:

<template>
  <h1>My Component</h1>

  <div v-sortable>
    <div>a</div>
    <div>b</div>
    <div>c</div>
  </div>
</template>

Options

You can pass an object of options, in order to affect the behavior of the directive:

  • disabled whether to disable the drag-and-drop behavior
  • options an object containing any Sortable option
<template>
  <div v-sortable="{ disabled: false, options: { animation: 250, easing: 'cubic-bezier(1, 0, 0, 1)' } }">
    <div>a</div>
    <div>b</div>
    <div>c</div>
  </div>
</template>

Events

A custom ready event will be triggered as soon as Sortable is registered on the component. You can use it to access the underlying Sortable instance. As well, you can listen to any native Sortable event.

  • @ready: Sortable is ready and attached to the component
  • @choose: element is chosen
  • @unchoose: element is unchosen
  • @start: element dragging started
  • @end: element dragging ended
  • @add: element is dropped into the list from another list
  • @update: changed sorting within list
  • @sort: called by any change to the list (add / update / remove)
  • @remove: element is removed from the list into another list
  • @filter: attempt to drag a filtered element
  • @move: event when you move an item in the list or between lists
  • @clone: called when creating a clone of element
  • @change: called when dragging element changes position
<template>
  <div v-sortable @ready="onReady" @end="onOrderChange">
    <div>a</div>
    <div>b</div>
    <div>c</div>
  </div>
</template>

<script>
export default {
  methods: {
    onReady(event) {
      console.log(event.sortable);
    },

    onOrderChange(event) {
      console.log(event.oldIndex);
      console.log(event.newIndex);
    }
  }
};
</script>

Order mutation

This wrapper only impacts the actual DOM order, it does not mutate the data order. This avoids a lot of overhead in the code, and gives you the full control on your data.

It is really simple to change the order in your data after an item is dropped:

<template>
  <div v-sortable @end="onOrderChange">
    <div v-for="item in items">
      {{ item }}
    </div>
  </div>

  <span>Items data: {{ items }}</span>
</template>

<script>
export default {
  data() {
    return {
      items: [ "a", "b", "c" ]
    }
  },

  methods: {
    onOrderChange(event) {
      // Remove item from old index
      let item = this.items.splice(event.oldIndex, 1)[0];

      // Insert it at new index
      this.items.splice(event.newIndex, 0, item);
    }
  }
};
</script>

Notes

It is highly recommended to set a key on the children items, to help Sortable track the DOM:

<template>
  <div v-sortable>
    <div key="a">a</div>
    <div key="b">b</div>
    <div key="c">c</div>
  </div>
</template>

In the same way, if you use the group option, it is highly recommended to set a key on the parent itself. Otherwise the DOM managed by Sortable can become out-of-sync with the actual data state. I have noticed this helps a lot when using Sortable with complex components.

The key must be based on the number of items the parent contains. This will force a re-render when an item is added / removed, and make Sortable re-initialize and start from a clean state every time. This may seem a bit hacky, but it's the only way to keep a consistant behavior.

<template>
  <h1>Foo</h1>

  <div v-sortable="{ options: { group: 'items' } }" @end="onOrderChange" :key="fooItems.length">
    <div v-for="item in fooItems" :key="item">
      {{ item }}
    </div>
  </div>

  <h1>Bar</h1>

  <div v-sortable="{ options: { group: 'items' } }" @end="onOrderChange" :key="barItems.length">
    <div v-for="item in barItems" :key="item">
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    onOrderChange(event) {
      // Mutate fooItems and barItems
    }
  }
};
</script>

License

vue3-sortablejs is released under the MIT License. See the bundled LICENSE file for details.