1
- # React-Draggable [ ![ Build Status] ( https://travis-ci.org/mzabriskie/react-draggable.svg?branch=master )] ( https://travis-ci.org/mzabriskie/react-draggable )
1
+ # React-Draggable [ ![ Build Status] ( https://travis-ci.org/mzabriskie/react-draggable.svg?branch=master )] ( https://travis-ci.org/mzabriskie/react-draggable ) [ ![ npm downloads ] ( https://img.shields.io/npm/dt/react-draggable.svg?maxAge=2592000 )] ( )
2
2
3
3
A simple component for making elements draggable.
4
4
5
- [ View the Changelog] ( CHANGELOG.md )
5
+ ``` js
6
+ < Draggable>
7
+ < div> I can now be moved around! < / div>
8
+ < / Draggable>
9
+ ```
10
+
11
+ - [ Demo] ( http://mzabriskie.github.io/react-draggable/example/ )
12
+ - [ Changelog] ( CHANGELOG.md )
6
13
7
- ### Demo
14
+ ------
15
+
16
+ #### Technical Documentation
17
+
18
+ - [ Installing] ( #installing )
19
+ - [ Exports] ( #exports )
20
+ - [ Draggable] ( #draggable )
21
+ - [ Draggable Usage] ( #draggable-usage )
22
+ - [ Draggable API] ( #draggable-api )
23
+ - [ Controlled vs. Uncontrolled] ( #controlled-vs-uncontrolled )
24
+ - [ DraggableCore] ( #draggablecore )
25
+ - [ DraggableCore API] ( #draggablecore-api )
8
26
9
- [ View Demo] ( http://mzabriskie.github.io/react-draggable/example/ )
10
27
11
28
12
29
### Installing
@@ -49,25 +66,69 @@ positioning (relative, absolute, or static). Elements can also be moved between
49
66
If the item you are dragging already has a CSS Transform applied, it will be overwritten by ` <Draggable> ` . Use
50
67
an intermediate wrapper (` <Draggable><span>...</span></Draggable> ` ) in this case.
51
68
69
+ ### Draggable Usage
70
+
71
+ View the [ Demo] ( http://mzabriskie.github.io/react-draggable/example/ ) and its
72
+ [ source] ( /example/index.html ) for more.
73
+
74
+ ``` js
75
+ import React from ' react' );
76
+ import ReactDOM from ' react-dom' ;
77
+ import Draggable from ' react-draggable' ;
78
+
79
+ class App extends React .Element {
80
+
81
+ eventLogger = (e : MouseEvent , data : Object ) => {
82
+ console .log (' Event: ' , event );
83
+ console .log (' Data: ' , data);
84
+ };
85
+
86
+ render () {
87
+ return (
88
+ < Draggable
89
+ axis= " x"
90
+ handle= " .handle"
91
+ defaultPosition= {{x: 0 , y: 0 }}
92
+ position= {null }
93
+ grid= {[25 , 25 ]}
94
+ zIndex= {100 }
95
+ onStart= {this .handleStart }
96
+ onDrag= {this .handleDrag }
97
+ onStop= {this .handleStop }>
98
+ < div>
99
+ < div className= " handle" > Drag from here< / div>
100
+ < div> This readme is really dragging on... < / div>
101
+ < / div>
102
+ < / Draggable>
103
+ );
104
+ }
105
+ }
106
+
107
+ ReactDOM .render (< App/ > , document .body );
108
+ ```
52
109
53
110
### Draggable API
54
111
55
- The ` <Draggable/> ` component transparently adds draggable to whatever element is supplied as ` this.props.children ` .
56
- ** Note** : Only a single element is allowed or an Error will be thrown.
112
+ The ` <Draggable/> ` component transparently adds draggability to its children.
113
+
114
+ ** Note** : Only a single child is allowed or an Error will be thrown.
57
115
58
116
For the ` <Draggable/> ` component to correctly attach itself to its child, the child element must provide support
59
117
for the following props:
60
118
- ` style ` is used to give the transform css to the child.
61
119
- ` className ` is used to apply the proper classes to the object being dragged.
62
120
- ` onMouseDown ` , ` onMouseUp ` , ` onTouchStart ` , and ` onTouchEnd ` are used to keep track of dragging state.
63
121
64
- React.DOM elements support the above six properties by default, so you may use those elements as children without
65
- any changes. If you wish to use a React component you created, you might find
66
- [ this React page ] ( https://facebook.github.io/react/docs/transferring-props.html ) helpful .
122
+ React.DOM elements support the above properties by default, so you may use those elements as children without
123
+ any changes. If you wish to use a React component you created, you'll need to be sure to
124
+ [ transfer prop ] ( https://facebook.github.io/react/docs/transferring-props.html ) .
67
125
68
- Props:
126
+ #### ` <Draggable> ` Props:
69
127
70
128
``` js
129
+ //
130
+ // Types:
131
+ //
71
132
type DraggableEventHandler = (e : Event , data : DraggableData ) => void | false ;
72
133
type DraggableData = {
73
134
node: HTMLElement ,
@@ -76,6 +137,10 @@ type DraggableData = {
76
137
deltaX: number, deltaY: number,
77
138
lastX: number, lastY: number
78
139
};
140
+
141
+ //
142
+ // Props:
143
+ //
79
144
{
80
145
// If set to `true`, will allow dragging on non left-button clicks.
81
146
allowAnyClick: boolean,
@@ -119,7 +184,7 @@ handle: string,
119
184
120
185
// Called whenever the user mouses down. Called regardless of handle or
121
186
// disabled status.
122
- onMouseDown : (e : MouseEvent ) => boolean ,
187
+ onMouseDown : (e : MouseEvent ) => void ,
123
188
124
189
// Called when dragging starts. If `false` is returned any handler,
125
190
// the action will cancel.
@@ -143,53 +208,6 @@ Note that sending `className`, `style`, or `transform` as properties will error
143
208
directly.
144
209
145
210
146
- ### Draggable Usage
147
-
148
- ``` js
149
- var React = require (' react' ),;
150
- var ReactDOM = require (' react-dom' );
151
- var Draggable = require (' react-draggable' );
152
-
153
- var App = React .createClass ({
154
- handleStart : function (event , ui ) {
155
- console .log (' Event: ' , event );
156
- console .log (' Position: ' , ui .position );
157
- },
158
-
159
- handleDrag : function (event , ui ) {
160
- console .log (' Event: ' , event );
161
- console .log (' Position: ' , ui .position );
162
- },
163
-
164
- handleStop : function (event , ui ) {
165
- console .log (' Event: ' , event );
166
- console .log (' Position: ' , ui .position );
167
- },
168
-
169
- render : function () {
170
- return (
171
- < Draggable
172
- axis= " x"
173
- handle= " .handle"
174
- defaultPosition= {{x: 0 , y: 0 }}
175
- position= {null }
176
- grid= {[25 , 25 ]}
177
- zIndex= {100 }
178
- onStart= {this .handleStart }
179
- onDrag= {this .handleDrag }
180
- onStop= {this .handleStop }>
181
- < div>
182
- < div className= " handle" > Drag from here< / div>
183
- < div> This readme is really dragging on... < / div>
184
- < / div>
185
- < / Draggable>
186
- );
187
- }
188
- });
189
-
190
- ReactDOM .render (< App/ > , document .body );
191
- ```
192
-
193
211
## Controlled vs. Uncontrolled
194
212
195
213
` <Draggable> ` is a 'batteries-included' component that manages its own state. If you want to completely
0 commit comments