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

Flickering effects on sortable example

Open kraftwer1 opened this issue 11 years ago • 12 comments

I have recorded a video of a strange behavior. On the sortable example, when dragging a smaller item into a larger item (I created larger items by making the list smaller just to illustrate), strange flickering effects occur. Probably the dropping should somehow prevented. Does anyone has a solution for that?

http://youtu.be/t4AmMcTeWto

kraftwer1 avatar Jul 15 '15 13:07 kraftwer1

This happens because the code in sortable examples is naïve and assumes the same height for every item. The move happens as soon as a card is hovered so two items with different heights will enter into a flickering loop where they are hovered and moved one after another.

The solution to this is a bit smarter hover implementation that does something like this:

hover(props, monitor, component) {
  const ownId = props.id;
  const draggedId = monitor.getItem().id;
  if (draggedId === ownId) {
    return;
  }

  // assuming something like findTodoIndex is implemented
  const ownIndex = props.findTodoIndex(ownId);
  const draggedIndex = props.findTodoIndex(draggedId);

  // What is my rectangle on screen?
  const boundingRect = React.findDOMNode(component).getBoundingClientRect();
  // Where is my vertical middle?
  const ownMiddleY = (boundingRect.bottom - boundingRect.top) / 2;
  // Where is the mouse right now?
  const clientOffset = monitor.getClientOffset();

  // We only want to move when the mouse has crossed half of the item's height.
  // If we're dragging down, we want to move only if we're below 50%.
  // If we're dragging up, we want to move only if we're above 50%.

  // Moving down: exit if we're in upper half
  if (draggedIndex < ownIndex && clientOffset.y < ownMiddleY) {
    return;
  }

  // Moving up: exit if we're in lower half
  if (draggedIndex > ownIndex && clientOffset.y > ownMiddleY) {
    return;
  }

  // Time to actually perform the action!
  props.moveCard(draggedId, ownId);
}

This is written in browser so may not be exactly right, but should give you an idea of how to approach this problem.

gaearon avatar Jul 15 '15 13:07 gaearon

Also if you figure this out, I'd appreciate a PR to the example fixing this problem.

gaearon avatar Jul 15 '15 13:07 gaearon

Thanks for your idea. Could it be that isOver() and maybe canDrop() are affected by this behaviour, too?

kraftwer1 avatar Jul 23 '15 10:07 kraftwer1

If you implement the hover to only move the item after the cursor is halfway above/below it, everything will work correctly.

gaearon avatar Jul 23 '15 11:07 gaearon

I found it out, your solution was almost perfect. I've handed in a PR https://github.com/gaearon/react-dnd/pull/251.

kraftwer1 avatar Jul 28 '15 09:07 kraftwer1

Hey, you should change

if (draggedIndex > ownIndex && offsetY > ownMiddleY) {
    return;
}

to

if (draggedIndex >= ownIndex && offsetY > ownMiddleY) {
    return;
}

or else there will still be jumping/flickering.

bkniffler avatar Aug 13 '15 13:08 bkniffler

:thumbsup: I was getting the same flicker issue in items with different heights. came across this, saved me a lot of time. Thanks guys.

ransanjeev avatar Sep 14 '15 05:09 ransanjeev

I had the same flicker issue despite following the latest example. I can also confirm that adding the "or equal to" check to both the dragging downwards and dragging upwards functions fixes this. Ideally the example would be updated to reflect this (although I'm not sure why that behavior isn't exhibited in the example).

michaelaflores avatar Oct 31 '17 16:10 michaelaflores

I'm having the flickering issues. I tried the fixes here, but I can't get it to work. My containers are equal height however. I noticed that when the flickering happens, it continually keeps calling the hover method, because it's position is stuck between nodes.

jeongl avatar Nov 04 '17 03:11 jeongl

@jeongl curious if you could get this up on a Codepen, would love to see if an extra pair of eyes on it identifies the issue.

michaelaflores avatar Nov 27 '17 19:11 michaelaflores

For me, the flickering stops but now the item which is of small height is not getting swapped with the one on hover sometimes. Any idea on this ?

collab-with-tushar-raj avatar Dec 09 '20 10:12 collab-with-tushar-raj

The same behaviour but for a horizontal list

hover(props, monitor, component) {
  const ownId = props.id;
  const draggedId = monitor.getItem().id;
  if (draggedId === ownId) {
    return;
  }

  // assuming something like findTodoIndex is implemented
  const ownIndex = props.findTodoIndex(ownId);
  const draggedIndex = props.findTodoIndex(draggedId);

  // What is my rectangle on screen?
  const boundingRect = React.findDOMNode(component).getBoundingClientRect();
  // Where is my horizontal x middle?
  const ownMiddleX =  boundingRect.left + ((boundingRect.right - boundingRect.left) / 2);
  // Where is the mouse right now?
  const clientOffset = monitor.getClientOffset();

  // We only want to move when the mouse has crossed half of the item's width.
  // If we're dragging left, we want to move only if we're 50% distance to the left
  // If we're dragging up, we want to move only if we're 50% distance to the right

  // Moving left: exit if we're in the left half
  if (draggedIndex > ownIndex && clientOffset.x > ownMiddleX) {
    return;
  }

  // Moving right: exit if we're in the right half
  if (draggedIndex < ownIndex && clientOffset.x < ownMiddleX) {
    return;
  }

  // Time to actually perform the action!
  props.moveCard(draggedId, ownId);
}

layerok avatar Nov 30 '23 21:11 layerok