Skip to content

Commit dda4fd3

Browse files
committed
Move off of react-draggable fork.
Uses new `bounds` option in react-draggable. Also moved to React.cloneElement with cloneElement wrapper.
1 parent 11acb5d commit dda4fd3

File tree

6 files changed

+52
-48
lines changed

6 files changed

+52
-48
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ See the example and associated code in [TestLayout](/test/TestLayout.jsx) and
1212
Make sure you use the associated styles in [/css/styles.css](/css/styles.css), as without them, you will have
1313
problems with handle placement and visibility.
1414

15-
This module depends on a fork of [mzabriskie's react-draggable](https://github.com/mzabriskie/react-draggable),
16-
hosted [here](https://github.com/strml/react-draggable). You can pass options directly to the underlying `Draggable`
17-
instance by using the prop `draggableOpts`. See the demo for more on this.
15+
You can pass options directly to the underlying `Draggable` instance by using the prop `draggableOpts`.
16+
See the [demo](/test/TestLayout.jsx) for more on this.
1817

1918

2019
### Usage

css/styles.css

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
.react-resizable {
22
position: relative;
33
}
4-
.react-draggable-active {
5-
-webkit-user-select: none;
6-
-moz-user-select: none;
7-
-ms-user-select: none;
8-
-o-user-select: none;
9-
user-select: none;
10-
}
114
.react-resizable-handle.react-draggable {
125
position: absolute;
136
width: 20px;
147
height: 20px;
8+
bottom: 0;
9+
right: 0;
1510
background: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pg08IS0tIEdlbmVyYXRvcjogQWRvYmUgRmlyZXdvcmtzIENTNiwgRXhwb3J0IFNWRyBFeHRlbnNpb24gYnkgQWFyb24gQmVhbGwgKGh0dHA6Ly9maXJld29ya3MuYWJlYWxsLmNvbSkgLiBWZXJzaW9uOiAwLjYuMSAgLS0+DTwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DTxzdmcgaWQ9IlVudGl0bGVkLVBhZ2UlMjAxIiB2aWV3Qm94PSIwIDAgNiA2IiBzdHlsZT0iYmFja2dyb3VuZC1jb2xvcjojZmZmZmZmMDAiIHZlcnNpb249IjEuMSINCXhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbDpzcGFjZT0icHJlc2VydmUiDQl4PSIwcHgiIHk9IjBweCIgd2lkdGg9IjZweCIgaGVpZ2h0PSI2cHgiDT4NCTxnIG9wYWNpdHk9IjAuMzAyIj4NCQk8cGF0aCBkPSJNIDYgNiBMIDAgNiBMIDAgNC4yIEwgNCA0LjIgTCA0LjIgNC4yIEwgNC4yIDAgTCA2IDAgTCA2IDYgTCA2IDYgWiIgZmlsbD0iIzAwMDAwMCIvPg0JPC9nPg08L3N2Zz4=');
1611
background-position: bottom right;
1712
padding: 0 3px 3px 0;
1813
background-repeat: no-repeat;
1914
background-origin: content-box;
2015
box-sizing: border-box;
2116
cursor: se-resize;
17+
/* Since this handle is absolutely positioned, we don't want the
18+
draggable transforms to actually move it */
19+
transform: none !important;
20+
-webkit-transform: none !important;
21+
-ms-transform: none !important;
22+
-o-transform: none !important;
23+
-moz-transform: none !important;
2224
}

lib/Resizable.jsx

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var React = require('react');
33
var Draggable = require('react-draggable');
44
var PureRenderMixin = require('react/lib/ReactComponentWithPureRenderMixin');
55
var assign = require('object-assign');
6-
var cloneWithProps = require('react/lib/cloneWithProps');
6+
var cloneElement = require('./cloneElement');
77

88
var Resizable = module.exports = React.createClass({
99
displayName: 'Resizable',
@@ -31,12 +31,24 @@ var Resizable = module.exports = React.createClass({
3131
};
3232
},
3333

34-
minConstraints() {
35-
return parseConstraints(this.props.minConstraints, this.props.handleSize) || this.props.handleSize;
34+
getInitialState: function() {
35+
return {
36+
bounds: this.constraintsToBounds(),
37+
initialWidth: this.props.width,
38+
initialHeight: this.props.height
39+
};
3640
},
3741

38-
maxConstraints() {
39-
return parseConstraints(this.props.maxConstraints, this.props.handleSize);
42+
constraintsToBounds() {
43+
var p = this.props;
44+
var mins = p.minConstraints || p.handleSize;
45+
var maxes = p.maxConstraints || [Infinity, Infinity];
46+
return {
47+
left: mins[0] - p.width,
48+
top: mins[1] - p.height,
49+
right: maxes[0] - p.width,
50+
bottom: maxes[1] - p.height
51+
};
4052
},
4153

4254

@@ -48,8 +60,8 @@ var Resizable = module.exports = React.createClass({
4860
*/
4961
resizeHandler(handlerName) {
5062
var me = this;
51-
return function(e, {element, position}) {
52-
me.props[handlerName] && me.props[handlerName](e, {element, size: calcWH(position, me.props.handleSize)});
63+
return function(e, {node, position}) {
64+
me.props[handlerName] && me.props[handlerName](e, {node, size: calcWH(me.state, position)});
5365
};
5466
},
5567

@@ -60,18 +72,15 @@ var Resizable = module.exports = React.createClass({
6072
// We are then defining its children as:
6173
// Its original children (resizable's child's children), and
6274
// A draggable handle.
63-
return cloneWithProps(p.children, assign({}, p, {
75+
return cloneElement(p.children, assign({}, p, {
6476
children: [
6577
p.children.props.children,
6678
<Draggable
6779
{...p.draggableOpts}
68-
start={{x: p.width - 20, y: p.height - 20}}
69-
moveOnStartChange={true}
7080
onStop={this.resizeHandler('onResizeStop')}
7181
onStart={this.resizeHandler('onResizeStart')}
7282
onDrag={this.resizeHandler('onResize')}
73-
minConstraints={this.minConstraints()}
74-
maxConstraints={this.maxConstraints()}
83+
bounds={this.state.bounds}
7584
>
7685
<span className="react-resizable-handle" />
7786
</Draggable>
@@ -81,27 +90,11 @@ var Resizable = module.exports = React.createClass({
8190
});
8291

8392
/**
84-
* Parse left and top coordinates; we have to add the handle size to get the full picture.
93+
* Parse left and top coordinates.
8594
* @param {Number} options.left Left coordinate.
8695
* @param {Number} options.top Top coordinate.
87-
* @param {Array} handleSize Handle data.
8896
* @return {Object} Coordinates
8997
*/
90-
function calcWH({left, top}, handleSize) {
91-
return {width: left + handleSize[0], height: top + handleSize[1]};
92-
}
93-
94-
/**
95-
* Constraints must be subtracted by the size of the handle to work properly.
96-
* This has a side-effect of effectively limiting the minimum size to the handleSize,
97-
* which IMO is fine.
98-
* @param {Array} constraints Constraints array.
99-
* @param {Array} handleSize Handle size array.
100-
* @return {Array} Transformed constraints.
101-
*/
102-
function parseConstraints(constraints, handleSize) {
103-
if (!constraints) return;
104-
return constraints.map(function(c, i) {
105-
return c - handleSize[i];
106-
});
98+
function calcWH({initialWidth, initialHeight}, {left, top}) {
99+
return {width: initialWidth + left, height: initialHeight + top};
107100
}

lib/ResizableBox.jsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ var ResizableBox = module.exports = React.createClass({
1515
getDefaultProps() {
1616
return {
1717
lockAspectRatio: false,
18-
handleSize: [20,20],
19-
minConstraints: [20,20]
18+
handleSize: [20,20]
2019
};
2120
},
2221

@@ -37,10 +36,7 @@ var ResizableBox = module.exports = React.createClass({
3736
[width, height] = this.preserveAspectRatio(width, height);
3837
}
3938

40-
this.setState({
41-
width: width,
42-
height: height
43-
});
39+
this.setState({width, height});
4440
},
4541

4642
// If you do this, be careful of constraints

lib/cloneElement.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var assign = require('object-assign');
4+
var React = require('react');
5+
6+
module.exports = function cloneElement(element, props) {
7+
if (props.style && element.props.style) {
8+
props.style = assign({}, element.props.style, props.style);
9+
}
10+
if (props.className && element.props.className) {
11+
props.className = element.props.className + ' ' + props.className;
12+
}
13+
return React.cloneElement(element, props);
14+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"dependencies": {
4545
"object-assign": "^2.0.0",
46-
"react-draggable": "strml/react-draggable#v0.3.1"
46+
"react-draggable": "^0.7.0"
4747
},
4848
"publishConfig": {
4949
"registry": "https://registry.npmjs.org"

0 commit comments

Comments
 (0)