| 155 | const SizeMeRenderWrapper = renderWrapper(WrappedComponent) |
| 156 | |
| 157 | class SizeAwareComponent extends React.Component { |
| 158 | static displayName = `SizeMe(${getDisplayName(WrappedComponent)})` |
| 159 | |
| 160 | domEl = null |
| 161 | |
| 162 | state = { |
| 163 | width: undefined, |
| 164 | height: undefined, |
| 165 | } |
| 166 | |
| 167 | componentDidMount() { |
| 168 | this.detector = resizeDetector(resizeDetectorStrategy) |
| 169 | this.determineStrategy(this.props) |
| 170 | this.handleDOMNode() |
| 171 | } |
| 172 | |
| 173 | componentDidUpdate() { |
| 174 | this.determineStrategy(this.props) |
| 175 | this.handleDOMNode() |
| 176 | } |
| 177 | |
| 178 | componentWillUnmount() { |
| 179 | // Change our size checker to a noop just in case we have some |
| 180 | // late running events. |
| 181 | this.hasSizeChanged = () => undefined |
| 182 | this.checkIfSizeChanged = () => undefined |
| 183 | this.uninstall() |
| 184 | } |
| 185 | |
| 186 | uninstall = () => { |
| 187 | if (this.domEl) { |
| 188 | try { |
| 189 | this.detector.uninstall(this.domEl) |
| 190 | } catch (err) { |
| 191 | // eslint-disable-next-line no-console |
| 192 | console.warn(errMsg) |
| 193 | } |
| 194 | this.domEl = null |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | determineStrategy = (props) => { |
| 199 | if (props.onSize) { |
| 200 | if (!this.callbackState) { |
| 201 | this.callbackState = { |
| 202 | ...this.state, |
| 203 | } |
| 204 | } |
| 205 | this.strategy = 'callback' |
| 206 | } else { |
| 207 | this.strategy = 'render' |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | strategisedSetState = (state) => { |
| 212 | if (this.strategy === 'callback') { |
| 213 | this.callbackState = state |
| 214 | this.props.onSize(state) |
nothing calls this directly
no test coverage detected
searching dependent graphs…