A <Draggable> element wraps an existing element and extends it with new event handlers and styles. It does not create a wrapper element in the DOM.
Draggable items are moved using CSS Transforms. This allows items to be dragged regardless of their current positioning (relative, absolute, or static). Elements can also be moved between drags without incident.
If the item you are dragging already has a CSS Transform applied, it will be overwritten by <Draggable>. Use an intermediate wrapper (<Draggable><span>...</span></Draggable>) in this case.
Restrict movement. Use 'parent', a CSS selector, or {left, top, right, bottom}
cancel
string
Note: Setting className, style, or transform on <Draggable> will error. Set them on the child element.
<DraggableCore>
For users that require full control, <DraggableCore> provides drag callbacks without managing state or styles. It does not set any transforms; you must handle positioning yourself.
To prevent text from being highlighted while dragging, react-draggable injects a
small <style> element into the document <head> the first time a drag starts
(the enableUserSelectHack, on by default). Under a strict Content Security
Policy that omits 'unsafe-inline' from style-src, the browser blocks that
element and logs a CSP violation.
You have three ways to handle this:
Pass a nonce. Provide the same nonce your CSP header advertises and it's
applied to the injected element:
Do nothing, if you use webpack. When no nonce prop is given,
react-draggable falls back to webpack's
__webpack_nonce__ global if your build
defines it — no per-component prop needed.
Opt out of the injected style. Set enableUserSelectHack={false} and add
the two rules to your own (CSP-compliant) stylesheet:
The nonce is only read when the element is first created. The same element is
shared by every <Draggable>/<DraggableCore> on the page, so set the nonce on
whichever instance drags first (or, more simply, set it consistently everywhere).
Contributing
Fork the project
Run yarn dev to start the development server
Make changes and add tests
Run yarn test to ensure tests pass
Submit a PR
Release Checklist
Update CHANGELOG.md
Run make release-patch, make release-minor, or make release-major
Run make publish
License
MIT
strml
samuel.trace.reed@gmail.com
-
CSS selector for elements that should not initiate drag
defaultClassName
string
'react-draggable'
Class name applied to the element
defaultClassNameDragging
string
'react-draggable-dragging'
Class name applied while dragging
defaultClassNameDragged
string
'react-draggable-dragged'
Class name applied after drag
defaultPosition
{x: number, y: number}
{x: 0, y: 0}
Starting position
disabled
boolean
false
Disable dragging
enableUserSelectHack
boolean
true
Add user-select: none while dragging
grid
[number, number]
-
Snap to grid [x, y]
handle
string
-
CSS selector for the drag handle
nodeRef
React.RefObject
-
Ref to the DOM element. Required for React Strict Mode