From c535afe772bcb0b5256fd3de18240046b83a0a98 Mon Sep 17 00:00:00 2001 From: Daniel Cooke Date: Sat, 30 Mar 2024 11:45:08 +0000 Subject: [PATCH 1/2] fix: don't update based on parent offset on mount * I'm not sure if this will work for all use cases, but for me it is workig much better, before this fix there was an issue when the component position was stored externally and the component is remounted with a defaultPos - it will jump by the parent offset pixels, which just isn't right. In my eyes it should just use the exact position passed to it from consumers as the starting position, if a consumer wants to use the absolute value they can pass that, otherwise the position they use `lastX` `lastY` for example is relative to the parent anyway. So whats the point of applying this offset to those values? --- src/index.tsx | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 8e43e2fc..5e2cc8da 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -239,12 +239,10 @@ export class Rnd extends React.PureComponent { } componentDidMount() { - this.updateOffsetFromParent(); - const { left, top } = this.offsetFromParent; const { x, y } = this.getDraggablePosition(); this.draggable.setState({ - x: x - left, - y: y - top, + x, + y, }); // HACK: Apply position adjustment this.forceUpdate(); @@ -363,11 +361,25 @@ export class Rnd extends React.PureComponent { if (!this.props.onDrag) return; const { left, top } = this.offsetFromParent; if (!this.props.dragAxis || this.props.dragAxis === "both") { - return this.props.onDrag(e, { ...data, x: data.x - left, y: data.y - top }); + return this.props.onDrag(e, { + ...data, + x: data.x - left, + y: data.y - top, + }); } else if (this.props.dragAxis === "x") { - return this.props.onDrag(e, { ...data, x: data.x + left, y: this.originalPosition.y + top, deltaY: 0 }); + return this.props.onDrag(e, { + ...data, + x: data.x + left, + y: this.originalPosition.y + top, + deltaY: 0, + }); } else if (this.props.dragAxis === "y") { - return this.props.onDrag(e, { ...data, x: this.originalPosition.x + left, y: data.y + top, deltaX: 0 }); + return this.props.onDrag(e, { + ...data, + x: this.originalPosition.x + left, + y: data.y + top, + deltaX: 0, + }); } } @@ -375,11 +387,25 @@ export class Rnd extends React.PureComponent { if (!this.props.onDragStop) return; const { left, top } = this.offsetFromParent; if (!this.props.dragAxis || this.props.dragAxis === "both") { - return this.props.onDragStop(e, { ...data, x: data.x + left, y: data.y + top }); + return this.props.onDragStop(e, { + ...data, + x: data.x + left, + y: data.y + top, + }); } else if (this.props.dragAxis === "x") { - return this.props.onDragStop(e, { ...data, x: data.x + left, y: this.originalPosition.y + top, deltaY: 0 }); + return this.props.onDragStop(e, { + ...data, + x: data.x + left, + y: this.originalPosition.y + top, + deltaY: 0, + }); } else if (this.props.dragAxis === "y") { - return this.props.onDragStop(e, { ...data, x: this.originalPosition.x + left, y: data.y + top, deltaX: 0 }); + return this.props.onDragStop(e, { + ...data, + x: this.originalPosition.x + left, + y: data.y + top, + deltaX: 0, + }); } } @@ -636,6 +662,8 @@ export class Rnd extends React.PureComponent { const pos = this.state.resizing ? undefined : draggablePosition; const dragAxisOrUndefined = this.state.resizing ? "both" : dragAxis; + console.log(defaultValue, pos); + return ( Date: Sat, 20 Apr 2024 18:51:02 +0100 Subject: [PATCH 2/2] Remove console log --- src/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 5e2cc8da..28da20ed 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -662,8 +662,6 @@ export class Rnd extends React.PureComponent { const pos = this.state.resizing ? undefined : draggablePosition; const dragAxisOrUndefined = this.state.resizing ? "both" : dragAxis; - console.log(defaultValue, pos); - return (