Skip to content

Commit 060ab9e

Browse files
authored
fix(cdk/drag-drop): not blocking initial move event (#21752)
In #21382 the `preventDefault` call was moved further down so it doesn't prevent events until the dragging threshold has been reached. The problem is that it'll only start calling `preventDefault` from the first event __after__ the threshold has been reached which can be enough time for the device to start scrolling. These changes add an extra call as soon as dragging has been considered as "started". Fixes #21749.
1 parent e773f90 commit 060ab9e

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,10 @@ describe('CdkDrag', () => {
318318
dispatchTouchEvent(fixture.componentInstance.dragElement.nativeElement, 'touchstart');
319319
fixture.detectChanges();
320320

321-
dispatchTouchEvent(document, 'touchmove');
322-
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented).toBe(true);
321+
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented)
322+
.toBe(true, 'Expected initial touchmove to be prevented.');
323+
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented)
324+
.toBe(true, 'Expected subsequent touchmose to be prevented.');
323325

324326
dispatchTouchEvent(document, 'touchend');
325327
fixture.detectChanges();

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

+3
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ export class DragRef<T = any> {
629629
// being dragged. This can happen while we're waiting for the drop animation to finish
630630
// and can cause errors, because some elements might still be moving around.
631631
if (!container || (!container.isDragging() && !container.isReceiving())) {
632+
// Prevent the default action as soon as the dragging sequence is considered as
633+
// "started" since waiting for the next event can allow the device to begin scrolling.
634+
event.preventDefault();
632635
this._hasStartedDragging = true;
633636
this._ngZone.run(() => this._startDragSequence(event));
634637
}

0 commit comments

Comments
 (0)