use stick to bottom
1.0.0
useStickToBottom
Entwickelt mit Blick auf KI-Chatbots,
bolt.new
von StackBlitz unterstützen
Ein leichter , unabhängiger React-Hook + -Komponente, der automatisch am Boden des Containers haftet und den Inhalt reibungslos animiert, um seine visuelle Position auf dem Bildschirm beizubehalten, während neuer Inhalt hinzugefügt wird.
overflow-anchor
-CSS-Unterstützung auf Browserebene, die Safari nicht unterstützt.isAtBottom
überprüfen und programmgesteuert nach unten scrollen.ResizeObserver
-API, um zu erkennen, wenn sich die Größe von Inhalten ändert.scrollToBottom
gibt ein Promise<boolean>
zurück, das zu true
aufgelöst wird, sobald der Bildlauf erfolgreich war, oder zu false
wenn der Bildlauf abgebrochen wurde.<StickToBottom>
-Komponente 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
Haken 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 >
) ;
}