Skip to content

Commit 172b1f6

Browse files
committed
[feat] drag start predicate
1 parent 4697d8e commit 172b1f6

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {DragRefConfig, Point, DragRef} from '../drag-ref';
1212
/** Possible values that can be used to configure the drag start delay. */
1313
export type DragStartDelay = number | {touch: number; mouse: number};
1414

15+
/** Function that is used to determine whether a drag operation is allowed to start. */
16+
export type DragStartPredicate = (event: MouseEvent | TouchEvent) => boolean;
17+
1518
/** Possible axis along which dragging can be locked. */
1619
export type DragAxis = 'x' | 'y';
1720

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ import {CDK_DRAG_PARENT} from '../drag-parent';
4646
import {DragRef, Point, PreviewContainer} from '../drag-ref';
4747
import type {CdkDropList} from './drop-list';
4848
import {DragDrop} from '../drag-drop';
49-
import {CDK_DRAG_CONFIG, DragDropConfig, DragStartDelay, DragAxis} from './config';
49+
import {
50+
CDK_DRAG_CONFIG,
51+
DragDropConfig,
52+
DragStartDelay,
53+
DragAxis,
54+
DragStartPredicate,
55+
} from './config';
5056
import {assertElementNode} from './assertions';
5157
import {DragDropRegistry} from '../drag-drop-registry';
5258

@@ -114,6 +120,9 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
114120
*/
115121
@Input('cdkDragStartDelay') dragStartDelay: DragStartDelay;
116122

123+
/** Function that is used to determine whether a drag operation is allowed to start. */
124+
@Input('cdkDragStartPredicate') dragStartPredicate?: DragStartPredicate;
125+
117126
/**
118127
* Sets the position of a `CdkDrag` that is outside of a drop container.
119128
* Can be used to restore the element's position for a returning user.
@@ -430,6 +439,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
430439
if (!ref.isDragging()) {
431440
const dir = this._dir;
432441
const dragStartDelay = this.dragStartDelay;
442+
const dragStartPredicate = this.dragStartPredicate;
433443
const placeholder = this._placeholderTemplate
434444
? {
435445
template: this._placeholderTemplate.templateRef,
@@ -453,6 +463,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
453463
typeof dragStartDelay === 'object' && dragStartDelay
454464
? dragStartDelay
455465
: coerceNumberProperty(dragStartDelay);
466+
ref.dragStartPredicate = dragStartPredicate;
456467
ref.constrainPosition = this.constrainPosition;
457468
ref.previewClass = this.previewClass;
458469
ref

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
import {DragDropRegistry} from './drag-drop-registry';
4040
import type {DropListRef} from './drop-list-ref';
4141
import {DragPreviewTemplate, PreviewRef} from './preview-ref';
42+
import {DragStartPredicate} from './directives/config';
4243

4344
/** Object that can be used to configure the behavior of DragRef. */
4445
export interface DragRefConfig {
@@ -286,6 +287,9 @@ export class DragRef<T = any> {
286287
*/
287288
dragStartDelay: number | {touch: number; mouse: number} = 0;
288289

290+
/** Function that is used to determine whether a drag operation is allowed to start. */
291+
dragStartPredicate: DragStartPredicate | undefined;
292+
289293
/** Class to be added to the preview element. */
290294
previewClass: string | string[] | undefined;
291295

@@ -904,6 +908,11 @@ export class DragRef<T = any> {
904908
? isFakeTouchstartFromScreenReader(event as TouchEvent)
905909
: isFakeMousedownFromScreenReader(event as MouseEvent);
906910

911+
// Check the drag start predicate if one is provided
912+
if (this.dragStartPredicate && !this.dragStartPredicate(event)) {
913+
return;
914+
}
915+
907916
// If the event started from an element with the native HTML drag&drop, it'll interfere
908917
// with our own dragging (e.g. `img` tags do it by default). Prevent the default action
909918
// to stop it from happening. Note that preventing on `dragstart` also seems to work, but

0 commit comments

Comments
 (0)