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

bounds='parent' moves draggable component to get stuck in y axis

Open cwaj400 opened this issue 2 years ago • 3 comments

Repro:

Using Next.Js - importing : import Draggable, {DraggableCore} from 'react-draggable';

<div className='border bg-gray-100 h-96'> <Draggable handle=".draggable" onDrag={eventControl} onStart={eventControl} bounds='parent' axis='both' onStop={eventControl} // bounds={{left: 0, top: 0, right: 100, bottom: 350}} > <div className="draggable" id='item'> <BsArrowReturnLeft size={30}></BsArrowReturnLeft> </div> </Draggable> </div>

When I console log the parent node: console.log(document.getElementById('item').parentNode); I get this component with classname <div className='border bg-gray-100 h-96'> which is correct. However, the draggable arrow snaps to the right side of the screen (not the parent), and won't move along the x axis. It should not snap past the parent, and it should move along both axis.


UPDATE

The issue here is that the Draggable component is being snapped to the parent - which would be the Sidebar, as this is the strict 'parent' wrapping all children in my react application: export default function App({Component, pageProps}) { return ( <div> <Sidebar> <Component {...pageProps} /> </Sidebar> </div> ); }

So the question is - how do I bound the draggable component to a specific HTMLElement, i.e. the immediate parent, the container, rather than the top level parent? I am assuming this is the issue here.

cwaj400 avatar Mar 19 '23 01:03 cwaj400

Having this exact same issue. Tried binding to parent via bounds="parent" and bounds=".container" (class immediately above the <Draggable> component) but no luck. My item gets snapped to the left of the screen moving only up and down.

izcoser avatar Jul 04 '23 17:07 izcoser

Screenshot 2023-07-07 at 11 31 21 AM you can set the position to absolute for the dragging item It fixed for me

safaldas-neoito avatar Jul 07 '23 06:07 safaldas-neoito

When I set bounds prop, I can only move the element to right and bottom, I can't move it back to top or left. Why?

Ha0ran2001 avatar Sep 19 '23 09:09 Ha0ran2001

Have the same issue. Binding bounds to a wide "parent" doesn't allow to move the element on x axis. Can't use "position: absolute" on the immediate Draggable's child for the fix because of design requirements.

Reproduction: https://codesandbox.io/p/sandbox/table-absolute-position-2h7gjg

a-hyssopus avatar Jan 20 '25 18:01 a-hyssopus

When I set bounds="#root", it seems to work as expected

pedro-brinfer-antunes avatar Jan 29 '25 18:01 pedro-brinfer-antunes