components icon indicating copy to clipboard operation
components copied to clipboard

Add cdk drag-and-drop features

Open jelbourn opened this issue 8 years ago • 94 comments

This issue tracks the high-level addition of drag-and-drop features to the cdk.

jelbourn avatar Dec 12 '17 19:12 jelbourn

@Angelinsky7 maybe you're already aware, but CdkDrag has

@Output('cdkDragStarted')
@Output('cdkDragEnded')
@Output('cdkDragEntered')
@Output('cdkDragExited')
@Output('cdkDragDropped')

jorroll avatar Aug 07 '18 17:08 jorroll

@thefliik thanks for your answer. Yes, i'm already aware of those events and i would like to have one more... because now, i need to inject CdkDragDropRegistry using pointerMove and it seems bad practise to me. I really think that it would be better to have a clean event, don't you ? But maybe, it's not the desired behaviour ? I would love to have a clean api to drag, drop, move and resize element Plus from a wpf/c# point of view i like to have the thumb class (https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.thumb(v=vs.110).aspx) to extend and use it to resize and move controls and i was under the impression that we could have the same in angular with this... But i'm maybe wrong.

Angelinsky7 avatar Aug 07 '18 21:08 Angelinsky7

@Angelinsky7 if I'm understanding it correctly, are you expecting to receive the MouseEvent/TouchEvent together with the CDK events?

crisbeto avatar Aug 08 '18 18:08 crisbeto

@crisbeto not exactly, i would like to have one more event (maybe called cdkDragMoved) that would be send each time that the item/mouse is moved. Finally it would only be an interface for convenience for the CdkDragDropRegistry.pointerMove observable, but we could hide the MouseEvent and had only correct position (relative to the start position, or relative to cdk parent, or else) I'm sorry if i'm not totally clear

Angelinsky7 avatar Aug 09 '18 10:08 Angelinsky7

Thanks, that makes sense. I think something similar came up during the initial drag&drop PR, but we decided not to do it, because that event would trigger change detection for every pixel that you've moved, which in turn would hurt performance.

crisbeto avatar Aug 09 '18 10:08 crisbeto

@crisbeto thanks for your answer... that is sad, is it not possible to deactivate change detection for this event ? or maybe publish a "public" api to use instead of directly using the registry ? But if we see it another way, it make sense that change detection for every pixel is a non sense in the drag and drop case but in the case of moving or resizing it could be perfectly normal, no ? I also understand that this is the "drag&drop" feature and not the "move something on the screen feature" but i was hoping that we could extend it because of the similarities...

Angelinsky7 avatar Aug 09 '18 10:08 Angelinsky7

What about making it into a method on CdkDrag? E.g. the consumption would be:

dragInstance.positionChanged().subscribe(event => ...);

This way we can know to trigger change detection since the consumer opted into it.

crisbeto avatar Aug 09 '18 11:08 crisbeto

i like the idea very much :-)

Angelinsky7 avatar Aug 09 '18 11:08 Angelinsky7

This module is already working quiet well for me. However, I've noticed that draggable items remain in their current dropzone if dropped anywhere but on another dropzone. Hence my suggestion:

Proposal: Drag Transitions I'd like to propose transitioning between dropzone-bound drags and free drags. Both are currently available, just not dynamically together afaik. Take a dock-based layout as an example, like Adobe Photoshop. When I drag panels between dropzones (say, docks), I can also drop it somewhere in between where there is no dropzone. I've seen other modules naming this a spill event. The panel then sits on top of all other elements, or position: fixed.

Since CDK already has a solid overlay module that allows for implemention of such overlays, we could use the spill event handler to open the draggable item as an overlay (using entryComponents), detaching it from any dropzones. It would then become freely draggable.

Whenever I re-enter a dropzone after free dragging, and drop the item, the already implemented CdkDragEnter and CdkDragDrop events can be utilized by the user to close the overlay (using detach() and/or dispose() methods) and re-attach the item to a dropzone. I guess re-attaching can be achieved using a DomPortalOutlet along with a ComponentPortal, but I'm unsure about this.

Summary

  • Implementation of a CdkDragSpill event. From there on, users can implement overlays as is.
  • Implementation of a re-attach method on a CdkDrop using the CdkDragEnter event to decide when and to what dropzone to re-attach to.

I think this would be a valuable addition to the module. Please let me know what you guys think!

nicky-lenaers avatar Aug 09 '18 23:08 nicky-lenaers

Just added this to my app, and it's working great with my nested dnd lists!

I noticed that there there is a bug where draggables don't animate correctly during the drag phase, if list items have variable heights. Is it useful to create a bug ticket for this while still under active development? Regardless—nice work, so far!

