Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cfaef58

Browse files
committed
Scroll the feed to the relevant update when goal title is clicked
1 parent b5ba8c8 commit cfaef58

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/components/HomePageFeed.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from '@/components'
3535
import { Goal } from './goals'
3636
import type { GoalResponse } from 'src/pages/[username]'
37+
import { scrollToContentWithId } from 'src/utils'
3738

3839
type LikeData = {
3940
count: number
@@ -141,7 +142,13 @@ export function HomePageFeedUpdate({
141142
</div>
142143
</div>
143144
<div className="mt-4 flex">
144-
<button onClick={() => setGoalId()} className="hidden lg:block">
145+
<button
146+
onClick={() => {
147+
setGoalId()
148+
scrollToContentWithId(`homepage-update-${update.id}`)
149+
}}
150+
className="hidden lg:block"
151+
>
145152
<span className="inline-flex items-center px-2.5 py-0.5 rounded-md text-sm font-medium bg-brand-100 text-brand-800 hover:text-brand-600">
146153
🚀 Goal: {goal.title}
147154
</span>

src/utils/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
export const delay = (ms: number) => new Promise((res) => setTimeout(res, ms))
2+
3+
export function scrollToContentWithId(id: string) {
4+
const element = document.getElementById(id)
5+
if (!element) {
6+
return
7+
}
8+
const yOffset = 80
9+
const y = element.getBoundingClientRect().top + window.pageYOffset - yOffset
10+
window.scrollTo({ behavior: 'smooth', top: y })
11+
}

0 commit comments

Comments
 (0)