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.

onDragEnd delta.y property inaccurate when inside position fixed container

See original GitHub issue

I’ve encountered an issue where the delta.y property on the onDragEnd callback of DndContext shows an incorrect result if the container element is inside an element that does not scroll with the page. The issue is much easier to show than explain, so I created a CodeSandbox for it: https://codesandbox.io/s/headless-glitter-54n1r?file=/src/App.js

In the real world, I’m trying to drag a little circle over an image and have the circle stay exactly where it is dropped. I was using the delta.x and delta.y properties to calculate the final CSS position based on the starting point and the width/height of the image. In my case, the image is inside a bootstrap modal, but for the codesandbox I just used a simple position:fixed div for demonstration purposes.

Is there a workaround for this? Or perhaps a better way to accomplish what I’m attempting to do? I found a random codepen that shows what I’m trying to accomplish: https://codepen.io/vinsongrant/pen/QGodXV

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
rawcompositioncommented, Jun 21, 2021

@nc my very “hacky” solution was to subtract the scrollTop from delta.y. Since my modal was scrolling, I also had to subtract the modal scrollTop.

const modalScrollOffset = imgRef.current?.closest(".modal-content")?.scrollTop || 0;
const documentScrollOffset = document.documentElement.scrollTop;
const correctedDeltaY = delta.y - modalScrollOffset - documentScrollOffset;
0reactions
rawcompositioncommented, Nov 11, 2021

@srmagura yeah I’m facing that issue too, even with the latest version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixed position but relative to container - Stack Overflow
However, the div is inside a centered container. When I use position:fixed it fixes the div relative to the browser window, such as...
Read more >
transform on parent messes up dragging positioning #128
Because Droppable area's position property still position:fixed. And unfortunately you can't override droppable area's css as draggable element.
Read more >
Drag'n'Drop with mouse events - The Modern JavaScript Tutorial
Taking something and dragging and dropping it is a clear and simple way to do many things, from copying and moving documents (as...
Read more >
react-rnd - npm
The x and y property is used to set the default position of the component. size?: { width: (number | string), height: (number...
Read more >
What If Our Sliders Actually Slid? - Smashing Magazine
See the Pen “Think” Outside The Box Toggle w/ CSS @property ✨ by Jhey. ... Any setup code, etc. we run inside an...
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