UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

243 lines (193 loc) 8.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.StyledSlide = void 0; var _react = _interopRequireDefault(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../../defaultTheme")); 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 _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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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 _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 getMaxHeight = function getMaxHeight(_ref) { var maxHeight = _ref.maxHeight; if (maxHeight === 0) return "0px"; if (!maxHeight) return undefined; return "".concat(maxHeight, "px"); }; var StyledSlide = _styledComponents.default.div.withConfig({ displayName: "Slide__StyledSlide", componentId: "sc-1xbmdp2-0" })(["position:relative;width:100%;transition:max-height ", " linear;max-height:", ";overflow:", ";visibility:", ";"], function (_ref2) { var theme = _ref2.theme; return theme.orbit.durationFast; }, getMaxHeight, function (_ref3) { var transitionFinished = _ref3.transitionFinished; return !transitionFinished && "hidden"; }, function (_ref4) { var visible = _ref4.visible; return !visible && "hidden"; }); exports.StyledSlide = StyledSlide; StyledSlide.defaultProps = { theme: _defaultTheme.default }; var Slide = /*#__PURE__*/ function (_React$Component) { _inherits(Slide, _React$Component); function Slide() { var _getPrototypeOf2; var _this; _classCallCheck(this, Slide); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Slide)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_this), "state", { maxHeight: 0, transitionFinished: false, visible: false }); _defineProperty(_assertThisInitialized(_this), "expandTimeout", null); _defineProperty(_assertThisInitialized(_this), "collapseTimeout", null); _defineProperty(_assertThisInitialized(_this), "transitionFinishedTimeout", null); _defineProperty(_assertThisInitialized(_this), "visibleTimeout", null); _defineProperty(_assertThisInitialized(_this), "setVisible", function (visible) { return function () { _this.setState({ visible: visible }); }; }); _defineProperty(_assertThisInitialized(_this), "setMaxHeight", function () { var maxHeight = _this.props.maxHeight; _this.setState({ maxHeight: maxHeight }); }); _defineProperty(_assertThisInitialized(_this), "expandCallback", function () { _this.setState({ maxHeight: null }); _this.transitionFinishedTimeout = setTimeout(_this.transitionFinishedCallback(true), 100); }); _defineProperty(_assertThisInitialized(_this), "collapseCallback", function () { _this.setState({ maxHeight: 0, transitionFinished: false }); _this.visibleTimeout = setTimeout(_this.setVisible(false), 150); if (_this.transitionFinishedTimeout && typeof clearTimeout === "function") { clearTimeout(_this.transitionFinishedTimeout); _this.transitionFinishedTimeout = null; } }); _defineProperty(_assertThisInitialized(_this), "transitionFinishedCallback", function (transitionFinished) { return function () { _this.setState({ transitionFinished: transitionFinished }); }; }); return _this; } _createClass(Slide, [{ key: "componentDidMount", value: function componentDidMount() { this.setMaxHeight(); if (this.props.expanded) { this.setState({ transitionFinished: true, visible: true }); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState, snapshot) { if (snapshot) { if (this.props.expanded) { this.setMaxHeight(); if (typeof setTimeout === "function") { if (this.visibleTimeout && typeof clearTimeout === "function") { clearTimeout(this.visibleTimeout); this.visibleTimeout = null; } this.setVisible(true)(); this.expandTimeout = setTimeout(this.expandCallback, 150); } } else { if (this.state.maxHeight !== this.props.maxHeight) { this.setMaxHeight(); } if (typeof setTimeout === "function") { if (this.expandTimeout && typeof clearTimeout === "function") { clearTimeout(this.expandTimeout); this.expandTimeout = null; } this.collapseTimeout = setTimeout(this.collapseCallback, 1); } } } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (typeof clearTimeout === "function") { if (this.expandTimeout) { clearTimeout(this.expandTimeout); } if (this.collapseTimeout) { clearTimeout(this.collapseTimeout); } if (this.transitionFinishedTimeout) { clearTimeout(this.transitionFinishedTimeout); } if (this.visibleTimeout) { clearTimeout(this.visibleTimeout); } } } }, { key: "getSnapshotBeforeUpdate", value: function getSnapshotBeforeUpdate(prevProps) { if (this.props.expanded === prevProps.expanded) return null; return true; } }, { key: "render", value: function render() { var _this$props = this.props, children = _this$props.children, _this$props$expanded = _this$props.expanded, expanded = _this$props$expanded === void 0 ? false : _this$props$expanded, id = _this$props.id, ariaLabelledBy = _this$props.ariaLabelledBy; var _this$state = this.state, transitionFinished = _this$state.transitionFinished, maxHeight = _this$state.maxHeight, visible = _this$state.visible; return _react.default.createElement(StyledSlide, { maxHeight: maxHeight, expanded: expanded, transitionFinished: transitionFinished, "aria-hidden": !expanded, id: id, "aria-labelledby": ariaLabelledBy, visible: visible }, children); } }]); return Slide; }(_react.default.Component); var _default = Slide; exports.default = _default;