In this episode, we tackle the classic chat UI challenge—displaying messages so that the newest ones always show up at the bottom, and making sure the scroll position stays pinned there, even when new messages arrive.
We start by building a wrapper for our chat messages and styling it so it looks like a proper chat window. That includes setting the height, adding nice rounded corners, some subtle borders, and enabling vertical scrolling with overflow-y: auto
. We then jump into rendering each chat message using a simple v-for loop, displaying the user avatar, their name, the message body, and when it was sent, all cleanly organized with a bit of Flexbox styling.
At this point, though, we notice a big problem: the latest message appears at the top, and when refreshing or when new messages arrive, our scroll position stays at the top as well. Not what we want! To fix this, we apply a Flexbox trick: we add flex-col-reverse
to our message container. This flips the display order so the newest messages always appear at the bottom. Plus, because of how scrolling works in this setup, the chat view is kept pinned to the latest message unless the user scrolls up manually.
To wrap things up, there's a quick mention that you could introduce an Intersection Observer to load more messages as the user scrolls to the top—perfect for implementing infinite scroll later!
By the end of this video, you’ll understand how to use Flexbox to perfectly order and scroll chat messages in a modern chat interface.