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 · 1 comment
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

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

1 participant