Skip to content

Commit

Permalink
fix: Jumping positions when custom styling and transform fix are acti…
Browse files Browse the repository at this point in the history
…ve together

* Styling top and left used as default positions on draggable init
  • Loading branch information
bcakmakoglu committed Aug 19, 2021
1 parent 13c80ac commit 30f0a35
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/hooks/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ const useDraggable = (target: MaybeRef<any>, options: Partial<DraggableOptions>)
const target = get(node);
if (!target) return;
target.style.transform = '';
target.style.left = '';
target.style.top = '';
target.style.position = 'relative';
const { x, y } = transformOpts();
target.style.left = Math.round(parseInt(<string>get(state).positionOffset?.x) || 0) + Math.round(x) + 'px';
target.style.top = Math.round(parseInt(<string>get(state).positionOffset?.y) || 0) + Math.round(y) + 'px';
target.style.left = Math.round(parseInt(<string>get(state).positionOffset?.x) || 0) + Math.round(Number(x)) + 'px';
target.style.top = Math.round(parseInt(<string>get(state).positionOffset?.y) || 0) + Math.round(Number(y)) + 'px';
};

const removeTransformFix = () => {
Expand Down Expand Up @@ -274,7 +276,15 @@ const useDraggable = (target: MaybeRef<any>, options: Partial<DraggableOptions>)
get(state).dragging = false;
});

tryOnMounted(() => addClasses());
tryOnMounted(() => {
const startX =
(get(state).position ? get(state).position?.x : get(state).defaultPosition.x) || parseInt(get(node).style.top, 10) || 0;
const startY =
(get(state).position ? get(state).position?.x : get(state).defaultPosition.x) || parseInt(get(node).style.left, 10) || 0;
get(state).defaultPosition.x = startX;
get(state).defaultPosition.y = startY;
addClasses() && onUpdated();
});

const { onDragStart: coreStart, onDrag: coreDrag, onDragStop: coreStop, state: coreState } = useDraggableCore(target, options);

Expand Down

0 comments on commit 30f0a35

Please sign in to comment.