UNPKG

@bigfishtv/cockpit

Version:

97 lines (82 loc) 3.36 kB
var _class, _temp2; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; /** * This component uses the ResizeObserver API to * detect when its size changes (due to children) * and ensures it never shrinks below the tallest * content height. This is helpful to prevent * visual jerkiness due to content height shrinking * and forcing the browser to "scroll up". * * Can be used as a normal component which will * render a div of its own. * * <MinimumHeight> * <Child /> * <Child /> * </MinimumHeight> * * Alternative usage is to use a function as a child. * * <MinimumHeight> * {(forwardRef, minHeight) => ( * <main ref={forwardRef} style={{ minHeight }}> * <Child /> * <Child /> * </main> * )} * </MinimumHeight> */ var MinimumHeight = (_temp2 = _class = function (_Component) { _inherits(MinimumHeight, _Component); function MinimumHeight() { var _temp, _this, _ret; _classCallCheck(this, MinimumHeight); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = { minHeight: _this.props.minHeight }, _this.registerElement = function (el) { if (!el && _this.el && _this.ro) { _this.ro.unobserve(_this.el); } _this.el = el; if (_this.el && !_this.ro) { _this.ro = new window.ResizeObserver(function (entries) { var height = entries[0].contentRect.height; if (height > _this.state.minHeight) { _this.setState({ minHeight: height }); } }); _this.ro.observe(_this.el); } }, _temp), _possibleConstructorReturn(_this, _ret); } MinimumHeight.prototype.componentWillUnmount = function componentWillUnmount() { if (this.ro && this.el) { this.ro.unobserve(this.el); } }; MinimumHeight.prototype.render = function render() { if (typeof this.props.children === 'function') { return this.props.children(this.registerElement, this.state.minHeight); } else { return React.createElement( 'div', { ref: this.registerElement, style: { minHeight: this.state.minHeight } }, this.props.children ); } }; return MinimumHeight; }(Component), _class.propTypes = { minHeight: PropTypes.number }, _class.defaultProps = { minHeight: 0 }, _temp2); export { MinimumHeight as default };