@@ -13,13 +13,15 @@ import { useChatMessages } from '@chainlit/react-client';
13
13
import { Button } from '@/components/ui/button' ;
14
14
15
15
interface Props {
16
+ autoScrollUserMessage ?: boolean ;
16
17
autoScrollRef ?: MutableRefObject < boolean > ;
17
18
children : React . ReactNode ;
18
19
className ?: string ;
19
20
}
20
21
21
22
export default function ScrollContainer ( {
22
23
autoScrollRef,
24
+ autoScrollUserMessage,
23
25
children,
24
26
className
25
27
} : Props ) {
@@ -31,40 +33,42 @@ export default function ScrollContainer({
31
33
32
34
// Calculate and update spacer height
33
35
const updateSpacerHeight = useCallback ( ( ) => {
34
- if ( ! ref . current || ! lastUserMessageRef . current ) return ;
36
+ if ( ! ref . current ) return ;
35
37
36
- const containerHeight = ref . current . clientHeight ;
37
- const lastMessageHeight = lastUserMessageRef . current . offsetHeight ;
38
+ if ( autoScrollUserMessage && lastUserMessageRef . current ) {
39
+ const containerHeight = ref . current . clientHeight ;
40
+ const lastMessageHeight = lastUserMessageRef . current . offsetHeight ;
38
41
39
- // Calculate the height of all elements after the last user message
40
- let afterMessagesHeight = 0 ;
41
- let currentElement = lastUserMessageRef . current . nextElementSibling ;
42
+ // Calculate the height of all elements after the last user message
43
+ let afterMessagesHeight = 0 ;
44
+ let currentElement = lastUserMessageRef . current . nextElementSibling ;
42
45
43
- // Iterate through all siblings after the last user message
44
- while ( currentElement && currentElement !== spacerRef . current ) {
45
- afterMessagesHeight += ( currentElement as HTMLElement ) . offsetHeight ;
46
- currentElement = currentElement . nextElementSibling ;
47
- }
46
+ // Iterate through all siblings after the last user message
47
+ while ( currentElement && currentElement !== spacerRef . current ) {
48
+ afterMessagesHeight += ( currentElement as HTMLElement ) . offsetHeight ;
49
+ currentElement = currentElement . nextElementSibling ;
50
+ }
48
51
49
- // Position the last user message at the top with some padding
50
- // Subtract both the message height and the height of any messages after it
51
- const newSpacerHeight =
52
- containerHeight - lastMessageHeight - afterMessagesHeight - 32 ;
52
+ // Position the last user message at the top with some padding
53
+ // Subtract both the message height and the height of any messages after it
54
+ const newSpacerHeight =
55
+ containerHeight - lastMessageHeight - afterMessagesHeight - 32 ;
53
56
54
- // Only set a positive spacer height
55
- if ( spacerRef . current ) {
56
- spacerRef . current . style . height = `${ Math . max ( 0 , newSpacerHeight ) } px` ;
57
- }
57
+ // Only set a positive spacer height
58
+ if ( spacerRef . current ) {
59
+ spacerRef . current . style . height = `${ Math . max ( 0 , newSpacerHeight ) } px` ;
60
+ }
58
61
59
- // Scroll to position the message at the top
60
- if ( afterMessagesHeight === 0 ) {
61
- scrollToPosition ( ) ;
62
- } else if ( autoScrollRef ?. current ) {
63
- if ( ref . current ) {
62
+ // Scroll to position the message at the top
63
+ if ( afterMessagesHeight === 0 ) {
64
+ scrollToPosition ( ) ;
65
+ } else if ( autoScrollRef ?. current ) {
64
66
ref . current . scrollTop = ref . current . scrollHeight ;
65
67
}
68
+ } else if ( autoScrollRef ?. current ) {
69
+ ref . current . scrollTop = ref . current . scrollHeight ;
66
70
}
67
- } , [ autoScrollRef ] ) ;
71
+ } , [ autoScrollUserMessage , autoScrollRef ] ) ;
68
72
69
73
// Find and set a ref to the last user message element
70
74
useEffect ( ( ) => {
@@ -87,6 +91,8 @@ export default function ScrollContainer({
87
91
88
92
// Add window resize listener to update spacer height
89
93
useEffect ( ( ) => {
94
+ if ( ! autoScrollUserMessage ) return ;
95
+
90
96
const handleResize = ( ) => {
91
97
updateSpacerHeight ( ) ;
92
98
} ;
@@ -99,7 +105,7 @@ export default function ScrollContainer({
99
105
return ( ) => {
100
106
window . removeEventListener ( 'resize' , handleResize ) ;
101
107
} ;
102
- } , [ updateSpacerHeight ] ) ;
108
+ } , [ autoScrollUserMessage , updateSpacerHeight ] ) ;
103
109
104
110
// Check scroll position on mount
105
111
useEffect ( ( ) => {
0 commit comments