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.

100 lines (91 loc) 4.12 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import * as React from "react"; import styled, { css } from "styled-components"; import transition from "../utils/transition"; import defaultTheme from "../defaultTheme"; import MenuHamburger from "../icons/MenuHamburger"; import ButtonLink from "../ButtonLink"; import useStateWithCallback from "../hooks/useStateWithCallback"; import useTranslate from "../hooks/useTranslate"; import mq from "../utils/mediaQuery"; var NAVBAR_HEIGHT = { MOBILE: 52, DESKTOP: 64 }; var StyledNavigationBarContent = styled.div.withConfig({ displayName: "NavigationBar__StyledNavigationBarContent", componentId: "sc-17lan40-0" })(["display:block;width:100%;margin-right:", ";"], function (_ref) { var theme = _ref.theme; return theme.orbit.spaceXSmall; }); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledNavigationBarContent.defaultProps = { theme: defaultTheme }; var StyledNavigationBar = styled.nav.withConfig({ displayName: "NavigationBar__StyledNavigationBar", componentId: "sc-17lan40-1" })(["position:fixed;top:0;left:0;right:0;height:", "px;width:100%;display:flex;align-items:center;background:", ";box-shadow:", ";padding:", ";box-sizing:border-box;z-index:700;transition:", ";transform:translate3d(0,", ",0);", ";"], NAVBAR_HEIGHT.MOBILE, function (_ref2) { var theme = _ref2.theme; return theme.orbit.paletteWhite; }, function (_ref3) { var theme = _ref3.theme; return theme.orbit.boxShadowFixed; }, function (_ref4) { var theme = _ref4.theme; return theme.orbit.spaceSmall; }, transition(["transform"], "normal", "ease-in-out"), function (_ref5) { var shown = _ref5.shown; return shown ? "0" : "-".concat(NAVBAR_HEIGHT.MOBILE, "px"); }, mq.tablet(css(["height:", "px;transform:translate3d(0,", ",0);"], NAVBAR_HEIGHT.DESKTOP, function (_ref6) { var shown = _ref6.shown; return shown ? "0" : "-".concat(NAVBAR_HEIGHT.DESKTOP, "px"); }))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledNavigationBar.defaultProps = { theme: defaultTheme }; var NavigationBar = function NavigationBar(_ref7) { var onMenuOpen = _ref7.onMenuOpen, children = _ref7.children, dataTest = _ref7.dataTest, onShow = _ref7.onShow, onHide = _ref7.onHide; var translate = useTranslate(); var resolveCallback = React.useCallback(function (state) { if (onHide && !state) onHide(); if (onShow && state) onShow(); }, [onHide, onShow]); var _useStateWithCallback = useStateWithCallback(true, resolveCallback), _useStateWithCallback2 = _slicedToArray(_useStateWithCallback, 2), shown = _useStateWithCallback2[0], setShown = _useStateWithCallback2[1]; var _React$useState = React.useState(0), _React$useState2 = _slicedToArray(_React$useState, 2), prevScrollPosition = _React$useState2[0], setPrevScrollPosition = _React$useState2[1]; var handleNavigationBarPosition = React.useCallback(function () { var currentScrollPosition = window.scrollY || window.pageYOffset || document.documentElement && document.documentElement.scrollTop; if (prevScrollPosition < currentScrollPosition && currentScrollPosition > NAVBAR_HEIGHT.DESKTOP) { setShown(false); } else { setShown(true); } setPrevScrollPosition(currentScrollPosition); }, [prevScrollPosition, setShown]); React.useEffect(function () { window.addEventListener("scroll", handleNavigationBarPosition); return function () { window.removeEventListener("scroll", handleNavigationBarPosition); }; }); return /*#__PURE__*/React.createElement(StyledNavigationBar, { "data-test": dataTest, shown: shown }, /*#__PURE__*/React.createElement(StyledNavigationBarContent, null, children), onMenuOpen && /*#__PURE__*/React.createElement(ButtonLink, { type: "secondary", onClick: onMenuOpen, iconLeft: /*#__PURE__*/React.createElement(MenuHamburger, null), title: translate("navigationbar_open_menu") })); }; export default NavigationBar;