+0
Daily
9,290
stars
1,048
forks
70
watchers
292
prs
520
issues
52
releases
README
React-Draggable
A simple component for making elements draggable.
[Demo | Changelog]
jsx
I can now be moved around!
Table of Contents
- Installation
- Compatibility
- Quick Start
- API
- Draggable
- DraggableCore
- Using nodeRef
- Controlled vs. Uncontrolled
- Content Security Policy
- Contributing
Installation
bash
npm install react-draggable
or
yarn add react-draggable
js
// ES Modules
import Draggable from 'react-draggable';
import { DraggableCore } from 'react-draggable';
// CommonJS
const Draggable = require('react-draggable');
const { DraggableCore } = require('react-draggable');
TypeScript types are included.
Compatibility
| Version | React Version |
|---------|---------------|
| 4.x | 16.3+ |
| 3.x | 15 - 16 |
| 2.x | 0.14 - 15 |
Quick Start
jsx
import React, { useRef } from 'react';
import Draggable from 'react-draggable';
function App() {
const nodeRef = useRef(null);
return (
Drag me!
);
}
View the Demo and its source for more examples.
API
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
ts
type DraggableEventHandler = (e: Event, data: DraggableData) = void | false;
type DraggableData = {
node: HTMLElement,
x: number, y: number,
deltaX: number, deltaY: number,
lastX: number, lastY: number,
};
| 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. RTop issues
#350Distinguish between click and drag25 comments · 2018-07-18#550OnClick inside Draggable doesn't work on mobile mode18 comments · 2021-04-03#771error in react 1914 comments · 2024-12-15#586Better API for rich components9 comments · 2021-07-21#480Using rem as unit in transforms9 comments · 2020-05-13#213bound='parent' option in flex box9 comments · 2016-12-28#727Getting Warnings in new React version.8 comments · 2023-07-24#603bug - dragging activating when using file element outside of draggable div, despite disabled being set to true dynamically in the draggable div 7 comments · 2021-08-28
Discussions
#759Any plans on working on a functional component centric documentation?0 comments · 2024-07-28#750Mouse position relative to draggable changes now and then.0 comments · 2024-05-01#747Installing react-draggable 4.4.60 comments · 2024-04-22#722Is it possible to have multiple bounds ?0 comments · 2023-06-22#721Can the draggable scroll the drop area when dragged from outside into the drop area?0 comments · 2023-06-15#718Restricting draggable to viewport?0 comments · 2023-04-22#706handleDrag with TypeScrypt0 comments · 2023-02-17#704Clickable draggable on mobile0 comments · 2023-02-10
Latest release
2.2.3 v2.2.3 2016-12-08- Bugfix: Fix an issue with the entire window scrolling on a drag on iDevices. Thanks @JaneCoder. See 183