diff --git a/polsevev.dev.frontend/src/Hooks/useComponentVisible.tsx b/polsevev.dev.frontend/src/Hooks/useComponentVisible.tsx new file mode 100644 index 0000000..03b9f97 --- /dev/null +++ b/polsevev.dev.frontend/src/Hooks/useComponentVisible.tsx @@ -0,0 +1,24 @@ +import { useEffect, useRef, useState } from "react"; + +export default function useComponentVisible(initialIsVisible: boolean) { + const [isCompVisible, setIsCompVisible] = + useState(initialIsVisible); + + const ref = useRef(null); + const handleClickOutsideComp = (event: Event) => { + if (ref.current && !ref.current.contains(event.target as Node)) { + setIsCompVisible(false); + } + }; + + useEffect(() => { + document.addEventListener("click", handleClickOutsideComp, true); + return () => { + document.removeEventListener("click", handleClickOutsideComp, true); + }; + }); + + const openComp = () => setIsCompVisible(true); + + return { ref, isCompVisible, openComp }; +} diff --git a/polsevev.dev.frontend/src/components/NavigationMenu.tsx b/polsevev.dev.frontend/src/components/NavigationMenu.tsx index ed83c72..4a49f5f 100644 --- a/polsevev.dev.frontend/src/components/NavigationMenu.tsx +++ b/polsevev.dev.frontend/src/components/NavigationMenu.tsx @@ -1,6 +1,7 @@ -import { FC, useEffect, useState } from "react"; +import { FC, useEffect, useRef, useState } from "react"; import { NavItem, SideBarBox } from "../styles/TopBarStyle"; import { useNavigate } from "react-router"; +import useComponentVisible from "../Hooks/useComponentVisible"; const NavigationMenu: FC = () => { const navigate = useNavigate(); @@ -9,11 +10,12 @@ const NavigationMenu: FC = () => { const [isPhone, setIsPhone] = useState( window.matchMedia("(min-width: 720px)").matches ); - const [showNavButtonsPhone, setShowNavButtonsPhone] = useState(false); const locationButtons = locations.map((location) => ( handleClick(location)}>{location} )); + const { ref, isCompVisible, openComp } = useComponentVisible(false); + useEffect(() => { const recheckIsPhone = () => { setIsPhone(window.matchMedia("(min-width: 720px)").matches); @@ -28,15 +30,11 @@ const NavigationMenu: FC = () => { locationButtons ) : ( <> - - {showNavButtonsPhone && ( - {locationButtons} + {isCompVisible && ( + {locationButtons} )} )} diff --git a/polsevev.dev.frontend/src/styles/TopBarStyle.tsx b/polsevev.dev.frontend/src/styles/TopBarStyle.tsx index 35e17e1..854d00a 100644 --- a/polsevev.dev.frontend/src/styles/TopBarStyle.tsx +++ b/polsevev.dev.frontend/src/styles/TopBarStyle.tsx @@ -1,4 +1,4 @@ -import styled from "styled-components"; +import styled, { keyframes } from "styled-components"; export const Wrapper = styled("div")` position: fixed; @@ -34,11 +34,25 @@ export const GithubLogo = styled("img")(() => ({ padding: "5px", })); -export const SideBarBox = styled("div")((props) => ({ - position: "fixed", - top: "5em", - left: "calc(100% - 10em)", - overflow: "overlay", - backgroundColor: props.theme.palette.aero, - textAlign: "center", -})); +const comeFromLeft = keyframes` + from{ + left: 100%; + } + to { + left: calc(100% - 10em); + } +`; + +export const SideBarBox = styled("div")` + position: absolute; + top: 0; + animation: ${comeFromLeft} 0.2s linear; + left: calc(100% - 10em); + overflow: overlay; + background-color: ${(props) => props.theme.palette.aero}; + text-align: center; + border-radius: 25px; + border: 2px solid #ffffff; + padding-bottom: 5px; + padding-top: 5px; +`;