Skip to content

Commit 39331a9

Browse files
committed
feat: make mobile navbar close when clicking on header, make it occupy full height
1 parent a79e377 commit 39331a9

File tree

5 files changed

+43
-51
lines changed

5 files changed

+43
-51
lines changed

web/src/layout/Header/MobileHeader.tsx

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import React, { useContext, useMemo, useRef } from "react";
1+
import React, { useState } from "react";
22
import styled, { css } from "styled-components";
3-
4-
import { useClickAway, useToggle } from "react-use";
5-
63
import HamburgerIcon from "svgs/header/hamburger.svg";
74

85
import { landscapeStyle } from "styles/landscapeStyle";
@@ -37,30 +34,35 @@ const StyledLightButton = styled(LightButton)`
3734
}
3835
`;
3936

40-
const OpenContext = React.createContext({
41-
isOpen: false,
42-
toggleIsOpen: () => {
43-
// Placeholder
44-
},
45-
});
46-
47-
export function useOpenContext() {
48-
return useContext(OpenContext);
49-
}
37+
const Overlay = styled.div<{ isOpen: boolean }>`
38+
display: ${({ isOpen }) => (isOpen ? "block" : "none")};
39+
position: absolute;
40+
top: 0;
41+
width: 100vw;
42+
height: 64px;
43+
z-index: 1;
44+
`;
5045

5146
const MobileHeader = () => {
52-
const [isOpen, toggleIsOpen] = useToggle(false);
53-
const containerRef = useRef(null);
54-
useClickAway(containerRef, () => toggleIsOpen(false));
55-
const memoizedContext = useMemo(() => ({ isOpen, toggleIsOpen }), [isOpen, toggleIsOpen]);
47+
const [isOpen, setIsOpen] = useState(false);
48+
49+
const handleOpenNavbar = () => {
50+
setIsOpen(true);
51+
};
52+
53+
const handleCloseNavbar = () => {
54+
setIsOpen(false);
55+
};
56+
5657
return (
57-
<Container ref={containerRef}>
58-
<OpenContext.Provider value={memoizedContext}>
59-
<Logo />
60-
<NavBar />
61-
<StyledLightButton text="" Icon={HamburgerIcon} onClick={toggleIsOpen} />
62-
</OpenContext.Provider>
58+
<Container>
59+
{isOpen ? <Overlay {...{ isOpen }} onClick={handleCloseNavbar} /> : null}
60+
61+
<Logo />
62+
<StyledLightButton text="" Icon={HamburgerIcon} onClick={handleOpenNavbar} />
63+
<NavBar {...{ isOpen, handleCloseNavbar }} />
6364
</Container>
6465
);
6566
};
67+
6668
export default MobileHeader;

web/src/layout/Header/navbar/DappList.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { landscapeStyle } from "styles/landscapeStyle";
1515
import { responsiveSize } from "styles/responsiveSize";
1616

1717
import Product from "./Product";
18+
import { IDappList } from "./index";
1819

1920
const Header = styled.h1`
2021
padding-top: 32px;
@@ -136,10 +137,6 @@ const ITEMS = [
136137
},
137138
];
138139

139-
interface IDappList {
140-
toggleIsDappListOpen: () => void;
141-
}
142-
143140
const DappList: React.FC<IDappList> = ({ toggleIsDappListOpen }) => {
144141
const containerRef = useRef(null);
145142
useClickAway(containerRef, () => toggleIsDappListOpen());

web/src/layout/Header/navbar/Explore.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { Link, useLocation } from "react-router-dom";
66
import { landscapeStyle } from "styles/landscapeStyle";
77
import { responsiveSize } from "styles/responsiveSize";
88

9-
import { useOpenContext } from "../MobileHeader";
10-
119
const Container = styled.div`
1210
display: flex;
1311
gap: 0px;
@@ -59,9 +57,12 @@ const links = [
5957
{ to: "/get-pnk", text: "Get PNK" },
6058
];
6159

62-
const Explore: React.FC = () => {
60+
interface IExplore {
61+
handleCloseNavbar: () => void;
62+
}
63+
64+
const Explore: React.FC<IExplore> = ({ handleCloseNavbar }) => {
6365
const location = useLocation();
64-
const { toggleIsOpen } = useOpenContext();
6566

6667
return (
6768
<Container>
@@ -70,7 +71,7 @@ const Explore: React.FC = () => {
7071
<LinkContainer key={text}>
7172
<StyledLink
7273
to={to}
73-
onClick={toggleIsOpen}
74+
onClick={handleCloseNavbar}
7475
isActive={to === "/" ? location.pathname === "/" : location.pathname.startsWith(to)}
7576
>
7677
{text}

web/src/layout/Header/navbar/Menu/Help.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { landscapeStyle } from "styles/landscapeStyle";
1717

1818
import Onboarding from "components/Popup/MiniGuides/Onboarding";
1919

20-
import { IHelp } from "..";
20+
import { IHelp } from "../index";
2121
import Debug from "../Debug";
2222

2323
const Container = styled.div`

web/src/layout/Header/navbar/index.tsx

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ import { useAccount } from "wagmi";
66

77
import KlerosSolutionsIcon from "svgs/menu-icons/kleros-solutions.svg";
88

9-
import { useLockOverlayScroll } from "hooks/useLockOverlayScroll";
10-
119
import ConnectWallet from "components/ConnectWallet";
1210
import LightButton from "components/LightButton";
1311
import { Overlay } from "components/Overlay";
14-
15-
import { useOpenContext } from "../MobileHeader";
16-
1712
import DappList from "./DappList";
1813
import Explore from "./Explore";
1914
import Menu from "./Menu";
@@ -40,7 +35,7 @@ const Container = styled.div<{ isOpen: boolean }>`
4035
top: 0;
4136
left: 0;
4237
right: 0;
43-
max-height: calc(100vh - 160px);
38+
height: 100%;
4439
overflow-y: auto;
4540
z-index: 1;
4641
background-color: ${({ theme }) => theme.whiteBackground};
@@ -94,28 +89,25 @@ export interface IDappList {
9489
toggleIsDappListOpen: () => void;
9590
}
9691

97-
const NavBar: React.FC = () => {
92+
interface INavBar {
93+
isOpen: boolean;
94+
handleCloseNavbar: () => void;
95+
}
96+
97+
const NavBar: React.FC<INavBar> = ({ isOpen, handleCloseNavbar }) => {
9898
const { isConnected } = useAccount();
9999
const [isDappListOpen, toggleIsDappListOpen] = useToggle(false);
100100
const [isHelpOpen, toggleIsHelpOpen] = useToggle(false);
101101
const [isSettingsOpen, toggleIsSettingsOpen] = useToggle(false);
102-
const { isOpen } = useOpenContext();
103-
useLockOverlayScroll(isOpen);
104102

105103
return (
106104
<>
107105
<Wrapper {...{ isOpen }}>
108106
<StyledOverlay>
109107
<Container {...{ isOpen }}>
110-
<LightButton
111-
text="Kleros Solutions"
112-
onClick={() => {
113-
toggleIsDappListOpen();
114-
}}
115-
Icon={KlerosSolutionsIcon}
116-
/>
108+
<LightButton text="Kleros Solutions" onClick={toggleIsDappListOpen} Icon={KlerosSolutionsIcon} />
117109
<hr />
118-
<Explore />
110+
<Explore {...{ handleCloseNavbar }} />
119111
<hr />
120112
<WalletContainer>
121113
<ConnectWallet />

0 commit comments

Comments
 (0)