Vue Draggable and Resizable Component

1.5.4 · active · verified Sun Apr 19

vue-drag-resize is a lightweight Vue.js component that enables elements to be made draggable and resizable within their parent container. It offers distinct versions for both Vue 2 (versions prior to 2.0.0, with `1.5.4` being a recent stable release in this line) and Vue 3 (versions 2.0.0 and above, with `2.0.3` being a recent stable for Vue 3). The library generally maintains an irregular release cadence, focusing on bug fixes and performance enhancements across its two major version tracks. Key differentiators include its zero external dependencies, fully reactive props, built-in support for touch events, grid snapping capabilities, options to maintain aspect ratio during resizing, strict confinement within parent boundaries, and the ability to restrict dragging or resizing to specific axes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate and use `vue-drag-resize` in a Vue 3 application using the Composition API (`<script setup>`). It shows how to initialize the component with dimensions and position, restrict its movement and resizing within a parent container, and handle the `resizing` and `dragging` events to update the component's state.

<script setup>
import { ref } from 'vue';
import VueDragResize from 'vue-drag-resize';

const width = ref(200);
const height = ref(200);
const top = ref(50);
const left = ref(50);

function resize(newRect) {
    width.value = newRect.width;
    height.value = newRect.height;
    top.value = newRect.top;
    left.value = newRect.left;
}
</script>

<template>
    <div id="app-container" style="width: 100%; height: 500px; border: 1px solid #ccc; position: relative;">
        <VueDragResize
            :isActive="true"
            :w="width"
            :h="height"
            :x="left"
            :y="top"
            :minw="50"
            :minh="50"
            :parentLimitation="true"
            v-on:resizing="resize"
            v-on:dragging="resize"
            style="background-color: #f0f0f0; border: 1px dashed #999; display: flex; flex-direction: column; justify-content: center; align-items: center;"
        >
            <h3>Hello Draggable World!</h3>
            <p>Position: {{ top }} х {{ left }} </p>
            <p>Size: {{ width }} х {{ height }}</p>
            <p>Try dragging or resizing me!</p>
        </VueDragResize>
    </div>
</template>

<style>
#app-container {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

view raw JSON →