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

drag ending prematurely

Open azadbolour opened this issue 9 years ago • 15 comments

I have a situation in which the drag of a piece is immediately and unexpectedly aborted while I am still dragging.

The sequence of calls to my drag source are: canDrag (returning true), beginDrag returning the item, and endDrag (with isDropped being false, as expected).

The application is a scrabble-like game, where pieces can be dragged to/from a board from/to a rack. Dragging a piece from the rack works. Dragging it from the board does not work - is aborted immediately.

In both cases I am using the same React component to represent the piece as the drag source.

I'd really appreciate any hints on how to go about debugging/troubleshooting this issue.

Thanks.

Azad

P.S. Using React 15.4.2, React DND 2.4.0, Webpack 1.12.1, and Chrome 58 on MAC OS/X.

azadbolour avatar May 10 '17 02:05 azadbolour

I managed to reproduce a similar problem with a minor tweak to the Chessboard example.

I cloned the latest react-dnd repository, and made sure the knight can be moved as expected. Then I edited examples/00\ Chessboard/Tutorial\ App/BoardSquare.js to return true all the time for canDrop:

const squareTarget = { canDrop(props) { return true; // return canMoveKnight(props.x, props.y); },

Now when I bring up the application, I cannot drag the knight.

Not sure why this is happening. But I hope figuring out this simple test case can shed light on my original problem of inability to drag pieces.

Thanks in advance for hints on what is going on here and how to fix it.

Azad

azadbolour avatar May 14 '17 18:05 azadbolour

same here, but only in Firefox. Firefox stops calling canDrop after 3 times while Chrome is continuously calling it.

carlesba avatar May 25 '17 09:05 carlesba

I hit that problem in Firefox 58.0b4 as well. canDrop is called a few times and then endDrag, though I'm still dragging element.

ul avatar Nov 16 '17 04:11 ul

Same issue here only on Firefox I'm using 52.6.0 ESR

deanmaster avatar Mar 12 '18 17:03 deanmaster

My colleague Allen Haim (@misterfish) found the following workaround which for now is working for me. I have not, however, done exhaustive testing on all major platform x browser combinations.

His idea is to make sure the drag item has actually moved before allowing a drop. Exactly why this solves the problem, I am not sure.

Here is what my working code looks like:

const MIN_MOVEMENT = 15;
const vectorMagnitude = (x, y) => Math.sqrt(x ** 2 + y ** 2);
const isMinimallyMoved = ({ x, y }) => vectorMagnitude(x, y) > MIN_MOVEMENT;

canDrop: function (props, monitor) {
  let piece = getMonitorPiece(monitor);
  let point = props.point;
  let legal = props.isLegalMove(piece, point);
  // A workaround to avoid react dnd glitch preventing drag.
  let minimallyMoved = isMinimallyMoved((monitor.getDifferenceFromInitialOffset()));
  return minimallyMoved && legal && props.enabled;
},
// ...

If you do experiment with this technique, please share your findings and insights.

Cheers.

Azad

azadbolour avatar Mar 18 '18 23:03 azadbolour

Not sure whether it is related but I sometimes end up here:

image

Which means dragging ends right after dragging starts.. Haven't been able to reproduce when exactly this happens but it seems like that some drag sessions are not finished correctly so it ends up here.

I'd say that this is an issue inside this library as it shouldn't fail like this when this situation occurs.

Update: my browser is Chrome 65.0.3325.181

jarben avatar Mar 26 '18 10:03 jarben

I am experiencing this issue as well. I have a dragSource and a dropSource that are siblings in a component. They sit on top of one another and the draggable has interactive elements inside as well. Whenever I go to change the Z-index of the sibling and my drag handle is over the changed area; the drag handle is lost and the endDrag method is called terminating the drag sequence. We are getting this in Chrome. FF works without issue. <<< edit >>> It would appear that this issue only occurs when the drop zone that is not set to !canDrop appears over the draggable item. Chrome basically shifts the Z-Order? over the draggable item causing the pointer event to end that in which triggers endDrag. Usually you just set the drop zone that sits over the draggable item to !canDrop but in most situations it needs something custom where a drop zone appears over a draggable item. You can see this effect in chrome by toggling pointer events on an off the drop zones. I was able to circumvent this effect by setting a 0 z-index on the canDrop=FALSE/isOver=FALSE and I could achieve the desired effect but you lose the effects on that drop zone as well as handling drop in that zone as well. Another short coming with it is that if you set up a "drop zones DOM or overlay i.e. GhostDom" you will also see this issue crop up as the zone over the item is set to canDrop. Solution for the library would be to allow some way for the draggable item to know that it is "overItself" in addition to "isOver" although it would take some intense work to get that done for a browser specific issue.

Hope this helps someone! Best strategy is to just not put a drop zone over your draggable item at all and it is always avoided which sucks; I know!

djErock avatar May 14 '18 20:05 djErock

I'm experiencing this as well. I've managed to make a small example https://codesandbox.io/s/7vmz86mpq It works in Firefox and Edge just fine, but doesn't work in Chrome. This example is also containing a workaround, replace it in Panel.js:

-let canDropIt = canDrop
+let canDropIt = canDropSafe

rooks avatar May 15 '18 20:05 rooks

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 06 '19 05:07 stale[bot]

Just for people stumbling on this issue in the future...

The comments above helped me figure out that the problem happens when the drop zone is overlapping the dragged item at the beginning of the move. Finding a logical way to detect the conflict on the drop target and set its canDrop property to false should do the trick. In my case, it totally made sense to prevent dropping in the same spot anyway...

Still, I don't think it should "crash" silently like it does right now...

Note that in my case, it happened on Chrome, but not on Firefox, where everything was working fine.

davidsavoie1 avatar Sep 12 '19 18:09 davidsavoie1

Hey. I have a similar issue with chrome, but the root cause is different. The issue is related to removing elements from drag preview and scrolling of the page. Hope that this information will help someone in the future. https://stackoverflow.com/questions/59172584/javascript-dragend-event-fired-immediatly-after-dragstart https://stackoverflow.com/questions/28408720/jquery-changing-the-dom-on-dragstart-event-fires-dragend-immediately

AndriiTsarenko avatar Dec 18 '20 18:12 AndriiTsarenko

anyone has found a proper solution or a good explanation ?

RomanPanaget avatar Jun 08 '21 11:06 RomanPanaget

I'm experiencing this as well. I've managed to make a small example https://codesandbox.io/s/7vmz86mpq It works in Firefox and Edge just fine, but doesn't work in Chrome. This example is also containing a workaround, replace it in Panel.js:

-let canDropIt = canDrop
+let canDropIt = canDropSafe

Hey, having the exact same issue as your example. Did you have a solution ?

Guenolekikabou avatar Aug 16 '21 08:08 Guenolekikabou

@Guenolekikabou What do you mean? You've quoted a solution for the problem → Instead of relying on react-dnd's state for canDrop I'm keeping my own state for this property. Just check out the codesanbox example I provided and yes, I'm still using this solution as a workaround for broken canDrop prop.

rooks avatar Aug 18 '21 21:08 rooks

I noted the canDrop gets cancelled because when you first drag it detects you are throwing the draggable over itself.

My implementation includes evaluating that we are not over the same index.

   `canDrop(item: any, monitor) {
    const { draggingElemIndex } = item

    const notSameIndex = draggingElemIndex !== dropIntoElemIndex
    const canDrop = notSameIndex && monitor.isOver()
    
    if (canDrop) {
      return true
    } else {
      return false
    }}`

binaryme avatar Jul 12 '22 12:07 binaryme