Adding a layout pass to SVG image components

text nodes are the only elements that need to be "measured". The
dimensions of all other image components can be determined based on the
dimensions of their children. This adds a pre-rendering pass to work out
dimensions so multiple renders don't need to happen
This commit is contained in:
Jeff Avallone
2019-01-26 17:25:38 -05:00
parent 21c392752e
commit b299d32fc3
11 changed files with 97 additions and 55 deletions
+12 -4
View File
@@ -28,13 +28,21 @@ const render = (data, extraProps) => {
class Render extends React.PureComponent {
static propTypes = {
parsed: PropTypes.object.isRequired,
data: PropTypes.object.isRequired,
onRender: PropTypes.func.isRequired
}
svgContainer = React.createRef()
provideSVGData = () => {
componentDidMount() {
this.provideSVGData();
}
componentDidUpdate() {
this.provideSVGData();
}
provideSVGData() {
if (!this.svgContainer.current) {
return;
}
@@ -48,10 +56,10 @@ class Render extends React.PureComponent {
}
render() {
const { parsed } = this.props;
const { data } = this.props;
return <div className={ style.render } ref={ this.svgContainer }>
{ render(parsed, { onReflow: this.provideSVGData }) }
{ render(data, { onReflow: this.provideSVGData }) }
</div>;
}
}