1. resources
  2. cookbook
  3. chat

Chat

Create a custom chat feed or AI prompt interface.

J

Jane

Yesterday @ 2:30pm

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Provident blanditiis quidem dolorum ab similique. Voluptatibus quibusdam unde mollitia corrupti assumenda libero. Quibusdam culpa illum unde asperiores accusantium! Unde, cupiditate tenetur.

Michael

Yesterday @ 2:45pm

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Provident blanditiis quidem dolorum ab similique. Voluptatibus quibusdam unde mollitia corrupti assumenda libero. Quibusdam culpa illum unde asperiores accusantium! Unde, cupiditate tenetur.

M
J

Jane

Yesterday @ 2:50pm

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Provident blanditiis quidem dolorum ab similique. Voluptatibus quibusdam unde mollitia corrupti assumenda libero. Quibusdam culpa illum unde asperiores accusantium! Unde, cupiditate tenetur.

Michael

Yesterday @ 2:52pm

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Provident blanditiis quidem dolorum ab similique. Voluptatibus quibusdam unde mollitia corrupti assumenda libero. Quibusdam culpa illum unde asperiores accusantium! Unde, cupiditate tenetur.

M

Layout Columns

Use Tailwind’s grid column utility classes to define horizontal columns for your layout.

(nav)
(feed)
(online)

Layout Rows

Use Tailwind’s grid row utility classes to define vertical layout rows for your layout.

(search)
(list)
(footer)

(feed)

Lorem, ipsum dolor sit amet consectetur adipisicing elit. Reprehenderit dolor ullam, qui et itaque quam distinctio dicta nostrum veritatis harum iure hic sequi aperiam, explicabo earum totam deserunt. Fugiat, temporibus.

(prompt)

Message Feed

The feed simply loops through the available feed data. Each <pre> tag represents a single bubble element.

{
  "role": "host",
  "id": 0,
  "host": true,
  "avatar": 48,
  "name": "Jane",
  "timestamp": "Yesterday @ 2:30pm",
  "message": "Some message text.",
  "color": "variant-soft-primary"
}
{
  "role": "guest",
  "id": 1,
  "host": false,
  "avatar": 14,
  "name": "Michael",
  "timestamp": "Yesterday @ 2:45pm",
  "message": "Some message text.",
  "color": "variant-soft-primary"
}

Message Bubbles

Provide styling to each bubble element.

J

Jane

Yesterday @ 2:30pm

Some message text.

Michael

Yesterday @ 2:45pm

Some message text.

M

Prompt

Use Skeleton’s input group styles to create a custom text prompt.


Scroll to Bottom

Bind your scrollable feed panel element reference (Svelte | React), then use scrollTo to scroll the panel to the bottom on demand. Scroll behavior can be set via behavior: 'smooth'.

function scrollChatBottom(behavior?: 'auto' | 'instant' | 'smooth' = 'smooth') {
// `elemChat` represents our scrollable panel element
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.

// Svelte Element Reference
let elemPrompt: HTMLElement;
// React Element Reference
let elemPrompt: HTMLElement = useRef();
<!-- Svelte Binding --->
<textarea bind:value={elemPrompt} ... />
<!-- React Binding --->
<textarea ref={elemPrompt} ... />

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: elemPrompt.value,
color: 'preset-tonal-primary'
};
// Append the new message to the message feed
messageFeed = [...messageFeed, newMessage];
// Clear the textarea message
elemPrompt.value = '';
// 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 ... onclick={addMessage}>Send</button>