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.

160 lines (134 loc) 6.28 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 { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import * as React from "react"; import styled, { css } from "styled-components"; import { addScrollHandler, removeScrollHandler, getScrollingElement } from "../utils/scroll"; import defaultTheme from "../defaultTheme"; var StyledSticky = styled.div.withConfig({ displayName: "Sticky__StyledSticky", componentId: "sc-15w40gm-0" })([""]); var StyledStickyContent = styled.div.withConfig({ displayName: "Sticky__StyledStickyContent", componentId: "sc-15w40gm-1" })(["position:", ";", ";box-shadow:0 2px 20px 6px rgba(23,27,30,0.15);border-radius:", ";"], function (_ref) { var sticky = _ref.sticky; return sticky ? "fixed" : "relative"; }, function (_ref2) { var size = _ref2.size, initialWidth = _ref2.initialWidth; return css(["top:", ";width:", ";"], size.height && "".concat(size.height, "px"), size.width && !initialWidth && "".concat(size.width, "px") || "100%"); }, function (_ref3) { var theme = _ref3.theme; return theme.orbit.borderRadiusNormal; }); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledStickyContent.defaultProps = { theme: defaultTheme }; var Sticky = /*#__PURE__*/function (_React$Component) { _inherits(Sticky, _React$Component); var _super = _createSuper(Sticky); function Sticky() { var _this; _classCallCheck(this, Sticky); 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", { sticky: false, height: 0, initialWidth: true, initialTop: 0, width: 0 }); _defineProperty(_assertThisInitialized(_this), "content", /*#__PURE__*/React.createRef()); _defineProperty(_assertThisInitialized(_this), "sticky", /*#__PURE__*/React.createRef()); _defineProperty(_assertThisInitialized(_this), "handleTop", function () { if (_this.sticky.current) { var values = _this.sticky.current.getBoundingClientRect(); _this.setState({ initialTop: values.top }); } }); _defineProperty(_assertThisInitialized(_this), "stickyState", function (sticky, height, width) { _this.setState({ sticky: sticky, height: height, width: width }); }); _defineProperty(_assertThisInitialized(_this), "handleScroll", function () { var element = _this.content.current; var sticky = _this.sticky.current; var elementHeight = element.offsetHeight; // $FlowFixMe var parent = sticky.parentNode.getBoundingClientRect(); var scrollingElement = getScrollingElement().getBoundingClientRect(); var _this$props$offset = _this.props.offset, offset = _this$props$offset === void 0 ? 0 : _this$props$offset; var initialTop = _this.state.initialTop; _this.setState({ initialWidth: false }); // if (sets fixed position if window with offset reaches elements and current position is not on the bottom of parent element) if (Math.abs(scrollingElement.top) + offset >= initialTop && parent.bottom - elementHeight - offset >= 0) { _this.stickyState(true, offset, parent.width); // turns off fixed if it's on the bottom of parent's element } else if (parent.bottom - elementHeight - offset <= 0) { _this.stickyState(false, parent.height - elementHeight, parent.width); } else { // just off fixed _this.stickyState(false, 0, parent.width); } }); return _this; } _createClass(Sticky, [{ key: "componentDidMount", value: function componentDidMount() { this.handleTop(); addScrollHandler(this.handleScroll); window.addEventListener("resize", this.handleTop); window.addEventListener("resize", this.handleScroll); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener("resize", this.handleTop); window.removeEventListener("resize", this.handleScroll); removeScrollHandler(this.handleScroll); } }, { key: "render", value: function render() { var _this$props = this.props, children = _this$props.children, dataTest = _this$props.dataTest; var _this$state = this.state, sticky = _this$state.sticky, height = _this$state.height, width = _this$state.width, initialWidth = _this$state.initialWidth; return /*#__PURE__*/React.createElement(StyledSticky, { ref: this.sticky, "data-test": dataTest }, /*#__PURE__*/React.createElement(StyledStickyContent, { sticky: sticky, size: { height: height, width: width }, initialWidth: initialWidth, ref: this.content }, children)); } }]); return Sticky; }(React.Component); export default Sticky;