react-rnd icon indicating copy to clipboard operation
react-rnd copied to clipboard

[BUG] Box moves when resizing it

Open turshija opened this issue 6 years ago • 5 comments

Overview of the problem

I'm using react-rnd version [10.1.3] (I've noticed that issue doesn't exist in 10.0.0 and starts happening in 10.1.0)

My browser is:
Chrome (79.0.3945.88) on macOS 10.14.6 Firefox (71.0) Safari (13.0.4)

I am sure this issue is not a duplicate. Similar issue was fixed recently in #618, maybe its related, but its still reproducible in 10.1.3 Also, this issue was mentioned in last comment in this closed issue #526 but there was no update.

Reproduced project

https://codesandbox.io/s/elated-hypatia-bpvdh

Description

timeline

When resizing a box from left side (or top), entire box moves (only visually). It always happens every second resize. Temporary solution is to downgrade library to 10.0.0 but then I'm getting a bunch of warnings regarding deprecation of "componentWillMount". It doesn't trigger any drag event (so internally its still on old position, but visually it moved), and when you try to drag it - it moves back to original position instead

Steps to Reproduce

  1. Open codesanbox url
  2. Drag the box using left handle
  3. ???
  4. Profit

Expected behavior

Box should resize without changing its position

Actual behavior

Box moved

turshija avatar Dec 20 '19 10:12 turshija

If you delete bounds prop, it works correctly !

yakupteke avatar Jan 08 '20 13:01 yakupteke

Yes it does, but its not the solution the problem because I need bounds prop.

turshija avatar Jan 08 '20 20:01 turshija

@turshija @yakupteke Thanks for your report. This issue has fixed. Please try latest version.

https://codesandbox.io/s/adoring-sea-vmogo

bokuweb avatar Jan 12 '20 17:01 bokuweb

Try this will help you. Dont use onResizeStop to set the positions

<Rnd
            bounds="window"
            size={{ width: position.width, height: position.height }}
            position={{ x: position.x, y: position.y }}
            minWidth={minWidthDialog}
            minHeight={minHeightDialog}
            dragHandleClassName="draggable-title"
            enableUserSelectHack={false}
            onDragStop={onDragStop}
            onResize={onResize}
          >

onResize :

const onResize: RndResizeCallback = (_e, _direction, ref) => {
    const { x, y } = ref.getBoundingClientRect();
    const onResizePosition = {
      x,
      y,
      width: ref.clientWidth,
      height: ref.clientHeight,
    };
    setPosition(onResizePosition);
  };

onDragStop :

const onDragStop: RndDragCallback = (_e, d) => {
    setPosition(previous => ({ ...previous, x: d.x, y: d.y }));
  };

noffy-tech avatar Mar 26 '24 04:03 noffy-tech

@noffy-tech solution is what worked for me

Joelfo avatar Sep 17 '24 18:09 Joelfo