Hack to force scroll to very bottom at mount. For #2500

This commit is contained in:
Gabe Kangas
2023-01-05 02:16:37 -08:00
parent 3620c2eb7d
commit 81c505d731
2 changed files with 21 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
import { Virtuoso } from 'react-virtuoso'; import { Virtuoso } from 'react-virtuoso';
import { useState, useMemo, useRef, CSSProperties, FC } from 'react'; import { useState, useMemo, useRef, CSSProperties, FC, useEffect } from 'react';
import { EditFilled } from '@ant-design/icons'; import { EditFilled } from '@ant-design/icons';
import { import {
ConnectedClientInfoEvent, ConnectedClientInfoEvent,
@@ -78,7 +78,7 @@ export const ChatContainer: FC<ChatContainerProps> = ({
showInput, showInput,
height, height,
}) => { }) => {
const [atBottom, setAtBottom] = useState(true); const [atBottom, setAtBottom] = useState(false);
const chatContainerRef = useRef(null); const chatContainerRef = useRef(null);
const getNameChangeViewForMessage = (message: NameChangeEvent) => { const getNameChangeViewForMessage = (message: NameChangeEvent) => {
@@ -171,6 +171,23 @@ export const ChatContainer: FC<ChatContainerProps> = ({
} }
}; };
const scrollChatToBottom = (ref, behavior = 'smooth') => {
ref.current?.scrollToIndex({
index: messages.length - 1,
behavior,
});
setAtBottom(true);
};
// This is a hack to force a scroll to the very bottom of the chat messages
// on initial mount of the component.
// For https://github.com/owncast/owncast/issues/2500
useEffect(() => {
setTimeout(() => {
scrollChatToBottom(chatContainerRef, 'auto');
}, 500);
}, []);
const MessagesTable = useMemo( const MessagesTable = useMemo(
() => ( () => (
<> <>
@@ -178,7 +195,6 @@ export const ChatContainer: FC<ChatContainerProps> = ({
style={{ height }} style={{ height }}
className={styles.virtuoso} className={styles.virtuoso}
ref={chatContainerRef} ref={chatContainerRef}
// initialTopMostItemIndex={999999}
data={messages} data={messages}
itemContent={(index, message) => getViewForMessage(index, message)} itemContent={(index, message) => getViewForMessage(index, message)}
followOutput={(isAtBottom: boolean) => { followOutput={(isAtBottom: boolean) => {
@@ -188,11 +204,10 @@ export const ChatContainer: FC<ChatContainerProps> = ({
return false; return false;
}} }}
alignToBottom alignToBottom
atBottomThreshold={70} atBottomThreshold={50}
atBottomStateChange={bottom => { atBottomStateChange={bottom => {
setAtBottom(bottom); setAtBottom(bottom);
}} }}
endReached={() => setAtBottom(true)}
/> />
{!atBottom && <ScrollToBotBtn chatContainerRef={chatContainerRef} messages={messages} />} {!atBottom && <ScrollToBotBtn chatContainerRef={chatContainerRef} messages={messages} />}
</> </>

View File

@@ -18,7 +18,7 @@ export const ScrollToBotBtn: FC<Props> = ({ chatContainerRef, messages }) => (
onClick={() => onClick={() =>
chatContainerRef.current.scrollToIndex({ chatContainerRef.current.scrollToIndex({
index: messages.length - 1, index: messages.length - 1,
behavior: 'smooth', behavior: 'auto',
}) })
} }
> >