Yesterday I created the front-end of the ChatBox for TripTime :)
Here’s what it looks like:
I met with a couple of challenges along the way, but happy to see how it turns out to look.
Positioning of the bubbles
Using the grid display to position the chat bubbles were my first challenge. Here’s what the code looks like for the container of message bubbles, which takes up the 100% width of the chat box and takes care of the positioning of avatar, info and chat content:
1 | .chatMessageContainer, .myChatMessageContainer { |
Here’s the grid structure of the containers:
Note here
For grie-template-columns
/grie-template-rows
,
auto
means the width of the child depends on itself.1fr
means the child takes up one fraction of whatever space is left.
So the empty space takes up1fr
, and the bubble takes upauto
.
The little triangle attached to the bubble
To get this tiny triangle, I added `before` pseudo-element for the bubbles:1 | .messageContent::before, .myMessageContent::before{ |
Managing the width of the bubble
The width of the bubble were adjusted responsively.
Firstly, for any screen size:
1 | .messageContent, .myMessageContent { |
Then for big screen:
1 | @media screen and (min-width: 1000px) { |
For small screen:
1 | @media screen and (max-width: 1000px) { |
When the 70vw is bigger than the space available, it will just take up the whole space instead of flowing out, which is pretty handy.
Keep at the bottom of container
To keep the ChatBox showing the newest message, I used an npm package react-scroll-to-bottom:
React container that will auto scroll to bottom or top if new content is added and viewport is at the bottom, similar to tail -f. Otherwise, a “jump to bottom” button will be shown to allow user to quickly jump to bottom.
1 | import ScrollToBottom from 'react-scroll-to-bottom'; |
Handling incoming messages
Only the ChatBox
component knows about the chat message list API.
1 | // inside ChatBox class: |
The ChatMessageList
will know about the current message list, and the user ID (so that it can tell between my messages and others):
1 | <ChatMessageList |
The ChatInputForm
will know about how to handle the user’s new message:
1 | <ChatInputForm |
The code can be find here.
The CSS module is here.
It’s fun to create a cutie like this, let’s see how it works with WebSocket in the future:)