Vue Drag and Drop Wrapper

1.1.4 · active · verified Sun Apr 19

vue-drag-drop is a lightweight Vue wrapper designed to simplify the often "wonky" native HTML Drag and Drop API. It aims to abstract away common pitfalls, such as the inability to access transferred data during `dragover` events, the need to manually serialize complex data types like objects and arrays, and the requirement for `event.preventDefault()` on drop zones. The current stable version is 1.1.4. While it does not specify a strict release cadence, updates have occurred since its major 1.0.0 release, which introduced significant changes. Key differentiators include its focus on simplicity and a minimal API, contrasting with more comprehensive drag-and-drop solutions that often include list reordering or extensive UI features. It is specifically built for Vue 2 applications and ships with TypeScript type definitions.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates basic drag and drop functionality using `vue-drag-drop` components, showing how to transfer data and respond to `drop`, `dragenter`, and `dragleave` events, including visual feedback for the drop zone.

<template>
  <div id="app">
    <h1>Vue Drag Drop Basic Demo</h1>
    <div style="display: flex; gap: 20px; justify-content: center;">
      <drag
        :transfer-data="{ type: 'file', name: 'document.pdf', size: '1.2MB' }"
        effect-allowed="copy"
        class="drag-item"
      >
        <span>Draggable File</span>
      </drag>

      <drop
        @drop="onDrop"
        @dragenter="onDragEnter"
        @dragleave="onDragLeave"
        @dragover.prevent
        class="drop-zone"
        :class="{ 'drag-over': isDragOver }"
      >
        <p>{{ dropMessage }}</p>
        <p v-if="droppedData">File: {{ droppedData.name }} ({{ droppedData.size }})</p>
      </drop>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { Drag, Drop } from 'vue-drag-drop';

export default Vue.extend({
  components: { Drag, Drop },
  data() {
    return {
      droppedData: null as { type: string; name: string; size: string } | null,
      dropMessage: 'Drop files here!',
      isDragOver: false,
    };
  },
  methods: {
    onDrop(data: any) {
      this.droppedData = data;
      this.dropMessage = `Dropped ${data.name}!`;
      this.isDragOver = false;
      console.log('Dropped data:', data);
    },
    onDragEnter() {
      this.isDragOver = true;
      this.dropMessage = 'Release to drop...';
    },
    onDragLeave() {
      this.isDragOver = false;
      this.dropMessage = 'Drop files here!';
    },
  },
});
</script>

<style scoped>
.drag-item { padding: 15px 25px; background-color: #42b983; color: white; border-radius: 5px; cursor: grab; }
.drop-zone { padding: 30px; border: 2px dashed #ccc; border-radius: 5px; min-width: 250px; text-align: center; }
.drop-zone.drag-over { border-color: #42b983; background-color: #e6ffe6; }
</style>

view raw JSON →