1.0.1 • Published 4 months ago

react-responsive-chat v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

React Responsive Chat

React Responsive Chat provides a great and customizable Chat UI for your React Web App

Install

  npm install react-responsive-chat

or

  yarn add react-responsive-chat

Usage

import React,{useState, useEffect} from 'react';
import ReactResponsiveChat from 'react-responsive-chat';

function App(){
     const user={
        _id: '...',
        createdAt: ...,
        fullName: 'Test',
        photo: 'https://...',
        lastActive: '...',
        isOnline: false,
      }

    <!-- For Chat Rooms -->

    const [isLoadingChatRooms, setIsLoadingChatRooms]=useState(false);
    const [chatRooms, setChatRooms]=useState([]);
    const [selectedChatRoom, setSelectedChatRoom]=useState(null);

     <!-- For Chats -->
     const [chats, setChats]=useState([]);
     const [isLoadingChats, setIsLoadingChats]=useState(false);

     <!--Required For Load More Case-->
      const [totalRooms, setTotalRooms] = useState(0);
      const [roomsPageNo, setRoomsPageNo] = useState(0);
      const [totalChats, setTotalChats] = useState(0);
      const [chatsPageNo, setChatsPageNo] = useState(0);
      const recordsLimit = 10;

    useEffect(() => {
      if(selectedChatRoom !== null){
        setChats([
            {
            _id: 1,
            text: 'Hello developer',
            createdAt: new Date(),
            user: {
                  _id: 2,
                  fullName: 'React Responsive Chat',
                  photo: 'https://...',
               },
               isSeen:false,
               room:"..." // must be room id
              },
            ])
          }
    }, [selectedChatRoom])

       return(
           <ReactResponsiveChat
              isLoadingChatRooms={isLoadingChatRooms}
              chatRooms={chatRooms}
              isLoadingChats={isLoadingChats}
              chats={chats}
              selectedChatRoom={selectedChatRoom}
              onChatRoomSelection={(e) => {
                 setPageNo(1);
                 setRoomData(e);
                 setIsShowingUnRead(true);
                 setMessages([]);
                }}
              user={user}
              onSend={(e)=> {...}}

              <!--Required For Load More Case-->
              chatsLimit={recordsLimit}
              chatsPage={chatsPageNo}
              isLoadMoreChats={Math.ceil(totalChats / recordsLimit) > chatsPageNo}
              isLoadMoreChatRooms={Math.ceil(totalRooms / recordsLimit) > roomsPageNo}
              onLoadMoreChats={async (e) => {
                 const pg = pageNo + 1;
                 await chatsApiCall(pg); // Note: Page must be updated after Api call
                 setPageNo(pg);
                }}
              onLoadMoreChatRooms={async () => {
                  const pg = pageNo + 1;
                  await chatRoomsApiCall(pg); // Note: Page must be updated after Api call
                  setRoomsPageNo(pg);
               }}
        />
      )}

Codesandbox Demo

Demo

Props

NameTypeDescriptionDefault
chatRoomsArrayUsed for Chat Rooms Display[]
onLoadMoreChatRoomsPromiseMust be promise functionundefined
renderChatRoomsLoaderFunctionMust be React Componentdefault
selectedChatRoomObjectWill be Selected Chat Room Objectundefined
onChatRoomSelectionFunctionWill be Call on Selection of Chat Roomundefined
isLoadingChatRoomsBooleanMust be Boolean valuefalse
isLoadMoreChatRoomsBooleanMust be Boolean valuefalse
renderRoomsHeaderFunctionMust be React Componentdefault
chatsArrayUsed for Chats Display[]
onLoadMoreChatsPromiseMust be promise functionundefined
renderChatsLoaderFunctionMust be React Componentdefault
isLoadingChatsBooleanMust be Boolean valuefalse
isLoadMoreChatsBooleanMust be Boolean valuefalse
renderChatsHeaderFunctionMust be React Componentdefault
onSendFunctionMust be promise functionundefined
onMoveToUnreadChatFunctionMust be functionundefined
containerClassNameStringMust be classundefined
isShowUnReadChatsCountBooleanMust be Boolean valuefalse
totalUnReadChatsCountNumberMust be Number0
userObjectMust be Object with keys (Mentioned Below)undefined
onBackFunctionMust be Functiondefault
isTypingBooleanMust be Boolean valuefalse
placeholderStringPlaceholder is for Chat InputEnter message here...
unReadChatIdStringMust be the UnRead Chat Id of Chat Roomundefined
renderChatTimeSeparatorFunctionMust be React Componentdefault
chatsLimitNumberMust be Number10
chatsPageNumberMust be Numberundefined
onTypingFunctionMust be Functionundefined
colorsObjectMust be Object with keys (Mentioned Below)undefined
refObjectMust be React Ref Objectnull

