React Layout Computation
Created
October 26, 2020 12:22
-
-
Save GGrassiant/175b501c4631a14f29758fc8f429b73f to your computer and use it in GitHub Desktop.
React Layout Computation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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