use stick to bottom
1.0.0
useStickToBottom
Projetado com bots de bate-papo de IA em mente, alimentando
bolt.new
por StackBlitz
Um gancho + componente React leve e de dependência zero que adere automaticamente à parte inferior do contêiner e anima suavemente o conteúdo para manter sua posição visual na tela enquanto novo conteúdo é adicionado.
overflow-anchor
, que o Safari não suporta.isAtBottom
e rolar programaticamente até o final.ResizeObserver
moderna, mas bem suportada, para detectar quando o conteúdo é redimensionado.scrollToBottom
retorna um Promise<boolean>
que será resolvido como true
assim que a rolagem for bem-sucedida ou false
se a rolagem for cancelada.<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 >
) ;
}