UNPKG

zarm-web

Version:
214 lines (180 loc) 8.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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 _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 percent = 0.06; var CollapseTransition = /*#__PURE__*/ function (_Component) { _inherits(CollapseTransition, _Component); function CollapseTransition() { var _getPrototypeOf2; var _this; _classCallCheck(this, CollapseTransition); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(CollapseTransition)).call.apply(_getPrototypeOf2, [this].concat(args))); _this.collapseWrap = (0, _react.createRef)(); return _this; } _createClass(CollapseTransition, [{ key: "componentDidMount", value: function componentDidMount() { var visible = this.props.visible; this.beforeEnter(); if (visible) { this.enter(); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(props) { var visible = this.props.visible; if (visible !== props.visible) this.triggerChange(visible); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.beforeLeave(); this.leave(); } }, { key: "triggerChange", value: function triggerChange(visible) { if (visible) { this.beforeEnter(); this.enter(); } else { this.beforeLeave(); this.leave(); } } }, { key: "beforeEnter", value: function beforeEnter() { var el = this.collapseWrap.current; el.dataset.oldPaddingTop = el.style.paddingTop; el.dataset.oldPaddingBottom = el.style.paddingBottom; el.dataset.oldOverflow = el.style.overflow; el.style.height = 0; el.style.paddingTop = 0; el.style.paddingBottom = 0; } }, { key: "enter", value: function enter() { var _this2 = this; var el = this.collapseWrap.current; var tempHeight = 0; var tempPaddingTop = 0; var tempPaddingBottom = 0; el.style.display = 'block'; function step(context) { tempHeight = Math.min(el.scrollHeight, tempHeight += el.scrollHeight * percent); tempPaddingTop = Math.min(el.dataset.oldPaddingTop, tempPaddingTop += el.dataset.oldPaddingTop * percent); tempPaddingBottom = Math.min(el.dataset.oldPaddingBottom, tempPaddingBottom += el.dataset.oldPaddingBottom * percent); el.style.height = "".concat(tempHeight, "px"); el.style.paddingTop = tempPaddingTop; el.style.paddingBottom = tempPaddingBottom; if (tempHeight < el.scrollHeight) { requestAnimationFrame(function () { step(context); }); } else { context.afterEnter(); } } requestAnimationFrame(function () { step(_this2); }); el.style.overflow = 'hidden'; } }, { key: "afterEnter", value: function afterEnter() { var el = this.collapseWrap.current; el.style.display = 'block'; el.style.height = ''; el.style.overflow = el.dataset.oldOverflow; } }, { key: "beforeLeave", value: function beforeLeave() { var el = this.collapseWrap.current; el.dataset.oldPaddingTop = el.style.paddingTop; el.dataset.oldPaddingBottom = el.style.paddingBottom; el.dataset.oldOverflow = el.style.overflow; el.style.display = 'block'; if (el.scrollHeight !== 0) { el.style.height = "".concat(el.scrollHeight, "px"); } el.style.overflow = 'hidden'; } }, { key: "leave", value: function leave() { var _this3 = this; var el = this.collapseWrap.current; var tempHeight = el.scrollHeight; var tempPaddingTop = el.dataset.oldPaddingTop; var tempPaddingBottom = el.dataset.oldPaddingBottom; function step(context) { tempHeight = Math.max(0, tempHeight -= el.scrollHeight * percent); tempPaddingTop = Math.max(0, tempPaddingTop -= el.dataset.oldPaddingTop * percent); tempPaddingBottom = Math.max(0, tempPaddingBottom -= el.dataset.oldPaddingBottom * percent); el.style.height = "".concat(tempHeight, "px"); el.style.paddingTop = tempPaddingTop; el.style.paddingBottom = tempPaddingBottom; if (tempHeight !== 0) { requestAnimationFrame(function () { step(context); }); } else { context.afterLeave(); } } requestAnimationFrame(function () { step(_this3); }); } }, { key: "afterLeave", value: function afterLeave() { var el = this.collapseWrap.current; if (!el) return; el.style.display = 'none'; el.style.height = ''; el.style.overflow = el.dataset.oldOverflow; el.style.paddingTop = el.dataset.oldPaddingTop; el.style.paddingBottom = el.dataset.oldPaddingBottom; } }, { key: "render", value: function render() { var children = this.props.children; return _react.default.createElement("div", { className: "collapse-transition", ref: this.collapseWrap, style: { overflow: 'hidden' } }, children); } }]); return CollapseTransition; }(_react.Component); exports.default = CollapseTransition;