Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Conditionally update positions onResizeStop causes strange jiggle #900

Open
donrsh opened this issue Jan 4, 2023 · 4 comments
Open

Comments

@donrsh
Copy link

donrsh commented Jan 4, 2023

Overview of the problem

I'm using react-rnd 10.4.1 [x.x.x]

My browser is: Chrome 108.0.5359.125 on Windows

I am sure this issue is not a duplicate?

Reproduced project

https://codesandbox.io/s/react-rnd-conditionally-resize-jiggle-cvkm8d?file=/src/App.tsx

Description

I want to implement a rnd block in controlled mode, with the constraint that if the block violates some rules (in the above case, the rule is cross the black line) when resizing, then bypass the operation.

It seems to work after resizing. However, when refocus on the rnd block, a strange jiggle happened. But the states (x/y/width/height) aren't changed.

resize-issue

Steps to Reproduce

  1. Resize the rnd block (the red block) from the right-top anchor to cross the black line.
  2. Refocus the rnd block and observe it moves without state changes.

Expected behavior

The rnd block stays where it was before resizing

Actual behavior

The rnd block moves.

@donrsh
Copy link
Author

donrsh commented Jan 5, 2023

I came up with a workaround. The idea is setting a key on the Rnd element, and update the key if the onResizeStop doesn't commit the resize, to clean up the broken Rnd element. The codes would be like:

function MyRnd() {
const [state, setState] = useState({x, y, width, height })
const [key, updateKey] = useReducer(x => x + 1, 0)

const onResizeStop = useCallback(() => {
  if (!shouldCommitResize()) {
    updateKey();
    return;
  }
  setState(newState);
});

return (
  <Rnd
    key={key}
    position={{...}}
    size={{...}}
    onResizeStop={onResizeStop}
   />
);
}

I've implemented this on the issue codesandbox

@Rados51
Copy link

Rados51 commented Jun 20, 2024

The issue is that x from argument position in function onResizeStop has wrong value.
During onResize event, there is wrongly updated position. If we resize an element by one unit, it is okay, but if it is resized by more than one unit, then the position has an offset of 25 more than it should.

In my case, it can be solved by removing this line as it seems that an update is not needed at that point anyway.

this.resizingPosition = { x, y };

Did not check whole state flow or codebase, so I am not sure from which part that wrong value comes from, but it is definitely a wrong value, and it would be better to fix it there instead of removing the line above.

@Rados51
Copy link

Rados51 commented Jun 20, 2024

@bokuweb It might be easier for you to spot where exactly the issue comes from, but if you are tight on time, I could possibly do a pull request.

@bokuweb
Copy link
Owner

bokuweb commented Jun 21, 2024

@Rados51 Thanks. If it's okay, may I request a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants