Skip to content

Instantly share code, notes, and snippets.

@GGrassiant
Created October 26, 2020 12:22
Show Gist options
  • Select an option

  • Save GGrassiant/175b501c4631a14f29758fc8f429b73f to your computer and use it in GitHub Desktop.

Select an option

Save GGrassiant/175b501c4631a14f29758fc8f429b73f to your computer and use it in GitHub Desktop.
React Layout Computation

React Layout Computation

import React from 'react';
class ImageCard extends React.Component {
constructor(props) {
super(props);
this.state = {
spans: 0,
wrapperHeight: 0
};
this.imageRef = React.createRef();
}
componentDidMount() {
this.imageRef.current.addEventListener('load', this.setSpans);
}
setSpans = () => {
const height = this.imageRef.current.clientHeight;
const wrapperHeight = height % 10 === 0 ?
(Math.floor(height / 10)) * 10 - 10 :
(Math.floor(height / 10)) * 10;
const spans = Math.ceil(height / 10);
this.setState({
spans,
wrapperHeight
});
};
render() {
const { description, urls } = this.props.image;
return (
<div style={{ gridRowEnd: `span ${this.state.spans}` }} className={'image__container'}>
<div style={{ height: `${this.state.wrapperHeight}px` }} className={'wrapper'}>
<img ref={this.imageRef} alt={description} src={urls.regular} />
</div>
</div>
);
}
}
export default ImageCard;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment