Vue Draggable Plus

0.6.1 · active · verified Sun Apr 19

Vue Draggable Plus is a universal drag-and-drop component library supporting both Vue 3 and Vue 2.7+ applications, building upon the capabilities of Sortablejs. The current stable version is 0.6.1, with an active release cadence reflecting ongoing development and maintenance. It addresses a gap where the official Sortablejs Vue components had become out of sync with modern Vue 3 practices. A key differentiator is its flexibility, offering multiple usage patterns including a component, a Composition API composable (`useDraggable`), and a directive (`v-draggable`). It uniquely solves the problem of integrating drag-and-drop with complex component libraries by allowing developers to specify any element as the drag container using a selector, overcoming limitations of previous implementations that required the component itself to be the direct list child. The library ships with comprehensive TypeScript types.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic draggable list using the `useDraggable` Composition API composable, binding it to a `ref` element and a reactive array. It includes basic styling and logs drag events.

```typescript
<template>
  <div ref="el" class="drag-container">
    <div v-for="item in list" :key="item.id" class="drag-item">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useDraggable } from 'vue-draggable-plus'

interface ListItem {
  name: string;
  id: number;
}

const el = ref<HTMLElement | null>(null)
const list = ref<ListItem[]>([
  { name: 'Item A', id: 1 },
  { name: 'Item B', id: 2 },
  { name: 'Item C', id: 3 },
  { name: 'Item D', id: 4 }
])

const draggable = useDraggable(el, list, {
  animation: 150,
  onStart() {
    console.log('Drag started')
  },
  onEnd(event) {
    console.log('Drag ended:', event.oldIndex, '->', event.newIndex)
    // The list.value is automatically updated by useDraggable
  }
})

onMounted(() => {
  // For demonstration, you could programmatically start/destroy draggable if needed
  // console.log('Draggable instance:', draggable)
})
</script>

<style>
.drag-container {
  border: 1px solid #eee;
  padding: 10px;
  min-height: 100px;
}
.drag-item {
  padding: 8px 12px;
  margin-bottom: 5px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  cursor: grab;
}
.drag-item:hover {
  background-color: #f0f0f0;
}
</style>
```

view raw JSON →