Skip to content

Commit f61cf13

Browse files
outsource set window scroll to
1 parent c126c16 commit f61cf13

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

src/components/hero/Hero.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Link from 'next/link'
44
import { Button } from '@/components/ui/button'
55
import { ContextTopUsers } from '@/context/ContextTopUsers'
66
import heroLogo from '@/assets/hero_logo.webp'
7+
import { setWindowScrollTo } from '@/utils/setWindowScrollTo'
78

89
const Hero = () => {
910
const contextTopUsers = useContext(ContextTopUsers)
@@ -13,15 +14,7 @@ const Hero = () => {
1314
const topTenUsers = contextTopUsers
1415

1516
const handleScrollToTopUsers = () => {
16-
if (topTenUsers.current) {
17-
const OFFSET = 64
18-
const top =
19-
topTenUsers.current.getBoundingClientRect().top +
20-
window.scrollY -
21-
OFFSET
22-
23-
window.scrollTo({ top, behavior: 'smooth' })
24-
}
17+
setWindowScrollTo(64, topTenUsers)
2518
}
2619

2720
return (

src/components/navigation/Navigation.tsx

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use client'
22

33
import { useContext, useRef, useState } from 'react'
4+
import NavigationButton from './NavigationButton'
45
import NavigationLogo from './NavigationLogo'
56
import { ContextQuote } from '@/context/ContextQuote'
67
import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
78
import { ContextTopUsers } from '@/context/ContextTopUsers'
9+
import { setWindowScrollTo } from '@/utils/setWindowScrollTo'
810
import { useNavigationOpacity } from '@/hooks/useNavigationOpacity'
9-
import NavigationButton from './NavigationButton'
11+
import { useScreenWidth } from '@/hooks/useScreenWidth'
1012

1113
const Navigation = () => {
1214
const contextQuote = useContext(ContextQuote)
@@ -33,45 +35,23 @@ const Navigation = () => {
3335
}
3436
const topUsersRef = contextTopUsers
3537

38+
const SCREEN_WIDTH = useScreenWidth()
39+
3640
const [navOpacity, setNavOpacity] = useState<string>('opacity-100')
3741
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
3842

3943
useNavigationOpacity({ setNavOpacity, timerRef })
4044

4145
const handleScrollToQuote = () => {
42-
if (quoteRef.current) {
43-
const OFFSET = 96
44-
const top =
45-
quoteRef.current.getBoundingClientRect().top +
46-
window.scrollY -
47-
OFFSET
48-
49-
window.scrollTo({ top, behavior: 'smooth' })
50-
}
46+
setWindowScrollTo(96, quoteRef)
5147
}
5248

5349
const handleScrollToTopUsers = () => {
54-
if (topUsersRef.current) {
55-
const OFFSET = 64
56-
const top =
57-
topUsersRef.current.getBoundingClientRect().top +
58-
window.scrollY -
59-
OFFSET
60-
61-
window.scrollTo({ top, behavior: 'smooth' })
62-
}
50+
setWindowScrollTo(64, topUsersRef)
6351
}
6452

6553
const handleScrollToTopTenPosts = () => {
66-
if (topTenPostsRef.current) {
67-
const OFFSET = 96
68-
const top =
69-
topTenPostsRef.current.getBoundingClientRect().top +
70-
window.scrollY -
71-
OFFSET
72-
73-
window.scrollTo({ top, behavior: 'smooth' })
74-
}
54+
setWindowScrollTo(96, topTenPostsRef)
7555
}
7656

7757
return (
@@ -80,7 +60,7 @@ const Navigation = () => {
8060
bg-stone-800 text-zinc-100 shadow-md shadow-stone-600/80`}
8161
data-testid="navigation"
8262
>
83-
<div className="max-w-7xl flex items-center justify-between m-auto h-16 px-2 sm:px-4 py-1 md:py-2">
63+
<div className="max-w-7xl flex items-center justify-between m-auto h-16 px-2 py-1 md:py-2">
8464
<NavigationLogo />
8565
<div>
8666
<NavigationButton

src/utils/setWindowScrollTo.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const setWindowScrollTo = (
2+
offset: number,
3+
ref: React.RefObject<HTMLDivElement | HTMLQuoteElement | null>
4+
) => {
5+
if (ref.current) {
6+
const top =
7+
ref.current.getBoundingClientRect().top + window.scrollY - offset
8+
9+
window.scrollTo({ top, behavior: 'smooth' })
10+
}
11+
}

0 commit comments

Comments
 (0)