Very first attempt for a hypespecific Drag-and-Drop-Enabler
–>No custom code required! <-- (For designers … )
Setup is done via the HTML-Attribute-Panel
The Elements that are exposed to be dragged can have four different data-attributes:
data-drag (value -> true)
… this enables observation. Set it to true otherwise nothing will happen
data-drop (value -> classname)
Elements with this class will accept the dragged element as a drop
data-dropsnap (value -> true)
dragged elements will snap to the left and top of the droppable element
data-dragreverse (value -> true)
if a dragged element does not end on an droppable element it’ll reverse to its startposition
A droppable Element requires a class corresponding to the data-drop-attribute of the draggable element.
data-custombehavior (value -> name of a custombehavior)
the custombehavior will be executed on successful drop. so here is a hook for some logic …
The solution relies on intersectionObserver. substitution of older Browsers will require a Polyfill -> not activated yet (an outcommented Polyfilllink is outcommented in the hypeexample).
All areas considered by the Intersection Observer API are rectangles; elements which are irregularly shaped are considered as occupying the smallest rectangle which encloses all of the element’s parts.
Please ensure that draggable and droppable elements share a same coordinatespace in hype. (do not place them in different groups/symbols with different top/left values …)
@MaxZiebs MutationObserver thx
HypeDragAndDropEnabler.js (already included: https://github.com/ChrisCavs/bounds.js). You’ll find it within the hypefile
Code is actually a mess and i hope it’ll work
Half day spend with creating this … i’ll switch to drinking beer now
Bugreports and improvement proposals are welcome