Skip to content

Commit

Permalink
Prevent unstable ref from being unset during drag operation
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Sep 18, 2024
1 parent c597b3f commit d302511
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/prevent-unstable-ref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/react': patch
---

Prevent unstable `ref` from being set to undefined during a drag operation on draggable sources during a drag operation.
8 changes: 8 additions & 0 deletions packages/react/src/core/draggable/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export function useDraggable<T extends Data = Data>(
),
ref: useCallback(
(element: Element | null) => {
if (
!element &&
draggable.element?.isConnected &&
!draggable.manager?.dragOperation.status.idle
) {
return;
}

draggable.element = element ?? undefined;
},
[draggable]
Expand Down
10 changes: 10 additions & 0 deletions packages/react/src/sortable/useSortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ export function useSortable<T extends Data = Data>(input: UseSortableInput<T>) {
),
sourceRef: useCallback(
(element: Element | null) => {
const {manager} = sortable;

if (
!element &&
sortable.source?.isConnected &&
!manager?.dragOperation.status.idle
) {
return;
}

sortable.source = element ?? undefined;
},
[sortable]
Expand Down

0 comments on commit d302511

Please sign in to comment.