Flickering effects on sortable example
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
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.
Also if you figure this out, I'd appreciate a PR to the example fixing this problem.
Thanks for your idea. Could it be that isOver() and maybe canDrop() are affected by this behaviour, too?
If you implement the hover to only move the item after the cursor is halfway above/below it, everything will work correctly.
I found it out, your solution was almost perfect. I've handed in a PR https://github.com/gaearon/react-dnd/pull/251.
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.
:thumbsup: I was getting the same flicker issue in items with different heights. came across this, saved me a lot of time. Thanks guys.
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).
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 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.
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 ?
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);
}