Skip to content

Commit a82d694

Browse files
committed
✨Feat: useScrollToBottom
1 parent 0d0dc21 commit a82d694

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/hooks/useScrollToBottom.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect, useRef } from 'react'
2+
3+
export function useScrollToBottom<T extends HTMLElement>() {
4+
const ref = useRef<T>(null)
5+
6+
useEffect(() => {
7+
if (ref.current) {
8+
ref.current.scrollTop = ref.current.scrollHeight
9+
}
10+
}, [])
11+
12+
return ref
13+
}

src/modals/ChatModal/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Separator } from '~components/Separator'
77
import { Typo11, Typo15 } from '~components/Typo'
88
import { useModalStore } from '~stores/modalStore'
99
import * as S from './styles'
10+
import { useScrollToBottom } from '~hooks/useScrollToBottom'
1011

1112
type ChatModalProps = {
1213
userId: string
@@ -15,9 +16,10 @@ type ChatModalProps = {
1516
export default function ChatModal({ userId }: ChatModalProps) {
1617
const { popModal } = useModalStore()
1718
console.log('ChatModal', userId) //todo fetch by userId
19+
const ref = useScrollToBottom<HTMLDivElement>()
1820

1921
return (
20-
<S.ChatModal>
22+
<S.ChatModal ref={ref}>
2123
<Header type='lg' prevBtn onClickPrev={popModal}>
2224
<S.ProfileWrapper>
2325
<Profile $size={40} $src='' userId={userId} />

0 commit comments

Comments
 (0)