|
1 |
| -# TODO |
| 1 | +The `@angular/cdk/drag-drop` module provides you with a way to easily and declaratively create |
| 2 | +drag&drop interfaces, with support for free dragging, sorting within a list, transferring items |
| 3 | +between lists, animations, touch devices, custom drag handles, previews, and placeholders, |
| 4 | +in addition to horizontal lists and locking along an axis. |
| 5 | + |
| 6 | +### Getting started |
| 7 | +To get started, you have to include the `DragDropModule` in your app's imports and add the |
| 8 | +`cdkDrag` directive to the element that you want to be draggable. When a `cdkDrag` element is |
| 9 | +outside of a `cdk-drop` element, it's in "free dragging" mode, which means that the user will be |
| 10 | +able to drag it freely around the page. |
| 11 | + |
| 12 | +<!-- example(cdk-drag-drop-overview) --> |
| 13 | + |
| 14 | +### Sorting |
| 15 | +If you want to take your UI a step further and allow the user to re-arrange a list of data using |
| 16 | +drag&drop, you can do so using the `cdk-drop` directive. When a `cdk-drop` is added around some |
| 17 | +`cdkDrag` elements, they'll become grouped in a list and the CDK will automatically sort them |
| 18 | +as the user is dragging. Note that the CDK won't change the data for you, which means that you'll |
| 19 | +have to listen for the `dropped` event and re-arrange the date yourself once the user is done |
| 20 | +dragging. There are some provided utilities like the `moveItemInArray` and `transferArrayItem` |
| 21 | +functions that make it easier to manipulate your data. |
| 22 | + |
| 23 | +<!-- example(cdk-drag-drop-sorting) --> |
| 24 | + |
| 25 | +### Sorting between lists |
| 26 | +The drag&drop module has the ability to transfer items between `cdk-drop` lists. You can connect |
| 27 | +two lists using the `connectedTo` input on the `cdk-drop` component, which tells the CDK the items |
| 28 | +from which lists can be dropped into the current list. |
| 29 | + |
| 30 | +<!-- example(cdk-drag-drop-connected-sorting) --> |
| 31 | + |
| 32 | +Note that `connectedTo` works both with a direct reference to another `cdk-drop`, or by |
| 33 | +referencing the `id` of another drop container: |
| 34 | + |
| 35 | +```html |
| 36 | +<!-- This is valid --> |
| 37 | +<cdk-drop #listOne [connectedTo]="[listTwo]"></cdk-drop> |
| 38 | +<cdk-drop #listTwo [connectedTo]="[listOne]"></cdk-drop> |
| 39 | + |
| 40 | +<!-- This is valid as well --> |
| 41 | +<cdk-drop id="list-one" [connectedTo]="['list-two']"></cdk-drop> |
| 42 | +<cdk-drop id="list-two" [connectedTo]="['list-one']"></cdk-drop> |
| 43 | +``` |
| 44 | + |
| 45 | +### Attaching data |
| 46 | +When dealing with connected sorting lists inside an `ngFor` that all reuse the same `dropped` |
| 47 | +callback, it can be difficult to know what list was being sorted or which item was dropped. To |
| 48 | +make this easier, you can associate data both with `cdk-drop` and `cdkDrag` through the |
| 49 | +`data` and `cdkDragData` inputs respectively. This data will be passed along to all of the |
| 50 | +emitted events. |
| 51 | + |
| 52 | +```html |
| 53 | +<cdk-drop [data]="list" *ngFor="let list of lists" (dropped)="drop($event)"> |
| 54 | + <div cdkDrag [cdkDragData]="item" *ngFor="let item of list"></div> |
| 55 | +</cdk-drop> |
| 56 | +``` |
| 57 | + |
| 58 | +### Styling |
| 59 | +By default, the CDK drag&drop comes with a minimal set of structural styles, which means that it's |
| 60 | +up to you to define what the components should look like. You can target the following selectors |
| 61 | +in order to customize the component's appearance: |
| 62 | + |
| 63 | +* `.cdk-drop` - Corresponds to the `<cdk-drop>` container. |
| 64 | +* `.cdk-drag` - Corresponds to a `cdkDrag` instance. |
| 65 | +* `.cdk-drag-preview` - This is the element that will be rendered next to the user's |
| 66 | + cursor as they're dragging an item in a sortable list. By default the element looks exactly like |
| 67 | + the element that is being dragged. |
| 68 | +* `.cdk-drag-placeholder` - This is element that will be shown instead of the real element as it's |
| 69 | + being dragged inside a `cdk-drop`. By default this will look exactly like the element that is |
| 70 | + being sorted. |
| 71 | +* `.cdk-drop-dragging` - A class that is added to `cdk-drop` while the user is dragging an item. |
| 72 | + |
| 73 | +### Animations |
| 74 | +The drag&drop module supports animations both while sorting an element inside a list, as well as |
| 75 | +animating it from the position that the user dropped it to its final place in the list. To set up |
| 76 | +your animations, you have to define a `transition` that targets the `transform` property. The |
| 77 | +following classes can be used for animations: |
| 78 | + |
| 79 | +* `.cdk-drag` - If you add a `transition` to this class, it'll animate as the user is sorting |
| 80 | + through a list. |
| 81 | +* `.cdk-drag-animating` - This class is added to a `cdkDrag` when the user has stopped dragging. |
| 82 | + If you add a `transition` to it, the CDK will animate the element from its drop position to |
| 83 | + the final position inside the `cdk-drop` container. |
| 84 | + |
| 85 | +Example animations: |
| 86 | + |
| 87 | +```css |
| 88 | +/* Animate items as they're being sorted. */ |
| 89 | +.cdk-drop-dragging .cdk-drag { |
| 90 | + transition: transform 250ms cubic-bezier(0, 0, 0.2, 1); |
| 91 | +} |
| 92 | + |
| 93 | +/* Animate an item that has been dropped. */ |
| 94 | +.cdk-drag-animating { |
| 95 | + transition: transform 300ms cubic-bezier(0, 0, 0.2, 1); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Customizing the drag area using a handle |
| 100 | +By default, the user can drag the entire `cdkDrag` element to move it around. If you want to |
| 101 | +restrict the user to only be able to do so using a handle element, you can do it by adding the |
| 102 | +`cdkDragHandle` directive to an element inside of `cdkDrag`. Note that you can have as many |
| 103 | +`cdkDragHandle` elements as you want: |
| 104 | + |
| 105 | +<!-- example(cdk-drag-drop-handle) --> |
| 106 | + |
| 107 | +### Customizing the preview element |
| 108 | +By default, when the user starts dragging an item inside a sortable list, the CDK will create a |
| 109 | +preview element that looks exactly like the element that is being dragged, and will position it |
| 110 | +next to the user's cursor. If you want to customize how the preview looks, you can either target |
| 111 | +the `.cdk-drag-preview` using CSS, or you can provide a completely new template using the |
| 112 | +`*cdkDragPreview` directive: |
| 113 | + |
| 114 | +<!-- example(cdk-drag-drop-custom-preview) --> |
| 115 | + |
| 116 | +### List orientation |
| 117 | +The `cdk-drop` component defaults to sorting lists vertically, however you can make a list that is |
| 118 | +sorted horizontally by setting the `orientation` input to `horizontal`: |
| 119 | + |
| 120 | +<!-- example(cdk-drag-drop-horizontal-sorting) --> |
| 121 | + |
| 122 | +### Restricting movement along an axis |
| 123 | +By default, the CDK allows the user to move the drag preview around freely in both the X and Y axis, |
| 124 | +however you can restrict movement along one of the using the `cdkDragLockAxis` directive. This |
| 125 | +directive is also available on the `cdk-drop` component as `lockAxis`. |
| 126 | + |
| 127 | +<!-- example(cdk-drag-drop-axis-lock) --> |
0 commit comments