Skip to content

Commit 759df27

Browse files
scroll to top ten posts
1 parent 4e65115 commit 759df27

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

src/app/page.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
'use client'
22

3+
import { useRef } from 'react'
34
import Feed from '@/components/feed/Feed'
45
import Footer from '@/components/footer/Footer'
56
import Hero from '@/components/hero/Hero'
67
import Navigation from '@/components/navigation/Navigation'
8+
import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
79
import { useFocusTrap } from '@/hooks/useFocusTrap'
810
import { useSetWindowScrollY } from '@/hooks/useSetWindowScrollY'
911

1012
export default function Home() {
13+
const topTenPostsRef = useRef<HTMLDivElement | null>(null)
14+
1115
useFocusTrap()
1216
useSetWindowScrollY()
1317

1418
return (
1519
<div className="min-h-svh flex flex-col items-center justify-start bg-gradient-to-b from-stone-700 to-stone-800">
16-
<Navigation />
17-
<Hero />
18-
<Feed />
19-
<Footer />
20+
<ContextTopTenPosts.Provider value={topTenPostsRef}>
21+
<Navigation />
22+
<Hero />
23+
<Feed />
24+
<Footer />
25+
</ContextTopTenPosts.Provider>
2026
</div>
2127
)
2228
}

src/components/feed/Feed.tsx

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

3-
import { useCallback, useEffect, useRef, useState } from 'react'
3+
import { useCallback, useContext, useEffect, useRef, useState } from 'react'
44
import { FetchLoading } from 'fetch-loading'
55
import { Badge } from '@/components/ui/badge'
66
import {
@@ -20,11 +20,20 @@ import {
2020
} from '@/components/ui/carousel'
2121
import { Skeleton } from '@/components/ui/skeleton'
2222
import fetchFeedItems from '@/api/fetchFeedItems'
23+
import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
2324
import { FeedItemsType } from '@/types/types'
2425
import { getItemFromSessionStorage } from '@/utils/getItemFromSessionStorage'
2526
import { getWindowScrollY } from '@/utils/getWindowScrollY'
2627

2728
const Feed = () => {
29+
const contextTopTenPosts = useContext(ContextTopTenPosts)
30+
if (!contextTopTenPosts) {
31+
throw new Error(
32+
'Feed must be used within a ContextTopTenPosts.Provider'
33+
)
34+
}
35+
const topTenPostsRef = contextTopTenPosts
36+
2837
const PAGE_SIZE = 10
2938
const nextPage = useRef<number>(1)
3039
const [feedItems, setFeedItems] = useState<Array<FeedItemsType>>([])
@@ -69,6 +78,7 @@ const Feed = () => {
6978
<main
7079
className="w-full flex flex-col items-center gap-8 lg:gap-12 max-w-7xl relative isolate bg-stone-300 text-stone-950 lg:rounded-lg p-3 sm:p-4 lg:p-6
7180
drop-shadow-stone-900 drop-shadow-sm"
81+
ref={topTenPostsRef}
7282
>
7383
<div className="w-full flex flex-col items-center px-14 sm:px-16 py-6 bg-stone-400/60 rounded-lg shadow-md shadow-stone-500">
7484
<div className="w-full flex flex-col gap-4 max-w-3xl">

src/components/hero/Hero.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1+
import { useContext } from 'react'
2+
import Image from 'next/image'
13
import Link from 'next/link'
24
import { Button } from '@/components/ui/button'
5+
import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
36
import heroLogo from '@/assets/hero_logo.webp'
4-
import Image from 'next/image'
57

68
const Hero = () => {
9+
const contextTopTenPosts = useContext(ContextTopTenPosts)
10+
if (!contextTopTenPosts) {
11+
throw new Error(
12+
'Feed must be used within a ContextTopTenPosts.Provider'
13+
)
14+
}
15+
const topTenPostsRef = contextTopTenPosts
16+
17+
const handleScrollToTopTenPosts = () => {
18+
if (topTenPostsRef.current) {
19+
const OFFSET = 64
20+
const top =
21+
topTenPostsRef.current.getBoundingClientRect().top +
22+
window.scrollY -
23+
OFFSET
24+
25+
window.scrollTo({ top, behavior: 'smooth' })
26+
}
27+
}
28+
729
return (
830
<div className="w-full flex flex-col items-center gap-8 max-w-7xl relative isolate bg-stone-100 text-stone-950 p-3 sm:p-4 lg:p-6 my-6 lg:my-8">
931
<h1 className="text-extremely-large font-mono font-semibold">
@@ -63,14 +85,14 @@ const Hero = () => {
6385
variant="outline"
6486
className="flex m-auto mt-4 px-8 w-fit"
6587
>
66-
<Link
67-
href=""
88+
<button
6889
className="text-normal"
6990
aria-label="Link to Top 10 Posts (scrolls to section)"
7091
title="Link to Top 10 Posts"
92+
onClick={handleScrollToTopTenPosts}
7193
>
7294
Top 10 Posts
73-
</Link>
95+
</button>
7496
</Button>
7597
</p>
7698
</div>

src/context/ContextTopTenPosts.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createContext, RefObject } from 'react'
2+
3+
export const ContextTopTenPosts = createContext<
4+
RefObject<HTMLDivElement | null> | undefined
5+
>(undefined)

0 commit comments

Comments
 (0)