Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] drag start predicate #30194

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cdk/drag-drop/directives/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {DragRefConfig, Point, DragRef} from '../drag-ref';
/** Possible values that can be used to configure the drag start delay. */
export type DragStartDelay = number | {touch: number; mouse: number};

/** Function that is used to determine whether a drag operation is allowed to start. */
export type DragStartPredicate = (event: MouseEvent | TouchEvent) => boolean;

/** Possible axis along which dragging can be locked. */
export type DragAxis = 'x' | 'y';

Expand Down
13 changes: 12 additions & 1 deletion src/cdk/drag-drop/directives/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ import {CDK_DRAG_PARENT} from '../drag-parent';
import {DragRef, Point, PreviewContainer} from '../drag-ref';
import type {CdkDropList} from './drop-list';
import {DragDrop} from '../drag-drop';
import {CDK_DRAG_CONFIG, DragDropConfig, DragStartDelay, DragAxis} from './config';
import {
CDK_DRAG_CONFIG,
DragDropConfig,
DragStartDelay,
DragAxis,
DragStartPredicate,
} from './config';
import {assertElementNode} from './assertions';
import {DragDropRegistry} from '../drag-drop-registry';

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

/** Function that is used to determine whether a drag operation is allowed to start. */
@Input('cdkDragStartPredicate') dragStartPredicate?: DragStartPredicate;

/**
* Sets the position of a `CdkDrag` that is outside of a drop container.
* Can be used to restore the element's position for a returning user.
Expand Down Expand Up @@ -430,6 +439,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
if (!ref.isDragging()) {
const dir = this._dir;
const dragStartDelay = this.dragStartDelay;
const dragStartPredicate = this.dragStartPredicate;
const placeholder = this._placeholderTemplate
? {
template: this._placeholderTemplate.templateRef,
Expand All @@ -453,6 +463,7 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
typeof dragStartDelay === 'object' && dragStartDelay
? dragStartDelay
: coerceNumberProperty(dragStartDelay);
ref.dragStartPredicate = dragStartPredicate;
ref.constrainPosition = this.constrainPosition;
ref.previewClass = this.previewClass;
ref
Expand Down
9 changes: 9 additions & 0 deletions src/cdk/drag-drop/drag-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import {DragDropRegistry} from './drag-drop-registry';
import type {DropListRef} from './drop-list-ref';
import {DragPreviewTemplate, PreviewRef} from './preview-ref';
import {DragStartPredicate} from './directives/config';

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

/** Function that is used to determine whether a drag operation is allowed to start. */
dragStartPredicate: DragStartPredicate | undefined;

/** Class to be added to the preview element. */
previewClass: string | string[] | undefined;

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

// Check the drag start predicate if one is provided
if (this.dragStartPredicate && !this.dragStartPredicate(event)) {
return;
}

// If the event started from an element with the native HTML drag&drop, it'll interfere
// with our own dragging (e.g. `img` tags do it by default). Prevent the default action
// to stop it from happening. Note that preventing on `dragstart` also seems to work, but
Expand Down
Loading