@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.
190 lines (167 loc) • 7.15 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
import * as React from "react";
import styled, { css } from "styled-components";
import defaultTheme from "../../../../defaultTheme";
import CardSectionContext from "../CardSectionContext";
import Slide, { StyledSlide } from "../../../../utils/Slide";
import media from "../../../../utils/mediaQuery";
var hasPaddingTop = function hasPaddingTop(_ref) {
var expandable = _ref.expandable,
expanded = _ref.expanded,
visible = _ref.visible;
return expanded || visible || !expandable;
};
export var StyledCardSectionContent = styled.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;
}, media.desktop(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;
})), StyledSlide, function (_ref11) {
var expandable = _ref11.expandable,
visible = _ref11.visible;
return expandable && visible && "none";
});
StyledCardSectionContent.defaultProps = {
theme: defaultTheme
};
var withConsumer = function withConsumer(CardSection) {
return function (_ref12) {
var children = _ref12.children,
visible = _ref12.visible;
return /*#__PURE__*/React.createElement(CardSectionContext.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) {
_inherits(CardSectionContent, _React$Component);
var _super = _createSuper(CardSectionContent);
function CardSectionContent() {
var _this;
_classCallCheck(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));
_defineProperty(_assertThisInitialized(_this), "state", {
contentHeight: 0
});
_defineProperty(_assertThisInitialized(_this), "node", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_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;
}
_createClass(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, {
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);
DecoratedComponent.displayName = "CardSectionContent";
export default DecoratedComponent;