### Setup React and Draggable for Browser Tests Source: https://github.com/react-grid-layout/react-draggable/blob/master/test/browser/test.html This script dynamically loads the react-draggable.min.js library and makes React, ReactDOM, and Draggable available globally. It's intended for use in browser test environments. ```javascript import React from 'react'; import ReactDOM from 'react-dom'; import * as ReactDOMClient from 'react-dom/client'; // Make React available globally for the Draggable bundle window.React = React; window.ReactDOM = { ...ReactDOM, ...ReactDOMClient }; // Dynamically load the built library (it expects React/ReactDOM as globals) const script = document.createElement('script'); script.src = '../../build/web/react-draggable.min.js'; script.onload = () => { // Make things available globally for tests window.Draggable = ReactDraggable.default; window.DraggableCore = ReactDraggable.DraggableCore; }; document.body.appendChild(script); ``` -------------------------------- ### Install React-Draggable using npm or yarn Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md Use npm or yarn to add react-draggable to your project dependencies. ```bash npm install react-draggable # or yarn add react-draggable ``` -------------------------------- ### Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md DraggableCore provides drag callbacks without managing state or styles, offering more control to the user. It does not handle positioning automatically. ```APIDOC ## ### Description For users that require full control, `` provides drag callbacks without managing state or styles. It does not set any transforms; you must handle positioning yourself. See [React-Resizable](https://github.com/react-grid-layout/react-resizable) and [React-Grid-Layout](https://github.com/react-grid-layout/react-grid-layout) for usage examples. ### Props `` accepts a subset of `` props: - `allowAnyClick` - `allowMobileScroll` - `cancel` - `disabled` - `enableUserSelectHack` - `grid` - `handle` - `nodeRef` - `offsetParent` - `onDrag` - `onMouseDown` - `onStart` - `onStop` - `scale` ``` -------------------------------- ### Handling Content Security Policy with Nonce Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md Provide a `nonce` prop to `Draggable` to apply it to the injected style element, satisfying strict Content Security Policies that omit `'unsafe-inline'` from `style-src`. ```jsx
Drag me
``` -------------------------------- ### Import Draggable and DraggableCore components Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md Import the necessary components from the 'react-draggable' library for use in your React application. Supports both ES Modules and CommonJS. ```javascript // ES Modules import Draggable from 'react-draggable'; import { DraggableCore } from 'react-draggable'; // CommonJS const Draggable = require('react-draggable'); const { DraggableCore } = require('react-draggable'); ``` -------------------------------- ### Using nodeRef Pattern in React Draggable Source: https://github.com/react-grid-layout/react-draggable/blob/master/CLAUDE.md Demonstrates the recommended way to use the `nodeRef` prop with Draggable components to avoid ReactDOM.findDOMNode deprecation warnings in React Strict Mode. ```jsx const nodeRef = React.useRef(null);
Content
``` -------------------------------- ### Basic Draggable Component Usage Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md This snippet shows the most basic usage of the Draggable component. It allows the contained div to be moved around the screen. ```jsx
I can now be moved around!
``` -------------------------------- ### Using nodeRef with Custom Components Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md For custom components, forward the `ref` and `props` to the underlying DOM element to correctly use `nodeRef` with `Draggable`. ```jsx const MyComponent = forwardRef((props, ref) => (
Draggable content
)); function App() { const nodeRef = useRef(null); return ( ); } ``` -------------------------------- ### Basic Draggable Component Usage Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md Import and use the Draggable component to make a div element draggable. Requires a ref for the DOM element, especially in React Strict Mode. ```jsx import React, { useRef } from 'react'; import Draggable from 'react-draggable'; function App() { const nodeRef = useRef(null); return (
Drag me!
); } ``` -------------------------------- ### Source: https://github.com/react-grid-layout/react-draggable/blob/master/README.md The Draggable component wraps an existing element, making it draggable. It manages state and styles for dragging, utilizing CSS Transforms for positioning. ```APIDOC ## ### Description A `` 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 ``. Use an intermediate wrapper (`...`) in this case. ### Props | Prop | Type | Default | Description | |---|---|---|---| | `allowAnyClick` | `boolean` | `false` | Allow dragging on non-left-button clicks | | `allowMobileScroll` | `boolean` | `false` | Don't prevent `touchstart`, allowing scrolling inside containers | | `axis` | `'both' | 'x' | 'y' | 'none'` | `'both'` | Axis to allow dragging on | | `bounds` | `object | string` | - | Restrict movement. Use `'parent'`, a CSS selector, or `{left, top, right, bottom}` | | `cancel` | `string` | - | 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 | | `nonce` | `string` | - | CSP nonce for the injected user-select `