Add cdk drag-and-drop features
This issue tracks the high-level addition of drag-and-drop features to the cdk.
Is there any design doc I can follow @jelbourn ? I've tried quite a few drag and drop libraries, all of the are bad in one way or another, those that use native APIs are rather limited when it comes to styling the content (which is also often glitched when you drag an item that is partially hidden behind overflow) that is being dragged and those that do it programatically often don't have must have accessibility features like scrolling when the item being dragged is on the side of the scrollable element, so I really wonder how you gonna tackle this.
Kristiyan is working on a design now. Any specific feedback you have about what you like/dislike about other libraries would be good to know.
Most of the libs (with native html dnd) such as angular-dnd, sortablejs, dragula handle DOM on their own which is very inconvinient when using unidirectional dataflow (ngrx). There is no an easy way to cancel these updates and walkarounds feel very hacky. This is the biggest issue with all the libs I've used so far I would love to see the following features:
- standard drag and drop
- drag and drop within a list - sortable list
- support for dnd between different lists - nested/non-nested
- uniderctional dataflow (option to prevent DOM updates by the lib itself)
- custom styling of ghost/dragged item
- support for small devices
- scroll the droppable container if dragging over the overflow.
Would be awesome if we would get something like https://github.com/atlassian/react-beautiful-dnd
Additionally, most seem designed to simply append elements onto lists of other elements when dropped into a drop zone. I would love to see a "canvas" mode implemented to simplify the dropping into an absolute position of an element. This would be extremely useful for design oriented apps, where the user is building something from smaller elements. I used interactjs years ago and really appreciated all it's features ( touch support, inertia, grid snapping, svg support ). It's an older library but could prove useful at least for feature ideas. I'm ready to contribute in any way I can once you all have laid the plans and groundwork.
As mentioned dojchek full support for state management like ngrx/redux is very important. We don't yet have an angular dnd library that does.
What would be an example of a scenario w/ ngrx interplay with drag-and-drop?
Not sure if this is quite the example you're looking for but I have a site that I maintain that uses drag and drop to create a data sorting functionality. We built it to be closely tied into ngrx where each of the containers (items to be sorted and a container for each category is populated via ngrx store selector. When the user drags an item from one list to another, we cancel all the default drag/drop actions and instead fire a ngrx event that updates the object in question with the new category and let the container selectors "move" the item (this allows for some other things like firing save events and such).
Having something built into the cdk that could facility the drag/drop actions and allow me to intercept at certain events (ie. onDrop) to do things like fire ngrx action and cancel any further drag/drop effects would be great.
I've ported an angularjs library but intentionally just implemented drop zones to be as general purpose as possible. By having a general purpose drop zone all the ngrx use cases should be possible as well because the library just informs you something has been dropped. What should happen with that event is all up to the developer.
https://github.com/reppners/ngx-drag-drop
So if you're asking for features I'd like to see for CDK than all of what the mentioned library does because it's quite basic (does not do the lists or sorting stuff internally) but configurable to cater all use cases.
Wether it's implemented with native HTML5 drag-and-drop or implementing it all in the angular stack is up to you, but what I'd like to be possible is drag stuff from the outside of the browser into an app. This is just possible with native drag-and-drop.
It's probably important to come up with a list of drag, and drag and drop related use cases. What sort of things are we dragging? What sort of drop targets are we going to have? I suspect there's probably more permutations of the latter: single drop targets, groups of drop targets, nested drop targets. There's also a variety of ways that you might want to execute logic when interacting with those targets while dragging or dropping.
ngrx
FWIW, I wouldn't worry about Redux or ngrx when it came to current drag state. That's really a UI concern, and not something I would do as critical to persist to state.
Interesting library written in ES6 Drag and Drop lib
If we could have this: https://github.com/react-dnd/react-dnd for angular, I would be very satisfied.
I am working on drag and drop in an Angular app this week. @dojchek already mentioned most of the features required for my use case, but here are a couple more:
- support for dynamically adding and removing drop containers
- support for swappable items
- support for dnd between a sortable container and a swappable container. Example: Items in container A are sortable. Items in container B are swappable. If item A1 is dragged over item B1, then item B1 takes the place of item A1, and item A1 can be dropped in the location of item B1. If item B1 is dragged over item A1, then item B1 is inserted into container A either before or after item A1.
Would be great!
On our teams wish list:
- ghost element features (an element which is appended to the
bodyduring the dragging, so it escapes hidden overflows etc). Maybe support providing ang-templatefor it? - consider multiple positioning strategies? (i.e. you have fixed positioning, absolute positioning, relative positioning, tranforms). Or just pick the best one of these.
- draggable anchor: a specific element that only triggers the drag start (instead of the whole draggable element).
- draggable areas: an area that draggable elements cannot escape from.
- sortable behaviour, but that could become too opiniated quickly.
- animation possibilities. E.g. ghost element animating back, or animated fitting within dropzone. Maybe this can be done using hooks?
- scroll behaviour
- drop behaviour
Another trick/feature is to have the center of the ghost element as reference for the drop behaviour (instead of the pointers position). E.g. you start dragging an element by "grabbing" it at its left side, and when the center of the dragged element covers the dropzone, but your pointer is not hovering it, you still want to be able to drop it in the dropzone?
By the way. When sharing ideas, why not sharing Stackblitzes? https://draggable-with-anchors-lucas-8eq4ht.stackblitz.io 😄
Today I found some time to work out some ideas. Please see: https://stackblitz.com/edit/draggable-system
What is in it:
- basic draggable implementation using pointer events
- draggable positioning 1: positioning the host element (see movable.directive)
- draggable positioning 2: positioning a helper element (using the CDK)
- movable area: an area in which elements keep "trapped"
- sortable behaviour
- animations (a little bit hacky yet)
The directives:
- draggable: for emitting the drag events
- draggable-helper: for showing a helper element (host elements stays in place)
- movable (extends draggable): element is draggable and will position the host element
- movable-area: keeps any child movables in its area
- sortable (extends draggable): element is draggable... nothing more. But is targeted by:
- sortable-list: keeps track of child sortables and will emit sort events (so user can update its model)
Just wanted to drop my ideas here...
@dirkluijk It it supposed to be interactive? I had a look and nothing was draggable, is it supposed to be?
Oops, forgot to add polyfill for pointer events. I guess you checked on a mobile browser. Should work now!
Nah just Firefox, seems they don't have it enabled by default yet.
On Feb 16, 2018 6:02 PM, Dirk Luijk [email protected] wrote:
Oops, forgot to add polyfill for pointer events. I guess you checked on a mobile browser. Should work now!
— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/angular/material2/issues/8963#issuecomment-366383519, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AF5opZI0f_njRwOL1dNdty2Guf1qVYD5ks5tVgkbgaJpZM4Q_jF0.
Firefox worked for me. @dirkluijk Nice work! That looks better than most Angular drag and drop libraries I have found. I'm happy to see that we have so many folks chompin' at the bit to contribute to this. I think this project will mature rapidly.
Here is a second version with some droppable ideas demonstrated: https://stackblitz.com/edit/draggable-system-2
- droppable (extends draggable, to keep things separated from the base directive):
- An additional
draggableDatacan be set which will be included in the custom events emitted. - A "dragGroup" can be set to identify separate drag/drop groups.
- An additional
- drop-zone: detects when droppables are dropped inside its host element.
- "dragGroups" can be set to accept only specific groups of droppables.
@dirkluijk looks like the polyfill you added didn't stick, it works great when I do add the polyfill though!