react-collapse
Version:
Component-wrapper for collapse animation for elements with variable (and dynamic) height
536 lines (434 loc) • 23.4 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("React"));
else if(typeof define === 'function' && define.amd)
define(["React"], factory);
else if(typeof exports === 'object')
exports["ReactCollapse"] = factory(require("React"));
else
root["ReactCollapse"] = factory(root["React"]);
})(self, function(__WEBPACK_EXTERNAL_MODULE__24__) {
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({
/***/ 920:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
;
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "Collapse": () => (/* binding */ Collapse)
/* harmony export */ });
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(24);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
function _typeof(obj) { "@babel/helpers - typeof"; 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 _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); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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; }
var Collapse = /*#__PURE__*/function (_React$Component) {
_inherits(Collapse, _React$Component);
var _super = _createSuper(Collapse);
function Collapse(props) {
var _this;
_classCallCheck(this, Collapse);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "timeout", undefined);
_defineProperty(_assertThisInitialized(_this), "container", undefined);
_defineProperty(_assertThisInitialized(_this), "content", undefined);
_defineProperty(_assertThisInitialized(_this), "onResize", function () {
clearTimeout(_this.timeout);
if (!_this.container || !_this.content) {
return;
}
var _this$props = _this.props,
isOpened = _this$props.isOpened,
checkTimeout = _this$props.checkTimeout;
var containerHeight = Math.floor(_this.container.clientHeight);
var contentHeight = Math.floor(_this.content.clientHeight);
var isFullyOpened = isOpened && Math.abs(contentHeight - containerHeight) <= 1;
var isFullyClosed = !isOpened && Math.abs(containerHeight) <= 1;
if (isFullyOpened || isFullyClosed) {
_this.onRest({
isFullyOpened: isFullyOpened,
isFullyClosed: isFullyClosed,
isOpened: isOpened,
containerHeight: containerHeight,
contentHeight: contentHeight
});
} else {
_this.onWork({
isFullyOpened: isFullyOpened,
isFullyClosed: isFullyClosed,
isOpened: isOpened,
containerHeight: containerHeight,
contentHeight: contentHeight
});
_this.timeout = setTimeout(function () {
return _this.onResize();
}, checkTimeout);
}
});
_defineProperty(_assertThisInitialized(_this), "onRest", function (_ref) {
var isFullyOpened = _ref.isFullyOpened,
isFullyClosed = _ref.isFullyClosed,
isOpened = _ref.isOpened,
containerHeight = _ref.containerHeight,
contentHeight = _ref.contentHeight;
if (!_this.container || !_this.content) {
return;
}
var hasOpened = isOpened && _this.container.style.height === "".concat(contentHeight, "px");
var hasClosed = !isOpened && _this.container.style.height === '0px';
if (hasOpened || hasClosed) {
_this.container.style.overflow = isOpened ? 'initial' : 'hidden';
_this.container.style.height = isOpened ? 'auto' : '0px';
var onRest = _this.props.onRest;
if (onRest) {
onRest({
isFullyOpened: isFullyOpened,
isFullyClosed: isFullyClosed,
isOpened: isOpened,
containerHeight: containerHeight,
contentHeight: contentHeight
});
}
}
});
_defineProperty(_assertThisInitialized(_this), "onWork", function (_ref2) {
var isFullyOpened = _ref2.isFullyOpened,
isFullyClosed = _ref2.isFullyClosed,
isOpened = _ref2.isOpened,
containerHeight = _ref2.containerHeight,
contentHeight = _ref2.contentHeight;
if (!_this.container || !_this.content) {
return;
}
var isOpenining = isOpened && _this.container.style.height === "".concat(contentHeight, "px");
var isClosing = !isOpened && _this.container.style.height === '0px';
if (isOpenining || isClosing) {
// No need to do any work
return;
}
_this.container.style.overflow = 'hidden';
_this.container.style.height = isOpened ? "".concat(contentHeight, "px") : '0px';
var onWork = _this.props.onWork;
if (onWork) {
onWork({
isFullyOpened: isFullyOpened,
isFullyClosed: isFullyClosed,
isOpened: isOpened,
containerHeight: containerHeight,
contentHeight: contentHeight
});
}
});
_defineProperty(_assertThisInitialized(_this), "onRefContainer", function (container) {
_this.container = container;
});
_defineProperty(_assertThisInitialized(_this), "onRefContent", function (content) {
_this.content = content;
});
if (props.initialStyle) {
_this.initialStyle = props.initialStyle;
} else {
_this.initialStyle = props.isOpened ? {
height: 'auto',
overflow: 'initial'
} : {
height: '0px',
overflow: 'hidden'
};
}
return _this;
}
_createClass(Collapse, [{
key: "componentDidMount",
value: function componentDidMount() {
this.onResize();
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps) {
var _this$props2 = this.props,
theme = _this$props2.theme,
isOpened = _this$props2.isOpened,
children = _this$props2.children;
return children !== nextProps.children || isOpened !== nextProps.isOpened || Object.keys(theme).some(function (c) {
return theme[c] !== nextProps.theme[c];
});
}
}, {
key: "getSnapshotBeforeUpdate",
value: function getSnapshotBeforeUpdate() {
if (!this.container || !this.content) {
return null;
}
if (this.container.style.height === 'auto') {
var contentHeight = this.content.clientHeight;
this.container.style.height = "".concat(contentHeight, "px");
}
return null;
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.onResize();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
clearTimeout(this.timeout);
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
theme = _this$props3.theme,
children = _this$props3.children,
isOpened = _this$props3.isOpened;
return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
ref: this.onRefContainer,
className: theme.collapse,
style: this.initialStyle,
"aria-hidden": !isOpened
}, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement("div", {
ref: this.onRefContent,
className: theme.content
}, children));
}
}]);
return Collapse;
}((react__WEBPACK_IMPORTED_MODULE_0___default().Component));
_defineProperty(Collapse, "defaultProps", {
theme: {
collapse: 'ReactCollapse--collapse',
content: 'ReactCollapse--content'
},
initialStyle: undefined,
onRest: undefined,
onWork: undefined,
checkTimeout: 50
});
/***/ }),
/***/ 98:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
;
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "UnmountClosed": () => (/* binding */ UnmountClosed)
/* harmony export */ });
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(24);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _Collapse__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(920);
function _typeof(obj) { "@babel/helpers - typeof"; 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); }
var _excluded = ["isOpened"],
_excluded2 = ["isOpened"];
function _extends() { _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; }; return _extends.apply(this, arguments); }
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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 _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); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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; }
var UnmountClosed = /*#__PURE__*/function (_React$PureComponent) {
_inherits(UnmountClosed, _React$PureComponent);
var _super = _createSuper(UnmountClosed);
function UnmountClosed(props) {
var _this;
_classCallCheck(this, UnmountClosed);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onWork", function (_ref) {
var isOpened = _ref.isOpened,
rest = _objectWithoutProperties(_ref, _excluded);
_this.setState({
isResting: false,
isOpened: isOpened
});
var onWork = _this.props.onWork;
if (onWork) {
onWork(_objectSpread({
isOpened: isOpened
}, rest));
}
});
_defineProperty(_assertThisInitialized(_this), "onRest", function (_ref2) {
var isOpened = _ref2.isOpened,
rest = _objectWithoutProperties(_ref2, _excluded2);
_this.setState({
isResting: true,
isOpened: isOpened,
isInitialRender: false
});
var onRest = _this.props.onRest;
if (onRest) {
onRest(_objectSpread({
isOpened: isOpened
}, rest));
}
});
_defineProperty(_assertThisInitialized(_this), "getInitialStyle", function () {
var _this$state = _this.state,
isOpened = _this$state.isOpened,
isInitialRender = _this$state.isInitialRender;
if (isInitialRender) {
return isOpened ? {
height: 'auto',
overflow: 'initial'
} : {
height: '0px',
overflow: 'hidden'
};
}
return {
height: '0px',
overflow: 'hidden'
};
});
_this.state = {
isResting: true,
isOpened: props.isOpened,
isInitialRender: true
};
return _this;
}
_createClass(UnmountClosed, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var isOpened = this.props.isOpened;
if (prevProps.isOpened !== isOpened) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({
isResting: false,
isOpened: isOpened,
isInitialRender: false
});
}
}
}, {
key: "render",
value: function render() {
var _this$state2 = this.state,
isResting = _this$state2.isResting,
isOpened = _this$state2.isOpened;
return isResting && !isOpened ? null : /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default().createElement(_Collapse__WEBPACK_IMPORTED_MODULE_1__.Collapse, _extends({}, this.props, {
initialStyle: this.getInitialStyle(),
onWork: this.onWork,
onRest: this.onRest
}));
}
}]);
return UnmountClosed;
}((react__WEBPACK_IMPORTED_MODULE_0___default().PureComponent));
_defineProperty(UnmountClosed, "defaultProps", {
onWork: undefined,
onRest: undefined
});
/***/ }),
/***/ 506:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
var _require = __webpack_require__(920),
Collapse = _require.Collapse;
var _require2 = __webpack_require__(98),
UnmountClosed = _require2.UnmountClosed; // Default export
module.exports = UnmountClosed; // Extra "named exports"
UnmountClosed.Collapse = Collapse;
UnmountClosed.UnmountClosed = UnmountClosed;
/***/ }),
/***/ 24:
/***/ ((module) => {
;
module.exports = __WEBPACK_EXTERNAL_MODULE__24__;
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => (module['default']) :
/******/ () => (module);
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __webpack_require__(506);
/******/
/******/ return __webpack_exports__;
/******/ })()
;
});