Skip to main content
Version: v4

Multi-Tab Chat UI Guide

This guide will help you achieve a tabbed layout (aka Multi-Tab Chat UI) of the components available in CometChatUIKit for React.

Image

We’ll use the CometChatConversationsWithMessages, CometChatUsersWithMessages, and CometChatGroupsWithMessages components and launch them as individual tab items within the tabbed layout.

Step 1: Create a new component CometChatUI

export function CometChatUI() {

return (
<div style={{ width: "100%", height: "100%" }}><div>
);
}

Step 2: Import the CometChatTabs component from the UI Kit.

import { CometChatTabs } from "@cometchat/chat-uikit-react";

export function CometChatUI() {
return (
<div style={{ width: "100%", height: "100%" }}>
<CometChatTabs
tabAlignment={TabAlignment.bottom}
tabs={tabs}
tabsStyle={tStyle}
/>
</div>
);
}

Step 3: Pass CometChatConversationsWithMessages, CometChatUsersWithMessages, CometChatGroupsWithMessages components to the Tabs components to launch them as individual tab item.

import { React, useEffect, useState } from "react";
import { TabAlignment, CometChatTabItem } from "@cometchat/uikit-resources";
import {
CometChatUsersWithMessages,
CometChatGroupsWithMessages,
CometChatConversationsWithMessages,
CometChatTabs,
} from "@cometchat/chat-uikit-react";

import usersTabIcon from "./assets/users.svg";
import groupsTabIcon from "./assets/groups.svg";
import chatsTabIcon from "./assets/chats.svg";

export function CometChatUI() {
const chatsTab = new CometChatTabItem({
id: "chats",
iconURL: chatsTabIcon,
title: "Chats",
childView: <CometChatConversationsWithMessages />,
});

const usersTab = new CometChatTabItem({
id: "users",
iconURL: usersTabIcon,
title: "Users",
childView: <CometChatUsersWithMessages />,
});

const groupsTab = new CometChatTabItem({
id: "groups",
title: "Groups",
iconURL: groupsTabIcon,
childView: <CometChatGroupsWithMessages />,
});

const tabs = [chatsTab, usersTab, groupsTab];

return (
<div style={{ width: "100%", height: "100%" }}>
<CometChatTabs
tabAlignment={TabAlignment.bottom}
tabs={tabs}
tabsStyle={tStyle}
/>
</div>
);
}

Step 4: We make the Chats tab the active tab by default. Apply styles to all the tab items, as well as the entire tabs component, and align them to the bottom of the container. The tab items are draggable so that the user is able to drag and drop them within the container.

import { BaseStyle, TabItemStyle } from "@cometchat/uikit-shared";
import {
CometChatConversationsWithMessages,
CometChatGroupsWithMessages,
CometChatTabs,
CometChatUsersWithMessages,
TabsStyle
} from "@cometchat/chat-uikit-react";
import { CometChatTabItem, TabAlignment } from "@cometchat/uikit-resources";
import { useEffect, useState } from "react";
import usersTabIcon from "./assets/users.svg";
import groupsTabIcon from "./assets/groups.svg";
import chatsTabIcon from "./assets/chats.svg";
export function CometChatUI() {
const [isMobileView, setIsMobileView] = useState(false);
const tabItemStyle = new TabItemStyle({
iconTint: "#6851D6",
width: "93px",
height: "auto",
activeTitleTextColor: "#6851D6",
background:'#fff',
activeIconTint:'#6851D6',
});
const tStyle = new TabsStyle({
tabListHeight: "50px",
tabListWidth: "280px",
});
const chatsTab = new CometChatTabItem({
id: "chats",
title: "Chats",
iconURL: chatsTabIcon,
style: tabItemStyle,
isActive: true,
childView: (
<CometChatConversationsWithMessages isMobileView={isMobileView} />
),
});
const usersTab = new CometChatTabItem({
id: "users",
title: "Users",
iconURL: usersTabIcon,
style: tabItemStyle,
childView: <CometChatUsersWithMessages />,
});
const groupsTab = new CometChatTabItem({
id: "groups",
title: "Groups",
iconURL: groupsTabIcon,
style: tabItemStyle,
childView: <CometChatGroupsWithMessages isMobileView={isMobileView} />,
});
const tabs = [chatsTab, usersTab, groupsTab];
const resizeWindow = () => {
const innerWidth = window.innerWidth;
if (innerWidth >= 320 && innerWidth <= 767) {
setIsMobileView(true);
} else {
setIsMobileView(false);
}
};
useEffect(() => {
resizeWindow();
window.addEventListener("resize", resizeWindow);
return () => window.removeEventListener("resize", resizeWindow);
}, []);
return (
<div style={{ width: "100%", height: "100%" }}>
<CometChatTabs
tabAlignment={TabAlignment.bottom}
tabs={tabs}
tabsStyle={tStyle}
/>
</div>
);
}

Step 5: Import the CometChatUI component into your App component.

import logo from "./logo.svg";
import "./App.css";
import { CometChatUI } from "./CometChatUI";

function App(props) {
return (
<div className="App">
<CometChatUI />
</div>
);
}

export default App;

You can now see chats, users and group components each as tab items. This is how you can launch CometChat UIKit’s individual component in a tabbed layout. 🎉

Image