use stick to bottom
1.0.0
useStickToBottom
AI チャット ボットを念頭に置いて設計され、StackBlitz による
bolt.new
強化
依存性のない軽量の React フック + コンポーネント。コンテナーの下部に自動的に貼り付けられ、新しいコンテンツが追加されている間、コンテンツをスムーズにアニメーション化して画面上の視覚的な位置を維持します。
overflow-anchor
ブラウザ レベルの CSS サポートは必要ありません。isAtBottom
チェックし、プログラムで一番下までスクロールできます。ResizeObserver
API を使用して、コンテンツのサイズ変更を検出します。scrollToBottom
スクロールが成功するとすぐにtrue
に解決されるPromise<boolean>
を返し、スクロールがキャンセルされた場合はfalse
解決されます。<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 >
) ;
}