vue-draggable-resizable

repository·main·Indexed 25 days ago

https://github.com/mauricius/vue-draggable-resizable

A 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.

Tokens
1.5K
Snippets
2
Records
9
Agent score
36%

What's inside vue-draggable-resizable

  1. Overview of VueDraggableResizable 3

    main
    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.
  2. Install and basic usage of vue-draggable-resizable

    main

    To use vue-draggable-resizable in 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>
  3. Run the Live Playground locally

    main

    To explore examples and interact with the component in a local environment, you can run the project's playground using Storybook by following these steps:

    1. Clone the repository.
    2. Install dependencies using npm install.
    3. Start the development server with npm run story:dev.
    4. Open your browser and visit http://localhost:6006/.
    npm install
    npm run story:dev
  4. Configure component CSS classes via Props

    main

    You 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 when draggable is enabled (default: draggable).
    • classNameResizable: Custom class when resizable is 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).
  5. Use lifecycle callback props for drag and resize

    main

    The 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 next x and y values.

    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.
  6. Listen to component events

    main

    The 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.
  7. Configure component behavior and dimensions via Props

    main

    Use these props to control the dimensions, constraints, and interaction modes of the component.

    Dimensions and Position

    • w: Initial width (Number|String, default: 200). Supports auto.
    • h: Initial height (Number|String, default: 200). Supports auto.
    • 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, or both, 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 with grid.
    • 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).