Skip to content

Commit 5b42c8a

Browse files
navigation button
1 parent 80d72d6 commit 5b42c8a

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/components/navigation/Navigation.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
'use client'
22

3+
import { useContext, useRef, useState } from 'react'
34
import NavigationLogo from './NavigationLogo'
4-
import { useRef, useState } from 'react'
5+
import { Button } from '@/components/ui/button'
6+
import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
57
import { useNavigationOpacity } from '@/hooks/useNavigationOpacity'
8+
import NavigationButton from './NavigationButton'
69

710
const Navigation = () => {
11+
const contextTopTenPosts = useContext(ContextTopTenPosts)
12+
if (!contextTopTenPosts) {
13+
throw new Error(
14+
'Feed must be used within a ContextTopTenPosts.Provider'
15+
)
16+
}
17+
const topTenPostsRef = contextTopTenPosts
18+
819
const [navOpacity, setNavOpacity] = useState<string>('opacity-100')
920
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
1021

1122
useNavigationOpacity({ setNavOpacity, timerRef })
1223

24+
const handleScrollToTopTenPosts = () => {
25+
if (topTenPostsRef.current) {
26+
const OFFSET = 64
27+
const top =
28+
topTenPostsRef.current.getBoundingClientRect().top +
29+
window.scrollY -
30+
OFFSET
31+
32+
window.scrollTo({ top, behavior: 'smooth' })
33+
}
34+
}
35+
1336
return (
1437
<nav
1538
className={`sticky top-0 z-50 w-full transition-opacity duration-1000 ease-in-out ${navOpacity}
@@ -18,6 +41,11 @@ const Navigation = () => {
1841
>
1942
<div className="max-w-7xl flex items-center justify-between m-auto h-16 px-2 sm:px-4 py-1 md:py-2">
2043
<NavigationLogo />
44+
45+
<NavigationButton
46+
handleScroll={handleScrollToTopTenPosts}
47+
label="Top 10 Posts"
48+
/>
2149
</div>
2250
</nav>
2351
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Button } from '@/components/ui/button'
2+
3+
type NavigationButtonProps = { handleScroll: () => void; label: string }
4+
5+
const NavigationButton = ({ handleScroll, label }: NavigationButtonProps) => {
6+
return (
7+
<Button asChild variant="ghost" className="px-4">
8+
<button
9+
className="text-normal"
10+
aria-label={`Scroll to ${label}`}
11+
title={`Scroll to ${label}`}
12+
onClick={handleScroll}
13+
>
14+
{label}
15+
</button>
16+
</Button>
17+
)
18+
}
19+
20+
export default NavigationButton

0 commit comments

Comments
 (0)