Skip to content

Commit 7523df8

Browse files
new navigation menu for mobile / small tablet users
1 parent aa160b1 commit 7523df8

File tree

1 file changed

+99
-25
lines changed

1 file changed

+99
-25
lines changed

src/components/navigation/Navigation.tsx

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

33
import { useContext, useRef, useState } from 'react'
4+
import { Button } from '@/components/ui/button'
45
import NavigationButton from './NavigationButton'
56
import NavigationLogo from './NavigationLogo'
67
import { ContextQuote } from '@/context/ContextQuote'
@@ -37,47 +38,120 @@ const Navigation = () => {
3738

3839
const SCREEN_WIDTH = useScreenWidth()
3940

41+
const [menuExpanded, setMenuExpanded] = useState<boolean>(false)
4042
const [navOpacity, setNavOpacity] = useState<string>('opacity-100')
4143
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
4244

4345
useNavigationOpacity({ setNavOpacity, timerRef })
4446

4547
const handleScrollToQuote = () => {
46-
setWindowScrollTo(96, quoteRef)
48+
setWindowScrollTo(
49+
['MOBILE', 'TABLET_SMALL'].includes(SCREEN_WIDTH) ? 144 : 72,
50+
quoteRef
51+
)
4752
}
4853

4954
const handleScrollToTopUsers = () => {
50-
setWindowScrollTo(64, topUsersRef)
55+
setWindowScrollTo(
56+
['MOBILE', 'TABLET_SMALL'].includes(SCREEN_WIDTH) ? 112 : 48,
57+
topUsersRef
58+
)
5159
}
5260

5361
const handleScrollToTopTenPosts = () => {
54-
setWindowScrollTo(96, topTenPostsRef)
62+
setWindowScrollTo(
63+
['MOBILE', 'TABLET_SMALL'].includes(SCREEN_WIDTH) ? 128 : 72,
64+
topTenPostsRef
65+
)
5566
}
5667

5768
return (
58-
<nav
59-
className={`sticky top-0 z-50 w-full transition-opacity duration-1000 ease-in-out ${navOpacity}
60-
bg-stone-800 text-zinc-100 shadow-md shadow-stone-600/80`}
61-
data-testid="navigation"
62-
>
63-
<div className="max-w-7xl flex items-center justify-between m-auto h-16 px-2 py-1 md:py-2">
64-
<NavigationLogo />
65-
<div>
66-
<NavigationButton
67-
handleScroll={handleScrollToTopUsers}
68-
label="Most Active Users"
69-
/>
70-
<NavigationButton
71-
handleScroll={handleScrollToQuote}
72-
label="Quote of the Day"
73-
/>
74-
<NavigationButton
75-
handleScroll={handleScrollToTopTenPosts}
76-
label="Top 10 Posts"
77-
/>
69+
<>
70+
<nav
71+
className={`sticky top-0 z-50 w-full transition-opacity duration-1000 ease-in-out ${navOpacity}
72+
bg-stone-800 text-zinc-100 shadow-md shadow-stone-600/80`}
73+
data-testid="navigation"
74+
>
75+
<div className="max-w-7xl flex items-center justify-between m-auto h-16 px-2 py-1 md:py-2">
76+
<NavigationLogo />
77+
{['TABLET', 'DESKTOP'].includes(SCREEN_WIDTH) ? (
78+
<div>
79+
<NavigationButton
80+
handleScroll={handleScrollToTopUsers}
81+
label="Most Active Users"
82+
variant="ghost"
83+
/>
84+
<NavigationButton
85+
handleScroll={handleScrollToQuote}
86+
label="Quote of the Day"
87+
variant="ghost"
88+
/>
89+
<NavigationButton
90+
handleScroll={handleScrollToTopTenPosts}
91+
label="Top 10 Posts"
92+
variant="ghost"
93+
/>
94+
</div>
95+
) : (
96+
<Button asChild variant="ghost">
97+
<button
98+
className="text-very-large rounded-lg h-12 w-12"
99+
onClick={() => setMenuExpanded((prev) => !prev)}
100+
aria-controls="mobile-navigation"
101+
aria-expanded={menuExpanded}
102+
aria-label={
103+
menuExpanded
104+
? 'Close mobile navigation menu'
105+
: 'Open mobile navigation menu'
106+
}
107+
title={
108+
menuExpanded
109+
? 'Close mobile navigation menu'
110+
: 'Open mobile navigation menu'
111+
}
112+
>
113+
{menuExpanded ? '✖' : '☰'}
114+
</button>
115+
</Button>
116+
)}
78117
</div>
79-
</div>
80-
</nav>
118+
</nav>
119+
{['TABLET_SMALL', 'MOBILE'].includes(SCREEN_WIDTH) &&
120+
menuExpanded && (
121+
<nav
122+
className={`sticky top-16 z-50 w-full transition-opacity duration-1000 ease-in-out ${navOpacity}
123+
bg-stone-800 text-zinc-100 shadow-md shadow-stone-600/80
124+
flex flex-wrap justify-center py-2 px-4 border-t-2 border-stone-600`}
125+
id="mobile-navigation"
126+
aria-label="Navigate to each Section"
127+
>
128+
<NavigationButton
129+
handleScroll={() => {
130+
handleScrollToTopUsers()
131+
setMenuExpanded(false)
132+
}}
133+
label="Most Active Users"
134+
variant="secondary"
135+
/>
136+
<NavigationButton
137+
handleScroll={() => {
138+
handleScrollToQuote()
139+
setMenuExpanded(false)
140+
}}
141+
label="Quote of the Day"
142+
variant="secondary"
143+
/>
144+
<NavigationButton
145+
handleScroll={() => {
146+
handleScrollToTopTenPosts()
147+
setMenuExpanded(false)
148+
}}
149+
label="Top 10 Posts"
150+
variant="secondary"
151+
/>
152+
</nav>
153+
)}
154+
</>
81155
)
82156
}
83157

0 commit comments

Comments
 (0)