Socket
Socket
Sign inDemoInstall

touch-drag-n-drop

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    touch-drag-n-drop

Vanilla JS library to handle drag 'n drop events on touch screen devices


Version published
Weekly downloads
2
decreased by-71.43%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Touch screen drag-n-drop

Lightweight vanilla JS library to handle drag 'n drop events on touch screen devices, with a simple and native-like API.

Usage

Import the library and init an handler, once the document is ready.
The init(document, config) method will return an handler object, to which we can bind all needed event listeners. Always pass a valid draggableSelector and droppableSelector to the config argument; drag/drop events won't be fired on any other DOM element.

const touchDragDropLibrary = require("touch-drag-n-drop");
const touchDragDropHandler = touchDragDropLibrary.init( document, {
    draggableSelector: '.draggable',
    droppableSelector: '.droppable'
});

Add an event handler

touchDragDropHandler.on( 'drop', (dragElement, dropElement, event) => {

    console.log('A draggable element was dropped inside a drop target');

    // implement further conditional logic if needed
    if( dropElement.matches('.someKindOfDropTarget') {
        console.log('This is a special drop target')
    }
});

Utility methods are available for all supported event (dragstart, dragend, dragenter, dragleave, drop). For example:

touchDragDropHandler.on( 'drop', myFunction )

// is equivalent to writing

touchDragDropHandler.onDrop( myFunction )

Note: dragstart and dragend only accept the draggable DOM node and the DOM event as arguments for the callback function, while drop, dragenter and dragleave also support a reference to the drop target node.

touchDragDropHandler.onDragEnd((dragElement, event)=>{
    dragElement.classList.remove('dragging');
})
touchDragDropHandler.onDragEnter((dragElement, dropElement, event)=>{
    dragElement.classList.add('draggingOver');
    dropElement.classList.add('draggedOver');
})

At some point you may need to remove an event callback you previously registered.
You can do so by storing the callbackId returned by the on method and later pass it to the removeCallback method.

// let's register a callback
const myCallback = touchDragDropHandler.onDrop( (dragElement, dropElement) => {
    console.log('A draggable element was dropped inside a drop target');
});

// we don't need it anymore
touchDragDropHandler.removeDropCallback( myCallback );
// or
touchDragDropHandler.removeCallback('drop', myCallback );

And that's it.
See the full demo here, or launch it locally with npm run demo from within this repo, ord preview it live here

Note:

Using the library as shown before won't affect how native drag/drop events are handled on non-touch-screen devices.
You may want to write separate code to handle those. Or even add an abstraction layer to wrap it all together:

touchDragDropHandler = touchDragDropLibrary.init( document, {
    draggableSelector: '.draggable',
    droppableSelector: '#dropTargetId'
});

const registerDropEventCallback = ( myFunction ) => {

    // code for NON touch screen devices
    document.getElementById('dropTargetId').addEventListener('drop', myFunction )

    // code for touch screen devices
    touchDragDropHandler.onDrop( myFunction )
}
registerDropEventCallback( () => {
    console.log('Something was dropped');
})

Keywords

FAQs

Last updated on 25 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc