@angular/cdk/drag-drop package provides directives and utilities for creating drag-and-drop interfaces, including sortable lists and free-form dragging.
Installation
Basic Dragging
Free Drag
Make any element draggable:Sortable List
Create a sortable list:Multiple Lists
Move items between lists:Advanced Features
Custom Drag Handle
Custom Preview
Custom Placeholder
Constrain Dragging
Drag Delay
Disabled Dragging
Events
API Reference
cdkDrag Directive
Selector:[cdkDrag]
| Input | Type | Description |
|---|---|---|
cdkDragData | T | Arbitrary data attached to drag instance |
cdkDragLockAxis | 'x' | 'y' | Lock dragging to axis |
cdkDragBoundary | string | ElementRef | HTMLElement | Element that limits dragging |
cdkDragDisabled | boolean | Whether dragging is disabled |
cdkDragStartDelay | number | {touch, mouse} | Delay before drag starts |
cdkDragConstrainPosition | (point, ref, dimensions) => point | Constrain position function |
cdkDragFreeDragPosition | {x, y} | Position for free drag items |
cdkDragPreviewClass | string | string[] | Classes for preview element |
| Output | Type | Description |
|---|---|---|
cdkDragStarted | CdkDragStart | Emitted when drag starts |
cdkDragMoved | CdkDragMove | Emitted as drag moves |
cdkDragEnded | CdkDragEnd | Emitted when drag ends |
cdkDragReleased | CdkDragRelease | Emitted when user releases drag |
cdkDragEntered | CdkDragEnter | Emitted when enters drop list |
cdkDragExited | CdkDragExit | Emitted when exits drop list |
cdkDropList Directive
Selector:[cdkDropList]
| Input | Type | Description |
|---|---|---|
cdkDropListData | T[] | Data array for the list |
cdkDropListConnectedTo | CdkDropList[] | Other lists to connect to |
cdkDropListOrientation | 'horizontal' | 'vertical' | Layout orientation |
cdkDropListDisabled | boolean | Whether dropping is disabled |
cdkDropListSortingDisabled | boolean | Disable internal sorting |
cdkDropListEnterPredicate | (drag, drop) => boolean | Predicate for accepting items |
| Output | Type | Description |
|---|---|---|
cdkDropListDropped | CdkDragDrop | Emitted when item is dropped |
cdkDropListEntered | CdkDragEnter | Emitted when drag enters |
cdkDropListExited | CdkDragExit | Emitted when drag exits |
cdkDropListSorted | CdkDragSortEvent | Emitted when list is sorted |
Utility Functions
moveItemInArray
Move an item within an array:transferArrayItem
Transfer item between arrays:copyArrayItem
Copy item between arrays:Accessibility
Best Practices
- Provide visual feedback - Use preview and placeholder templates
- Handle both touch and mouse - Test on mobile devices
- Add ARIA attributes - Make drag-drop accessible
- Optimize performance - Limit DOM updates during drag
- Preserve data - Use moveItemInArray to maintain array references