Skip to content

Commit 23df7aa

Browse files
crisbetojosephperrott
authored andcommitted
fix(drag-drop): error if drag sequence is started while another one is finishing (#16081)
1 parent 4d0d080 commit 23df7aa

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,23 @@ describe('CdkDrag', () => {
26402640
}).not.toThrow();
26412641
}));
26422642

2643+
it('should not be able to start a drag sequence while another one is still active',
2644+
fakeAsync(() => {
2645+
const fixture = createComponent(DraggableInDropZone);
2646+
fixture.detectChanges();
2647+
const [item, otherItem] = fixture.componentInstance.dragItems.toArray();
2648+
2649+
startDraggingViaMouse(fixture, item.element.nativeElement);
2650+
2651+
expect(document.querySelectorAll('.cdk-drag-dragging').length)
2652+
.toBe(1, 'Expected one item to be dragged initially.');
2653+
2654+
startDraggingViaMouse(fixture, otherItem.element.nativeElement);
2655+
2656+
expect(document.querySelectorAll('.cdk-drag-dragging').length)
2657+
.toBe(1, 'Expected only one item to continue to be dragged.');
2658+
}));
2659+
26432660
});
26442661

26452662
describe('in a connected drop container', () => {

src/cdk/drag-drop/drag-ref.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,13 @@ export class DragRef<T = any> {
510510
// in the `pointerMove` subscription, because we're not guaranteed to have one move event
511511
// per pixel of movement (e.g. if the user moves their pointer quickly).
512512
if (isOverThreshold && (Date.now() >= this._dragStartTime + (this.dragStartDelay || 0))) {
513-
this._hasStartedDragging = true;
514-
this._ngZone.run(() => this._startDragSequence(event));
513+
// Prevent other drag sequences from starting while something in the container is still
514+
// being dragged. This can happen while we're waiting for the drop animation to finish
515+
// and can cause errors, because some elements might still be moving around.
516+
if (!this._dropContainer || !this._dropContainer.isDragging()) {
517+
this._hasStartedDragging = true;
518+
this._ngZone.run(() => this._startDragSequence(event));
519+
}
515520
}
516521

517522
return;

0 commit comments

Comments
 (0)