Skip to content

Draggable

The Draggable component allows you to make any element draggable within a specified area. It provides a simple way to implement drag-and-drop functionality with customizable options for boundaries, drag handles, and event callbacks.

  • Metro UI Core
  • DOM module
<!-- Make an element draggable -->
<div data-role="draggable">
This element can be dragged
</div>
<!-- Make an element draggable with a specific handle -->
<div data-role="draggable" data-drag-element=".handle">
<div class="handle">Drag here</div>
<div>Content of the draggable element</div>
</div>
<!-- Allow dragging beyond container boundaries -->
<div class="container">
<div data-role="draggable" data-boundary-restriction="false">
This element can be dragged outside its container
</div>
</div>
// Initialize with default options
Metro.makePlugin(element, "draggable");
// Initialize with custom options
Metro.makePlugin(element, "draggable", {
dragArea: "#container",
boundaryRestriction: true,
onDragStart: function(event) {
console.log("Started dragging", event);
}
});
ParameterTypeDefaultDescription
dragContextObjectnullContext object passed to event callbacks
draggableDeferredNumber0Delay in milliseconds before initialization
dragElementString"self"Selector for the drag handle element. Use “self” to make the entire element draggable
dragAreaString"parent"Selector for the container that limits the draggable area. Can be “parent”, “body”, or any valid CSS selector
timeoutNumber0Timeout for initialization
boundaryRestrictionBooleantrueWhen true, restricts dragging within the boundaries of the dragArea
onCanDragFunctionMetro.noop_trueCallback function that determines if dragging is allowed. Should return true to allow dragging
onDragStartFunctionMetro.noopCallback function triggered when dragging starts
onDragStopFunctionMetro.noopCallback function triggered when dragging stops
onDragMoveFunctionMetro.noopCallback function triggered during dragging movement
onDraggableCreateFunctionMetro.noopCallback function triggered when the draggable component is created

Enables dragging functionality for the element.

const draggable = Metro.getPlugin(element, "draggable");
draggable.on();

Disables dragging functionality for the element.

const draggable = Metro.getPlugin(element, "draggable");
draggable.off();

Removes the draggable functionality and event listeners from the element.

const draggable = Metro.getPlugin(element, "draggable");
draggable.destroy();
EventDescription
onCanDragTriggered to determine if dragging is allowed. Should return true to allow dragging
onDragStartTriggered when dragging starts
onDragStopTriggered when dragging stops
onDragMoveTriggered during dragging movement
onDraggableCreateTriggered when the draggable component is created

The Draggable component doesn’t use CSS variables for styling. It applies a class to elements while they are being dragged.

  • .draggable - Applied to elements while they are being dragged. Adds a shadow effect.
.draggable {
box-shadow: rgba(0, 0, 0, 0.35) 0 5px 15px!important;
}
/* Custom styling for dragged elements */
.draggable {
box-shadow: rgba(0, 0, 255, 0.4) 0 0 20px!important;
opacity: 0.8;
transform: rotate(2deg);
}
  1. Use the dragElement option to specify a handle when you want only part of an element to be draggable
  2. Set boundaryRestriction to true to prevent elements from being dragged outside their container
  3. Use the dragArea option to define a specific area where the element can be dragged
  4. Implement the onCanDrag callback to conditionally allow or prevent dragging based on your application’s state
  5. Consider the user experience when implementing draggable elements, ensuring they provide visual feedback during dragging