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.

How do I get the coordinates of my dropped item relative to the drop target container?

See original GitHub issue

I am working with 1.0.0-rc.

In drag-around-naive:

const boxTarget = {
  drop(props, monitor, component) {
    const item = monitor.getItem();
    const delta = monitor.getDifferenceFromInitialOffset();
    const left = Math.round(item.left + delta.x);
    const top = Math.round(item.top + delta.y);

    component.moveBox(item.id, left, top);
  }
};

At present it looks like I need to get the offset from the page of the original item, then I can find the offset from the page of where I dropped, then I need to find the offset of the drop container from the page, then do that calculation and I have the relative position in my drop target container.

This seems like a lot of work for something pretty common…

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:15
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

45reactions
gaearoncommented, May 13, 2015

Yeah I guess we could have this for symmetry. For now, you can just use findDOMNode(component).getBoundingClientRect(), but I wouldn’t mind adding this if more people voice support. Let’s keep this open and see what others say.

19reactions
sheeruncommented, Jan 24, 2016

@gaearon I’m interested in generalized solution. How can we use react-dnd-html5-backend instead of getBoundingClientRect directly?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get co-ordinates in relation to drop target with Draggable
After an item has been successfully dropped I need to get the X and Y co-ordinates of the dropped item in relation to...
Read more >
Get element position in the DOM on React DnD drop?
Workbench: const boxTarget = { drop(props, monitor, component) { const item = monitor. getItem() console. log(component, item.
Read more >
Drag and Drop | Ext JS 6.2.0
A drag operation, essentially, is a click gesture on some UI element while ... We'll need to make the rented, repair and vehicle...
Read more >
ondrop Event
The ondrop event occurs when a draggable element or text selection is dropped on a valid drop target. Drag and drop is a...
Read more >
Introduction to JavaScript Drag and Drop API
The dragenter event fires as soon as you drag the element over a drop target. After the dragenter event fires, the dragover event...
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