Custom headers

You can easily customize (or omit entirely) the chat headers that TalkJS provides out the box. This opens up the possibility to create a completely new header element for your TalkJS UIs and include custom content in them such as product/order information, additional user information or something different altogether.

Removing the default header

To remove the default Chatbox header, simply create a chatbox as you normally would do, but also pass in the optional chatboxOptions object and set showChatHeader to false as shown below.

const chatbox = session.createChatbox({
// Don't use the default header provided by TalkJS
showChatHeader: false,

Although the above example uses the Chatbox, the header for the Inbox and Popup UI can similarly be removed.

Creating a custom header

To create a custom header start by moving the div where TalkJS is mounted to inside a new container div which we'll call chatbox-container. This div will be responsible for holding the custom header and the conversation UI. Next, add another div which we'll call chatbox-header to be used for our custom header, as shown in the example below.

<!-- Container element for all TalkJS UI elements -->
<div class="chatbox-container">
<!-- Custom TalkJS chat header -->
<div class="chatbox-header"></div>
<!-- container element in which TalkJS will display a chat UI -->
<div id="talkjs-container" style="height: 500px"><i>Loading chat...</i></div>

Now you can style the new custom-header as you wish or add additional features! For a full example implementation of using custom header see our how-to blog post.

Custom headers for the inbox

The inbox is a more complex UI because it allows the user to switch between conversations they're a part of. It has 2 separate parts: the feed (list of conversations) and the chatbox where the current conversation is shown.

Adjusting the UI for different screen sizes

If the inbox is wider than 640px, it is shown in desktop mode, where these parts are shown next to each other, with the feed on the left and the chatbox on the right (for left-to-right languages). If the inbox is 640px wide or narrower, we only show the feed if no conversation is selected, and only the chatbox if a conversation is selected.

If you look at the CSS in the full example at the end of this tutorial, you'll see how you can adjust the widths of your headers, and whether they are shown, for different screen sizes. The widths and margins are based on TalkJS's default theme, so if you're using a custom theme, you may need to adjust the widths and margins of your headers as well.

Handling the ConversationSelected event

To respond to the user selecting a chat or going back to the feed, you'll want to listen for the conversationSelected event. When the user selects a conversation, your handler will receive a ConversationSelectedEvent object containing information about the selected conversation and its participants. When the user goes back to the feed, the handler is called with null. You can also use this event to update the chatbox header to show the conversation's subject and/or participants.

inbox.onConversationSelected(({ conversation }) => {
if (conversation === null) {
// Make sure the feed header is shown, and the chatbox header is hidden.
} else {
// Make sure the chatbox header is shown, and the feed header is hidden.
// Maybe update the chatbox header to reflect the selected chat, with something like this:
document.getElementById('chat-header').innerText = conversation.subject;

Adding a custom "Back" button

By default, we show a button at the top of the chatbox, allowing the user to go back to the feed. If you'd prefer to put this back button in the header, or somewhere else on your site, you can hide the built-in back button by passing showMobileBackButton: false as an option when creating the inbox. Then, when the user clicks your custom back button, call and they'll be taken back to the feed. The conversationSelected event is still triggered, even though the selection was done programmatically, so no need to update your layout here.

A complete custom header example

Here's a complete example of a custom header for the inbox. To run and edit this yourself, see our JSFiddle example.

<div id="container">
<div id="header-row">
<div id="feed-header">Conversations</div>
<div id="chatbox-header">
<a href="#" id="back-btn">⬅︎</a>
<span id="chat-title">Chat Header</span>
<div id="talkjs-container" />

To learn more about customizing the rest of the interface, read up on our themes feature. As always, do get in touch with us through live chat or send us an email if you need help with anything TalkJS related!