question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hover and drop not being called when a DragLayer is used.

See original GitHub issue

When I use the DragLayer HOC to customise the drag preview, hovering over a drop target doesn’t work if the drag preview is placed directly under the mouse pointer. However if the drag preview is placed adjacent to the pointer everything works as expected.

So basically if I define a DragLayer like this…

const dragLayerConnector = monitor => ({
  isDragging: monitor.isDragging(),
  itemType: monitor.getItemType(),
  item: monitor.getItem(),
  currentOffset: monitor.getSourceClientOffset(),
  initialClientOffset: monitor.getInitialClientOffset(),
  initialSourceClientOffset: monitor.getInitialSourceClientOffset(),
});

class MyDragLayer extends Component {
  getStyles() {
    const { currentOffset } = this.props;

    if (!currentOffset) {
      return {
        display: 'none',
      };
    }
    const { x, y } = currentOffset;
    
    return {
      transform: `translate(${x}px, ${y}px)`,
      position: 'absolute',
      width: '200px',
    };
  }

  render() {
    const {
      isDragging,
      itemType,
      item,
      currentOffset,
      initialClientOffset,
      initialSourceClientOffset,
      ...other
    } = this.props;

    if (!isDragging) {
      return null;
    }

    if (itemType !== Types.MyItem) {
      return null;
    }

    if (!item) {
      return null;
    }

    const style = this.getStyles();

    return (
      <div style={style}>
        Preview test
      </div>
    );
  }
}

export default DragLayer(dragLayerConnector)(MyDragLayer);

…if my drag source is wider than 200px and I begin to drag from beyond the 200px that the drag layer draws, everything works as expected. However if I begin to drag from within the 200px width, where the draglayer preview is rendered, dragging is automatically stopped as soon as the drag layer renders the first time.

I’ve managed to work around this by leaving the preview drawn by the drag layer one pixel under my pointer, using the following solution inside getStyles, however it would be nice if this wasn’t necessary:

getStyles() {
    const { currentOffset, initialClientOffset, initialSourceClientOffset } = this.props;

    if (!currentOffset || !initialClientOffset || !initialSourceClientOffset) {
      return {
        display: 'none',
      };
    }
    const { x, y } = currentOffset;
    const { y: yInitPointer } = initialClientOffset;
    const { y: yInitSource } = initialSourceClientOffset;

    return {
      transform: `translate(${x}px, ${y + (yInitPointer - yInitSource + 1)}px)`,
      position: 'absolute',
      width: '200px',
    };
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MihirGHcommented, Sep 22, 2021

Adding pointer-events: none in the preview that is rendered in my DragLayer solved this issue.

0reactions
MihirGHcommented, Sep 20, 2021

Hi, I am also facing the same issue. Here is the link to the codesandbox: https://codesandbox.io/s/awesome-currying-q646k?file=/src/components/Table.tsx

Steps to reproduce in the codesandbox:

  • Click on the button to enable custom drag layer
  • Try dragging the first column cell and drop - hover/drop is not getting called and hence the rows are not getting swapped.

For me the solution above is also not working. Can someone please help?

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-dnd not detecting hover and drop events - Stack Overflow
I'm having trouble getting react-dnd to to work. Specifically, while I can confirm dragging is being detected properly, my droppable targets ...
Read more >
gaearon/react-dnd - Gitter
I have a problem: my droptargets are not working. I can drag a component and beginDrag and endDrag are called, but my droptargets'...
Read more >
How to use the react-dnd.useDrop function in react-dnd | Snyk
To help you get started, we've selected a few react-dnd. ... theme }) { const ref = useRef(null) const [, drop] = useDrop({...
Read more >
DropTarget - React-dnd - Breword 文档集合
To use DropTarget , don't forget to wrap the top-level component of your app in ... to verify whether a child, or the...
Read more >
React Drag/Drop - Mendix Marketplace
Not for the faint hearted! This is not an easy widget to work with! Take your time when you want to use it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found