# Chat
> A chat panel, message bubbles and a prompt input for AI and messaging UIs.
Source: https://shellui.dev/docs/v0.4/components/advanced/chat
The chat components are **new in v0.4**. `Chat` is the panel, `ChatMessage` is one bubble, and `ChatInput` is the prompt box. They only render the conversation: you keep the message list and call your own model or API.
## Installation
```bash
shellui add chat
```
This installs all three. `chat-message` and `chat-input` can also be added on their own.
## Usage
### A working chat
`Chat` has three slots: `Header`, the messages (`ChildContent`) and `Footer`. The message area scrolls and stays pinned to the newest message. While you wait for a reply, render a `ChatMessage` with `IsStreaming`, and set `IsLoading` on the input so a second message can't be sent.
```razor
Assistant@messages.Count messages
@foreach (var message in messages)
{
}
@if (thinking)
{
}
@code {
private readonly List<(string Role, string Text)> messages = new()
{
("assistant", "Hi! I'm a ShellUI chat demo. Ask me anything."),
};
private string? draft;
private bool thinking;
private async Task SendAsync(string text)
{
messages.Add(("user", text));
thinking = true;
StateHasChanged();
await Task.Delay(800);
messages.Add(("assistant", $"You said:\n{text}"));
thinking = false;
}
}
```
### Messages
`Role` sets the side and colour: `user` is right-aligned in the primary colour, `assistant` is left-aligned and muted, and `system` is an outlined note. Use `Text` for plain text, where line breaks are kept, or child content for markup. `Avatar` and `Actions` are optional slots.
```razor
UI
Run shellui add button, then use <Button> in any page.
```
### Input
Enter sends, and Shift+Enter adds a line. `OnSubmit` receives the text, and the bound value is cleared. `Actions` adds buttons next to the send button.
```razor
Last sent: @(sent ?? "nothing yet")
@code {
private string? draft;
private string? sent;
private void Send(string text) => sent = text;
}
```
## Streaming a reply
To stream tokens, add an empty assistant message, append to it as chunks arrive and call `StateHasChanged()`:
```razor
@code {
private string reply = "";
private bool streaming;
private async Task SendAsync(string prompt)
{
reply = "";
streaming = true;
await foreach (var chunk in Client.StreamAsync(prompt))
{
reply += chunk;
StateHasChanged();
}
streaming = false;
}
}
```
## API Reference
### Chat
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `Height` | `string?` | `null` | CSS height of the panel, such as `380px`; without it the panel grows with its messages |
| `Header` | `RenderFragment?` | `null` | Content above the messages |
| `ChildContent` | `RenderFragment?` | `null` | The messages |
| `Footer` | `RenderFragment?` | `null` | Content below the messages, usually a `ChatInput` |
| `Class` | `string?` | `null` | Additional CSS classes |
### ChatMessage
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `Role` | `string` | `"assistant"` | `user`, `assistant` or `system` |
| `Text` | `string?` | `null` | Plain-text content; line breaks are kept |
| `Name` | `string?` | `null` | Sender name above the bubble |
| `Timestamp` | `string?` | `null` | Time shown next to the name |
| `IsStreaming` | `bool` | `false` | Shows the typing dots |
| `Avatar` | `RenderFragment?` | `null` | Avatar beside the bubble |
| `Actions` | `RenderFragment?` | `null` | Buttons under the bubble |
| `ChildContent` | `RenderFragment?` | `null` | Markup content, rendered after `Text` |
| `Class` | `string?` | `null` | Additional CSS classes |
### ChatInput
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `Value` | `string?` | `null` | The draft; use `@bind-Value` |
| `ValueChanged` | `EventCallback` | - | Fires as the draft changes |
| `OnSubmit` | `EventCallback` | - | Fires with the text on Enter or the send button |
| `Placeholder` | `string` | `"Send a message..."` | Placeholder, also used as the accessible name |
| `Disabled` | `bool` | `false` | Disables the input |
| `IsLoading` | `bool` | `false` | Blocks sending and shows a spinner in the send button |
| `Rows` | `int` | `1` | Initial height in text rows |
| `Actions` | `RenderFragment?` | `null` | Buttons before the send button |
| `Class` | `string?` | `null` | Additional CSS classes |
## Accessibility
- The message area is `role="log"` with `aria-live="polite"`, so new messages are announced
- The typing indicator is labelled "Typing"
- The send button is labelled "Send" and is disabled while the draft is empty