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

bug(CdkDrag): CdkDrag stays outside the boundary if it's size change #30325

Open
1 task
gnemeth-ibl opened this issue Jan 14, 2025 · 3 comments · May be fixed by #30436
Open
1 task

bug(CdkDrag): CdkDrag stays outside the boundary if it's size change #30325

gnemeth-ibl opened this issue Jan 14, 2025 · 3 comments · May be fixed by #30436
Labels
area: cdk/drag-drop feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@gnemeth-ibl
Copy link

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

If the size of the drag boundary changes, the draggable element does not recalculate its position and remains outside the boundary.

Reproduction

StackBlitz link: https://stackblitz.com/edit/l8npyx4z?file=src%2Fexample%2Fcdk-drag-drop-boundary-example.html,src%2Fexample%2Fcdk-drag-drop-boundary-example.ts
Steps to reproduce:

  1. Drag the element to the right edge
  2. Click the resize button
  3. The drag element is outside the boundary

Image

Expected Behavior

The drag element should stay inside the boundary,

Actual Behavior

It stays outside the boundary.

Environment

  • Angular: 19.0.5
  • CDK/Material: 19.0.5
  • Browser(s): Chrome 131.0.6778.265
  • Operating System (e.g. Windows, macOS, Ubuntu): Win 11
@gnemeth-ibl gnemeth-ibl added the needs triage This issue needs to be triaged by the team label Jan 14, 2025
@crisbeto
Copy link
Member

Doing this automatically won't be efficient, because we'll have to poll the DOM for changes in the boundary element's position. That being said, we should have an API that will re-align the element to the boundary on demand.

@crisbeto crisbeto added feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: cdk/drag-drop and removed needs triage This issue needs to be triaged by the team labels Jan 21, 2025
@naaajii
Copy link
Contributor

naaajii commented Jan 26, 2025

would something like this work?

<div #boundary class="example-boundary">
  <div #dragItem="cdkDrag" class="example-box" cdkDragBoundary=".example-boundary" cdkDrag>
    I can only be dragged within the dotted container
  </div>
</div>

<button mat-button (click)="onResizeClick(boundary, dragItem);">Resize boundary</button>
onResizeClick(boundary: HTMLElement, dragItem: CdkDrag) {
    boundary.style.width = '220px';
    dragItem.reset();
}

@gnemeth-ibl
Copy link
Author

would something like this work?

I can only be dragged within the dotted container

<button mat-button (click)="onResizeClick(boundary, dragItem);">Resize boundary
onResizeClick(boundary: HTMLElement, dragItem: CdkDrag) {
boundary.style.width = '220px';
dragItem.reset();
}

I've tried it as workaround, but the problem with this reset is, that it moves the dragItem to its original position.

naaajii added a commit to naaajii/components that referenced this issue Feb 2, 2025
this commit introduces `resetToBoundary` in DragRef which allows user to align DragItem to
its boundary on demand if at one point it was at a place where the boundary element
used to be and has shrinked causing DragItem to be outside of the boundary box

fixes angular#30325
@naaajii naaajii linked a pull request Feb 2, 2025 that will close this issue
naaajii added a commit to naaajii/components that referenced this issue Feb 2, 2025
this commit introduces `resetToBoundary` in DragRef which allows user to align DragItem to
its boundary on demand if at one point it was at a place where the boundary element
used to be and has shrinked causing DragItem to be outside of the boundary box

fixes angular#30325
naaajii added a commit to naaajii/components that referenced this issue Feb 16, 2025
this commit introduces `resetToBoundary` in DragRef which allows user to align DragItem to
its boundary on demand if at one point it was at a place where the boundary element
used to be and has shrinked causing DragItem to be outside of the boundary box

fixes angular#30325
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cdk/drag-drop feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants