Skip to content

Commit 4ee6636

Browse files
atscottvivian-hu-zz
authored andcommittedNov 7, 2018
fix(drag-drop): enable drag interactions when there is a drag handle (#13780)
If there is a drag handle, do not disable native drag events on the cdkDrag element. Fixes #13779.
1 parent f895622 commit 4ee6636

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

Diff for: ‎src/cdk/drag-drop/drag.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ describe('CdkDrag', () => {
435435
}).not.toThrow();
436436
}));
437437

438+
it('should not disable native drag interactions when there is a drag handle', () => {
439+
const fixture = createComponent(StandaloneDraggableWithHandle);
440+
fixture.detectChanges();
441+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
442+
expect(dragElement.style.touchAction)
443+
.not.toEqual('none', 'should not disable touchAction on when there is a drag handle');
444+
});
445+
438446
});
439447

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

Diff for: ‎src/cdk/drag-drop/drag.ts

+9-4
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,
@@ -274,7 +274,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
274274
const rootElement = this._rootElement = this._getRootElement();
275275
rootElement.addEventListener('mousedown', this._pointerDown, passiveEventListenerOptions);
276276
rootElement.addEventListener('touchstart', this._pointerDown, passiveEventListenerOptions);
277-
toggleNativeDragInteractions(rootElement , false);
277+
this._handles.changes.pipe(startWith(null)).subscribe(() =>
278+
toggleNativeDragInteractions(rootElement, this.getChildHandles().length > 0));
278279
});
279280
}
280281

@@ -309,10 +310,14 @@ export class CdkDrag<T = any> implements AfterViewInit, OnDestroy {
309310
return this._dragDropRegistry.isDragging(this);
310311
}
311312

313+
/** Gets only handles that are not inside descendant `CdkDrag` instances. */
314+
private getChildHandles() {
315+
return this._handles.filter(handle => handle._parentDrag === this);
316+
}
317+
312318
/** Handler for the `mousedown`/`touchstart` events. */
313319
_pointerDown = (event: MouseEvent | TouchEvent) => {
314-
// Skip handles inside descendant `CdkDrag` instances.
315-
const handles = this._handles.filter(handle => handle._parentDrag === this);
320+
const handles = this.getChildHandles();
316321

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

0 commit comments

Comments
 (0)
Please sign in to comment.