Skip to content

Commit 5ed320d

Browse files
committed
Add scrollbar to stay at bottom
1 parent f74cae7 commit 5ed320d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

app/src/components/bottom/ChatRoom.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { useState } from 'react';
2+
import { useState, useRef, useEffect } from 'react';
33
import { useSelector } from 'react-redux';
44
import { RootState } from '../../redux/store';
55
import { emitEvent } from '../../helperFunctions/socket';
@@ -89,13 +89,22 @@ const Chatroom = (props): JSX.Element => {
8989
});
9090
};
9191

92+
const containerRef = useRef<HTMLDivElement>(null);
93+
94+
// Scroll to the bottom of the container whenever new messages are added
95+
useEffect(() => {
96+
if (containerRef.current) {
97+
containerRef.current.scrollTop = containerRef.current.scrollHeight;
98+
}
99+
}, [messages]);
100+
92101
return (
93102
<div
94103
className="livechat-panel"
95104
style={{ paddingLeft: '10px', width: '100%', height: '100%' }}
96105
>
97106
<div style={{ justifyContent: 'center', display: 'flex', height: '80%' }}>
98-
<div id="message-container" style={wrapperStyles}>
107+
<div id="message-container" style={wrapperStyles} ref={containerRef}>
99108
{renderMessages()}
100109
</div>
101110
</div>

0 commit comments

Comments
 (0)