BirdInTheCity avatar Aug 15 '18 20:08 BirdInTheCity

@BirdInTheCity yes, it would be useful to have a ticket for it.

crisbeto avatar Aug 15 '18 20:08 crisbeto

@crisbeto Posted the bug. Includes animated gif to help explain. Take a look. Thanks!

BirdInTheCity avatar Aug 16 '18 02:08 BirdInTheCity

Was trying to use cdkDrag earlier and getting griefed a fair bit. After making a minimal reproduction in StackBlitz I realised that the textarea element within cdkDrag causes issues along with cdkDragHandler: https://github.com/angular/material2/issues/12718

Reproduction: https://stackblitz.com/edit/angular-3h4hbb?file=src%2Fapp%2Fapp.component.html

intellix avatar Aug 17 '18 14:08 intellix

To @Tiedye and others — as @thefliik noted above, I built the react-dnd implementation, angular-skyhook. It works very well with NgRx. There are a number of examples using it on the demo site, including a clone of the macOS calendar.

I see that Cdk is targeting a sortable, and angular-skyhook is much more general purpose than that, but I am working on a sortable implementation on top — if anyone has any input on the API design or wants to contribute, it would be very welcome over at https://github.com/cormacrelf/angular-skyhook/issues/69.

(Also, @crisbeto, I read the comments about change detection on fast-moving values — you may find some of the core angular-skyhook code interesting. I used a separate zone for all code, that comes back to Angular when users opt into events they’re interested in (‘interested’ = distinctUntilChanged on the listen() observable, or a spec method is provided). Observables can even be scheduled to have CD run after they are processed. It works optimally, even as a blanket rule.)

cormacrelf avatar Aug 18 '18 18:08 cormacrelf

A nice place to get started I found this on google. https://github.com/misha130/ngx-drag-and-drop-lists

ganeshkbhat avatar Aug 24 '18 04:08 ganeshkbhat

Really the drag and drop feature addition.

However I noticed a problem where input elements such as buttons, text fields, and checkboxes don't receive click events. All clicks on a CdkDrag element seem to be fire a CdkDragDrop event but no click event. Is the CdkDragDrop event consuming the click and preventing the click from passing through to the input element?

Has anyone been able to get a button working in a CdkDrag element?

Here's a stackblitz with the minimum code necessary to reproduce the issue: https://angular-qpqmw8.stackblitz.io

Let me know if it'd help to create an issue for this.

ptoro avatar Sep 15 '18 23:09 ptoro

@ptoro seems like you would want a drag handle on any of those elements if you'd like to drag them. I can see how it would be useful for a form builder or something similar but you'd remove the cdkDrag directive after form is built and button would act like a traditional button. It feels like you are overloading these elements, but I might be missing something.

nayfin avatar Sep 17 '18 16:09 nayfin

@nayfin

seems like you would want a drag handle on any of those elements if you'd like to drag them.

Not necessarily. For example, think of an image gallery where you might want to be able to drag a picture to reorder it but also click to enlarge it. It's not just an image gallery though. It's true any time you have an element itself you want to grab hold of to drag but also click.

Even regardless of individual use cases, a drag and drop library should not consume all ClickEvents as all ClickEvents are not drag and drop events and should be available for consumption by the developer.

Dragula (ng2-dragula) does a good job implementing this: https://valor-software.com/ng2-dragula/index.html (scroll to the section "Click or Drag!").

ptoro avatar Sep 18 '18 06:09 ptoro

@ptoro @nayfin we had a TODO to avoid interfering with click events. It'll be resolved by https://github.com/angular/material2/pull/13152.

crisbeto avatar Sep 18 '18 07:09 crisbeto

Thanks for creating that PR @crisbeto.

Playing around with the feature a bit more, I ran into another problem. If you want to only allow one-way dragging (e.g. left list to right list), you remove the connectedTo directive from one of the lists. This causes a problem where if you drag from one list to another list and back to the original list without dropping, the cdkDrag element you dragged won't go back to the original list. Here's a gif that showcases this behavior: cdk drag-drop one-way drag issue reprodution

I can provide a StackBiltz but the only change I had to make from this StackBlitz for the Transferring items between lists example to reproduce this behavior was to remove [connectedTo]="[todoList]" from the donelist's cdkDrop selector. (Side note: I had to copy the StackBlitz code and run it locally because none of the StackBiltzs linked to on this page are working. They either won't drag or it shows an error. I didn't notice anyone else mention this or an existing issue for it.)

Happy to create an issue for this if necessary.

ptoro avatar Sep 19 '18 22:09 ptoro