feat: Implement auto-scrolling to the bottom of chat messages and a scroll-to-bottom button that appears when scrolled up.

This commit is contained in:
pradeepkumar
2026-01-19 00:14:41 +05:30
parent f7388642d1
commit 071354be87

View File

@@ -1,6 +1,6 @@
'use client'; 'use client';
import { useState } from 'react'; import { useState, useRef, useEffect } from 'react';
import Image from 'next/image'; import Image from 'next/image';
import { ChatHeader, MessageInput } from './component'; import { ChatHeader, MessageInput } from './component';
@@ -209,12 +209,36 @@ const messages = [
export default function MessagePage() { export default function MessagePage() {
const [selectedConversation, setSelectedConversation] = useState<string | null>('1'); const [selectedConversation, setSelectedConversation] = useState<string | null>('1');
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const [showScrollButton, setShowScrollButton] = useState(false);
const messagesContainerRef = useRef<HTMLDivElement>(null);
const handleSendMessage = (message: string) => { const handleSendMessage = (message: string) => {
console.log('Sending message:', message); console.log('Sending message:', message);
// In production, this would send the message to the API // In production, this would send the message to the API
}; };
const handleScroll = () => {
if (messagesContainerRef.current) {
const { scrollTop, scrollHeight, clientHeight } = messagesContainerRef.current;
// Show button if not at bottom (with 100px threshold)
setShowScrollButton(scrollHeight - scrollTop - clientHeight > 100);
}
};
const scrollToBottom = () => {
if (messagesContainerRef.current) {
messagesContainerRef.current.scrollTo({
top: messagesContainerRef.current.scrollHeight,
behavior: 'smooth'
});
}
};
useEffect(() => {
// Scroll to bottom on initial load
scrollToBottom();
}, [selectedConversation]);
const filteredConversations = conversations.filter((conv) => const filteredConversations = conversations.filter((conv) =>
conv.name.toLowerCase().includes(searchQuery.toLowerCase()) conv.name.toLowerCase().includes(searchQuery.toLowerCase())
); );
@@ -329,39 +353,48 @@ export default function MessagePage() {
{/* Chat header */} {/* Chat header */}
<ChatHeader {...selectedUserData} /> <ChatHeader {...selectedUserData} />
{/* Messages area */} {/* Messages area wrapper */}
<div className="flex-1 overflow-y-auto py-4 relative custom-scrollbar"> <div className="flex-1 relative">
<div className="space-y-4 px-2"> <div
{messages.map((message) => ( ref={messagesContainerRef}
<div onScroll={handleScroll}
key={message.id} className="absolute inset-0 overflow-y-auto py-4 custom-scrollbar"
className={`flex ${message.isOwn ? 'justify-end' : 'justify-start'}`} >
> <div className="space-y-4 px-2">
{messages.map((message) => (
<div <div
className={`max-w-[70%] rounded-[15px] px-4 py-3 ${ key={message.id}
message.isOwn className={`flex ${message.isOwn ? 'justify-end' : 'justify-start'}`}
? 'bg-[#e58625] text-[#00293D]'
: 'bg-[#00293d]/10 text-[#00293D]'
}`}
> >
<p className="font-serif font-normal text-[14px] leading-[19px]"> <div
{message.text} className={`max-w-[70%] rounded-[15px] px-4 py-3 ${
</p> message.isOwn
<p ? 'bg-[#e58625] text-[#00293D]'
className={`font-serif font-normal text-[12px] leading-[16px] mt-1 ${ : 'bg-[#00293d]/10 text-[#00293D]'
message.isOwn ? 'text-[#00293D]/70' : 'text-[#00293D]/50'
}`} }`}
> >
{message.timestamp} <p className="font-serif font-normal text-[14px] leading-[19px]">
</p> {message.text}
</p>
<p
className={`font-serif font-normal text-[12px] leading-[16px] mt-1 ${
message.isOwn ? 'text-[#00293D]/70' : 'text-[#00293D]/50'
}`}
>
{message.timestamp}
</p>
</div>
</div> </div>
</div> ))}
))} </div>
</div> </div>
{/* Scroll to bottom button */} {/* Scroll to bottom button - only shows when scrolled up */}
<div className="absolute bottom-4 right-4"> {showScrollButton && (
<button className="w-[50px] h-[50px] bg-[#00293d]/10 rounded-full flex items-center justify-center hover:bg-[#00293d]/20 transition-colors cursor-pointer"> <button
onClick={scrollToBottom}
className="absolute bottom-4 right-4 w-[50px] h-[50px] bg-[#00293d]/10 rounded-full flex items-center justify-center hover:bg-[#00293d]/20 transition-all cursor-pointer shadow-lg z-10"
>
<Image <Image
src="/assets/icons/arrow-down-icon.svg" src="/assets/icons/arrow-down-icon.svg"
alt="Scroll to bottom" alt="Scroll to bottom"
@@ -369,7 +402,7 @@ export default function MessagePage() {
height={20} height={20}
/> />
</button> </button>
</div> )}
</div> </div>
{/* Message input */} {/* Message input */}