The designer I am working with would really like the rows of a multi-columned set of text to be equal. It is for a fairly simple eBook, but when I get the height, sometimes it renders correctly and at other times it doesn't. Image for reference:
So, what I am targeting (and is also working some of the time) is for the rendered header to loop through and set the height to the tallest of the three headers. In the image above, the function should have rendered a similar view, but where each of the headers retains its margin. Instead, it has calculated them all to be the same size so the margin is cut. Unfortunately, it seems that (usually) on headers that have just one orphan, the height is being calculated in correctly. Does anyone have any better ideas? My code is below.
Columns.js (updateHeight being the important function here) —
import React from 'react' import { branch } from 'baobab-react/higher-order' import classNames from 'classnames' import _parseInt from 'lodash.parseint' import _filter from 'lodash.filter' import InlineSVG from 'react-inlinesvg' // Column Components import ImageContainer from './columns/ImageContainer' import IconContainer from './columns/IconContainer' import ColumnHeader from './columns/ColumnHeader' import Content from './columns/Content' class Columns extends React.Component { constructor(props, context) { super(props, context) this.state = { content: 0, header: 0, } } updateHeight(height, component) { if (window.innerWidth > 768 && window.innerHeight > 768) { if (height > this.state[component]) { this.setState({ [component]: height }) } else { return false } } else { return false } } getItems(column, index) { let data = column[0] || column const icon = data.icon ? '/assets/icons/' + data.icon : null const image = data.bioImage ? '/assets/bio-photos/' + data.bioImage : null const ref = `column${ index }` return ( <div className="column" key={ index } ref={ ref }> { image ? <ImageContainer title={ data.title } image={ image } /> : null } { icon ? <IconContainer icon={ icon } /> : null } <ColumnHeader title={ data.title } subtitle={ data.subtitle } index={ index } updateHeight={ this.updateHeight.bind(this) } height={ index === undefined ? 'auto' : this.state.header } /> <Content content={ data.content } updateHeight={ this.updateHeight.bind(this) } height={ index === undefined ? 'auto' : this.state.content } /> { this.props.type === 'team' ? <div className="team__social"><InlineSVG src="/assets/icons/linkedin.svg"></InlineSVG></div> : null } </div> ) } render() { const data = this.props.data.columns || this.props.data const type = this.props.type const count = data.length || 1 let columns = count > 1 ? data.map((column, index) => this.getItems(column, index)) : this.getItems(data, 1) let columnClasses = classNames({ 'columns': true, [`columns--${count}`]: true, 'columns--icons': type === 'icons' ? true : false, 'columns--team': type === 'team' ? true : false, }) return ( <div className={ columnClasses }> { columns } </div> ) } } export default branch(Columns, { cursors: { navOpen: ['navOpen'], } }) ColumnHeader.js —
import React from 'react' import { branch } from 'baobab-react/higher-order' class ColumnHeader extends React.Component { constructor(props, context) { super(props, context) } componentDidMount() { this.props.updateHeight(this.refs.header.offsetHeight, 'header') } render() { let { title, subtitle } = this.props let height = (this.props.height === 0) ? 'auto' : this.props.height return ( <div className="column__header" ref="header" style={{ height: height }}> <h1 className="title">{ title }</h1> { subtitle ? (<h2 className="subtitle">{ subtitle }</h2>) : null } </div> ) } } export default ColumnHeader 
