use stick to bottom
1.0.0
useStickToBottom
StackBlitz의
bolt.new
지원하는 AI 채팅 봇을 염두에 두고 설계됨
컨테이너 바닥에 자동으로 고정되고 콘텐츠에 부드럽게 애니메이션을 적용하여 새 콘텐츠가 추가되는 동안 화면의 시각적 위치를 유지하는 경량 의 종속성이 없는 React 후크 + 구성 요소입니다.
overflow-anchor
브라우저 수준 CSS 지원이 필요하지 않습니다.isAtBottom
확인하고 프로그래밍 방식으로 아래쪽으로 스크롤할 수 있습니다.ResizeObserver
API를 사용하여 콘텐츠 크기가 조정되는 시기를 감지합니다.scrollToBottom
스크롤이 성공하자마자 true
로 확인되고, 스크롤이 취소되면 false
로 확인되는 Promise<boolean>
을 반환합니다.<StickToBottom>
구성요소 import { StickToBottom , useStickToBottomContext } from 'use-stick-to-bottom' ;
function Chat ( ) {
return (
< StickToBottom className = "h-[50vh] relative" resize = "smooth" initial = "smooth" >
< StickToBottom . Content className = "flex flex-col gap-4" >
{ messages . map ( ( message ) => (
< Message key = { message . id } message = { message } / >
) ) }
< / StickToBottom . Content >
< ScrollToBottom / >
{ /* This component uses `useStickToBottomContext` to scroll to bottom when the user enters a message */ }
< ChatBox / >
< / StickToBottom >
) ;
}
function ScrollToBottom ( ) {
const { isAtBottom , scrollToBottom } = useStickToBottomContext ( ) ;
return (
! isAtBottom && (
< button
className = "absolute i-ph-arrow-circle-down-fill text-4xl rounded-lg left-[50%] translate-x-[-50%] bottom-0"
onClick = { ( ) => scrollToBottom ( ) }
/ >
)
) ;
}
useStickToBottom
후크 import { useStickToBottom } from 'use-stick-to-bottom' ;
function Component ( ) {
const { scrollRef , contentRef } = useStickToBottom ( ) ;
return (
< div style = { { overflow : 'auto' } } ref = { scrollRef } >
< div ref = { contentRef } >
{ messages . map ( ( message ) => (
< Message key = { message . id } message = { message } / >
) ) }
< / div >
< / div >
) ;
}