npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

touch-drag-n-drop

v1.0.5

Published

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

Downloads

17

Readme

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');
})