react-flip-toolkit
Version:
Configurable FLIP animation helpers for React
80 lines (63 loc) • 3.22 kB
JavaScript
var _class, _temp2;
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, createContext } from "react";
import PropTypes from "prop-types";
import { animateMove, getFlippedElementPositions } from "./flipHelpers";
import * as popmotionEasing from "popmotion/easing";
export var FlipContext = React.createContext("flip");
var Flipper = (_temp2 = _class = function (_Component) {
_inherits(Flipper, _Component);
function Flipper() {
var _temp, _this, _ret;
_classCallCheck(this, Flipper);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.inProgressAnimations = {}, _this.flipCallbacks = {}, _temp), _possibleConstructorReturn(_this, _ret);
}
Flipper.prototype.getSnapshotBeforeUpdate = function getSnapshotBeforeUpdate(prevProps) {
if (prevProps.flipKey !== this.props.flipKey) {
return getFlippedElementPositions(this.el);
}
return null;
};
Flipper.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState, cachedFlipChildrenPositions) {
if (this.props.flipKey !== prevProps.flipKey) {
this.inProgressAnimations = animateMove({
cachedFlipChildrenPositions: cachedFlipChildrenPositions,
containerEl: this.el,
duration: this.props.duration,
ease: popmotionEasing[this.props.ease],
inProgressAnimations: this.inProgressAnimations,
flipCallbacks: this.flipCallbacks
});
}
};
Flipper.prototype.render = function render() {
var _this2 = this;
return React.createElement(
FlipContext.Provider,
{ value: this.flipCallbacks },
React.createElement(
"div",
{ ref: function ref(el) {
return _this2.el = el;
} },
this.props.children
)
);
};
return Flipper;
}(Component), _class.defaultProps = {
duration: 250,
ease: "easeOut"
}, _temp2);
Flipper.propTypes = process.env.NODE_ENV !== "production" ? {
flipKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
children: PropTypes.node.isRequired,
duration: PropTypes.number,
ease: PropTypes.string
} : {};
export default Flipper;