Object Structure Examples

User Object

const userObject={
_id: '...',
createdAt: '...',
fullName: 'Test',
photo: 'https://...',
lastActive: '...',
isOnline: false,
}

Message Object

const message={
_id:'...',
createdAt:new Date(),
text:'Test Message',
isSeen:false,
room:'...',
user:{
  _id: '...',
  createdAt: '...',
  fullName: 'Test User',
  photo: 'https://...',
  lastActive: '...', // Optional
  isOnline: false, // Optional
},
}

Chat Room Object

const chatRoom={
_id:'...',
createdAt:new Date(),
isSeen:false,
room:'...',
unReadCount:2,
sender:{
  _id:'...',
  photo:'https://...',
  fullName:'Test User 1'
},
receiver:{
  _id:'...',
  photo:'https://...',
  fullName:"Test User 2",
  lastActive: '...',
  isOnline: false,
},
lastMessage:{
  _id:'...',
  createdAt:new Date(),
  text:'Test Last Message',
  user:{
   _id:'...',
   photo:'https://...',
   fullName:'Test User 1'
   lastActive: '...',
   isOnline: false,
},
}

Colors Object

const customColors = {
chatRoom: {
  name: "#000000",
  message: "grey",
  time: "black",
  bg: "#ffffff",
  separator: "#ae93ff",
  hoverBg: "#af94ff3b",
  unReadCountBg: "#ae93ff",
  unReadCount: "white",
},
chatRoomSelected: {
  name: "#000000",
  message: "grey",
  time: "#ae93ff",
  bg: "#af94ff1a",
  separator: "#ae93ff",
  unReadCountBg: "#ae93ff",
  unReadCount: "white",
},
myMessage: {
  text: "white",
  time: "white",
  bg: "#ae93ff",
},
otherMessage: {
  text: "black",
  time: "black",
  bg: "#d8d8d8",
},
chatRoomsHeader: {
  bg: "#ae93ff",
  text: "white",
},
chatsHeader: {
  bg: "#ae93ff",
  text: "white",
},
chatsFooter: {
  bg: "#0000ff17",
  btnBg: "#ae93ff",
  btnText: "white",
  inputBg: "#fff",
  inputText: "black",
  inputScrollTrack: "#d8d8d87d",
  inputScrollBar: "#ae93ff",
},
chatsContent: {
  bg: "#fff",
  timeIndicator: "#d8d8d8",
  timeText: "black",
},
chatsScroller: {
  track: "#d8d8d87d",
  bar: "#ae93ff",
},
chatRoomsScroller: {
  track: "#d8d8d87d",
  bar: "#ae93ff",
},
noChatRoomsFound: {
  icon: "#ae93ff",
  text: "#ae93ff",
},
welcomeToChat: {
  icon: "#ae93ff",
  text: "#ae93ff",
},
};

Upcoming Features

  1. Media Support
  2. Search Chats or Users Support
  3. Block User Chat Rooms
  4. Delete Chat Rooms or Single Message