-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose grab method #375
base: master
Are you sure you want to change the base?
Expose grab method #375
Conversation
Definitely would like this. My use case would be having a handle completely outside the draggable element, which means I'd need to initiate the drag when the external handle is target of mousedown. |
Thanks for this one @ben-girardet |
Please merge :) I guess this could also be used to enable grabbing and dragging multiple items? |
Hi, I'm trying to implement this, have pulled your commit into my app, but I'm not exactly understanding how the native dragula behaviour is being stopped by this. I can see that you are firing the grab event manually, but doesn't the event need to have its default behaviour prevented somehow? I can't see where you are doing this. |
@rohan-deshpande I use the standard dragula API for everything else but manually grab an element. From there, I continue to manage the behavior with the standard drake instance ( |
@ben-girardet Hmm I guess what I'm saying is, Dragula itself fires the |
@rohan-deshpande yeah you need to find your workaround for this. So my way of doing this follow this logic: // this is inside an touch or mouse event when I must determine if I want to grab or not the element, it's not dragula code, just plain HTML5/JS code.
if (this.dragApi && !this.isScrolling) {
stepEl.classList.add('lifted');
setTimeout(() => {
stepEl.classList.remove('lifted');
}, 200);
this.lifted = true;
this.dragApi.grab(event);
setTimeout(() => {
this.lifted = false;
}, 100);
} The idea here is that I add or remove a "lifted" CSS class in the element depending on timeout value. Then in the dragula config object, I use this class to allow or not the move: this.dragApi = dragula({
containers: [document.querySelector('steps-container')],
isContainer: (el) => {
if (el.id === 'drag-container') return false;
if (el.classList.contains('drop-step')) return true;
return false;
},
revertOnSpill: true,
delay: 200,
mirrorContainer: document.querySelector('#drag-container'),
moves: (el) => {
if (this.lifted) {
this.lifted = false;
return el.classList.contains('step');
}
return false;
}
}); Hope this can help you. |
Exposing the
grab
method allow developers to initiate a grab movement using custom logic.This simple change gives powerful freedom to developers in the way they want the drag and drop to operate. In my case I'm now able to add a timeout before dragging can start. Here is my process:
mousedown
andtouchstart
events on elements I would like to be draggableshouldLift()
method that which start a timeout functionmouseup
andtouchend
events. These events are received in aclearShouldLift()
method which clear the timeoutclearShouldLift()
occurs, I fire a.grab(event)
in the drake instance, passing along the event received frommousedown
ortouchstart
I find that exposing the
grab
method is a minor change increasing the opportunities on the way to use the dragula api.