Skip to content

Commit

Permalink
feat(minHeight): issues/403 set min-height, avoid page jumps (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Sep 30, 2020
1 parent 107e42a commit 26f097d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`MinHeight Component should render a non-connected component: non-connected 1`] = `
<div
style={
Object {
"minHeight": 0,
}
}
>
lorem ipsum
</div>
`;
35 changes: 35 additions & 0 deletions src/components/minHeight/__tests__/minHeight.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import { shallow } from 'enzyme';
import { MinHeight } from '../minHeight';

describe('MinHeight Component', () => {
it('should render a non-connected component', () => {
const props = {};

const component = shallow(<MinHeight {...props}>lorem ipsum</MinHeight>);
expect(component).toMatchSnapshot('non-connected');
});

it('should set minHeight in relation to contents and props', () => {
const props = {};
const component = shallow(<MinHeight {...props}>lorem ipsum</MinHeight>);

expect(component.instance().setMinHeight).toBeDefined();

// initial height should be zero
expect(component.instance().updatedMinHeight).toEqual(0);

// set the container size arbitrarily
component.instance().containerRef.current = { clientHeight: 100 };
component.instance().componentDidUpdate();
expect(component.instance().updatedMinHeight).toEqual(100);

// set a minimum minHeight
component.setProps({ minHeight: 200 });
expect(component.instance().updatedMinHeight).toEqual(200);

// disable "on update" minHeight adjustments
component.setProps({ autoUpdate: false, minHeight: 300 });
expect(component.instance().updatedMinHeight).toEqual(200);
});
});
82 changes: 82 additions & 0 deletions src/components/minHeight/minHeight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from 'react';
import PropTypes from 'prop-types';

/**
* Set a min-height to prevent page jump component.
*
* @augments React.Component
*/
class MinHeight extends React.Component {
containerRef = React.createRef();

updatedMinHeight = 0;

componentDidMount() {
this.setMinHeight();
}

componentDidUpdate() {
const { autoUpdate } = this.props;

if (autoUpdate) {
this.setMinHeight();
}
}

/**
* Set minHeight on mount or update.
*/
setMinHeight() {
const { updatedMinHeight } = this;
const { minHeight: overrideMinHeight } = this.props;
// const { clientHeight = 0 } = this.containerRef.current || {};
const clientHeight = this.containerRef?.current?.clientHeight || 0;

if (clientHeight !== updatedMinHeight) {
this.updatedMinHeight = clientHeight;
}

if (overrideMinHeight > this.updatedMinHeight) {
this.updatedMinHeight = overrideMinHeight;
}
}

/**
* Render a min-height div with children.
*
* @returns {Node}
*/
render() {
const { updatedMinHeight } = this;
const { children } = this.props;

return (
<div ref={this.containerRef} style={{ minHeight: updatedMinHeight }}>
{children}
</div>
);
}
}

/**
* Prop types.
*
* @type {{minHeight: number, autoUpdate: boolean, children: Node}}
*/
MinHeight.propTypes = {
autoUpdate: PropTypes.bool,
children: PropTypes.node.isRequired,
minHeight: PropTypes.number
};

/**
* Default props.
*
* @type {{minHeight: number, autoUpdate: boolean}}
*/
MinHeight.defaultProps = {
autoUpdate: true,
minHeight: 0
};

export { MinHeight as default, MinHeight };

0 comments on commit 26f097d

Please sign in to comment.