Skip to content

Commit 719e67d

Browse files
committed
fix(cdk/drag-drop): avoid retaining destroyed items until next drag (#30514)
We have some logic that registers and de-registers the items on create/destroy. That logic only syncs the item with the internal `DragRef` once the next dragging starts which means that we might retain a destroyed item if another drag doesn't start. These changes switch to always syncing the list when an item is removed. I've also added some context around why things are set up as they are right now since it took a while to remember the reasoning. Fixes #30506. (cherry picked from commit 26765a4)
1 parent 90ac613 commit 719e67d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ export class CdkDropList<T = any> implements OnDestroy {
219219
addItem(item: CdkDrag): void {
220220
this._unsortedItems.add(item);
221221

222+
// Only sync the items while dragging since this method is
223+
// called when items are being initialized one-by-one.
222224
if (this._dropListRef.isDragging()) {
223225
this._syncItemsWithRef();
224226
}
@@ -228,9 +230,8 @@ export class CdkDropList<T = any> implements OnDestroy {
228230
removeItem(item: CdkDrag): void {
229231
this._unsortedItems.delete(item);
230232

231-
if (this._dropListRef.isDragging()) {
232-
this._syncItemsWithRef();
233-
}
233+
// This method might be called on destroy so we always want to sync with the ref.
234+
this._syncItemsWithRef();
234235
}
235236

236237
/** Gets the registered items in the list, sorted by their position in the DOM. */

0 commit comments

Comments
 (0)