React-grid-layout: `onDragStart` triggered on item click but `onDragEnd` Doesn't

Created on 5 Aug 2020  路  10Comments  路  Source: STRML/react-grid-layout

When implementing the onDragStart and onDragEnd. A click on an item (not an actual drag) would trigger onDragStart but doesn't trigger onDragEnd.

Please check this gif

2020-08-05 12 28 16

I am not sure if the fix here would require not triggering onDragStart on click or triggering onDragEnd after the click is done.

reproduce steps

1- https://codesandbox.io/s/zealous-drake-q3ube?file=/src/ShowcaseLayout.js:1923-1932
2- click on an item and check the logs

stale

Most helpful comment

As stated in my previous comment, there is an actual bug here, regardless of the decision behind the callback issue in the title.

All 10 comments

I'm also running into this.

Weird, I am not getting this behaviour

Same here. My workaround is to set another event handler on handle's onClick, in which I set isDragging = false.
I think the issue is not directly related to react-grid-layout lib, but rather react-draggable

Same here. My workaround is to set another event handler on handle's onClick, in which I set isDragging = false.
I think the issue is not directly related to react-grid-layout lib, but rather react-draggable

Nice. I came to a similar workaround, just using onDrag and onDragStop. In my case, I'm using it to prevent clicks on links when dragging so a user doesn't accidentally open a link when they are trying to rearrange the grid.

  1. Declare isDragging state
    const [isDragging, setIsDragging] = useState(false);

  2. Create handlers for onDrag and onDragStop. Conditional in handleDrag is to ensure it is not called continually

const handleDrag = () => {
    if (!isDragging) {
        setIsDragging(true);
    }
}

const handleDragStop = () => {
    setIsDragging(false);
}
  1. Use useEffect to do the work
useEffect(() => {
    if (isDragging) {
        // Do one thing
    }
    else {
        // Do a different thing
    }
}, [isDragging]);

Same here. My workaround is to set another event handler on handle's onClick, in which I set isDragging = false.
I think the issue is not directly related to react-grid-layout lib, but rather react-draggable

Nice. I came to a similar workaround, just using onDrag and onDragStop. In my case, I'm using it to prevent clicks on links when dragging so a user doesn't accidentally open a link when they are trying to rearrange the grid.

  1. Declare isDragging state
    const [isDragging, setIsDragging] = useState(false);
  2. Create handlers for onDrag and onDragStop. Conditional in handleDrag is to ensure it is not called continually
const handleDrag = () => {
    if (!isDragging) {
        setIsDragging(true);
    }
}

const handleDragStop = () => {
    setIsDragging(false);
}
  1. Use useEffect to do the work
useEffect(() => {
    if (isDragging) {
        // Do one thing
    }
    else {
        // Do a different thing
    }
}, [isDragging]);

This is way clearer than what I was doing, thanks!

I found this due to the same problem, however I think the solution is a lot easier than above.

   const handleDragStart = useCallback((layout, oldItem, newItem, placeholder, e) => {
        window.addEventListener('mouseup', handleDragEnd);
        // rest of the code
    }, []);

    const handleDragEnd = useCallback(() => {
        window.removeEventListener('mouseup', handleDragEnd);
    }, []);

Whether or not onDragEnd should fire, the GridItem definitely needs to reset the draggable state.

GridItem.jsx#L414:

    this.setState({ dragging: newPosition });

    // Call callback with this data
    const { x, y } = calcXY(
      this.getPositionParams(),
      newPosition.top,
      newPosition.left,
      this.props.w,
      this.props.h
    );

    return onDragStart.call(this, this.props.i, x, y, {
      e,
      node,
      newPosition
    });
  };

However, if onDragStart.call returns false, denoting a cancellation of the drag event, the dragging state is not reset. This has the side effect of breaking grid resizing (as the tile is being "dragged" during the resize events).

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 7 days

As stated in my previous comment, there is an actual bug here, regardless of the decision behind the callback issue in the title.

Can we reopen, I am also seeing this bug.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

moritzschaefer picture moritzschaefer  路  3Comments

victor-unsw picture victor-unsw  路  3Comments

hepiyellow picture hepiyellow  路  4Comments

sasha240100 picture sasha240100  路  3Comments

gfafei picture gfafei  路  3Comments