@@ -27,8 +27,8 @@ import classes from './ChatAgentMessage.module.scss';
2727interface Props {
2828 message : UIAgentMessage ;
2929 isLast ?: boolean ;
30+ isFirst ?: boolean ;
3031 containerScrollableRef ?: RefObject < HTMLDivElement | null > ;
31- onShow ?: ( ) => void ;
3232}
3333
3434export function ChatAgentMessage ( props : Props ) {
@@ -39,16 +39,12 @@ export function ChatAgentMessage(props: Props) {
3939 ) ;
4040}
4141
42- function Message ( { message, isLast, containerScrollableRef , onShow } : Props ) {
42+ function Message ( { message, isLast, isFirst , containerScrollableRef } : Props ) {
4343 const contentRef = useRef < HTMLDivElement > ( null ) ;
4444 const rootRef = useRef < HTMLDivElement > ( null ) ;
4545
4646 const { props } = useMessageInteractionProps ( ) ;
4747
48- useEffect ( ( ) => {
49- onShow ?.( ) ;
50- } , [ onShow ] ) ;
51-
5248 const updateHeight = useCallback ( ( ) => {
5349 if ( ! containerScrollableRef ?. current || ! rootRef . current ) {
5450 return ;
@@ -63,15 +59,20 @@ function Message({ message, isLast, containerScrollableRef, onShow }: Props) {
6359 const messagesElem = listItemElem ?. parentElement ;
6460 const prevMessageElem = listItemElem ?. nextElementSibling ; // Messages are in reverse order
6561
66- if ( prevMessageElem instanceof HTMLLIElement && messagesElem ) {
67- const nextSiblingHeight = prevMessageElem ?. offsetHeight ?? 0 ;
68- const rowGap = parseFloat ( window . getComputedStyle ( messagesElem ) . rowGap ) ;
62+ if ( messagesElem ) {
63+ const nextSiblingHeight =
64+ prevMessageElem instanceof HTMLLIElement
65+ ? prevMessageElem . offsetHeight
66+ : isFirst
67+ ? MESSAGE_PLACEHOLDER_HEIGHT
68+ : 0 ;
69+ const rowGap = nextSiblingHeight ? parseFloat ( window . getComputedStyle ( messagesElem ) . rowGap ) : 0 ;
6970
7071 const availableHeight = Math . max ( 0 , containerHeight - nextSiblingHeight - rowGap ) ;
7172 rootRef . current . style . minBlockSize = rem ( availableHeight ) ;
7273 }
7374 }
74- } , [ isLast , containerScrollableRef ] ) ;
75+ } , [ containerScrollableRef , isLast , isFirst ] ) ;
7576
7677 useEffect ( ( ) => {
7778 updateHeight ( ) ;
@@ -119,3 +120,5 @@ function Message({ message, isLast, containerScrollableRef, onShow }: Props) {
119120 </ div >
120121 ) ;
121122}
123+
124+ const MESSAGE_PLACEHOLDER_HEIGHT = 46 ; // Height in px to reserve for the last user message to avoid layout shift
0 commit comments