/zwDragResize

Primary LanguageJavaScript

logo

ZwDragResize

Latest Version on NPM Software License npm

Optimization of vue-draggable-resizable made by Maurizio

Maurizio
vue-draggable-resizable

Vue2 Component for draggable and resizable elements.

Table of Contents

Features

  • No dependencies
  • Use draggable, resizable or both
  • Define handles for resizing
  • Restrict size and movement to parent element
  • Snap element to custom grid
  • Restrict drag to vertical or horizontal axis

Demo

Demo


Install and basic usage

$ npm install --save zw-drag-resize

Register the component

import Vue from 'vue'
import ZwDragResize from 'zw-drag-resize'

Vue.component('zw-drag-resize', ZwDragResize)

You may now use the component in your markup

<template>
  <div style="height: 500px; width: 500px; border: 1px solid red; position: relative;">
    <zw-drag-resize :w="100" :h="100" v-on:dragging="onDrag" v-on:resizing="onResize" :parent="true">
      <p>Hello! I'm a flexible component. You can drag me around and you can resize me.<br>
      X: {{ x }} / Y: {{ y }} - Width: {{ width }} / Height: {{ height }}</p>
    </zw-drag-resize>
  </div>
</template>

<script>
import ZwDragResize from 'zw-drag-resize'

export default {
  data: function () {
    return {
      width: 0,
      height: 0,
      x: 0,
      y: 0
    }
  },
  methods: {
    onResize: function (e) {
      const {x,y,width,height} = e
      this.x = x
      this.y = y
      this.width = width
      this.height = height
    },
    onDrag: function (e) {
      const {x,y} = e
      this.x = x
      this.y = y
    }
  }
}
</script>

Props

active

Type: Boolean
Required: false
Default: false

Determines if the component should be active or not. The prop reacts to changes and also can be used with the syncmodifier to keep the state in sync with the parent.

<zw-drag-resize :active="true">

draggable

Type: Boolean
Required: false
Default: true

Defines it the component should be draggable or not.

<zw-drag-resize :draggable="false">

resizable

Type: Boolean
Required: false
Default: true

Defines it the component should be resizable or not.

<zw-drag-resize :resizable="false">

w

Type: Number
Required: false
Default: 200

Define the width of the element,it is programmable.

<zw-drag-resize :w="200">

h

Type: Number
Required: false
Default: 200

Define the height of the element,it is programmable.

<zw-drag-resize :h="200">

minw

Type: Number
Required: false
Default: 50

Define the minimal width of the element.

<zw-drag-resize :minw="50">

minh

Type: Number
Required: false
Default: 50

Define the minimal height of the element.

<zw-drag-resize :minh="50">

x

Type: Number
Required: false
Default: 0

Define the x position of the element,it is programmable.

<zw-drag-resize :x="0">

y

Type: Number
Required: false
Default: 0

Define the y position of the element,it is programmable.

<zw-drag-resize :y="0">

z

Type: Number|String
Required: false
Default: auto

Define the zIndex of the element.

<zw-drag-resize :z="999">

handles

Type: Array
Required: false
Default: ['tl', 'tm', 'tr', 'mr', 'br', 'bm', 'bl', 'ml']

Define the array of handles to restrict the element resizing:

  • tl - Top left
  • tm - Top middle
  • tr - Top right
  • mr - Middle right
  • br - Bottom right
  • bm - Bottom middle
  • bl - Bottom left
  • ml - Middle left
<zw-drag-resize :handles="['tm','bm','ml','mr']">

axis

Type: String
Required: false
Default: both

Define the axis on which the element is draggable. Available values are x, y or both.

<zw-drag-resize axis="x">

grid

Type: Array
Required: false
Default: [1,1]

Define the grid on which the element is snapped.

<zw-drag-resize :grid="[1,1]">

parent

Type: Boolean
Required: false
Default: false

Restricts the movement and the dimensions of the element to the parent.

<zw-drag-resize :parent="true">

dragHandle

Type: String
Required: false

Defines the selector that should be used to drag the component.

<zw-drag-resize drag-handle=".drag">

dragCancel

Type: String
Required: false

Defines a selector that should be used to prevent drag initialization.

<zw-drag-resize drag-cancel=".drag">

maximize

Type: Boolean
Required: false
Default: false

If set to true allows the component to fill its parent when double-clicked.

<zw-drag-resize :maximize="true">

Events

activated

Required: false
Parameters: -

Called whenever the component gets clicked, in order to show handles.

<zw-drag-resize @activated="onActivated">

deactivated

Required: false
Parameters: -

Called whenever the user clicks anywhere outside the component, in order to deactivate it.

<zw-drag-resize @deactivated="onDeactivated">

resizing

Required: false
Parameters: Object with properties below

  • left the X position of the element
  • top the Y position of the element
  • width the width of the element
  • height the height of the element

Called whenever the component gets resized.

<zw-drag-resize @resizing="onResizing">

resizestop

Required: false
Parameters: Object with properties below

  • left the X position of the element
  • top the Y position of the element
  • width the width of the element
  • height the height of the element

Called whenever the component stops getting resized.

<zw-drag-resize @resizestop="onResizstop">

dragging

Required: false
Parameters: Object with properties below

  • left the X position of the element
  • top the Y position of the element

Called whenever the component gets dragged.

<zw-drag-resize @dragging="onDragging">

dragstop

Required: false
Parameters: Object with properties below

  • left the X position of the element
  • top the Y position of the element

Called whenever the component stops getting dragged.

<zw-drag-resize @dragstop="onDragstop">

Gotchas

Be careful to use appropriate values for x, y, w, h, minh and minh props when you want to restrict the component in its parent element.

Bonus

If resizing, parent and maximize props are true you can double-click on the element to make it fill the parent.

Roadmap

  • Touch support (thanks @ojczeo)
  • Fix grid issues (#34, #58) - In progress
  • Implement Storybook - In progress
  • Aspect ratio on resizing (#26) - In progress
  • maxWidth and maxHeight props (#76) - In progress
  • Different approach with CSS styles (#73)
  • Watching props for changes (requires refactoring and possibly breaking changes)

Contributing

Any contribution to the code or any part of the documentation and any idea and/or suggestion are very welcome.

# serve with hot reload at localhost:8080
npm run dev

# distribution build
npm run build

# build the docs into gh-pages
npm run docs

# run unit tests
npm run test

License

MIT license