generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.tsx
159 lines (125 loc) Β· 3.86 KB
/
index.tsx
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
import * as React from 'react';
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
import toArray from 'rc-util/lib/Children/toArray';
import warning from 'rc-util/lib/warning';
import { composeRef, supportRef } from 'rc-util/lib/ref';
import ResizeObserver from 'resize-observer-polyfill';
const INTERNAL_PREFIX_KEY = 'rc-observer-key';
interface ResizeObserverProps {
children: React.ReactNode;
disabled?: boolean;
/** Trigger if element resized. Will always trigger when first time render. */
onResize?: (size: { width: number; height: number }) => void;
}
interface ResizeObserverState {
height: number;
width: number;
}
type RefNode = React.ReactInstance | HTMLElement | null;
// Still need to be compatible with React 15, we use class component here
class ReactResizeObserver extends React.Component<
ResizeObserverProps,
ResizeObserverState
> {
static displayName = 'ResizeObserver';
resizeObserver: ResizeObserver | null = null;
childNode: RefNode = null;
currentElement: Element | null = null;
state = {
width: 0,
height: 0,
};
componentDidMount() {
this.onComponentUpdated();
}
componentDidUpdate() {
this.onComponentUpdated();
}
componentWillUnmount() {
this.destroyObserver();
}
onComponentUpdated() {
const { disabled } = this.props;
// Unregister if disabled
if (disabled) {
this.destroyObserver();
return;
}
// Unregister if element changed
const element = findDOMNode(this.childNode || this) as Element;
const 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);
}
}
onResize: ResizeObserverCallback = (entries: ResizeObserverEntry[]) => {
const { onResize } = this.props;
const { target } = entries[0];
const { width, height } = target.getBoundingClientRect();
/**
* 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.
*/
const fixedWidth = Math.floor(width);
const fixedHeight = Math.floor(height);
if (this.state.width !== fixedWidth || this.state.height !== fixedHeight) {
const size = { width: fixedWidth, height: fixedHeight };
this.setState(size);
if (onResize) {
onResize(size);
}
}
};
setChildNode = (node: RefNode) => {
this.childNode = node;
};
destroyObserver() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
this.resizeObserver = null;
}
}
render() {
const { children } = this.props;
const 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;
}
const childNode = childNodes[0];
if (React.isValidElement(childNode) && supportRef(childNode)) {
const { ref } = childNode as any;
childNodes[0] = React.cloneElement(childNode as any, {
ref: composeRef(ref, this.setChildNode),
});
}
return childNodes.length === 1
? childNodes[0]
: childNodes.map((node, index) => {
if (
!React.isValidElement(node) ||
('key' in node && node.key !== null)
) {
return node;
}
return React.cloneElement(node, {
key: `${INTERNAL_PREFIX_KEY}-${index}`,
});
});
}
}
export default ReactResizeObserver;