๐Ÿ–ฑ๏ธ Draggable Mixin

Add Drag Gesture Detection to Any Element

โ† Back to Demo Page

About Draggable Mixin

This mixin adds drag gesture detection to any web component. It supports drag start, move, and end events with customizable thresholds. Perfect for creating draggable interfaces and interactive elements.

๐Ÿ’ก Tip: This mixin coordinates with other mixins using the PointerCoordinator, so it can work alongside SwipeableMixin without conflicts!

๐Ÿงช Simple Test

Try dragging this red box first to test basic functionality (use mouse on desktop or touch on mobile):

Simple Test Element

Drag this red box to test basic functionality (threshold: 5px)

๐ŸŽฏ Interactive Drag Demo

Try dragging the container below in different directions:

๐Ÿ–ฑ๏ธ

Drag Me!

Use mouse or touch to drag this element

Distance: 0px
Direction: None
Status: Idle
0 Drag Start
0 Drag Move
0 Drag End

โš™๏ธ Drag Settings

px (minimum distance to start dragging)

๐Ÿ“œ Drag History

Drag history will appear here...

๐ŸŽจ Multiple Draggable Elements

Card 1

๐Ÿ”„

Drag to interact

Card 2

๐ŸŽฏ

Different behavior

๐Ÿค Mixin Coordination Demo

This element has both DraggableMixin and SwipeableMixin. Try dragging and swiping to see how they coordinate:

๐Ÿค

Drag + Swipe

Drag to move, swipe for quick gestures

Last Action: None
Drag Events: 0
Swipe Events: 0

๐Ÿ”„ Reversed Order Coordination Demo

This element has SwipeableMixin as parent and DraggableMixin as child. Test if coordination works in reverse order:

๐Ÿ”„

Swipe + Drag (Reversed)

Swipe for quick gestures, drag to move

Last Action: None
Drag Events: 0
Swipe Events: 0

๐Ÿ“œ Scroll Prevention Test

This area is scrollable. Try scrolling normally, then try dragging/swiping the colored boxes to see how scroll prevention works:

Scrollable content starts here...

Scroll down to see more content...

Test Area 1 - Draggable Element

Drag this box
Scroll prevention should activate when dragging

Test Area 2 - Swipeable Element

Swipe this box
Scroll prevention should activate when swiping

Test Area 3 - Combined Element

Drag or Swipe this box
Both gestures should work and prevent scrolling

More scrollable content...

Scroll up to see the test elements again...

๐Ÿ’ก How it works:
โ€ข Normal scrolling works when not interacting with mixins
โ€ข Scroll prevention activates only when gestures are detected
โ€ข Scroll prevention deactivates when gestures end
โ€ข Multiple mixins coordinate through PointerCoordinator

๐Ÿ“– Implementation Guide

How to Use the Draggable Mixin:

1. Use the element: <draggable-mixin>...content...</draggable-mixin>

2. Set optional attributes: drag-threshold

3. Listen for events: dragstart, dragmove, dragend

// Basic usage <draggable-mixin drag-threshold="5"> <div>Drag me!</div> </draggable-mixin> // JavaScript: listen for drag events const draggable = document.querySelector('draggable-mixin'); draggable.addEventListener('dragstart', (e) => { console.log('Drag started:', e.detail); }); draggable.addEventListener('dragmove', (e) => { console.log('Dragging:', e.detail); }); draggable.addEventListener('dragend', (e) => { console.log('Drag ended:', e.detail); });

โœจ Features

What the Draggable Mixin Provides:

  • Drag Detection: Detects when dragging starts, moves, and ends
  • Customizable Threshold: Adjustable minimum distance to start dragging
  • Direction Detection: Tracks drag direction (left, right, up, down)
  • Event Dispatching: Automatically dispatches drag events
  • Mixin Coordination: Works with other mixins using PointerCoordinator
  • Performance Optimized: Efficient pointer event handling
  • Easy Integration: Simple mixin pattern for any component