feat(chat): auto-scroll to latest message

Implement an auto-scrolling feature for the chat window.

This uses the `useEffect` and `useRef` hooks to automatically scroll the message container to the bottom whenever new messages are added or the typing indicator appears, ensuring the latest content is always in view.
This commit is contained in:
Emmanuel Rizky
2025-08-03 13:07:34 +07:00
parent ecc5ab4898
commit 64bd6d59e2

View File

@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect, useRef } from 'react';
import { FiCamera, FiSend, FiUser } from 'react-icons/fi';
const ChatbotPage = () => {
@@ -7,6 +7,17 @@ const ChatbotPage = () => {
]);
const [inputMessage, setInputMessage] = useState('');
const [isTyping, setIsTyping] = useState(false);
const messagesEndRef = useRef(null);
const scrollToBottom = () => {
setTimeout(() => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, 0);
};
useEffect(() => {
scrollToBottom();
}, [messages, isTyping]);
const handleSendMessage = () => {
if (inputMessage.trim() === '') return;
@@ -92,6 +103,7 @@ const ChatbotPage = () => {
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
{/* Input Area */}