@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.
79 lines (75 loc) • 2.87 kB
JavaScript
import React, { useState, useEffect, useCallback } from "react";
import styled from "styled-components";
import defaultTheme from "../defaultTheme";
import MenuHamburger from "../icons/MenuHamburger";
import ButtonLink from "../ButtonLink";
import useStateWithCallback from "../hooks/useStateWithCallback";
import useTranslate from "../hooks/useTranslate";
const NAVBAR_HEIGHT = 64;
const StyledNavigationBarContent = styled.div.withConfig({
displayName: "NavigationBar__StyledNavigationBarContent",
componentId: "sc-17lan40-0"
})(["display:block;width:100%;margin-right:", ";"], ({
theme
}) => theme.orbit.spaceXXSmall);
StyledNavigationBarContent.defaultProps = {
theme: defaultTheme
};
const 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 ", " ease-in-out;transform:translate3d(0,", ",0);"], NAVBAR_HEIGHT, ({
theme
}) => theme.orbit.paletteWhite, ({
theme
}) => theme.orbit.boxShadowFixed, ({
theme
}) => theme.orbit.spaceSmall, ({
theme
}) => theme.orbit.durationNormal, ({
shown
}) => shown ? "0" : `-${NAVBAR_HEIGHT}px`);
StyledNavigationBar.defaultProps = {
theme: defaultTheme
};
const NavigationBar = ({
onMenuOpen,
children,
dataTest,
onShow,
onHide
}) => {
const translate = useTranslate();
const resolveCallback = useCallback(state => {
if (onHide && !state) onHide();
if (onShow && state) onShow();
}, [onHide, onShow]);
const [shown, setShown] = useStateWithCallback(true, resolveCallback);
const [prevScrollPosition, setPrevScrollPosition] = useState(0);
const handleNavigationBarPosition = useCallback(() => {
const currentScrollPosition = window.scrollY || window.pageYOffset || document.documentElement && document.documentElement.scrollTop;
if (prevScrollPosition < currentScrollPosition && currentScrollPosition > NAVBAR_HEIGHT) {
setShown(false);
} else {
setShown(true);
}
setPrevScrollPosition(currentScrollPosition);
}, [prevScrollPosition, setShown]);
useEffect(() => {
window.addEventListener("scroll", handleNavigationBarPosition);
return () => {
window.removeEventListener("scroll", handleNavigationBarPosition);
};
});
return React.createElement(StyledNavigationBar, {
"data-test": dataTest,
shown: shown
}, React.createElement(StyledNavigationBarContent, null, children), onMenuOpen && React.createElement(ButtonLink, {
type: "secondary",
onClick: onMenuOpen,
iconLeft: React.createElement(MenuHamburger, null),
transparent: true,
title: translate("navigationbar_open_menu")
}));
};
export default NavigationBar;