From d939e16ee6fd715f6288b4a77cec78c5d79d2f53 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sun, 28 Aug 2022 16:55:53 -0500 Subject: [PATCH] feat: scroll to bottom of transcript when number of messages change --- .../ChatTranscript/ChatTranscript.tsx | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/components/ChatTranscript/ChatTranscript.tsx b/src/components/ChatTranscript/ChatTranscript.tsx index 4064665..9714b27 100644 --- a/src/components/ChatTranscript/ChatTranscript.tsx +++ b/src/components/ChatTranscript/ChatTranscript.tsx @@ -1,4 +1,4 @@ -import { HTMLAttributes } from 'react' +import { HTMLAttributes, useRef, useEffect } from 'react' import cx from 'classnames' import Box from '@mui/material/Box' @@ -15,10 +15,40 @@ export const ChatTranscript = ({ messageLog, userId, }: ChatTranscriptProps) => { + const boxRef = useRef(null) + + useEffect(() => { + const { current: boxEl } = boxRef + if (!boxEl) return + + const { scrollHeight, clientHeight, scrollTop, children } = boxEl + const scrollTopMax = scrollHeight - clientHeight + + if (children.length === 0) return + + const lastChild = children[children.length - 1] + const lastChildHeight = lastChild.clientHeight + const previousScrollTopMax = scrollTopMax - lastChildHeight + + if ( + Math.ceil(scrollTop) >= Math.ceil(previousScrollTopMax) && + // scrollTo is not defined in the unit test environment + 'scrollTo' in boxEl + ) { + boxEl.scrollTo({ top: scrollTopMax }) + } + }, [messageLog.length]) + return ( - + {messageLog.map(message => { - return + return ( + // This wrapper div is necessary for accurate layout calculations + // when new messages cause the transcript to scroll to the bottom. +
+ +
+ ) })}
)