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.

123 lines (106 loc) 4.95 kB
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 Slide from "../../../utils/Slide"; import defaultTheme from "../../../defaultTheme"; var StyledTileExpandable = styled.div.withConfig({ displayName: "TileExpandable__StyledTileExpandable", componentId: "sc-19849bk-0" })(["border-top:", ";padding:", ";margin:0 ", ";", ";font-size:", ";line-height:", ";color:", ";"], function (_ref) { var theme = _ref.theme, expanded = _ref.expanded; return expanded ? "1px solid ".concat(theme.orbit.paletteCloudNormal) : "0px solid ".concat(theme.orbit.paletteCloudNormal); }, function (_ref2) { var theme = _ref2.theme, expanded = _ref2.expanded; return expanded && "".concat(theme.orbit.spaceMedium, " 0"); }, function (_ref3) { var theme = _ref3.theme; return theme.orbit.spaceMedium; }, function (_ref4) { var initialExpanded = _ref4.initialExpanded, theme = _ref4.theme; return !initialExpanded && css(["transition:max-height ", " ease-in-out,padding ", " ease-in-out,border-top ", " ease-in-out;"], theme.orbit.durationFast, theme.orbit.durationFast, theme.orbit.durationFast); }, function (_ref5) { var theme = _ref5.theme; return theme.orbit.fontSizeTextNormal; }, function (_ref6) { var theme = _ref6.theme; return theme.orbit.lineHeightTextNormal; }, function (_ref7) { var theme = _ref7.theme; return theme.orbit.colorTextPrimary; }); StyledTileExpandable.defaultProps = { theme: defaultTheme }; var TileExpandable = /*#__PURE__*/function (_React$PureComponent) { _inherits(TileExpandable, _React$PureComponent); var _super = _createSuper(TileExpandable); function TileExpandable(props) { var _this; _classCallCheck(this, TileExpandable); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "setHeight", function () { var _this$node$current; var contentHeight = (_this$node$current = _this.node.current) === null || _this$node$current === void 0 ? void 0 : _this$node$current.clientHeight; _this.setState({ contentHeight: contentHeight }); }); _this.node = /*#__PURE__*/React.createRef(); _this.state = { contentHeight: 0 }; return _this; } _createClass(TileExpandable, [{ key: "componentDidMount", value: function componentDidMount() { this.timeout = setTimeout(this.setHeight, 250); // Prevent showing children on initial render window.addEventListener("resize", this.setHeight); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { // Calculate height on expand - for dynamic components like TripSector this.setHeight(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.timeout) { clearTimeout(this.timeout); } window.removeEventListener("resize", this.setHeight); } }, { key: "render", value: function render() { var _this$props = this.props, expanded = _this$props.expanded, children = _this$props.children, initialExpanded = _this$props.initialExpanded; return /*#__PURE__*/React.createElement(StyledTileExpandable, { expanded: expanded, contentHeight: this.state.contentHeight, initialExpanded: initialExpanded }, /*#__PURE__*/React.createElement(Slide, { maxHeight: this.state.contentHeight, expanded: expanded }, /*#__PURE__*/React.createElement("div", { ref: this.node }, children))); } }]); return TileExpandable; }(React.PureComponent); export default TileExpandable;