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.

212 lines (176 loc) 8.25 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.StyledCardSectionContent = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var React = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _defaultTheme = _interopRequireDefault(require("../../../../defaultTheme")); var _CardSectionContext = _interopRequireDefault(require("../CardSectionContext")); var _Slide = _interopRequireWildcard(require("../../../../utils/Slide")); var _mediaQuery = _interopRequireDefault(require("../../../../utils/mediaQuery")); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; } 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; } } var hasPaddingTop = function hasPaddingTop(_ref) { var expandable = _ref.expandable, expanded = _ref.expanded, visible = _ref.visible; return expanded || visible || !expandable; }; var StyledCardSectionContent = _styledComponents.default.div.withConfig({ displayName: "CardSectionContent__StyledCardSectionContent", componentId: "ki0e2d-0" })(["font-family:", ";font-size:", ";line-height:", ";color:", ";border-top:", ";padding-top:", ";transition:padding ", " linear,border-top ", " linear;", " ", "{max-height:", ";}"], function (_ref2) { var theme = _ref2.theme; return theme.orbit.fontFamily; }, function (_ref3) { var theme = _ref3.theme; return theme.orbit.fontSizeTextNormal; }, function (_ref4) { var theme = _ref4.theme; return theme.orbit.lineHeightTextNormal; }, function (_ref5) { var theme = _ref5.theme; return theme.orbit.colorTextPrimary; }, function (_ref6) { var theme = _ref6.theme, expanded = _ref6.expanded; return expanded ? "1px solid ".concat(theme.orbit.paletteCloudNormal) : "0px solid ".concat(theme.orbit.paletteCloudNormal); }, function (_ref7) { var theme = _ref7.theme, expandable = _ref7.expandable, expanded = _ref7.expanded, visible = _ref7.visible; return hasPaddingTop({ expandable: expandable, expanded: expanded, visible: visible }) && theme.orbit.spaceMedium; }, function (_ref8) { var theme = _ref8.theme; return theme.orbit.durationFast; }, function (_ref9) { var theme = _ref9.theme; return theme.orbit.durationFast; }, _mediaQuery.default.desktop((0, _styledComponents.css)(["padding-top:", ";"], function (_ref10) { var theme = _ref10.theme, expandable = _ref10.expandable, expanded = _ref10.expanded, visible = _ref10.visible; return hasPaddingTop({ expandable: expandable, expanded: expanded, visible: visible }) && theme.orbit.spaceLarge; })), _Slide.StyledSlide, function (_ref11) { var expandable = _ref11.expandable, visible = _ref11.visible; return expandable && visible && "none"; }); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 exports.StyledCardSectionContent = StyledCardSectionContent; StyledCardSectionContent.defaultProps = { theme: _defaultTheme.default }; var withConsumer = function withConsumer(CardSection) { return function (_ref12) { var children = _ref12.children, visible = _ref12.visible; return /*#__PURE__*/React.createElement(_CardSectionContext.default.Consumer, null, function (_ref13) { var expandable = _ref13.expandable, expanded = _ref13.expanded; return /*#__PURE__*/React.createElement(CardSection, { expandable: expandable, expanded: expanded, visible: visible }, children); }); }; }; var CardSectionContent = /*#__PURE__*/function (_React$Component) { (0, _inherits2.default)(CardSectionContent, _React$Component); var _super = _createSuper(CardSectionContent); function CardSectionContent() { var _this; (0, _classCallCheck2.default)(this, CardSectionContent); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", { contentHeight: 0 }); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "node", /*#__PURE__*/React.createRef()); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setHeight", function () { var _this$node$current; _this.setState({ contentHeight: (_this$node$current = _this.node.current) === null || _this$node$current === void 0 ? void 0 : _this$node$current.clientHeight }); }); return _this; } (0, _createClass2.default)(CardSectionContent, [{ key: "componentDidMount", value: function componentDidMount() { var expandable = this.props.expandable; if (expandable) { window.addEventListener("resize", this.setHeight); setTimeout(this.setHeight, 10); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { var expandable = this.props.expandable; if (expandable) { if (prevState.contentHeight !== this.state.contentHeight || prevProps.children !== this.props.children) { setTimeout(this.setHeight, 10); } if (prevProps.expanded !== this.props.expanded) { // Calculate height on expand - for dynamic components like TripSector this.setHeight(); } } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener("resize", this.setHeight); } }, { key: "render", value: function render() { var _this$props = this.props, children = _this$props.children, expanded = _this$props.expanded, expandable = _this$props.expandable, visible = _this$props.visible; return /*#__PURE__*/React.createElement(StyledCardSectionContent, { expanded: expanded, expandable: expandable, visible: visible, contentHeight: this.state.contentHeight }, expandable ? /*#__PURE__*/React.createElement(_Slide.default, { maxHeight: this.state.contentHeight, expanded: expanded }, /*#__PURE__*/React.createElement("div", { ref: this.node }, children)) : /*#__PURE__*/React.createElement("div", { ref: this.node }, children)); } }]); return CardSectionContent; }(React.Component); var DecoratedComponent = withConsumer(CardSectionContent); // $FlowFixMe DecoratedComponent.displayName = "CardSectionContent"; var _default = DecoratedComponent; exports.default = _default;