Skip to content

Commit 7c82b32

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 6c741c4 commit 7c82b32

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
@@ -427,6 +427,14 @@ describe('CdkDrag', () => {
427427
expect(event.stopPropagation).toHaveBeenCalled();
428428
}));
429429

430+
it('should not disable native drag interactions when there is a drag handle', () => {
431+
const fixture = createComponent(StandaloneDraggableWithHandle);
432+
fixture.detectChanges();
433+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
434+
expect(dragElement.style.touchAction)
435+
.not.toEqual('none', 'should not disable touchAction on when there is a drag handle');
436+
});
437+
430438
});
431439

432440
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 {normalizePassiveListenerOptions} 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,
@@ -269,7 +269,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
269269
const rootElement = this._rootElement = this._getRootElement();
270270
rootElement.addEventListener('mousedown', this._pointerDown, passiveEventListenerOptions);
271271
rootElement.addEventListener('touchstart', this._pointerDown, passiveEventListenerOptions);
272-
toggleNativeDragInteractions(rootElement , false);
272+
273+
this._handles.changes.pipe(startWith(null)).subscribe(() =>
274+
toggleNativeDragInteractions(rootElement, this.getChildHandles().length > 0));
273275
});
274276
}
275277

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

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

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

0 commit comments

Comments
 (0)