Chat
Create a custom chat feed or AI prompt interface using features provided by Skeleton and Tailwind.
Demo
Jane
Yesterday @ 2:30pmOmnis cumque accusantium eaque. Repellendus quam rem iusto aliquid odio expedita labore aperiam ea. Ratione asperiores incidunt itaque.
Michael
Yesterday @ 2:45pmOmnis cumque accusantium eaque. Repellendus quam rem iusto aliquid odio expedita labore aperiam ea. Ratione asperiores incidunt itaque.
Jane
Yesterday @ 2:50pmOmnis cumque accusantium eaque. Repellendus quam rem iusto aliquid odio expedita labore aperiam ea. Ratione asperiores incidunt itaque.
Michael
Yesterday @ 2:52pmOmnis cumque accusantium eaque. Repellendus quam rem iusto aliquid odio expedita labore aperiam ea. Ratione asperiores incidunt itaque.
If you wish to review the source for the complete example above, please tap the Page Source button near the top of the page. We'll cover each core principle in the sections below.
Layout Columns
We recommend using Tailwind's grid column utility classes to define horizontal columns for your layout.
Layout Rows
We recommend using Tailwind's grid row utility classes to define vertical layout rows for your layout.
Message Feed
Within our feed element, we'll generate a feed of messages. Note that we'll replace the pre tags in the next section.
Message Bubbles
By mixing Skeleton features with Tailwind styling, we can provided message bubble interfaces for each type of feed post.
Prompt
We can utilize a Skeleton Input Group to create a custom text prompt.
Scroll to Bottom
Chat windows typically remain fixed at the bottom of the scrollable pane. This can be triggered on page load and when a new message is added.
Use bind:this
to bind your scrollable feed element.
let elemChat: HTMLElement;
<div bind:this={elemChat} class="overflow-y-auto">(chat)</div>
Then use Javascript's scrollTo
method to scroll the binded element to the bottom on demand. You may also set smooth scrolling via
behavior: 'smooth'
.
function scrollChatBottom(behavior?: ScrollBehavior): void {
elemChat.scrollTo({ top: elemChat.scrollHeight, behavior });
}
Add a Message
Below we'll cover how to append the message feed with a new message from the host user.
Per our above examples, we'll use the same messageFeed
data structure.
let messageFeed = [ /* ...*/ ];
Then bind to the textarea for your prompt in order to capture any message typed by the user.
let currentMessage = '';
<textarea
bind:value={currentMessage} name="prompt" id="prompt"
placeholder="Write a message..."
/>
Here's an example of how we might append a new message to the messageFeed
array.
function addMessage(): void {
const newMessage = {
id: messageFeed.length,
host: true,
avatar: 48,
name: 'Jane',
timestamp: new date(),
message: currentMessage,
color: 'variant-soft-primary'
};
// Append the new message to the message feed
messageFeed = [...messageFeed, newMessage];
// Clear the textarea message
currentMessage = '';
// Smoothly scroll to the bottom of the feed
setTimeout(() => { scrollChatBottom('smooth'); }, 0);
}
This can be triggered when the prompt's SEND button is clicked.
<button ... on:click={addMessage}>Send</button>