Vue 3 Smooth Drag and Drop

0.0.6 · active · verified Sun Apr 19

vue3-smooth-dnd is a specialized Vue 3 wrapper for the highly performant `smooth-dnd` library, providing components for intuitive drag-and-drop functionality within Vue applications. Currently at version 0.0.6, the package primarily focuses on maintaining compatibility with the underlying `smooth-dnd` library and fixing integration-specific bugs. While release cadence is not formally established, updates appear to be driven by reported issues. Its key differentiator lies in directly porting the well-regarded Vue 2 `vue-smooth-dnd` wrapper to the Vue 3 ecosystem, aiming for a seamless transition for developers familiar with the original. It offers a robust, highly customizable, and visually smooth solution for list reordering and item movement, distinguishing itself from simpler drag-and-drop solutions by leveraging `smooth-dnd`'s optimized animation and physics.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up a basic draggable list, render items using `Draggable` components within a `Container`, and handle drag-and-drop events to update the list's order.

<template>
  <div>
    <span>Studio Ghibli Tier List</span>
    <Container @drop="onDrop">            
      <Draggable v-for="(item, i) in items" :key="item.id">
        <div>
           {{i + 1}} -> {{item.data}}
        </div>
      </Draggable>
    </Container>
  </div>
</template>

<script>
import { Container, Draggable } from "vue3-smooth-dnd";
export default {
  name: "app",
  components: { Container, Draggable },
  data() {
    return {
      items: [
        { id: 1, data: "Princess Mononoke" },
        { id: 2, data: "Spirited Away" },
        { id: 3, data: "My Neighbor Totoro" },
        { id: 4, data: "Howl's Moving Castle" }
      ]
    };
  },
  methods: {  
    onDrop(dropResult){
      this.items = this.applyDrag(this.items, dropResult);
    },
    applyDrag(arr, dragResult){
      const { removedIndex, addedIndex, payload } = dragResult;

      if (removedIndex === null && addedIndex === null) return arr;
      const result = [...arr];
      let itemToAdd = payload;
      
      if (removedIndex !== null) {
        itemToAdd = result.splice(removedIndex, 1)[0];
      }
      if (addedIndex !== null) {
        result.splice(addedIndex, 0, itemToAdd);
      }
      return result;
    }
  }
}
</script>

view raw JSON →