react-resizable 1.10.1 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.eslintrc +10 -6
  2. package/.flowconfig +8 -8
  3. package/CHANGELOG.md +48 -37
  4. package/README.md +13 -3
  5. package/__tests__/Resizable.test.js +245 -0
  6. package/__tests__/ResizableBox.test.js +99 -0
  7. package/__tests__/__snapshots__/Resizable.test.js.snap +29 -0
  8. package/__tests__/__snapshots__/ResizableBox.test.js.snap +23 -0
  9. package/build/Resizable.js +100 -134
  10. package/build/Resizable.js.flow +87 -161
  11. package/build/ResizableBox.js +32 -29
  12. package/build/ResizableBox.js.flow +44 -28
  13. package/build/propTypes.js +112 -0
  14. package/build/propTypes.js.flow +135 -0
  15. package/build/utils.js +3 -3
  16. package/coverage/clover.xml +107 -0
  17. package/coverage/coverage-final.json +5 -0
  18. package/coverage/lcov-report/Resizable.js.html +665 -0
  19. package/coverage/lcov-report/ResizableBox.js.html +374 -0
  20. package/coverage/lcov-report/base.css +224 -0
  21. package/coverage/lcov-report/block-navigation.js +79 -0
  22. package/coverage/lcov-report/favicon.png +0 -0
  23. package/coverage/lcov-report/flow-typed/npm/index.html +111 -0
  24. package/coverage/lcov-report/flow-typed/npm/jest_v26.x.x.js.html +3734 -0
  25. package/coverage/lcov-report/index.html +156 -0
  26. package/coverage/lcov-report/prettify.css +1 -0
  27. package/coverage/lcov-report/prettify.js +2 -0
  28. package/coverage/lcov-report/propTypes.js.html +485 -0
  29. package/coverage/lcov-report/react-resizable/dist/bundle.js.html +95 -0
  30. package/coverage/lcov-report/react-resizable/dist/index.html +111 -0
  31. package/coverage/lcov-report/react-resizable/flow-typed/npm/index.html +111 -0
  32. package/coverage/lcov-report/react-resizable/flow-typed/npm/jest_v26.x.x.js.html +3734 -0
  33. package/coverage/lcov-report/react-resizable/index.html +111 -0
  34. package/coverage/lcov-report/react-resizable/index.js.html +101 -0
  35. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  36. package/coverage/lcov-report/sorter.js +170 -0
  37. package/coverage/lcov-report/utils.js.html +122 -0
  38. package/coverage/lcov.info +233 -0
  39. package/dist/bundle.js +6 -0
  40. package/flow-typed/npm/jest_v26.x.x.js +1218 -0
  41. package/package.json +26 -20
  42. package/setupTests/enzyme.js +4 -0
  43. package/index.html +0 -15
@@ -5,19 +5,19 @@ exports.default = void 0;
5
5
 
6
6
  var _react = _interopRequireDefault(require("react"));
7
7
 
8
- var _propTypes = _interopRequireDefault(require("prop-types"));
9
-
10
8
  var _reactDraggable = require("react-draggable");
11
9
 
12
10
  var _utils = require("./utils");
13
11
 
12
+ var _propTypes = require("./propTypes");
13
+
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
 
16
16
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
17
17
 
18
18
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
19
19
 
20
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
20
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
21
21
 
22
22
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
23
23
 
@@ -27,9 +27,7 @@ function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.crea
27
27
 
28
28
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
29
29
 
