react-crossfade-image
Version:
Simple React component for crossfading images
112 lines (93 loc) • 4.87 kB
JavaScript
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; }; }();
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";
var CrossfadeImage = function (_Component) {
_inherits(CrossfadeImage, _Component);
function CrossfadeImage(props) {
_classCallCheck(this, CrossfadeImage);
var _this = _possibleConstructorReturn(this, (CrossfadeImage.__proto__ || Object.getPrototypeOf(CrossfadeImage)).call(this, props));
_this.state = {
topSrc: props.src,
bottomOpacity: 0,
bottomSrc: props.src
};
return _this;
}
_createClass(CrossfadeImage, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(newProps) {
var _this2 = this;
var oldSrc = this.state.topSrc;
var newSrc = newProps.src;
if (newSrc !== oldSrc) {
// Reset the component everytime we receive new prop, to
// cancel out any animation that's still going on
this.setState({ bottomSrc: false, topSrc: false }, function () {
return _this2.setState(
// Opacity less than 1 takes precendence in stacking order
{ bottomSrc: oldSrc, topSrc: newSrc, bottomOpacity: 0.99 }, function () {
// One of the few times setTimeout does wonders, this is for
// getting fade out transition without css keyframe
if (!_this2.timeout) clearTimeout(_this2.timeout);
_this2.timeout = setTimeout(function () {
return _this2.setState({ bottomOpacity: 0 });
}, 20);
});
});
}
}
}, {
key: "render",
value: function render() {
var _props = this.props,
containerClass = _props.containerClass,
duration = _props.duration,
timingFunction = _props.timingFunction,
delay = _props.delay,
style = _props.style,
alt = _props.alt;
var _state = this.state,
topSrc = _state.topSrc,
bottomOpacity = _state.bottomOpacity,
bottomSrc = _state.bottomSrc;
return React.createElement(
"div",
{ className: containerClass, style: _extends({}, defaultStyle, { position: "relative" }) },
topSrc && React.createElement("img", {
style: _extends({}, defaultStyle, style, { position: "absolute" }),
src: topSrc,
alt: alt
}),
bottomSrc && React.createElement("img", {
style: _extends({}, defaultStyle, style, {
opacity: bottomOpacity,
transition: "opacity " + duration / 1000 + "s " + timingFunction + " " + delay / 1000 + "s"
}),
src: bottomSrc
})
);
}
}]);
return CrossfadeImage;
}(Component);
export default CrossfadeImage;
var defaultStyle = { maxWidth: "100%", maxHeight: "100%" };
CrossfadeImage.propTypes = {
src: PropTypes.string.isRequired,
alt: PropTypes.string,
duration: PropTypes.number,
timingFunction: PropTypes.string,
delay: PropTypes.number,
style: PropTypes.object,
containerClass: PropTypes.string
};
CrossfadeImage.defaultProps = {
duration: 500,
timingFunction: "ease",
delay: 0,
containerClass: "CrossfadeImage"
};