Skip to content

Commit af59543

Browse files
committed
README updates for new API & to be more clear about controlled vs uncontrolled
1 parent f7df5a4 commit af59543

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

README.md

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ repository and running `$ make`. This will create umd dist files in the `dist/`
2424

2525
### Exports
2626

27-
The default export is `<Draggable>`. At the `.DraggableCore` property is `<DraggableCore>`. Here's how to use it:
27+
The default export is `<Draggable>`. At the `.DraggableCore` property is [`<DraggableCore>`](#draggablecore).
28+
Here's how to use it:
2829

2930
```js
3031
// ES6
@@ -37,7 +38,7 @@ let Draggable = require('react-draggable');
3738
let DraggableCore = Draggable.DraggableCore;
3839
```
3940

40-
## Draggable
41+
## `<Draggable>`
4142

4243
A `<Draggable>` element wraps an existing element and extends it with new event handlers and styles.
4344
It does not create a wrapper element in the DOM.
@@ -54,15 +55,15 @@ an intermediate wrapper (`<Draggable><span>...</span></Draggable>`) in this case
5455
The `<Draggable/>` component transparently adds draggable to whatever element is supplied as `this.props.children`.
5556
**Note**: Only a single element is allowed or an Error will be thrown.
5657

57-
For the `<Draggable/>` component to correctly attach itself to its child, the child element must provide support for the following props:
58+
For the `<Draggable/>` component to correctly attach itself to its child, the child element must provide support
59+
for the following props:
5860
- `style` is used to give the transform css to the child.
5961
- `className` is used to apply the proper classes to the object being dragged.
60-
- `onMouseDown` is used along with onMouseUp to keep track of dragging state.
61-
- `onMouseUp` is used along with onMouseDown to keep track of dragging state.
62-
- `onTouchStart` is used along with onTouchEnd to keep track of dragging state.
63-
- `onTouchEnd` is used along with onTouchStart to keep track of dragging state.
62+
- `onMouseDown`, `onMouseUp`, `onTouchStart`, and `onTouchEnd` are used to keep track of dragging state.
6463

65-
React.DOM elements support the above six properties by default, so you may use those elements as children without any changes. If you wish to use a React component you created, you might find [this React page](https://facebook.github.io/react/docs/transferring-props.html) helpful.
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.
6667

6768
Props:
6869

@@ -189,15 +190,36 @@ var App = React.createClass({
189190
ReactDOM.render(<App/>, document.body);
190191
```
191192

192-
## <DraggableCore>
193+
## Controlled vs. Uncontrolled
193194

194-
For users that require more control, a `<DraggableCore>` element is available. This is useful for more programmatic
195-
usage of the element. See [React-Resizable](https://github.com/STRML/react-resizable) and
196-
[React-Grid-Layout](https://github.com/STRML/react-grid-layout) for some examples of this.
195+
`<Draggable>` is a 'batteries-included' component that manages its own state. If you want to completely
196+
control the lifecycle of the component, use `<DraggableCore>`.
197+
198+
For some users, they may want the nice state management that `<Draggable>` provides, but occasionally want
199+
to programmatically reposition their components. `<Draggable>` allows this customization via a system that
200+
is similar to how React handles form components.
201+
202+
If the prop `position: {x: number, y: number}` is defined, the `<Draggable>` will ignore its internal state and use
203+
the provided position instead. Altneratively, you can seed the position using `defaultPosition`. Technically, since
204+
`<Draggable>` works only on position deltas, you could also seed the initial position using CSS `top/left`.
205+
206+
We make one modification to the React philosophy here - we still allow dragging while a component is controlled.
207+
We then expect you to use at least an `onDrag` or `onStop` handler to synchronize state.
208+
209+
To disable dragging while controlled, send the prop `disabled={true}` - at this point the `<Draggable>` will operate
210+
like a completely static component.
211+
212+
## `<DraggableCore>`
213+
214+
For users that require absolute control, a `<DraggableCore>` element is available. This is useful as an abstraction
215+
over touch and mouse events, but with full control. `<DraggableCore>` has no internal state.
216+
217+
See [React-Resizable](https://github.com/STRML/react-resizable) and
218+
[React-Grid-Layout](https://github.com/STRML/react-grid-layout) for some usage examples.
197219

198220
`<DraggableCore>` is a useful building block for other libraries that simply want to abstract browser-specific
199221
quirks and receive callbacks when a user attempts to move an element. It does not set styles or transforms
200-
on itself.
222+
on itself and thus must have callbacks attached to be useful.
201223

202224
### DraggableCore API
203225

@@ -222,20 +244,7 @@ Note that there is no start position. `<DraggableCore>` simply calls `drag` hand
222244
indicating its position (as inferred from the underlying MouseEvent) and deltas. It is up to the parent
223245
to set actual positions on `<DraggableCore>`.
224246

225-
Drag callbacks (`onDragStart`, `onDrag`, `onDragEnd`) are called with the following parameters:
226-
227-
```js
228-
(
229-
event: Event,
230-
data: {
231-
node: HTMLElement,
232-
// lastX + deltaX === x
233-
x: number, y: number,
234-
deltaX: number, deltaY: number,
235-
lastX: number, lastY: number
236-
}
237-
)
238-
```
247+
Drag callbacks (`onStart`, `onDrag`, `onStop`) are called with the [same arguments as `<Draggable>`](#draggable-api).
239248

240249
----
241250

0 commit comments

Comments
 (0)