Skip to content

Commit b0dbb3a

Browse files
committed
fix(drag-drop): enable drag interactions when there is a drag handle
If there is a drag handle, do not disable native drag events on the cdkDrag element. Fixes #13779.
1 parent 985774a commit b0dbb3a

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ describe('CdkDrag', () => {
412412
expect(dragElement.style.transform).toBeFalsy();
413413
}));
414414

415+
it('should not disable native drag interactions when there is a drag handle', () => {
416+
const fixture = createComponent(StandaloneDraggableWithHandle);
417+
fixture.detectChanges();
418+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
419+
expect(dragElement.style.touchAction)
420+
.not.toEqual('none', 'should not disable touchAction on when there is a drag handle');
421+
});
422+
415423
});
416424

417425
describe('draggable with a handle', () => {

src/cdk/drag-drop/drag.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from '@angular/core';
3131
import {supportsPassiveEventListeners} from '@angular/cdk/platform';
3232
import {Observable, Subject, Subscription, Observer} from 'rxjs';
33-
import {take} from 'rxjs/operators';
33+
import {startWith, take} from 'rxjs/operators';
3434
import {DragDropRegistry} from './drag-drop-registry';
3535
import {
3636
CdkDragDrop,
@@ -270,7 +270,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
270270
const rootElement = this._rootElement = this._getRootElement();
271271
rootElement.addEventListener('mousedown', this._pointerDown, passiveEventListenerOptions);
272272
rootElement.addEventListener('touchstart', this._pointerDown, passiveEventListenerOptions);
273-
toggleNativeDragInteractions(rootElement , false);
273+
274+
this._handles.changes.pipe(startWith(null)).subscribe(() =>
275+
toggleNativeDragInteractions(rootElement, this.getChildHandles().length > 0));
274276
});
275277
}
276278

@@ -301,10 +303,14 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
301303
return this._dragDropRegistry.isDragging(this);
302304
}
303305

306+
/** Gets only handles that are not inside descendant `CdkDrag` instances. */
307+
private getChildHandles() {
308+
return this._handles.filter(handle => handle._parentDrag === this);
309+
}
310+
304311
/** Handler for the `mousedown`/`touchstart` events. */
305312
_pointerDown = (event: MouseEvent | TouchEvent) => {
306-
// Skip handles inside descendant `CdkDrag` instances.
307-
const handles = this._handles.filter(handle => handle._parentDrag === this);
313+
const handles = this.getChildHandles();
308314

309315
// Delegate the event based on whether it started from a handle or the element itself.
310316
if (handles.length) {

0 commit comments

Comments
 (0)