Skip to content

Commit 88fbc8d

Browse files
authored
Merge pull request #4146 from udecode/fix/stale-drag-item-element
Fix stale `dragItem.element` causing incorrect drag operations
2 parents 53e314c + a5d8ebf commit 88fbc8d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

.changeset/weak-ravens-impress.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@udecode/plate-dnd': patch
3+
---
4+
5+
Fix: `onDropNode` uses a stale `element` object for the dragged node, resulting in incorrect drag operations.

packages/dnd/src/hooks/useDragNode.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export interface UseDragNodeOptions
3131
*/
3232
export const useDragNode = (
3333
editor: PlateEditor,
34-
{ element, item, ...options }: UseDragNodeOptions
34+
{ element: staleElement, item, ...options }: UseDragNodeOptions
3535
) => {
36+
const elementId = staleElement.id as string;
3637
return useDrag<DragItemNode, unknown, { isDragging: boolean }>(
3738
() => ({
3839
collect: (monitor) => ({
@@ -47,16 +48,17 @@ export const useDragNode = (
4748
document.body.classList.add('dragging');
4849

4950
const _item = typeof item === 'function' ? item(monitor) : item;
51+
const [element] = editor.api.node<TElement>({ id: elementId, at: [] })!;
5052

5153
return {
52-
id: element.id as string,
54+
id: elementId,
5355
editorId: editor.id,
5456
element,
5557
..._item,
5658
};
5759
},
5860
...options,
5961
}),
60-
[]
62+
[editor, elementId]
6163
);
6264
};

0 commit comments

Comments
 (0)