UNPKG

@crossfield/react-read-more

Version:

Read more container that expands html elements to their full height

137 lines (111 loc) 5.23 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } 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; } var ReadMore = function (_React$Component) { _inherits(ReadMore, _React$Component); function ReadMore(_ref) { var initialHeight = _ref.initialHeight; _classCallCheck(this, ReadMore); var _this = _possibleConstructorReturn(this, (ReadMore.__proto__ || Object.getPrototypeOf(ReadMore)).call(this)); _this.state = { initialHeight: initialHeight, maxHeight: initialHeight }; return _this; } _createClass(ReadMore, [{ key: 'toggle', value: function toggle() { var _state = this.state, maxHeight = _state.maxHeight, initialHeight = _state.initialHeight; //if expanded, close it if (maxHeight !== initialHeight) return this.setState({ maxHeight: initialHeight }); var height = this.calculateHeight(); //set the full height this.setState({ maxHeight: height }); } }, { key: 'calculateHeight', value: function calculateHeight() { //calculate height of all the children var children = [].concat(_toConsumableArray(this.container.children)); var height = 0; children.forEach(function (child) { return height += child.offsetHeight; }); return height; } }, { key: 'componentDidMount', value: function componentDidMount() { if (this.calculateHeight() <= this.props.initialHeight) this.setState({ hideReadMore: true }); } }, { key: 'render', value: function render() { var _this2 = this; var _props = this.props, children = _props.children, readMore = _props.readMore, blurStyle = _props.blurStyle, overhangSize = _props.overhangSize; var _state2 = this.state, maxHeight = _state2.maxHeight, initialHeight = _state2.initialHeight, hideReadMore = _state2.hideReadMore; var open = maxHeight !== initialHeight; return _react2.default.createElement( _react2.default.Fragment, null, _react2.default.createElement( 'div', { className: 'readmore container', style: { maxHeight: open ? maxHeight : maxHeight - overhangSize, transition: 'max-height .5s ease', position: 'relative', overflow: 'hidden' }, ref: function ref(el) { return _this2.container = el; } }, children, hideReadMore ? null : _react2.default.createElement('div', { className: 'readmore overhang', style: Object.assign({ transition: 'opacity 0.25s', opacity: open ? 0 : 1, backgroundImage: 'linear-gradient(to bottom, rgba(255, 255, 255, 0.44), #ffffff )', content: '', height: overhangSize + 'px', width: '100%', position: 'absolute', bottom: '0', left: '0' }, blurStyle) }) ), hideReadMore ? null : readMore({ open: open, onClick: function onClick() { return _this2.toggle(); } }) ); } }]); return ReadMore; }(_react2.default.Component); ReadMore.defaultProps = { overhangSize: 160 }; exports.default = ReadMore;