UNPKG

zarm-web

Version:
290 lines (235 loc) 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = require("react"); var _classnames3 = _interopRequireDefault(require("classnames")); var _utils = require("../utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var TRANSITION_TYPE = { ENTERING: 'show', EXITING: 'hide' }; var Transition = /*#__PURE__*/ function (_Component) { _inherits(Transition, _Component); function Transition(_props) { var _this; _classCallCheck(this, Transition); _this = _possibleConstructorReturn(this, _getPrototypeOf(Transition).call(this, _props)); _this.nextStatus = void 0; _this.timeoutId = void 0; _this.computeInitialStatus = function () { var _this$props = _this.props, visible = _this$props.visible, mountOnShow = _this$props.mountOnShow, transitionOnMount = _this$props.transitionOnMount, unmountOnHide = _this$props.unmountOnHide; if (visible) { if (transitionOnMount) { return { initial: Transition.EXITED, next: Transition.ENTERING }; } return { initial: Transition.ENTERED }; } if (mountOnShow || unmountOnHide) return { initial: Transition.UNMOUNTED }; return { initial: Transition.EXITED }; }; _this.computeNextStatus = function () { var _this$state = _this.state, animating = _this$state.animating, status = _this$state.status; if (animating) return status === Transition.ENTERING ? Transition.EXITING : Transition.ENTERING; return status === Transition.ENTERED ? Transition.EXITING : Transition.ENTERING; }; _this.computeStatus = function (props) { var status = _this.state.status; var visible = props.visible; if (visible) { return { status: status === Transition.UNMOUNTED && Transition.EXITED, next: status !== Transition.ENTERING && status !== Transition.ENTERED && Transition.ENTERING }; } return { next: (status === Transition.ENTERING || status === Transition.ENTERED) && Transition.EXITING }; }; _this.updateStatus = function () { var animating = _this.state.animating; if (_this.nextStatus) { _this.nextStatus = _this.computeNextStatus(); if (!animating) _this.handleStart(); } }; _this.handleStart = function () { var duration = _this.props.duration; var status = _this.nextStatus; _this.nextStatus = null; _this.setState({ status: status, animating: true }, function () { _this.props.onStart(_objectSpread({}, _this.props, { status: status })); _this.timeoutId = setTimeout(function () { _this.handleComplete(); }, duration); }); }; _this.handleComplete = function () { var status = _this.state.status; _this.props.onComplete(_objectSpread({}, _this.props, { status: status })); if (_this.nextStatus) { return _this.handleStart(); } var completeStatus = _this.computeCompletedStatus(); var beforeHook = status === Transition.ENTERING ? 'onBeforeShow' : 'onBeforeHide'; var callback = status === Transition.ENTERING ? 'onShow' : 'onHide'; _this.props[beforeHook](_objectSpread({}, _this.props, { status: completeStatus })); _this.setState({ status: completeStatus, animating: false }, function () { _this.props[callback](_objectSpread({}, _this.props, { status: completeStatus })); }); }; _this.computeCompletedStatus = function () { var unmountOnHide = _this.props.unmountOnHide; var status = _this.state.status; if (status === Transition.ENTERING) return Transition.ENTERED; return unmountOnHide ? Transition.UNMOUNTED : Transition.EXITED; }; _this.computeClasses = function () { var _this$props2 = _this.props, name = _this$props2.name, children = _this$props2.children, directional = _this$props2.directional; var _this$state2 = _this.state, animating = _this$state2.animating, status = _this$state2.status; var childrenClass = children.props.className; if (directional) { var _classnames; return (0, _classnames3.default)(childrenClass, (_classnames = { animating: animating }, _defineProperty(_classnames, "".concat(name), name), _defineProperty(_classnames, "".concat(name, "-in"), status === Transition.ENTERING), _defineProperty(_classnames, "".concat(name, "-out"), status === Transition.EXITING), _defineProperty(_classnames, "hidden", status === Transition.EXITED), _defineProperty(_classnames, "visible", status !== Transition.EXITED), _defineProperty(_classnames, "transition", true), _classnames)); } return (0, _classnames3.default)(childrenClass, _defineProperty({ animating: animating, transition: true }, "".concat(name), name)); }; _this.computeStyle = function () { var _this$props3 = _this.props, children = _this$props3.children, duration = _this$props3.duration; var status = _this.state.status; var childrenStyle = children.props.style; var type = TRANSITION_TYPE[status]; var animationDuration = type && "".concat(duration, "ms"); return _objectSpread({}, childrenStyle, { animationDuration: animationDuration }); }; var _this$computeInitialS = _this.computeInitialStatus(), _status = _this$computeInitialS.initial, next = _this$computeInitialS.next; _this.nextStatus = next; _this.state = { status: _status }; return _this; } _createClass(Transition, [{ key: "componentDidMount", value: function componentDidMount() { this.updateStatus(); } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var _this$computeStatus = this.computeStatus(nextProps), status = _this$computeStatus.status, next = _this$computeStatus.next; this.nextStatus = next; if (status) this.setState({ status: status }); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.updateStatus(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { clearTimeout(this.timeoutId); } }, { key: "render", value: function render() { var children = this.props.children; var status = this.state.status; if (status === Transition.UNMOUNTED) return null; if ((0, _react.isValidElement)(children) && _react.Children.only(children)) { return (0, _react.cloneElement)(children, { className: this.computeClasses(), style: this.computeStyle() }); } } }]); return Transition; }(_react.Component); exports.default = Transition; Transition.defaultProps = { name: 'fade', duration: 500, directional: true, visible: true, mountOnShow: true, transitionOnMount: false, unmountOnHide: false, onStart: _utils.noop, onShow: _utils.noop, onHide: _utils.noop, onComplete: _utils.noop, onBeforeShow: _utils.noop, onBeforeHide: _utils.noop }; Transition.ENTERED = 'ENTERED'; Transition.ENTERING = 'ENTERING'; Transition.EXITED = 'EXITED'; Transition.EXITING = 'EXITING'; Transition.UNMOUNTED = 'UNMOUNTED';