-
Notifications
You must be signed in to change notification settings - Fork 91
/
index.js
246 lines (191 loc) · 8.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.withResizeDetector = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _resizeObserverPolyfill = require('resize-observer-polyfill');
var _resizeObserverPolyfill2 = _interopRequireDefault(_resizeObserverPolyfill);
var _lodash = require('lodash.debounce');
var _lodash2 = _interopRequireDefault(_lodash);
var _lodash3 = require('lodash.throttle');
var _lodash4 = _interopRequireDefault(_lodash3);
var _lodash5 = require('lodash.isfunction');
var _lodash6 = _interopRequireDefault(_lodash5);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var listMode = { debounce: _lodash2.default, throttle: _lodash4.default };
var styles = {
position: 'absolute',
width: 0,
height: 0,
visibility: 'hidden',
display: 'none'
};
/**
* detect component's children and convert them to array
* @param {*} children - component's children
*/
function convertChildrenToArray(children) {
if (!children) return [];
if (!Array.isArray(children)) return [children];
return children;
}
var ResizeDetector = function (_PureComponent) {
_inherits(ResizeDetector, _PureComponent);
function ResizeDetector(props) {
_classCallCheck(this, ResizeDetector);
var _this = _possibleConstructorReturn(this, (ResizeDetector.__proto__ || Object.getPrototypeOf(ResizeDetector)).call(this, props));
_this.getElement = function () {
var resizableElementId = _this.props.resizableElementId;
var otherElement = resizableElementId && document.getElementById(resizableElementId);
var parentElement = _this.el && _this.el.parentElement;
var resizableElement = otherElement || parentElement;
return resizableElement;
};
_this.createResizeHandler = function (entries) {
var _this$props = _this.props,
handleWidth = _this$props.handleWidth,
handleHeight = _this$props.handleHeight,
onResize = _this$props.onResize;
entries.forEach(function (entry) {
var _entry$contentRect = entry.contentRect,
width = _entry$contentRect.width,
height = _entry$contentRect.height;
var notifyWidth = handleWidth && _this.state.width !== width;
var notifyHeight = handleHeight && _this.state.height !== height;
if (!_this.skipOnMount && (notifyWidth || notifyHeight) && typeof window !== 'undefined') {
_this.animationFrameID = window.requestAnimationFrame(function () {
onResize(width, height);
_this.setState({ width: width, height: height });
});
}
_this.skipOnMount = false;
});
};
_this.handleRenderProp = function () {
var _this$state = _this.state,
width = _this$state.width,
height = _this$state.height;
var render = _this.props.render;
if (render && typeof render === 'function') {
return (0, _react.cloneElement)(render({ width: width, height: height }), { key: 'render' });
}
return undefined;
};
_this.renderChildren = function () {
var _this$state2 = _this.state,
width = _this$state2.width,
height = _this$state2.height;
var children = _this.props.children;
return convertChildrenToArray(children).filter(function (child) {
return !!child;
}).map(function (child, key) {
if ((0, _lodash6.default)(child)) return (0, _react.cloneElement)(child(width, height), { key: key });
if ((0, _react.isValidElement)(child)) return (0, _react.cloneElement)(child, { width: width, height: height, key: key });
return child;
});
};
var skipOnMount = props.skipOnMount,
refreshMode = props.refreshMode,
refreshRate = props.refreshRate;
_this.state = {
width: undefined,
height: undefined
};
_this.skipOnMount = skipOnMount;
_this.animationFrameID = null;
_this.resizeHandler = listMode[refreshMode] && listMode[refreshMode](_this.createResizeHandler, refreshRate) || _this.createResizeHandler;
_this.ro = new _resizeObserverPolyfill2.default(_this.resizeHandler);
return _this;
}
_createClass(ResizeDetector, [{
key: 'componentDidMount',
value: function componentDidMount() {
var resizableElement = this.getElement();
if (resizableElement) this.ro.observe(resizableElement);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var resizableElement = this.getElement();
if (resizableElement) this.ro.unobserve(resizableElement);
if (typeof window !== 'undefined' && this.animationFrameID) {
window.cancelAnimationFrame(this.animationFrameID);
}
if (this.resizeHandler && this.resizeHandler.cancel) {
// cancel debounced handler
this.resizeHandler.cancel();
}
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
return [_react2.default.createElement('div', {
key: 'resize-detector',
style: styles,
ref: function ref(el) {
_this2.el = el;
}
}), this.handleRenderProp()].concat(_toConsumableArray(this.renderChildren()));
}
}]);
return ResizeDetector;
}(_react.PureComponent);
ResizeDetector.propTypes = {
handleWidth: _propTypes2.default.bool,
handleHeight: _propTypes2.default.bool,
skipOnMount: _propTypes2.default.bool,
refreshRate: _propTypes2.default.number,
refreshMode: _propTypes2.default.string,
resizableElementId: _propTypes2.default.string,
onResize: _propTypes2.default.func,
render: _propTypes2.default.func,
children: _propTypes2.default.any // eslint-disable-line react/forbid-prop-types
};
ResizeDetector.defaultProps = {
handleWidth: false,
handleHeight: false,
skipOnMount: false,
refreshRate: 1000,
refreshMode: undefined,
resizableElementId: '',
onResize: function onResize(e) {
return e;
},
render: undefined,
children: null
};
var withResizeDetector = exports.withResizeDetector = function withResizeDetector(WrappedComponent) {
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { handleWidth: true, handleHeight: true };
return (
// eslint-disable-next-line
function (_Component) {
_inherits(ResizeDetectorHOC, _Component);
function ResizeDetectorHOC() {
_classCallCheck(this, ResizeDetectorHOC);
return _possibleConstructorReturn(this, (ResizeDetectorHOC.__proto__ || Object.getPrototypeOf(ResizeDetectorHOC)).apply(this, arguments));
}
_createClass(ResizeDetectorHOC, [{
key: 'render',
value: function render() {
return _react2.default.createElement(
ResizeDetector,
props,
_react2.default.createElement(WrappedComponent, this.props)
);
}
}]);
return ResizeDetectorHOC;
}(_react.Component)
);
};
exports.default = ResizeDetector;