Ask HN: Current best practice for drag-and-drop?
Originally it was built with HTML5 drag-and-drop, but we've started to see many issues, specifically in Chrome. For instance, previously we got a ghost image of the element for free in both Firefox and Chrome, but recently Chrome has reverted to a little gray box with the element text. We could use setDragImage, but since we're dragging plain old DOM elements that seems excessive. Chrome also seems to treat the dragEnter events strangely, making nested element hover handling difficult.
We could potentially implement a custom solution (the front-end is React, so it wouldn't be too hard) but that also seems excessive.
I'm about to add a dependency on jQuery and jQuery UI just to allow the use of their bulletproof Draggable and Droppable elements. Somehow that feels dirty. Is there a good, modern way to allow us to drag regular DOM elements and handle the hover and drop events? It doesn't seem like it should be this hard.
Do not fear making your own drag interactions from raw mouse events. I avoided it for ages, then discovered it is easier than I expected, and satisfying because what you produce is so much better quality. It's just combinations of mousedown, mousemove, mouseenter, mouseleave, and mouseup, and the confidence to attach/detach listeners for those events on the fly during the interaction. Also by thinking about the whole event sequence on a lower level you will often realise a better way to design the UX of the interaction.
If you want a halfway house, I've just remembered this little jQuery plugin, not well known at all, but it is the best thing available:
http://threedubmedia.com/code/event/drag
For me this plugin was a bridge between using DnD libraries and writing raw mouse listeners. It basically just fires off events at key points during a drag sequence, and it's up to you to update element styles to move them around... ie, you can't see anything physically happening on the page unless you update the DOM yourself (not difficult). It's basically sugar around mouse events to make them a lot less intimidating, eg, on each move it gives you the mouse position relative to wherever the drag started, so you don't have to keep track of this yourself.
Compared to any regular DnD library, it's better performing and miles more flexible. It's almost as good as doing it with raw mouse events. Look at the demos and view the code, they are impressive. It's old though. Hope it still works with modern jQuery.
> I'm about to add a dependency on jQuery and jQuery UI just to allow the use of their bulletproof Draggable and Droppable elements. Somehow that feels dirty.
What feels even more dirty is a UI that doesnt work properly across browsers.Better have a solid UI rather than thinking using jQuery == DIRTY.
And as I mentioned in another comment, jQuery itself isn't dirty, but adding a dependency on a swiss army knife library for just one feature does seem dirty.
1. http://gtramontina.github.io/draggable.js/ 2. http://kbjr.github.io/DragDrop/index.html
Even with jQuery and jQuery UI, I see a few bugs and inconsistencies across browsers from time to time. However, you only have to look at the bug trackers for those projects to see how many more issues they have already resolved in this area.
I’m all for keeping things light and avoiding unnecessary dependencies on external libraries, but this is one of those issues where you’d be mad to try and get everything working properly yourself. This is one of the nastiest, most edge-case-ridden areas of front-end web development today, and you surely have better things to do with your time than fix every browser oddity one by one.
In other words, do not worry - it is not dirty, it is exactly the purpose of jQuery.
I'm currently trying JQUI with touchpunch and have considered hammer.js. Anyone have experience with scrolling and dnd on mobile?
The native iOS answer is actually if the finger is static for a certain time interval then it is a click, but if it begins moving immediately then it is a scroll (swipe).
Here it is.
http://www.codingupfengshui.io/functional-reactive-programmi...