50 lines
1,016 B
TypeScript
50 lines
1,016 B
TypeScript
|
|
|
|
export default function useScroll(element: string) {
|
|
|
|
let isScrolledToBottom: boolean;
|
|
|
|
// Set the initial scroll position.
|
|
const setScroll = (smooth: boolean) => {
|
|
const view = document.getElementById(element)
|
|
|
|
if (view) {
|
|
view.scrollTo({
|
|
top: view.scrollHeight - view.clientHeight,
|
|
behavior: (smooth) ? 'smooth' : 'auto'
|
|
});
|
|
}
|
|
}
|
|
|
|
// Update isScrolledToBottom
|
|
const updateScrollRef = () => {
|
|
const view = document.getElementById(element)
|
|
|
|
if (view) {
|
|
isScrolledToBottom = view.scrollHeight - view.clientHeight <= view.scrollTop + 1
|
|
}
|
|
|
|
}
|
|
|
|
// Adjust scroll after we add content to the messenger.
|
|
const adjustScroll = () => {
|
|
const view = document.getElementById(element)
|
|
|
|
if (view) {
|
|
if (isScrolledToBottom) {
|
|
view.scrollTo({
|
|
top: view.scrollHeight - view.clientHeight,
|
|
behavior: 'smooth'
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
updateScrollRef,
|
|
adjustScroll,
|
|
setScroll,
|
|
}
|
|
|
|
}
|