Skip to content

Commit 1385c6c

Browse files
committed
Use position/defaultPosition for controlled/uncontrolled behavior.
Misc. cleanup. See #140 for context.
1 parent 0959b27 commit 1385c6c

File tree

12 files changed

+370
-342
lines changed

12 files changed

+370
-342
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"quotes": [1, "single"],
99
"curly": [1, "multi-line"],
1010
"camelcase": 0,
11-
"comma-dangle": 1,
11+
"comma-dangle": 0,
12+
"no-console": 2,
1213
"no-use-before-define": [1, "nofunc"],
1314
"no-underscore-dangle": 0,
1415
"no-unused-vars": 1,
1516
"new-cap": 0,
17+
"prefer-const": 1,
1618
"semi": 1
1719
},
1820
env: {

.flowconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowBug.*
1313
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIgnore.*
1414
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowNewLine.*
1515
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIssue
16-
esproposal.class_instance_fields=ignore
17-
esproposal.class_static_fields=ignore
16+
esproposal.class_instance_fields=enable
17+
esproposal.class_static_fields=enable
1818
module.file_ext=.js
1919
module.file_ext=.jsx
2020
module.file_ext=.es5

CHANGELOG.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
# Changelog
22

3+
### 2.0.0-beta1 (Apr 14, 2016)
4+
5+
- Due to API changes, this is a major release.
6+
7+
#### Breaking Changes:
8+
9+
- Both `<DraggableCore>` and `<Draggable>` have had their callback types changed and unified.
10+
11+
```js
12+
type DraggableEventHandler = (e: Event, data: DraggableData) => void | false;
13+
type DraggableData = {
14+
node: HTMLElement,
15+
// lastX + deltaX === x
16+
x: number, y: number,
17+
deltaX: number, deltaY: number,
18+
lastX: number, lastY: number
19+
};
20+
```
21+
22+
- The `start` option has been renamed to `defaultPosition`.
23+
- The `zIndex` option has been removed.
24+
25+
#### Possibly Breaking Changes:
26+
27+
- When determining deltas, we now use a new method that checks the delta against the Draggable's `offsetParent`.
28+
This method allows us to support arbitrary nested scrollable ancestors without scroll handlers!
29+
- This may cause issues in certain layouts. If you find one, please open an issue.
30+
31+
#### Enhancements:
32+
33+
- `<Draggable>` now has a `position` attribute. Its relationship to `defaultPosition` is much like
34+
`value` to `defaultValue` on React `<input>` nodes. If set, the position is fixed and cannot be mutated.
35+
If empty, the component will manage its own state. See [#140](https://github.com/mzabriskie/react-draggable/pull/140)
36+
for more info & motivations.
37+
- Misc. bugfixes.
38+
339
### 1.4.0-beta1 (Apr 13, 2016)
440

541
- Major improvements to drag tracking that now support even nested scroll boxes.
@@ -11,7 +47,7 @@
1147

1248
### 1.3.6 (Apr 8, 2016)
1349

14-
- Republish after 1.3.5 contained a bundling error.
50+
- Republished after 1.3.5 contained a bundling error.
1551

1652
### 1.3.5 (Apr 8, 2016)
1753

README.md

Lines changed: 73 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -67,46 +67,15 @@ React.DOM elements support the above six properties by default, so you may use t
6767
Props:
6868

6969
```js
70+
type DraggableEventHandler = (e: Event, data: DraggableData) => void | false;
71+
type DraggableData = {
72+
node: HTMLElement,
73+
// lastX + deltaX === x
74+
x: number, y: number,
75+
deltaX: number, deltaY: number,
76+
lastX: number, lastY: number
77+
};
7078
{
71-
// Called when dragging starts. If `false` is returned from this method,
72-
// dragging will cancel.
73-
// These callbacks are called with the arity:
74-
// (event: Event,
75-
// {
76-
// position: {left: number, top: number},
77-
// deltaX: number,
78-
// deltaY: number
79-
// }
80-
// )
81-
onStart: Function,
82-
83-
// Called while dragging.
84-
onDrag: Function,
85-
86-
// Called when dragging stops.
87-
onStop: Function,
88-
89-
// Called whenever the user mouses down. Called regardless of handle or
90-
// disabled status.
91-
onMouseDown: Function,
92-
93-
// Specifies the `x` and `y` that the dragged item should start at.
94-
// This is generally not necessary to use (you can use absolute or relative
95-
// positioning of the child directly), but can be helpful for uniformity in
96-
// your callbacks and with css transforms.
97-
start: {x: number, y: number},
98-
99-
// If true, will not call any drag handlers.
100-
disabled: boolean,
101-
102-
// Specifies a selector to be used to prevent drag initialization.
103-
// Example: '.body'
104-
cancel: string,
105-
106-
// Specifies a selector to be used as the handle that initiates drag.
107-
// Example: '.handle'
108-
handle: string,
109-
11079
// If set to `true`, will allow dragging on non left-button clicks.
11180
allowAnyClick: boolean,
11281

@@ -127,11 +96,44 @@ axis: string,
12796
// can be moved.
12897
bounds: {left: number, top: number, right: number, bottom: number} | string,
12998

99+
// Specifies a selector to be used to prevent drag initialization.
100+
// Example: '.body'
101+
cancel: string,
102+
103+
// Specifies the `x` and `y` that the dragged item should start at.
104+
// This is generally not necessary to use (you can use absolute or relative
105+
// positioning of the child directly), but can be helpful for uniformity in
106+
// your callbacks and with css transforms.
107+
defaultPosition: {x: number, y: number},
108+
109+
// If true, will not call any drag handlers.
110+
disabled: boolean,
111+
130112
// Specifies the x and y that dragging should snap to.
131113
grid: [number, number],
132114

133-
// Specifies the zIndex to use while dragging.
134-
zIndex: number
115+
// Specifies a selector to be used as the handle that initiates drag.
116+
// Example: '.handle'
117+
handle: string,
118+
119+
// Called whenever the user mouses down. Called regardless of handle or
120+
// disabled status.
121+
onMouseDown: (e: MouseEvent) => boolean,
122+
123+
// Called when dragging starts. If `false` is returned any handler,
124+
// the action will cancel.
125+
onStart: DraggableEventHandler,
126+
127+
// Called while dragging.
128+
onDrag: DraggableEventHandler,
129+
130+
// Called when dragging stops.
131+
onStop: DraggableEventHandler,
132+
133+
// Much like React form elements, if this property is present, the item
134+
// becomes 'controlled' and is not responsive to user input. Use `position`
135+
// if you need to have direct control of the element.
136+
position: {x: number, y: number}
135137
}
136138
```
137139

@@ -168,7 +170,8 @@ var App = React.createClass({
168170
<Draggable
169171
axis="x"
170172
handle=".handle"
171-
start={{x: 0, y: 0}}
173+
defaultPosition={{x: 0, y: 0}}
174+
position={null}
172175
grid={[25, 25]}
173176
zIndex={100}
174177
onStart={this.handleStart}
@@ -198,28 +201,39 @@ on itself.
198201

199202
### DraggableCore API
200203

201-
`<DraggableCore>` takes all of the above `<Draggable>` options, with the exception of:
204+
`<DraggableCore>` takes a limited subset of options:
205+
206+
```js
207+
{
208+
allowAnyClick: boolean,
209+
cancel: string,
210+
disabled: boolean,
211+
enableUserSelectHack: boolean,
212+
grid: [number, number],
213+
handle: string,
214+
onStart: DraggableEventHandler,
215+
onDrag: DraggableEventHandler,
216+
onStop: DraggableEventHandler
217+
onMouseDown: (e: MouseEvent) => void
218+
}
219+
```
202220

203-
* `axis`
204-
* `bounds`
205-
* `start`
206-
* `zIndex`
221+
Note that there is no start position. `<DraggableCore>` simply calls `drag` handlers with the below parameters,
222+
indicating its position (as inferred from the underlying MouseEvent) and deltas. It is up to the parent
223+
to set actual positions on `<DraggableCore>`.
207224

208-
Drag callbacks are called with the following parameters:
225+
Drag callbacks (`onDragStart`, `onDrag`, `onDragEnd`) are called with the following parameters:
209226

210227
```js
211228
(
212-
event: Event,
213-
ui:{
214-
node: Node
215-
position:
216-
{
217-
// lastX + deltaX === clientX
218-
deltaX: number, deltaY: number,
219-
lastX: number, lastY: number,
220-
clientX: number, clientY: number
221-
}
222-
}
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+
}
223237
)
224238
```
225239

0 commit comments

Comments
 (0)