30
- var Resizable =
31
- /*#__PURE__*/
32
- function (_React$Component) {
30
+ var Resizable = /*#__PURE__*/function (_React$Component) {
33
31
  _inheritsLoose(Resizable, _React$Component);
34
32
 
35
33
  function Resizable() {
@@ -41,31 +39,42 @@ function (_React$Component) {
41
39
 
42
40
  _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
43
41
 
44
- _defineProperty(_assertThisInitialized(_this), "state", {
45
- slackW: 0,
46
- slackH: 0
47
- });
42
+ _defineProperty(_assertThisInitialized(_this), "state", undefined);
43
+
44
+ _defineProperty(_assertThisInitialized(_this), "lastHandleRect", null);
45
+
46
+ _defineProperty(_assertThisInitialized(_this), "slack", null);
48
47
 
49
48
  return _this;
50
49
  }
51
50
 
52
51
  var _proto = Resizable.prototype;
53
52
 
53
+ _proto.componentWillUnmount = function componentWillUnmount() {
54
+ this.resetData();
55
+ };
56
+
54
57
  _proto.lockAspectRatio = function lockAspectRatio(width, height, aspectRatio) {
55
58
  height = width / aspectRatio;
56
59
  width = height * aspectRatio;
57
60
  return [width, height];
58
- } // If you do this, be careful of constraints
61
+ };
62
+
63
+ _proto.resetData = function resetData() {
64
+ this.lastHandleRect = this.slack = null;
65
+ } // Clamp width and height within provided constraints
59
66
  ;
60
67
 
61
68
  _proto.runConstraints = function runConstraints(width, height) {
62
69
  var _ref = [this.props.minConstraints, this.props.maxConstraints],
63
70
  min = _ref[0],
64
71
  max = _ref[1];
65
- if (!min && !max) return [width, height]; // Fit width & height to aspect ratio
72
+ if (!min && !max) return [width, height]; // If constraining to min and max, we need to also fit width and height to aspect ratio.
66
73
 
67
74
  if (this.props.lockAspectRatio) {
68
- if (height === this.props.height) {
75
+ var resizingHorizontally = height === this.props.height;
76
+
77
+ if (resizingHorizontally) {
69
78
  var ratio = this.props.width / this.props.height;
70
79
  height = width / ratio;
71
80
  width = height * ratio;
@@ -84,9 +93,10 @@ function (_React$Component) {
84
93
  // we start removing slack, the element won't react to it right away until it's been
85
94
  // completely removed.
86
95
 
87
- var _this$state = this.state,
88
- slackW = _this$state.slackW,
89
- slackH = _this$state.slackH;
96
+ var _ref2 = this.slack || [0, 0],
97
+ slackW = _ref2[0],
98
+ slackH = _ref2[1];
99
+
90
100
  width += slackW;
91
101
  height += slackH;
92
102
 
@@ -98,19 +108,10 @@ function (_React$Component) {
98
108
  if (max) {
99
109
  width = Math.min(max[0], width);
100
110
  height = Math.min(max[1], height);
101
- } // If the numbers changed, we must have introduced some slack. Record it for the next iteration.
111
+ } // If the width or height changed, we must have introduced some slack. Record it for the next iteration.
102
112
 
103
113
 
104
- slackW += oldW - width;
105
- slackH += oldH - height;
106
-
107
- if (slackW !== this.state.slackW || slackH !== this.state.slackH) {
108
- this.setState({
109
- slackW: slackW,
110
- slackH: slackH
111
- });
112
- }
113
-
114
+ this.slack = [slackW + (oldW - width), slackH + (oldH - height)];
114
115
  return [width, height];
115
116
  }
116
117
  /**
@@ -124,94 +125,105 @@ function (_React$Component) {
124
125
  _proto.resizeHandler = function resizeHandler(handlerName, axis) {
125
126
  var _this2 = this;
126
127
 
127
- return function (e, _ref2) {
128
- var node = _ref2.node,
129
- deltaX = _ref2.deltaX,
130
- deltaY = _ref2.deltaY;
131
- deltaX /= _this2.props.transformScale;
132
- deltaY /= _this2.props.transformScale; // Axis restrictions
128
+ return function (e, _ref3) {
129
+ var node = _ref3.node,
130
+ deltaX = _ref3.deltaX,
131
+ deltaY = _ref3.deltaY;
132
+ // Reset data in case it was left over somehow (should not be possible)
133
+ if (handlerName === 'onResizeStart') _this2.resetData(); // Axis restrictions
133
134
 
134
- var canDragX = (_this2.props.axis === 'both' || _this2.props.axis === 'x') && ['n', 's'].indexOf(axis) === -1;
135
- var canDragY = (_this2.props.axis === 'both' || _this2.props.axis === 'y') && ['e', 'w'].indexOf(axis) === -1; // reverse delta if using top or left drag handles
135
+ var canDragX = (_this2.props.axis === 'both' || _this2.props.axis === 'x') && axis !== 'n' && axis !== 's';
136
+ var canDragY = (_this2.props.axis === 'both' || _this2.props.axis === 'y') && axis !== 'e' && axis !== 'w'; // No dragging possible.
136
137
 
137
- if (canDragX && axis[axis.length - 1] === 'w') {
138
- deltaX = -deltaX;
139
- }
138
+ if (!canDragX && !canDragY) return; // Decompose axis for later use
139
+
140
+ var axisV = axis[0];
141
+ var axisH = axis[axis.length - 1]; // intentionally not axis[1], so that this catches axis === 'w' for example
142
+ // Track the element being dragged to account for changes in position.
143
+ // If a handle's position is changed between callbacks, we need to factor this in to the next callback.
144
+ // Failure to do so will cause the element to "skip" when resized upwards or leftwards.
145
+
146
+ var handleRect = node.getBoundingClientRect();
147
+
148
+ if (_this2.lastHandleRect != null) {
149
+ // If the handle has repositioned on either axis since last render,
150
+ // we need to increase our callback values by this much.
151
+ // Only checking 'n', 'w' since resizing by 's', 'w' won't affect the overall position on page,
152
+ if (axisH === 'w') {
153
+ var deltaLeftSinceLast = handleRect.left - _this2.lastHandleRect.left;
154
+ deltaX += deltaLeftSinceLast;
155
+ }
140
156
 
141
- if (canDragY && axis[0] === 'n') {
142
- deltaY = -deltaY;
143
- } // Update w/h
157
+ if (axisV === 'n') {
158
+ var deltaTopSinceLast = handleRect.top - _this2.lastHandleRect.top;
159
+ deltaY += deltaTopSinceLast;
160
+ }
161
+ } // Storage of last rect so we know how much it has really moved.
144
162
 
145
163
 
146
- var width = _this2.props.width + (canDragX ? deltaX : 0);
147
- var height = _this2.props.height + (canDragY ? deltaY : 0); // Early return if no change
164
+ _this2.lastHandleRect = handleRect; // Reverse delta if using top or left drag handles.
148
165
 
149
- var widthChanged = width !== _this2.props.width,
150
- heightChanged = height !== _this2.props.height;
151
- if (handlerName === 'onResize' && !widthChanged && !heightChanged) return;
166
+ if (axisH === 'w') deltaX = -deltaX;
167
+ if (axisV === 'n') deltaY = -deltaY; // Update w/h by the deltas. Also factor in transformScale.
168
+
169
+ var width = _this2.props.width + (canDragX ? deltaX / _this2.props.transformScale : 0);
170
+ var height = _this2.props.height + (canDragY ? deltaY / _this2.props.transformScale : 0); // Run user-provided constraints.
152
171
 
153
172
  var _this2$runConstraints = _this2.runConstraints(width, height);
154
173
 
155
174
  width = _this2$runConstraints[0];
156
175
  height = _this2$runConstraints[1];
157
- // Set the appropriate state for this handler.
158
- var newState = {};
176
+ var dimensionsChanged = width !== _this2.props.width || height !== _this2.props.height; // Call user-supplied callback if present.
159
177
 
160
- if (handlerName === 'onResizeStart') {// nothing
161
- } else if (handlerName === 'onResizeStop') {
162
- newState.slackW = newState.slackH = 0;
163
- } else {
164
- // Early return if no change after constraints
165
- if (width === _this2.props.width && height === _this2.props.height) return;
166
- }
178
+ var cb = typeof _this2.props[handlerName] === 'function' ? _this2.props[handlerName] : null; // Don't call 'onResize' if dimensions haven't changed.
167
179
 
168
- var hasCb = typeof _this2.props[handlerName] === 'function';
180
+ var shouldSkipCb = handlerName === 'onResize' && !dimensionsChanged;
169
181
 
170
- if (hasCb) {
171
- // $FlowIgnore isn't refining this correctly to SyntheticEvent
182
+ if (cb && !shouldSkipCb) {
172
183
  if (typeof e.persist === 'function') e.persist();
173
-
174
- _this2.setState(newState, function () {
175
- return _this2.props[handlerName](e, {
176
- node: node,
177
- size: {
178
- width: width,
179
- height: height
180
- },
181
- handle: axis
182
- });
184
+ cb(e, {
185
+ node: node,
186
+ size: {
187
+ width: width,
188
+ height: height
189
+ },
190
+ handle: axis
183
191
  });
184
- } else {
185
- _this2.setState(newState);
186
- }
192
+ } // Reset internal data
193
+
194
+
195
+ if (handlerName === 'onResizeStop') _this2.resetData();
187
196
  };
188
197
  };
189
198
 
190
- _proto.renderResizeHandle = function renderResizeHandle(resizeHandle) {
199
+ _proto.renderResizeHandle = function renderResizeHandle(resizeHandleAxis) {
191
200
  var handle = this.props.handle;
192
201
 
193
202
  if (handle) {
194
203
  if (typeof handle === 'function') {
195
- return handle(resizeHandle);
204
+ return handle(resizeHandleAxis);
196
205
  }
197
206
 
198
207
  return handle;
199
208
  }
200
209
 
201
- return _react.default.createElement("span", {
202
- className: "react-resizable-handle react-resizable-handle-" + resizeHandle
210
+ return /*#__PURE__*/_react.default.createElement("span", {
211
+ className: "react-resizable-handle react-resizable-handle-" + resizeHandleAxis
203
212
  });
204
213
  };
205
214
 
206
215
  _proto.render = function render() {
207
216
  var _this3 = this;
208
217
 
218
+ // Pass along only props not meant for the `<Resizable>`.`
209
219
  // eslint-disable-next-line no-unused-vars
210
220
  var _this$props = this.props,
211
221
  children = _this$props.children,
222
+ className = _this$props.className,
212
223
  draggableOpts = _this$props.draggableOpts,
213
224
  width = _this$props.width,
214
225
  height = _this$props.height,
226
+ handle = _this$props.handle,
215
227
  handleSize = _this$props.handleSize,
216
228
  lockAspectRatio = _this$props.lockAspectRatio,
217
229
  axis = _this$props.axis,
@@ -222,23 +234,22 @@ function (_React$Component) {
222
234
  onResizeStart = _this$props.onResizeStart,
223
235
  resizeHandles = _this$props.resizeHandles,
224
236
  transformScale = _this$props.transformScale,
225
- p = _objectWithoutPropertiesLoose(_this$props, ["children", "draggableOpts", "width", "height", "handleSize", "lockAspectRatio", "axis", "minConstraints", "maxConstraints", "onResize", "onResizeStop", "onResizeStart", "resizeHandles", "transformScale"]);
226
-
227
- var className = p.className ? p.className + " react-resizable" : 'react-resizable'; // What we're doing here is getting the child of this element, and cloning it with this element's props.
237
+ p = _objectWithoutPropertiesLoose(_this$props, ["children", "className", "draggableOpts", "width", "height", "handle", "handleSize", "lockAspectRatio", "axis", "minConstraints", "maxConstraints", "onResize", "onResizeStop", "onResizeStart", "resizeHandles", "transformScale"]); // What we're doing here is getting the child of this element, and cloning it with this element's props.
228
238
  // We are then defining its children as:
229
239
  // Its original children (resizable's child's children), and
230
240
  // One or more draggable handles.
231
241
 
232
- return (0, _utils.cloneElement)(children, _objectSpread({}, p, {
233
- className: className,
234
- children: [children.props.children, resizeHandles.map(function (h) {
235
- return _react.default.createElement(_reactDraggable.DraggableCore, _extends({}, draggableOpts, {
236
- key: "resizableHandle-" + h,
237
- onStop: _this3.resizeHandler('onResizeStop', h),
238
- onStart: _this3.resizeHandler('onResizeStart', h),
239
- onDrag: _this3.resizeHandler('onResize', h)
240
- }), _this3.renderResizeHandle(h));
241
- })]
242
+
243
+ return (0, _utils.cloneElement)(children, _objectSpread(_objectSpread({}, p), {}, {
244
+ className: (className ? className + " " : '') + "react-resizable",
245
+ children: [].concat(children.props.children, resizeHandles.map(function (handleAxis) {
246
+ return /*#__PURE__*/_react.default.createElement(_reactDraggable.DraggableCore, _extends({}, draggableOpts, {
247
+ key: "resizableHandle-" + handleAxis,
248
+ onStop: _this3.resizeHandler('onResizeStop', handleAxis),
249
+ onStart: _this3.resizeHandler('onResizeStart', handleAxis),
250
+ onDrag: _this3.resizeHandler('onResize', handleAxis)
251
+ }), _this3.renderResizeHandle(handleAxis));
252
+ }))
242
253
  }));
243
254
  };
244
255
 
@@ -247,52 +258,7 @@ function (_React$Component) {
247
258
 
248
259
  exports.default = Resizable;
249
260
 
250
- _defineProperty(Resizable, "propTypes", {
251
- //
252
- // Required Props
253
- //
254
- // Require that one and only one child be present.
255
- children: _propTypes.default.element.isRequired,
256
- // Initial w/h
257
- width: _propTypes.default.number.isRequired,
258
- height: _propTypes.default.number.isRequired,
259
- //
260
- // Optional props
261
- //
262
- // Custom resize handle
263
- handle: _propTypes.default.element,
264
- // If you change this, be sure to update your css
265
- handleSize: _propTypes.default.array,
266
- // Defines which resize handles should be rendered (default: 'se')
267
- // Allows for any combination of:
268
- // 's' - South handle (bottom-center)
269
- // 'w' - West handle (left-center)
270
- // 'e' - East handle (right-center)
271
- // 'n' - North handle (top-center)
272
- // 'sw' - Southwest handle (bottom-left)
273
- // 'nw' - Northwest handle (top-left)
274
- // 'se' - Southeast handle (bottom-right)
275
- // 'ne' - Northeast handle (top-center)
276
- resizeHandles: _propTypes.default.arrayOf(_propTypes.default.oneOf(['s', 'w', 'e', 'n', 'sw', 'nw', 'se', 'ne'])),
277
- transformScale: _propTypes.default.number,
278
- // If true, will only allow width/height to move in lockstep
279
- lockAspectRatio: _propTypes.default.bool,
280
- // Restricts resizing to a particular axis (default: 'both')
281
- // 'both' - allows resizing by width or height
282
- // 'x' - only allows the width to be changed
283
- // 'y' - only allows the height to be changed
284
- // 'none' - disables resizing altogether
285
- axis: _propTypes.default.oneOf(['both', 'x', 'y', 'none']),
286
- // Min/max size
287
- minConstraints: _propTypes.default.arrayOf(_propTypes.default.number),
288
- maxConstraints: _propTypes.default.arrayOf(_propTypes.default.number),
289
- // Callbacks
290
- onResizeStop: _propTypes.default.func,
291
- onResizeStart: _propTypes.default.func,
292
- onResize: _propTypes.default.func,
293
- // These will be passed wholesale to react-draggable's DraggableCore
294
- draggableOpts: _propTypes.default.object
295
- });
261
+ _defineProperty(Resizable, "propTypes", _propTypes.resizableProps);
296
262
 
297
263
  _defineProperty(Resizable, "defaultProps", {
298
264
  handleSize: [20, 20],