vue-draggable-resizable
repository·main·Indexed 25 days ago
https://github.com/mauricius/vue-draggable-resizableA lightweight, zero-dependency Vue 3 component for creating draggable and resizable web elements. Version 3.0.0-beta.2 supports custom resize handles, grid snapping, aspect ratio maintenance, touch support, and provides lifecycle callbacks and events for drag and resize actions.
What's inside vue-draggable-resizable
- VueDraggableResizable 3 is a Vue component designed to make elements draggable, resizable, or both. It is a zero-dependency component that supports advanced features like custom resize handles, grid snapping, aspect ratio maintenance, and touch support.
Customize handles with named slots
mainThe component provides named slots for each handle. This allows you to inject custom markup into the resize handles.Install and basic usage of vue-draggable-resizable
mainTo use
vue-draggable-resizablein your Vue project, install it via npm and register it globally. Note that the component does not include CSS by default; you must import the styles separately.$ npm install --save vue-draggable-resizable// 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')// App.vue <template> <div style="height: 500px; width: 500px; border: 1px solid red; position: relative;"> <vue-draggable-resizable :w="100" :h="100" :parent="true"> <p>Hello! I'm a flexible component. You can drag me around and you can resize me.</p> </vue-draggable-resizable> </div> </template> <style> @import "vue-draggable-resizable/style.css"; </style>Run the Live Playground locally
mainTo explore examples and interact with the component in a local environment, you can run the project's playground using Storybook by following these steps:
- Clone the repository.
- Install dependencies using
npm install. - Start the development server with
npm run story:dev. - Open your browser and visit
http://localhost:6006/.
npm install npm run story:devConfigure component CSS classes via Props
mainYou can customize the CSS classes used by the component for various states and elements using the following props. This allows for easier custom styling of the draggable/resizable container and its handles.
className: Custom class for the component (default:vdr).classNameDraggable: Custom class whendraggableis enabled (default:draggable).classNameResizable: Custom class whenresizableis enabled (default:resizable).classNameDragging: Custom class while dragging (default:dragging).classNameResizing: Custom class while resizing (default:resizing).classNameActive: Custom class when active (default:active).classNameHandle: Custom common class for each handle element (default:handle). You can style specific handles using<your-class>-<handle-code>(e.g.,.my-handle-class-tl).
Install vue-draggable-resizable as a Vue plugin
mainTo usevue-draggable-resizableglobally in your Vue application, import the package and use it withVue.use(). This registers theVueDraggableResizablecomponent globally. The package also automatically imports its required CSS.Use lifecycle callback props for drag and resize
mainThe component provides several callback props that allow you to intercept and potentially cancel drag or resize actions. If a callback returns
false, the action is cancelled.Drag Callbacks
onDragStart(ev): Called when dragging starts. Use to prevent bubbling or cancel action.onDrag(x, y): Called before the element is dragged. Receives nextxandyvalues.
Resize Callbacks
onResizeStart(handle, ev): Called when resizing starts. Receives the handle and event.onResize(handle, x, y, width, height): Called before the element is resized. Receives handle and next dimensions/position.
Listen to component events
mainThe component emits several events to track state changes:
@activated: Emitted when the component is clicked to show handles.@deactivated: Emitted when the user clicks outside the component.@resizing: Emitted during resize. Parameters:left,top,width,height.@resizestop: Emitted when resizing ends. Parameters:left,top,width,height.@dragging: Emitted during drag. Parameters:left,top.@dragstop: Emitted when dragging ends. Parameters:left,top.
Configure component behavior and dimensions via Props
mainUse these props to control the dimensions, constraints, and interaction modes of the component.
Dimensions and Position
w: Initial width (Number|String, default:200). Supportsauto.h: Initial height (Number|String, default:200). Supportsauto.x: Initial X position (Number, default:0).y: Initial Y position (Number, default:0).z: Z-index (Number|String, default:auto).minWidth/minHeight: Minimum dimensions (Number, default:50).maxWidth/maxHeight: Maximum dimensions (Number, default:null).
Interaction and Constraints
draggable: Enable/disable dragging (Boolean, default:true).resizable: Enable/disable resizing (Boolean, default:true).axis: Axis for dragging (x,y, orboth, default:both).handles: Array of allowed resize handles (default:['tl', 'tm', 'tr', 'mr', 'br', 'bm', 'bl', 'ml']).grid: Snapping grid (Array, default:[1,1]).parent: Restrict movement/dimensions to parent (Boolean, default:false).lockAspectRatio: Lock aspect ratio (Boolean, default:false). Note: Do not use withgrid.active: Determines if component is active (Boolean, default:false).preventDeactivation: Prevent deactivation when clicking outside (Boolean, default:false).disableUserSelect: Prevents text selection during drag (Boolean, default:true).enableNativeDrag: Enables browser native drag/drop (Boolean, default:false).dragHandle: CSS selector for the drag handle (String).dragCancel: CSS selector to prevent drag (String).