tuya-panel-kit
Version:
a functional component library for developing tuya device panels!
194 lines (155 loc) • 6.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _jsxFileName = 'src/components/motion/fade.js';
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 _reactNative = require('react-native');
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; }
var DEFAULT_ANIMATION_CONFIG = {
duration: 300,
delay: 0,
isInteraction: true,
useNativeDriver: true
};
var Fade = function (_PureComponent) {
_inherits(Fade, _PureComponent);
function Fade(props) {
_classCallCheck(this, Fade);
var _this = _possibleConstructorReturn(this, (Fade.__proto__ || Object.getPrototypeOf(Fade)).call(this, props));
_this.startAnimation = function (show) {
if (show) {
_this.setState({ show: true, isAnimating: true }, _this.startShowAnimation);
} else {
_this.setState({ isAnimating: true }, _this.startHideAnimation);
}
};
_this.startShowAnimation = function () {
var _this$props = _this.props,
fadeOpacity = _this$props.fadeOpacity,
onShow = _this$props.onShow,
showDuration = _this$props.showDuration;
var animationConfig = _extends({}, DEFAULT_ANIMATION_CONFIG, _this.props.animationConfig);
_reactNative.Animated.timing(_this.state.opacity, _extends({
toValue: fadeOpacity
}, animationConfig, {
duration: showDuration,
easing: _reactNative.Easing.bezier(0, 0, 0.25, 1)
})).start(function (_ref) {
var finished = _ref.finished;
if (finished) {
_this.setState({ isAnimating: false }, function () {
typeof onShow === 'function' && onShow();
});
}
});
};
_this.startHideAnimation = function () {
var _this$props2 = _this.props,
onHide = _this$props2.onHide,
hideDuration = _this$props2.hideDuration;
var animationConfig = _extends({}, DEFAULT_ANIMATION_CONFIG, _this.props.animationConfig);
_reactNative.Animated.timing(_this.state.opacity, _extends({
toValue: 0
}, animationConfig, {
duration: hideDuration,
easing: _reactNative.Easing.bezier(0.42, 0, 1, 1)
})).start(function (_ref2) {
var finished = _ref2.finished;
if (finished) {
_this.setState({ show: false, isAnimating: false }, function () {
typeof onHide === 'function' && onHide();
});
}
});
};
_this.state = {
show: props.show,
opacity: new _reactNative.Animated.Value(0),
isAnimating: false
};
return _this;
}
_createClass(Fade, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (this.state.show) {
this.startShowAnimation();
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
var show = nextProps.show;
if (typeof show !== 'undefined' && show !== this.state.show) {
this.startAnimation(show);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.state.opacity.stopAnimation();
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
style = _props.style,
children = _props.children;
if (!this.state.show || !_react2.default.isValidElement(children)) {
return null;
}
return _react2.default.createElement(
_reactNative.Animated.View,
{
renderToHardwareTextureAndroid: this.state.isAnimating,
style: [styles.container, style, { opacity: this.state.opacity }],
__source: {
fileName: _jsxFileName,
lineNumber: 143
}
},
children
);
}
}]);
return Fade;
}(_react.PureComponent);
Fade.displayName = 'Motion.Fade';
Fade.propTypes = {
style: _reactNative.ViewPropTypes.style,
show: _propTypes2.default.bool,
fadeOpacity: _propTypes2.default.number,
children: _propTypes2.default.element.isRequired,
showDuration: _propTypes2.default.number,
hideDuration: _propTypes2.default.number,
onShow: _propTypes2.default.func,
onHide: _propTypes2.default.func,
animationConfig: _propTypes2.default.shape({
duration: _propTypes2.default.number,
delay: _propTypes2.default.number,
isInteraction: _propTypes2.default.bool,
useNativeDriver: _propTypes2.default.bool
})
};
Fade.defaultProps = {
style: null,
show: undefined,
fadeOpacity: 1,
showDuration: 300,
hideDuration: 300,
onShow: function onShow() {},
onHide: function onHide() {},
animationConfig: DEFAULT_ANIMATION_CONFIG
};
var styles = _reactNative.StyleSheet.create({
container: {}
});
exports.default = Fade;