Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FredrikNoren/4708ca59180c97266d2befe8a1131507 to your computer and use it in GitHub Desktop.
Save FredrikNoren/4708ca59180c97266d2befe8a1131507 to your computer and use it in GitHub Desktop.
MeasureSize.tsx
import * as React from 'react';
export interface MeasureSizeProps {
onResize?: (size: number[]) => void;
}
export class MeasureSize extends React.Component<MeasureSizeProps, null> {
componentDidMount() {
this.dispatchResize();
}
componentDidUpdate() {
this.dispatchResize();
}
rootElement: HTMLElement;
size: number[];
dispatchResize() {
if (this.props.onResize) {
let s = [this.rootElement.offsetWidth, this.rootElement.offsetHeight];
if (!this.size || this.size[0] !== s[0] || this.size[1] !== s[1]) {
this.size = s;
this.props.onResize(s);
}
}
}
render() {
return (
<div className="MeasureSize" ref={element => this.rootElement = element}>
{this.props.children}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment