/
index.js
165 lines (145 loc) · 3.75 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
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
const supportResizeObserver = typeof ResizeObserver !== 'undefined';
const styles = {
container: {
position: 'relative',
width: '100%',
height: '100%'
},
wrapper: {
position: 'absolute',
width: '100%',
height: '100%',
zIndex: -1,
top: 0
},
resizeWrapper: {
overflow: 'hidden',
width: '100%',
height: '100%'
},
shrink: {
height: '200%',
width: '200%'
}
};
class Resize extends Component {
static propTypes = {
onExpand: PropTypes.func,
onShrink: PropTypes.func
}
constructor(props) {
super(props);
this.state = {
domWidth: 0,
domHeight: 0
};
}
componentDidMount() {
if (supportResizeObserver) {
this._resizeObserver = new ResizeObserver((entries) => {
entries.forEach((entry) => {
const { target } = entry;
// 修复某些case下target不在DOM中, 触发的width/height都为0
if (!target.parentElement) return;
this._onResize({ height: target.offsetHeight, width: target.offsetWidth });
});
});
const resizeNode = ReactDOM.findDOMNode(this);
this._resizeNode = resizeNode;
if (!resizeNode) return;
this._onResize({ height: this._resizeNode.offsetHeight, width: this._resizeNode.offsetWidth });
this._resizeObserver.observe(resizeNode);
} else {
this._resize();
}
}
componentDidUpdate() {
if (!supportResizeObserver) {
this.refExpand.scrollTop = this.refExpand.scrollHeight;
this.refExpand.scrollLeft = this.refExpand.scrollWidth;
this.refShrink.scrollTop = this.refShrink.scrollHeight;
this.refShrink.scrollLeft = this.refShrink.scrollWidth;
}
}
componentWillUnmount() {
if (this._resizeObserver) {
this._resizeObserver.disconnect();
this._resizeObserver = null;
}
}
_onResize(rect) {
if (this.props.onResize) {
this.props.onResize(rect);
}
}
_onExpand() {
const domRect = this._getRect();
this._resize(domRect);
if (this.props.onExpand) {
this.props.onExpand(domRect);
}
this._onResize(domRect);
}
_onShrink() {
const domRect = this._getRect();
this._resize(domRect);
if (this.props.onShrink) {
this.props.onShrink(domRect);
}
this._onResize(domRect);
}
_resize(rect) {
rect = rect || this._getRect();
const domWidth = rect.width;
const domHeight = rect.height;
this.setState({
domWidth: domWidth + 1,
domHeight: domHeight + 1
});
}
_getRect() {
const dom = this.refNode;
return dom.getBoundingClientRect();
}
renderResizeEl() {
const children = React.Children.only(this.props.children);
return children;
}
renderPolyfillEl() {
const { domWidth, domHeight } = this.state;
return (
<div style={styles.wrapper} ref={(node) => { this.refNode = node; }}>
<div style={styles.resizeWrapper}
ref={(node) => { this.refExpand = node; }}
onScroll={this._onExpand.bind(this)}>
<div style={{ width: domWidth, height: domHeight }}>
</div>
</div>
<div ref="shrink" style={styles.resizeWrapper}
ref={(node) => { this.refShrink = node; }}
onScroll={this._onShrink.bind(this)}>
<div style={styles.shrink}>
</div>
</div>
</div>
);
}
renderResizeContainer() {
return (
<div style={styles.container}>
{ this.props.children }
{ this.renderPolyfillEl() }
</div>
);
}
render() {
if (supportResizeObserver) {
return this.renderResizeEl();
}
return this.renderResizeContainer();
}
}
export default Resize;