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.

81 lines (75 loc) 3.44 kB
import * as React from "react"; import styled, { css } from "styled-components"; import { right } from "../utils/rtl"; 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"; const NAVBAR_HEIGHT = { MOBILE: 52, DESKTOP: 64 }; const StyledNavigationBarContent = styled.div.withConfig({ displayName: "NavigationBar__StyledNavigationBarContent", componentId: "sc-xlfrap-0" })(["", ""], ({ theme }) => css(["display:block;width:100%;margin-", ":", ";"], right, theme.orbit.spaceXSmall)); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledNavigationBarContent.defaultProps = { theme: defaultTheme }; const StyledNavigationBar = styled.nav.withConfig({ displayName: "NavigationBar__StyledNavigationBar", componentId: "sc-xlfrap-1" })(["", ""], ({ theme, shown }) => css(["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, theme.orbit.paletteWhite, theme.orbit.boxShadowFixed, theme.orbit.spaceSmall, transition(["transform"], "normal", "ease-in-out"), shown ? "0" : `-${NAVBAR_HEIGHT.MOBILE}px`, mq.tablet(css(["height:", "px;transform:translate3d(0,", ",0);"], NAVBAR_HEIGHT.DESKTOP, shown ? "0" : `-${NAVBAR_HEIGHT.DESKTOP}px`)))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198 StyledNavigationBar.defaultProps = { theme: defaultTheme }; const NavigationBar = ({ onMenuOpen, children, dataTest, onShow, onHide, hideOnScroll = true }) => { const translate = useTranslate(); const resolveCallback = React.useCallback(state => { if (onHide && !state) onHide(); if (onShow && state) onShow(); }, [onHide, onShow]); const [shown, setShown] = useStateWithCallback(true, resolveCallback); const [prevScrollPosition, setPrevScrollPosition] = React.useState(0); const handleNavigationBarPosition = React.useCallback(() => { const currentScrollPosition = window.scrollY || window.pageYOffset || document.documentElement && document.documentElement.scrollTop; if (!hideOnScroll) return; if (prevScrollPosition < currentScrollPosition && currentScrollPosition > NAVBAR_HEIGHT.DESKTOP) { setShown(false); } else { setShown(true); } setPrevScrollPosition(currentScrollPosition); }, [prevScrollPosition, setShown, hideOnScroll]); React.useEffect(() => { window.addEventListener("scroll", handleNavigationBarPosition); return () => { 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;