use stick to bottom
1.0.0
useStickToBottom
Dirancang dengan mempertimbangkan bot obrolan AI, mendukung
bolt.new
oleh StackBlitz
React hook + Component ringan tanpa ketergantungan yang secara otomatis menempel di bagian bawah wadah dan menganimasikan konten dengan lancar untuk mempertahankan posisi visualnya di layar saat konten baru ditambahkan.
overflow-anchor
yang tidak didukung Safari.isAtBottom
& menggulir ke bawah secara terprogram.ResizeObserver
API yang modern namun didukung dengan baik untuk mendeteksi kapan konten diubah ukurannya.scrollToBottom
mengembalikan Promise<boolean>
yang akan menjadi true
segera setelah pengguliran berhasil, atau false
jika pengguliran dibatalkan.<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
Hook 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 >
) ;
}