index.js
5.1 KB
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
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import * as React from 'react';
import findDOMNode from "rc-util/es/Dom/findDOMNode";
import toArray from "rc-util/es/Children/toArray";
import warning from "rc-util/es/warning";
import { composeRef, supportRef } from "rc-util/es/ref";
import ResizeObserver from 'resize-observer-polyfill';
var INTERNAL_PREFIX_KEY = 'rc-observer-key'; // Still need to be compatible with React 15, we use class component here
var ReactResizeObserver = /*#__PURE__*/function (_React$Component) {
_inherits(ReactResizeObserver, _React$Component);
var _super = _createSuper(ReactResizeObserver);
function ReactResizeObserver() {
var _this;
_classCallCheck(this, ReactResizeObserver);
_this = _super.apply(this, arguments);
_this.resizeObserver = null;
_this.childNode = null;
_this.currentElement = null;
_this.state = {
width: 0,
height: 0,
offsetHeight: 0,
offsetWidth: 0
};
_this.onResize = function (entries) {
var onResize = _this.props.onResize;
var target = entries[0].target;
var _target$getBoundingCl = target.getBoundingClientRect(),
width = _target$getBoundingCl.width,
height = _target$getBoundingCl.height;
var offsetWidth = target.offsetWidth,
offsetHeight = target.offsetHeight;
/**
* Resize observer trigger when content size changed.
* In most case we just care about element size,
* let's use `boundary` instead of `contentRect` here to avoid shaking.
*/
var fixedWidth = Math.floor(width);
var fixedHeight = Math.floor(height);
if (_this.state.width !== fixedWidth || _this.state.height !== fixedHeight || _this.state.offsetWidth !== offsetWidth || _this.state.offsetHeight !== offsetHeight) {
var size = {
width: fixedWidth,
height: fixedHeight,
offsetWidth: offsetWidth,
offsetHeight: offsetHeight
};
_this.setState(size);
if (onResize) {
// defer the callback but not defer to next frame
Promise.resolve().then(function () {
onResize(_objectSpread(_objectSpread({}, size), {}, {
offsetWidth: offsetWidth,
offsetHeight: offsetHeight
}));
});
}
}
};
_this.setChildNode = function (node) {
_this.childNode = node;
};
return _this;
}
_createClass(ReactResizeObserver, [{
key: "componentDidMount",
value: function componentDidMount() {
this.onComponentUpdated();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.onComponentUpdated();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.destroyObserver();
}
}, {
key: "onComponentUpdated",
value: function onComponentUpdated() {
var disabled = this.props.disabled; // Unregister if disabled
if (disabled) {
this.destroyObserver();
return;
} // Unregister if element changed
var element = findDOMNode(this.childNode || this);
var elementChanged = element !== this.currentElement;
if (elementChanged) {
this.destroyObserver();
this.currentElement = element;
}
if (!this.resizeObserver && element) {
this.resizeObserver = new ResizeObserver(this.onResize);
this.resizeObserver.observe(element);
}
}
}, {
key: "destroyObserver",
value: function destroyObserver() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
this.resizeObserver = null;
}
}
}, {
key: "render",
value: function render() {
var children = this.props.children;
var childNodes = toArray(children);
if (childNodes.length > 1) {
warning(false, 'Find more than one child node with `children` in ResizeObserver. Will only observe first one.');
} else if (childNodes.length === 0) {
warning(false, '`children` of ResizeObserver is empty. Nothing is in observe.');
return null;
}
var childNode = childNodes[0];
if (React.isValidElement(childNode) && supportRef(childNode)) {
var ref = childNode.ref;
childNodes[0] = React.cloneElement(childNode, {
ref: composeRef(ref, this.setChildNode)
});
}
return childNodes.length === 1 ? childNodes[0] : childNodes.map(function (node, index) {
if (!React.isValidElement(node) || 'key' in node && node.key !== null) {
return node;
}
return React.cloneElement(node, {
key: "".concat(INTERNAL_PREFIX_KEY, "-").concat(index)
});
});
}
}]);
return ReactResizeObserver;
}(React.Component);
ReactResizeObserver.displayName = 'ResizeObserver';
export default ReactResizeObserver;