Skip to content

Commit

Permalink
Merge pull request #4146 from udecode/fix/stale-drag-item-element
Browse files Browse the repository at this point in the history
Fix stale `dragItem.element` causing incorrect drag operations
  • Loading branch information
zbeyens authored Mar 6, 2025
2 parents 53e314c + a5d8ebf commit 88fbc8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-ravens-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-dnd': patch
---

Fix: `onDropNode` uses a stale `element` object for the dragged node, resulting in incorrect drag operations.
8 changes: 5 additions & 3 deletions packages/dnd/src/hooks/useDragNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export interface UseDragNodeOptions
*/
export const useDragNode = (
editor: PlateEditor,
{ element, item, ...options }: UseDragNodeOptions
{ element: staleElement, item, ...options }: UseDragNodeOptions
) => {
const elementId = staleElement.id as string;
return useDrag<DragItemNode, unknown, { isDragging: boolean }>(
() => ({
collect: (monitor) => ({
Expand All @@ -47,16 +48,17 @@ export const useDragNode = (
document.body.classList.add('dragging');

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

return {
id: element.id as string,
id: elementId,
editorId: editor.id,
element,
..._item,
};
},
...options,
}),
[]
[editor, elementId]
);
};

0 comments on commit 88fbc8d

Please sign in to comment.