Message

The Message component displays a chat interface message from either a user or an AI.
It includes an avatar, a name, and a message content.

Hello, how are you?
Ha
Hello! I'm doing well, thank you for asking. How can I help you today?
As
Can you help me understand Svelte 5 runes?
Ha
Absolutely! Svelte 5 introduces runes like $state, $derived, and $props which provide a more powerful and flexible reactivity system. Would you like me to explain any specific rune?
As

Installation

pnpm dlx shadcn-svelte@latest add https://svelte-ai-elements.vercel.app/r/message.json

If you prefer using jsrepo, please install via the command below:

pnpm dlx jsrepo add @ai/elements/ai-elements/message

Usage

Usage with AI SDK

Render messages in a list with the AI SDK's Chat class.

Add the following component to your frontend:

+page.svelte

Add the following route to your backend:

api/chat/+server.ts

Props

Message

PropTypeDefaultDescription
from'user' | 'assistant'-The role of the message sender (user or assistant)
childrenSnippet-Child components to render (typically MessageContent and MessageAvatar)
classstring-Additional CSS classes to apply to the message container
...restPropsHTMLAttributes<HTMLDivElement>-All other div props are supported

MessageAvatar

PropTypeDefaultDescription
srcstring-URL of the avatar image
namestring-Name of the user (used for fallback text)
classstring-Additional CSS classes to apply to the avatar
...restPropsComponentProps<typeof Avatar>-All other Avatar component props are supported

MessageContent

PropTypeDefaultDescription
variant'contained' | 'flat''contained'Visual style variant of the message content
childrenSnippet-Content to render inside the message
classstring-Additional CSS classes to apply to the content container
...restPropsHTMLAttributes<HTMLDivElement>-All other div props are supported