Drag: Constraints
An example of making an element draggable using Motion for React's drag prop. You can even set drag constraints by providing a ref to another element.
Source code
"use client"
import * as motion from "motion/react-client"
import { useRef } from "react"
export default function DragConstraints() {
const constraintsRef = useRef<HTMLDivElement>(null)
return (
<motion.div ref={constraintsRef} style={constraints}>
<motion.div
drag
dragConstraints={constraintsRef}
dragElastic={0.2}
style={box}
/>
</motion.div>
)
}
/**
* ============== Styles ================
*/
const constraints = {
width: 300,
height: 300,
backgroundColor: "var(--hue-1-transparent)",
borderRadius: 10,
}
const box = {
width: 100,
height: 100,
backgroundColor: "var(--hue-1)",
borderRadius: 10,
}Related examples
Latest in React
Motion+
Unlock all 400+ examples
- Source code for every Plus example.
- Provide examples direct to your agent via Motion's MCP.
- Lifetime access to new examples and APIs.








