Vue3 Component for resizable and draggable elements
npm install vue-draggable-resizable



> Vue Component for draggable and resizable elements.
If you are looking for the version 1 of the component, it is available on the v1 branch.
* Features
* Live Playground
* Install and basic usage
* Props
* Events
* Styling
* Contributing
* License
* 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
* Maintain aspect ratio
* Touch enabled
* Use your own classes
* Provide your own markup for handles
For examples of the component go to the live playground
Alternatively you can run the playground on your own computer:
* Clone this repository
* npm install
* npm run story:dev
* Visit http://localhost:6006/
---
``bash`
$ npm install --save vue-draggable-resizable
Register the component globally
`js
// main.js
import { createApp } from 'vue'
import VueDraggableResizable from 'vue-draggable-resizable'
import App from './App.vue'
createApp(App)
.component("vue-draggable-resizable", VueDraggableResizable)
.mount('#app')
`
You may now use the component in your markup
` Hello! I'm a flexible component. You can drag me around and you can resize me.vue`
// App.vue
The component itself does not include any CSS. You'll need to include it separately in your App.vue:
`vue`
#### className
Type: Stringfalse
Required: vdr
Default:
Used to set the custom class of a draggable-resizable component.
`html`
#### classNameDraggable
Type: Stringfalse
Required: draggable
Default:
Used to set the custom class of a draggable-resizable component when draggable is enable.
`html`
#### classNameResizable
Type: Stringfalse
Required: resizable
Default:
Used to set the custom class of a draggable-resizable component when resizable is enable.
`html`
#### classNameDragging
Type: Stringfalse
Required: dragging
Default:
Used to set the custom class of a draggable-resizable component when is dragging.
`html`
#### classNameResizing
Type: Stringfalse
Required: resizing
Default:
Used to set the custom class of a draggable-resizable component when is resizing.
`html`
#### classNameActive
Type: Stringfalse
Required: active
Default:
Used to set the custom class of a draggable-resizable component when is active.
`html`
#### classNameHandle
Type: Stringfalse
Required: handle
Default:
Used to set the custom common class of each handle element. This way you can style each handle individually using the selector , where handle code identifies one of the handles provided by the handle prop.
So for example, this component:
`html`
renders the following:
`html`
[...]
#### scale
Type: Number|Arrayfalse
Required: 1
Default:
The scale prop controls the scale property when the CSS 3 scale transformation is applied to one of the parent elements. If not provided the default value is 1.
`html
`
#### disableUserSelect
Type: Booleanfalse
Required: true
Default:
By default, the component adds the style declaration 'user-select:none' to itself to prevent text selection during drag. You can disable this behaviour by setting this prop to false.
`html`
#### enableNativeDrag
Type: Booleanfalse
Required: false
Default:
By default, the browser's native drag and drop funcionality (usually used for images and some other elements) is disabled, as it may conflict with the one provided by the component. If you need, for whatever reason, to have this functionality back you can set this prop to true.
`html`
#### active
Type: Booleanfalse
Required: false
Default:
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. You can use along with the preventDeactivation prop in order to fully control the active behavior from outside the component.
`html`
#### preventDeactivation
Type: Booleanfalse
Required: false
Default:
Determines if the component should be deactivated when the user clicks/taps outside it.
`html`
#### draggable
Type: Booleanfalse
Required: true
Default:
Defines it the component should be draggable or not.
`html`
#### resizable
Type: Booleanfalse
Required: true
Default:
Defines it the component should be resizable or not.
`html`
#### w
Type: Number|Stringfalse
Required: 200
Default:
Define the initial width of the element. It also supports auto, but when you start resizing the value will fallback to a number.
`html`
#### h
Type: Number|Stringfalse
Required: 200
Default:
Define the initial height of the element. It also supports auto, but when you start resizing the value will fallback to a number.
`html`
#### minWidth
Type: Numberfalse
Required: 50
Default:
Define the minimal width of the element.
`html`
#### minHeight
Type: Numberfalse
Required: 50
Default:
Define the minimal height of the element.
`html`
#### maxWidth
Type: Numberfalse
Required: null
Default:
Define the maximum width of the element.
`html`
#### maxHeight
Type: Numberfalse
Required: null
Default:
Define the maximum height of the element.
`html`
#### x
Type: Numberfalse
Required: 0
Default:
Define the initial x position of the element.
`html`
#### y
Type: Numberfalse
Required: 0
Default:
Define the initial y position of the element.
`html`
#### z
Type: Number|Stringfalse
Required: auto
Default:
Define the z-index of the element.
`html`
#### handles
Type: Arrayfalse
Required: ['tl', 'tm', 'tr', 'mr', 'br', 'bm', 'bl', 'ml']
Default:
Define the array of handles to restrict the element resizing:
* tl - Top lefttm
* - Top middletr
* - Top rightmr
* - Middle rightbr
* - Bottom rightbm
* - Bottom middlebl
* - Bottom leftml
* - Middle left
`html`
#### axis
Type: Stringfalse
Required: both
Default:
Define the axis on which the element is draggable. Available values are x, y or both.
`html`
#### grid
Type: Arrayfalse
Required: [1,1]
Default:
Define the grid on which the element is snapped.
`html`
#### parent
Type: Booleanfalse
Required: false
Default:
Restricts the movement and the dimensions of the component to the parent.
`html`
#### dragHandle
Type: Stringfalse
Required:
Defines the selector that should be used to drag the component.
`html`
#### dragCancel
Type: Stringfalse
Required:
Defines a selector that should be used to prevent drag initialization.
`html`
#### lockAspectRatio
Type: Booleanfalse
Required: false
Default:
The lockAspectRatio property is used to lock aspect ratio. This property doesn't play well with grid, so make sure to use only one at a time.
`html`
#### onDragStart
Type: Functionfalse
Required: null
Default:
Called when dragging starts (element is clicked or touched). If false is returned by any handler, the action will cancel. You can use this function to prevent bubbling of events.
`html`
`js`
function onDragStartCallback(ev){
...
// return false; — for cancel
}
#### onDrag
Type: Functionfalse
Required: null
Default:
Called before the element is dragged. The function receives the next values of x and y. If false is returned by any handler, the action will cancel.
`html`
`js`
function onDragStartCallback(x, y){
...
// return false; — for cancel
}
#### onResizeStart
Type: Functionfalse
Required: null
Default:
Called when resizing starts (handle is clicked or touched). If false is returned by any handler, the action will cancel.
`html`
`js
function onResizeStartCallback(handle, ev){
...
// return false; — for cancel
}
`
#### onResize
Type: Functionfalse
Required: null
Default:
Called before the element is resized. The function receives the handle and the next values of x, y, width and height. If false is returned by any handler, the action will cancel.
`html`
`js
function onResizeStartCallback(handle, x, y, width, height){
...
// return false; — for cancel
}
`
---
#### activated
Parameters: -
Called whenever the component gets clicked, in order to show handles.
`html`
#### deactivated
Parameters: -
Called whenever the user clicks anywhere outside the component, in order to deactivate it.
`html`
#### resizing
Parameters:
* left the X position of the elementtop
* the Y position of the elementwidth
* the width of the elementheight
* the height of the element
Called whenever the component gets resized.
`html`
#### resizestop
Parameters:
* left the X position of the elementtop
* the Y position of the elementwidth
* the width of the elementheight
* the height of the element
Called whenever the component stops getting resized.
`html`
#### dragging
Parameters:
* left the X position of the elementtop
* the Y position of the element
Called whenever the component gets dragged.
`html`
#### dragstop
Parameters:
* left the X position of the elementtop
* the Y position of the element
Called whenever the component stops getting dragged.
`html`
---
You can style the component using appropriate class names passed as props to the component. Moreover you can replace the default styles for the handles, provided in the source file vue-draggable-resizable.css, but you should take care to define position and size for them. The default classes for handles are handle and handle-tl, handle-br and so on.
The component also provides named slots for each handle, so you can use your markup inside each one.
Thanks to @kirillmurashov for his work on vue-drag-resize component.
If you discover any security related issues, please email maurizio.bonani@gmail.com instead of using the issue tracker.
Any contribution to the code or any part of the documentation and any idea and/or suggestion are very welcome.
` bashserve with hot reload at localhost:8080
npm run dev
The MIT License (MIT). Please see License File for more information.