Vue Smooth DnD

0.8.1 · active · verified Sun Apr 19

Vue Smooth DnD is a lightweight and performant Vue.js wrapper library for the underlying `smooth-dnd` core library. Currently at version 0.8.1, it provides comprehensive drag-and-drop and sortable functionalities for Vue applications. The library exposes Vue components like `Container` and `Draggable`, which allow developers to easily implement interactive UIs with features such as vertical or horizontal orientation, different drag behaviors (move, copy, drop-zone), drag handles, and auto-scrolling. While a specific release cadence isn't detailed, its evolution is closely tied to the `smooth-dnd` project. It stands out by offering a highly configurable, efficient solution, making it suitable for a wide array of user interaction requirements within the Vue ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic sortable list using `Container` and `Draggable` components, handling drops with the `onDrop` event and a utility `applyDrag` function to update the item order.

<template>
  <div>
    <div class="simple-page">
        <Container @drop="onDrop">            
          <Draggable v-for="item in items" :key="item.id">
            <div class="draggable-item">
              {{item.data}}
            </div>
          </Draggable>
        </Container>
    </div>
  </div>
</template>

<script lang="ts">
import { Container, Draggable } from "vue-smooth-dnd";
// Assume applyDrag and generateItems are utility functions provided by the user or an example
// For a runnable example, we'll define simple versions.

function applyDrag(arr: any[], dragResult: any) {
  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;
}

function generateItems(count: number, creator: (index: number) => any) {
  const result = [];
  for (let i = 0; i < count; i++) {
    result.push(creator(i));
  }
  return result;
}

export default {
  name: "SimpleDnD",
  components: { Container, Draggable },
  data() {
    return {
      items: generateItems(5, i => ({ id: i, data: "Draggable " + i }))
    };
  },
  methods: {  
    onDrop(dropResult: any) {
      this.items = applyDrag(this.items, dropResult);
    }
  }
};
</script>

<style>
.draggable-item {
  padding: 10px;
  margin-bottom: 5px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
  cursor: grab;
}
.simple-page {
  width: 300px;
  border: 1px dashed #eee;
  padding: 10px;
}
</style>

view raw JSON →