Full answer
The CDK drag and drop module handles pointer and touch dragging, animation, auto-scroll, and placeholder preview. You supply the data change.
Basic set-up:
- Import
CdkDropListandCdkDrag(orDragDropModule). <ul cdkDropList (cdkDropListDropped)="drop($event)">and<li cdkDrag>for each item.- The event gives
previousIndexandcurrentIndex. In the handler, move the item in your data.moveItemInArray(array, from, to)mutates the array it receives. With signals, copy first:this.items.update(list => { const next = [...list]; moveItemInArray(next, e.previousIndex, e.currentIndex); return next; }). - Between two lists use
cdkDropListGroupor[cdkDropListConnectedTo]andtransferArrayItem(previousData, currentData, previousIndex, currentIndex). Give each list its data with[cdkDropListData]. - Useful options:
cdkDragHandle(drag only by a grip icon),cdkDragDisabled,cdkDragPreviewandcdkDragPlaceholdertemplates,cdkDropListOrientation="horizontal", andcdkDropListLockAxis.
Accessibility: dragging needs a pointer. The CDK drag-drop does not support keyboard reordering. A complete solution gives the same action another way: "Move up" and "Move down" buttons that call the same move function, with a label naming the item, and an announcement (LiveAnnouncer) of the new position. Also persist the new order on the server and handle failures by restoring the old order.
Performance notes: for hundreds of items combine with virtual scroll carefully (the CDK has limited support), or use pagination. Use track with a stable id in @for.
import { CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList, moveItemInArray } from '@angular/cdk/drag-drop';
import { LiveAnnouncer } from '@angular/cdk/a11y';
import { Component, inject, signal } from '@angular/core';
@Component({
selector: 'app-priority-list',
imports: [CdkDropList, CdkDrag, CdkDragHandle],
template: `
<ul cdkDropList (cdkDropListDropped)="drop($event)" aria-label="Study topics in priority order">
@for (topic of topics(); track topic; let i = $index; let first = $first; let last = $last) {
<li cdkDrag class="item">
<span cdkDragHandle aria-hidden="true">::</span>
{{ i + 1 }}. {{ topic }}
<button type="button" [disabled]="first" (click)="move(i, i - 1)" [attr.aria-label]="'Move ' + topic + ' up'">Up</button>
<button type="button" [disabled]="last" (click)="move(i, i + 1)" [attr.aria-label]="'Move ' + topic + ' down'">Down</button>
</li>
}
</ul>
`,
styles: `
.item { display: flex; gap: 8px; padding: 6px; border: 1px solid #ccc; background: white; }
`,
})
export class PriorityListComponent {
private readonly announcer = inject(LiveAnnouncer);
readonly topics = signal(['Signals', 'Routing', 'Forms', 'Testing']);
drop(event: CdkDragDrop<string[]>): void {
this.move(event.previousIndex, event.currentIndex);
}
move(from: number, to: number): void {
if (to < 0 || to >= this.topics().length || from === to) {
return;
}
this.topics.update((list) => {
const next = [...list];
moveItemInArray(next, from, to);
return next;
});
void this.announcer.announce(`${this.topics()[to]} moved to position ${to + 1}`);
}
}Angular 20.3 with CDK 20.2, strict template check passes. The move buttons call the same function as the drop handler.
Key points
- `cdkDropList` on the container, `cdkDrag` on the items, handle `cdkDropListDropped`.
- `moveItemInArray` mutates, so copy first when the data lives in a signal.
- `transferArrayItem` and connected lists move items between lists.
- Provide keyboard buttons and announcements, because dragging is pointer-only.
Common mistakes
- Mutating the signal's array in place, so the view does not refresh.
- Providing drag as the only way to reorder.
- Not using a stable `track` expression, which breaks the drag animation.
Follow-ups
- How would you build a kanban board with three columns?
- How would you save the new order and handle a failed save?
- How would you limit dragging to a handle?
Tips
- Raise the keyboard alternative yourself. It shows you think about all users.
- Mention the copy-before-move rule for signals.
- Describe optimistic update with rollback for saving.
Evaluation rubric
- Uses the drag and drop directives and event correctly.
- Updates signal state immutably.
- Provides an accessible alternative.