What is dragula?
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.
What are dragula's main functionalities?
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);
});
Other packages similar to dragula
sortablejs
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
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
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.
Dragula
Drag and drop so simple it hurts
Browser support includes every sane browser and IE7+.
Inspiration
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!
Features
- Super easy to set up
- No bloated dependencies
- Figures out sort order on its own
Install
You can get it on npm.
npm install dragula --save
Or bower, too.
bower install dragula --save
Usage
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
, it'll be sent back to the container it was originally taken from.
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([left, right]);
You can also provide an options
object. The options are detailed below.
options.accepts
You can set accepts
to a method with the following signature: (el, target, source)
. It'll be called to make sure that an element el
, that came from container source
, can be dropped on container target
. Note that if options.copy
is set to true
the copied element will be provided as el
, instead of the original.
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 |
cancel | Element will stay at source | Nothing happens |
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.
API
The dragula
method returns a tiny object with a concise API. We'll refer to the API returned by dragula
as drake
.
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.
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 |
drop | el, container | el was dropped into container |
cancel | el, container | el was being dragged but it got nowhere and went back into container |
License
MIT