UNPKG

react-preload-image

Version:

Preload and fade in an image. Optional support for lazy loading.

108 lines (86 loc) 4.21 kB
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; }; 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 from 'react'; var PreloadImage = function (_React$Component) { _inherits(PreloadImage, _React$Component); function PreloadImage(props) { _classCallCheck(this, PreloadImage); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.state = { loaded: false, src: null }; return _this; } PreloadImage.prototype.componentDidMount = function componentDidMount() { if (this.props.lazy && 'IntersectionObserver' in window) { this.setObserver(); } else { this.setPreloader(); } }; PreloadImage.prototype.setObserver = function setObserver() { var _this2 = this; this.observer = new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (entry.isIntersecting) { _this2.setPreloader(); _this2.observer.disconnect(); } }); }); this.observer.observe(this.el); }; PreloadImage.prototype.setPreloader = function setPreloader() { var _this3 = this; this.preloader = new Image(); this.preloader.onload = function () { return _this3.setState({ loaded: true, src: 'url(' + _this3.props.src + ')' }); }; this.preloader.src = this.props.src; }; PreloadImage.prototype.componentWillUnmount = function componentWillUnmount() { if (this.observer) this.observer.disconnect(); if (this.preloader) this.preloader.onload = null; }; PreloadImage.prototype.render = function render() { var _this4 = this; var backgroundSize = this.props.innerStyle && this.props.innerStyle.backgroundSize ? this.props.innerStyle.backgroundSize : "cover"; var backgroundPosition = this.props.innerStyle && this.props.innerStyle.backgroundPosition ? this.props.innerStyle.backgroundPosition : "center"; var backgroundRepeat = this.props.innerStyle && this.props.innerStyle.backgroundRepeat ? this.props.innerStyle.backgroundRepeat : "no-repeat"; return React.createElement( 'div', { // Required: Relative, absolute, or fixed position // Required: Width & height (explicitly or via top/right/bottom/left) // Optional: Background color or placeholder image className: this.props.className, style: _extends({}, this.props.style), ref: function ref(el) { return _this4.el = el; } }, React.createElement('div', { style: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0, backgroundImage: this.state.src, backgroundSize: backgroundSize, backgroundPosition: backgroundPosition, backgroundRepeat: backgroundRepeat, transition: 'opacity ' + (this.props.duration || '300ms') + ' ' + (this.props.ease || 'cubic-bezier(0.215, 0.61, 0.355, 1)'), opacity: this.state.loaded ? 1 : 0 } }), this.props.children ); }; return PreloadImage; }(React.Component); export default PreloadImage;