UNPKG

twreporter-react-index-page

Version:
113 lines (86 loc) 4.05 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 _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _styledComponents = require('styled-components'); var _styledComponents2 = _interopRequireDefault(_styledComponents); var _get = require('lodash/get'); var _get2 = _interopRequireDefault(_get); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } /* global React */ var _ = { get: _get2.default }; var ImgObjectFit = _styledComponents2.default.div.withConfig({ displayName: 'img-wrapper__ImgObjectFit', componentId: 's1a41c93-0' })(['width: 100%;height: 100%;> img {width: 100%;height: 100%;object-fit: cover;}']); var ImgFallback = _styledComponents2.default.div.withConfig({ displayName: 'img-wrapper__ImgFallback', componentId: 's1a41c93-1' })(['width: 100%;height: 100%;background-size: cover;background-image: ', ';background-position: center center;'], function (props) { return 'url(' + _.get(props, 'url') + ')'; }); var ImgWrapper = function (_React$Component) { _inherits(ImgWrapper, _React$Component); function ImgWrapper(props) { _classCallCheck(this, ImgWrapper); var _this = _possibleConstructorReturn(this, (ImgWrapper.__proto__ || Object.getPrototypeOf(ImgWrapper)).call(this, props)); _this.state = { isObjectFit: true }; return _this; } _createClass(ImgWrapper, [{ key: 'componentWillMount', value: function componentWillMount() { this.setState({ isObjectFit: 'objectFit' in _.get(document, 'documentElement.style') }); } }, { key: 'render', value: function render() { var _props = this.props, src = _props.src, alt = _props.alt, srcSet = _props.srcSet; var isObjectFit = this.state.isObjectFit; return isObjectFit ? React.createElement( ImgObjectFit, null, React.createElement('img', { alt: alt, src: src, srcSet: srcSet }), this.props.children ) : React.createElement( ImgFallback, { url: src }, this.props.children ); } }]); return ImgWrapper; }(React.Component); ImgWrapper.defaultProps = { alt: '', children: null, src: '', srcSet: '' }; ImgWrapper.propTypes = { alt: _propTypes2.default.string, children: _propTypes2.default.element, src: _propTypes2.default.string.isRequired, srcSet: _propTypes2.default.string }; exports.default = ImgWrapper;