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: When dropping aligned to a grid, it does not drop where it should #453

Open
fairps opened this issue Sep 27, 2018 · 8 comments
Open

Comments

@fairps
Copy link

fairps commented Sep 27, 2018

I would consider this a bug.

Overview of the problem

The dragGrid is honored when dragging the object, but when it is dropped it "jumps" when ending the drag. You can see it in the example if you release the drag carefully so you don't move when releasing it and you can watch it jump when you let it go. It is now impossible to drag it back on top of the second item that was never moved, because they are now locked to different 50 x 50 grids (in this case).

Reproduced project

The following CodeSandbox demonstrates the issue:
https://codesandbox.io/s/pyyj7l55mx

Description

This is a modified clone of the size/position example. There are two rectangles that start on top of eachother, and each has a dragGrid and resizeGrid of [50, 50].

Steps to Reproduce

  1. There are two squares on top of each other. Drag one of them somewhere
  2. Try and drag it back and lay it on the second one that hasn't been moved - you will notice they are offset
  3. You will also notice a jump any time you drag one and let it drop

Expected behavior

I expected them to drop exactly as a they appeared before release and honoring the dragGrid to keep them on the grid.

Actual behavior

They don't drop where you drop them, and they move off the grid, causing them to have their own grid that is not aligned with other objects using a dragGrid

@kpettinga
Copy link

Experiencing the same issue. Thanks for posting.

@kpettinga
Copy link

My temporary workaround for this issue is to use the lastX and lastY properties from the DraggableData argument passed in the event handler.

example:

onDragStop(event, data) {
    this.setState({
        x: data.lastX
        y: data.lastY
    })
}

This leads me to believe the issue is isolated to the onDragStop callback since the onDrag callback seems to respect the given grid.

@pebbledesigns
Copy link

Any updates on this issue? I have re-created with a potential workaround by @kpettinga but no luck unfortunately https://codesandbox.io/s/4l310zyzw7

@pebbledesigns
Copy link

Found a fix for anyone looking. I just commented out //position={{ x: this.state.x, y: this.state.y }}

@flashtheman
Copy link

Facing the issue as well. x and y returned from onDragStop is not what it should be according to the snapGrid array setting.

@flashtheman
Copy link

Just checked the issue seems to be in the source library (react-draggable). I created an issue for this: react-grid-layout/react-draggable#413

@ghost
Copy link

ghost commented Nov 19, 2019

I had the same problem

It was necessary to move and resize the element

Using this combination of properties and methods, the necessary behavior was achieved

          onDragStop={(e, d) => this.handleDragStop(e, d)}
          onResize={(e, direction, ref, delta, position) =>
                      this.handleResizeStop(e, direction, ref, delta, position)
                    }
           dragGrid={[10, 10]}
           resizeGrid={[10, 10]}
           size={{
                  width: this.state.width,
                  height: this.state.height,
           }}
           position={{
                      x: this.state.x,
                      y: this.state.y,
                    }}
 handleDragStop = (e, d) => {
    this.setState({ x: d.lastX ,y: d.lastY });
  };

@nicholaschiang
Copy link

I had the same problem. And @pebbledesigns, that isn't a solution (that's just making the component uncontrolled... it won't help with us folks who need to control the position).

STRML added a commit to react-grid-layout/react-draggable that referenced this issue Jul 29, 2020
This was caused by `onStop` erroneously recalculating coordinates,
which is not necessary / should not happen in the real world, as `mouseup`
and `touchend` should not pass `clientX` and `clientY` values meaningfully
different than the last corresponding move event. In fact, they are already
ignored by `<Draggable>`.

For this reason, we can simply trust the `lastX` and `lastY` in `onDragStop`,
and pass those back.

Fixes #413, bokuweb/react-rnd#453
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

5 participants