Skip to content

Commit

Permalink
fix(cdk/drag-drop): not blocking initial move event (#21752)
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 060ab9e)
  • Loading branch information
crisbeto authored and annieyw committed Feb 5, 2021
1 parent 936ecd6 commit 518f1d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/cdk/drag-drop/directives/drag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ describe('CdkDrag', () => {
dispatchTouchEvent(fixture.componentInstance.dragElement.nativeElement, 'touchstart');
fixture.detectChanges();

dispatchTouchEvent(document, 'touchmove');
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented).toBe(true);
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented)
.toBe(true, 'Expected initial touchmove to be prevented.');
expect(dispatchTouchEvent(document, 'touchmove').defaultPrevented)
.toBe(true, 'Expected subsequent touchmose to be prevented.');

dispatchTouchEvent(document, 'touchend');
fixture.detectChanges();
Expand Down
3 changes: 3 additions & 0 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,9 @@ export class DragRef<T = any> {
// being dragged. This can happen while we're waiting for the drop animation to finish
// and can cause errors, because some elements might still be moving around.
if (!container || (!container.isDragging() && !container.isReceiving())) {
// Prevent the default action as soon as the dragging sequence is considered as
// "started" since waiting for the next event can allow the device to begin scrolling.
event.preventDefault();
this._hasStartedDragging = true;
this._ngZone.run(() => this._startDragSequence(event));
}
Expand Down

0 comments on commit 518f1d0

Please sign in to comment.