Skip to content

[drag-drop] cdk-drag node is detached after element is dropped from one container to another #30506

Closed
@TetianaMoshon

Description

@TetianaMoshon

Description

If the element is dropped from one container to another, a detached node is created because of CdkDropList._dropListRef._draggables.

Reproduction

StackBlitz link: https://stackblitz.com/edit/stackblitz-starters-vygnusmw

Steps to reproduce:

  1. Download the project from StackBlitz.
  2. Start it and open in the Edge browser. Open the deprecated "Detached elements" tool.
  3. Drag an item from the "To do" column to the "In progress" column and then to "Done".
  4. Collect garbage in the "Detached elements" tool and check detached items.

Expected Behavior

There are no detached nodes related to the cdk-drag element.

Actual Behavior

There are 2 cdk-drag-dragging detached nodes.

Image

The same result will be achieved if performed in Chrome, but more details can be seen in the Edge. If the "Analyze" button is clicked - you will get an id to the retainer (instead of the initial text "Object not found in memory anymore"). Double-clicking on it will show the retainer info. In this case, it's the CdkDropList._dropListRef._draggables array.

Image

Some info after my analysis: _draggables are set in the DropListRef's withItems method.

Image

While debugging I should have entered it via _syncItemsWithRef, but there's a this._dropListRef.isDragging() condition in the removeItem method. In my case, I've already dropped an item and it's no longer dragging. Therefore _draggables are not updated and there's a cdk-drag detached node because of that.

Image

I'm not sure if that condition should be there at all, cause surely we need to sync items with ref not only when dragging is in progress. Here's a link to the commit where those changes were added.

For my project, I monkey-patched removeItem method and removed isDragging() condition. For us, the case when an item should be removed while dragging is not relevant.

Environment

  • Angular: 19.0.6
  • CDK/Material: 19.0.0
  • Browser(s): Chrome/Edge
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows 11 Pro

Metadata

Metadata

Assignees

Labels

P4A relatively minor issue that is not relevant to core functionsarea: cdk/drag-drop

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions