UNPKG

ep-react-ui

Version:
137 lines (102 loc) 6.65 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _assign2 = require('lodash/assign'); var _assign3 = _interopRequireDefault(_assign2); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _InView = require('./InView'); var _InView2 = _interopRequireDefault(_InView); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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; } /** * Image Lazy Load */ var LazyImage = function (_PureComponent) { _inherits(LazyImage, _PureComponent); function LazyImage(props) { _classCallCheck(this, LazyImage); var _this = _possibleConstructorReturn(this, (LazyImage.__proto__ || Object.getPrototypeOf(LazyImage)).call(this, props)); _this.state = { isLoaded: false }; // events _this._onInView = _this._onInView.bind(_this); _this._onImageLoad = _this._onImageLoad.bind(_this); return _this; } _createClass(LazyImage, [{ key: 'render', value: function render() { var _props = this.props, className = _props.className, uniqId = _props.uniqId, src = _props.src, width = _props.width, height = _props.height, useBg = _props.useBg, others = _objectWithoutProperties(_props, ['className', 'uniqId', 'src', 'width', 'height', 'useBg']); var isLoaded = this.state.isLoaded; var TagName = useBg ? 'div' : 'img'; var attr = _extends({}, others, { className: (0, _classnames2.default)('img', className) }); if (useBg) { attr.style = (0, _assign3.default)({}, this.props.style, { width: width, height: height, backgroundImage: isLoaded ? 'url(' + src + ')' : undefined }); } else { attr.src = isLoaded ? src : undefined; attr.width = width; attr.height = height; } return _react2.default.createElement( _InView2.default, { className: 'LazyImage', uniqId: uniqId, onInView: this._onInView }, _react2.default.createElement(TagName, attr) ); } }, { key: '_onInView', value: function _onInView() { var src = this.props.src; var isLoaded = this.state.isLoaded; if (isLoaded) { return; } this.__img = new Image(); this.__img.addEventListener('load', this._onImageLoad); this.__img.src = src; } }, { key: '_onImageLoad', value: function _onImageLoad() { this.__img.removeEventListener('load', this._onImageLoad); this.setState({ isLoaded: true }); } }]); return LazyImage; }(_react.PureComponent); LazyImage.propTypes = { uniqId: _propTypes2.default.string.isRequired, // unique ID for InView to work src: _propTypes2.default.string.isRequired, // image src width: _propTypes2.default.string.isRequired, // image width height: _propTypes2.default.string.isRequired, // image height useBg: _propTypes2.default.bool // using background-image instead of img tag }; exports.default = LazyImage;