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

Conversation

endlacer
Copy link

@endlacer endlacer commented Dec 17, 2024

Feature

Problem Statement

Currently, the CdkDrag directive doesn't provide a way to conditionally prevent drag operations from starting based on the initial mouse/touch event. This makes it impossible to implement complex drag start conditions, such as only allowing drags from specific elements or under certain event conditions.
Add a new input [cdkDragStartPredicate] to the CdkDrag directive that accepts a predicate function to determine whether a drag operation should start.

Proposed API

@Input('cdkDragStartPredicate')
dragStartPredicate: ((event: TouchEvent | MouseEvent) => boolean) | undefined;

Example Usage

HTML:

<div cdkDrag [cdkDragStartPredicate]="myDragStartPredicate">
  Draggable content
  <input #input>
</div>

component:

myDragStartPredicate(event: TouchEvent | MouseEvent): boolean {
  const element = event.target as HTMLElement;
  // Check if the target or any of its parents is an input element
  return !element.closest('input, textarea, [contenteditable="true"]');
}

See: https://stackblitz.com/edit/wc3rt4?file=src%2Fexample%2Fcdk-drag-drop-sorting-example.css

Details

Benefits

  • More granular control over drag initialization
  • Better integration with custom UI patterns
  • Improved accessibility by allowing precise control over drag triggers
  • Reduced need for workarounds using multiple directives

Implementation Notes

  • The predicate should be called before the drag sequence begins
  • Return true to allow the drag to start, false to prevent it
  • Should work with both mouse and touch events
  • Should integrate well with existing cdkDragHandle functionality

Related Issues

closes: #30101
also resolve this issue: #14117

Use Case

  • Prevent drags from starting on specific child elements
  • Implement custom handle logic without using cdkDragHandle
  • Add conditional drag start behavior based on application state
  • Implement complex touch gesture requirements

@endlacer endlacer requested a review from a team as a code owner December 17, 2024 08:55
@endlacer endlacer requested review from mmalerba and andrewseguin and removed request for a team December 17, 2024 08:55
Copy link

google-cla bot commented Dec 17, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@endlacer
Copy link
Author

#Problems
I have trouble testing/writing tests since I cannot get it to run tests locally. yarn test drag-drop and yarn dev-appboth run into errors.yarn build` works fine.

INFO: Build completed successfully, 7 total actions
ERROR: Error creating runfiles: Failed to create runfiles symlinks: build-runfiles.exe failed: error executing command
cd /d C:\users\andre_bazel_andre\a7swqcex\execroot\angular_material
C:\users\andre_bazel_andre\install\123df92719b107a86cd72886e258fa44\build-runfiles.exe bazel-out/x64_windows-fastbuild/bin/src/cdk/drag-drop/unit_tests_debug.exe.runfiles_manifest bazel-out/x64_windows-fastbuild/bin/src/cdk/drag-drop/unit_tests_debug.exe.runfiles: Process exited with status 1
build-runfiles error: CreateSymbolicLinkW failed:
Bazel needs to create symlinks to build the runfiles tree.
Creating symlinks on Windows requires one of the following:
1. Bazel is run with administrator privileges.
2. The system version is Windows 10 Creators Update (1703) or later and developer mode is enabled.

manifest file name: bazel-out/x64_windows-fastbuild/bin/src/cdk/drag-drop/unit_tests_debug.exe.runfiles_manifest
runfiles base directory: bazel-out/x64_windows-fastbuild/bin/src/cdk/drag-drop/unit_tests_debug.exe.runfiles
: Process exited with status 1
INFO: Build completed successfully, 7 total actions
iBazel [11:59AM]: Error starting process: fork/exec C:\Users\andre\AppData\Local\Temp\bazel_script_path1308474478.bat: %1 ist keine zulässige Win32-Anwendung.
iBazel [11:59AM]: Run start failed fork/exec C:\Users\andre\AppData\Local\Temp\bazel_script_path1308474478.bat: %1 ist keine zulässige Win32-Anwendung.

Somebody any Idea?
I created the PR nevertheless, to get some feedback

@endlacer endlacer marked this pull request as draft December 17, 2024 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(drag-drop): New Optional Input [cdkDragStartPredicate]
1 participant