Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Dragula is a JavaScript library that provides drag-and-drop functionality for web applications. It is designed to be simple and easy to use, allowing developers to add drag-and-drop features to their projects with minimal configuration.
Basic Drag-and-Drop
This feature allows you to enable basic drag-and-drop functionality between two containers. The code sample demonstrates how to set up drag-and-drop between two HTML elements with IDs 'left' and 'right'.
const dragula = require('dragula');
const containers = [document.querySelector('#left'), document.querySelector('#right')];
dragula(containers);
Copy Items
This feature allows you to copy items instead of moving them. The code sample shows how to enable the copy functionality, so items dragged from one container to another are duplicated rather than moved.
const dragula = require('dragula');
const containers = [document.querySelector('#left'), document.querySelector('#right')];
dragula(containers, {
copy: true
});
Handle Drag Events
This feature allows you to handle drag events. The code sample demonstrates how to listen for the 'drag' event and log a message when an element is being dragged.
const dragula = require('dragula');
const containers = [document.querySelector('#left'), document.querySelector('#right')];
const drake = dragula(containers);
drake.on('drag', function(el) {
console.log('Element is being dragged:', el);
});
SortableJS is a popular library for creating sortable lists and grids using native drag-and-drop functionality. It offers more advanced features compared to Dragula, such as multi-drag, swap, and custom animations.
React Beautiful DnD is a drag-and-drop library specifically designed for React applications. It provides a higher level of customization and better integration with React's state management compared to Dragula.
InteractJS is a versatile library that supports drag-and-drop, resizing, and multi-touch gestures. It offers more comprehensive functionality and customization options than Dragula, making it suitable for complex interactions.
Drag and drop so simple it hurts
Browser support includes every sane browser and IE7+. (Granted you polyfill the functional Array
methods in ES5)
Have you ever wanted a drag and drop library that just works? That doesn't just depend on bloated frameworks, that has great support? That actually understands where to place the elements when they are dropped? That doesn't need you to do a zillion things to get it to work? Well, so did I!
You can get it on npm.
npm install dragula --save
Or bower, too. (note that it's called dragula.js
in bower)
bower install dragula.js --save
Dragula provides the easiest possible API to make drag and drop a breeze in your applications.
dragula(containers?, options?)
By default, dragula
will allow the user to drag an element in any of the containers
and drop it in any other container in the list. If the element is dropped anywhere that's not one of the containers
, the event will be gracefully cancelled according to the revertOnSpill
and removeOnSpill
options.
Note that dragging is only triggered on left clicks, and only if no meta keys are pressed. Clicks on buttons and anchor tags are ignored, too.
The example below allows the user to drag elements from left
into right
, and from right
into left
.
dragula([document.querySelector('#left'), document.querySelector('#right')]);
You can also provide an options
object. Here's an overview of the default values.
dragula(containers, {
isContainer: function (el) {
return false; // only elements in drake.containers will be taken into account
},
moves: function (el, container, handle) {
return true; // elements are always draggable by default
},
accepts: function (el, target, source, sibling) {
return true; // elements can be dropped in any of the `containers` by default
},
invalid: function (el, target) { // prevent buttons and anchor tags from starting a drag
return el.tagName === 'A' || el.tagName === 'BUTTON';
},
direction: 'vertical', // Y axis is considered when determining where an element would be dropped
copy: false, // elements are moved by default, not copied
revertOnSpill: false, // spilling will put the element back where it was dragged from, if this is true
removeOnSpill: false, // spilling will `.remove` the element, if this is true
delay: false // enable regular clicks by setting to true or a number of milliseconds
});
You can omit the containers
argument and add containers dynamically later on.
var drake = dragula({
copy: true
});
drake.containers.push(container);
You can also set the containers
from the options
object.
var drake = dragula({ containers: containers });
And you could also not set any arguments, which defaults to a drake without containers and with the default options.
var drake = dragula();
The options are detailed below.
options.containers
Setting this option is effectively the same as passing the containers in the first argument to dragula(containers, options)
.
options.isContainer
Besides the containers that you pass to dragula
, or the containers you dynamically push
or unshift
from drake.containers, you can also use this method to specify any sort of logic that defines what is a container for this particular drake
instance.
The example below dynamically treats all DOM elements with a CSS class of dragula-container
as dragula containers for this drake
.
var drake = dragula({
isContainer: function (el) {
return el.classList.contains('dragula-container');
}
});
options.moves
You can define a moves
method which will be invoked with (el, container, handle)
whenever an element is clicked. If this method returns false
, a drag event won't begin, and the event won't be prevented either. The handle
element will be the original click target, which comes in handy to test if that element is an expected "drag handle".
options.accepts
You can set accepts
to a method with the following signature: (el, target, source, sibling)
. It'll be called to make sure that an element el
, that came from container source
, can be dropped on container target
before a sibling
element. The sibling
can be null
, which would mean that the element would be placed as the last element in the container. Note that if options.copy
is set to true
, el
will be set to the copy, instead of the originally dragged element.
Also note that the position where a drag starts is always going to be a valid place where to drop the element, even if accepts
returned false
for all cases.
options.copy
If copy
is set to true
, items will be copied rather than moved. This implies the following differences:
Event | Move | Copy |
---|---|---|
drag | Element will be concealed from source | Nothing happens |
drop | Element will be moved into target | Element will be cloned into target |
remove | Element will be removed from DOM | Nothing happens |
cancel | Element will stay in source | Nothing happens |
options.revertOnSpill
By default, spilling an element outside of any containers will move the element back to the drop position previewed by the feedback shadow. Setting revertOnSpill
to true
will ensure elements dropped outside of any approved containers are moved back to the source element where the drag event began, rather than stay at the drop position previewed by the feedback shadow.
options.removeOnSpill
By default, spilling an element outside of any containers will move the element back to the drop position previewed by the feedback shadow. Setting removeOnSpill
to true
will ensure elements dropped outside of any approved containers are removed from the DOM. Note that remove
events won't fire if copy
is set to true
.
options.direction
When an element is dropped onto a container, it'll be placed near the point where the mouse was released. If the direction
is 'vertical'
, the default value, the Y axis will be considered. Otherwise, if the direction
is 'horizontal'
, the X axis will be considered.
options.delay
Number of milliseconds during which clicks where the mouse button is released will be treated as regular clicks instead of very short lived drags. When delay
is set to true
, a default of 300
milliseconds is used. Defaults to false
.
options.invalid
You can provide an invalid
method with a (el, target)
signature. This method should return true
for elements that shouldn't trigger a drag. Here's the default implementation, which prevents drags originating from anchor elements and buttons.
function invalidTarget (el) {
return el.tagName === 'A' || el.tagName === 'BUTTON';
}
Note that invalid
will be invoked on the DOM element that was clicked and every parent up to immediate children of a drake
container.
The dragula
method returns a tiny object with a concise API. We'll refer to the API returned by dragula
as drake
.
drake.addContainer(container)
DEPRECATED. Use drake.containers instead. Adds a container
to the containers
collection. It can be a single DOM element or an array.
drake.removeContainer(container)
DEPRECATED. Use drake.containers instead. Removes a container
from the containers
collection. It can be a single DOM element or an array.
drake.containers
This property contains the collection of containers that was passed to dragula
when building this drake
instance. You can push
more containers and splice
old containers at will.
drake.dragging
This property will be true
whenever an element is being dragged.
drake.start(item)
Enter drag mode without a shadow. This method is most useful when providing complementary keyboard shortcuts to an existing drag and drop solution. Even though a shadow won't be created at first, the user will get one as soon as they click on item
and start dragging it around. Note that if they click and drag something else, .end
will be called before picking up the new item.
drake.end()
Gracefully end the drag event as if using the last position marked by the preview shadow as the drop target. The proper cancel
or drop
event will be fired, depending on whether the item was dropped back where it was originally lifted from (which is essentially a no-op that's treated as a cancel
event).
drake.cancel(revert)
If an element managed by drake
is currently being dragged, this method will gracefully cancel the drag action. You can also pass in revert
at the method invocation level, effectively producing the same result as if revertOnSpill
was true
.
Note that a "cancellation" will result in a cancel
event only in the following scenarios.
revertOnSpill
is true
drake.remove()
If an element managed by drake
is currently being dragged, this method will gracefully remove it from the DOM.
drake.on
(Events)The drake
is an event emitter. The following events can be tracked using drake.on(type, listener)
:
Event Name | Listener Arguments | Event Description |
---|---|---|
drag | el, container | el was lifted from container |
dragend | el | Dragging event for el ended with either cancel , remove , or drop |
drop | el, container, source | el was dropped into container , and originally came from source |
cancel | el, container | el was being dragged but it got nowhere and went back into container , its last stable parent |
remove | el, container | el was being dragged but it got nowhere and it was removed from the DOM. Its last stable parent was container . |
shadow | el, container | el , the visual aid shadow, was moved into container . May trigger many times as the position of el changes, even within the same container |
cloned | clone, original | DOM element original was cloned as clone . Triggers for mirror images and when copy: true |
drake.destroy()
Removes all drag and drop events used by dragula
to manage drag and drop between the containers
. If .destroy
is called while an element is being dragged, the drag will be effectively cancelled.
MIT
FAQs
Drag and drop so simple it hurts
The npm package dragula receives a total of 194,997 weekly downloads. As such, dragula popularity was classified as popular.
We found that dragula demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.