Chat
A chat panel, message bubbles and a prompt input for AI and messaging UIs.
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
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.
<Chat Height="380px" Class="w-full max-w-xl">
<Header>
<div class="flex items-center justify-between">
<span class="text-sm font-medium text-foreground">Assistant</span>
<span class="text-xs text-muted-foreground">@messages.Count messages</span>
</div>
</Header>
<ChildContent>
@foreach (var message in messages)
{
<ChatMessage Role="@message.Role" Name="@(message.Role == "user" ? "You" : "ShellUI")" Text="@message.Text" />
}
@if (thinking)
{
<ChatMessage Role="assistant" Name="ShellUI" IsStreaming="true" />
}
</ChildContent>
<Footer>
<ChatInput @bind-Value="draft" OnSubmit="SendAsync" IsLoading="thinking" />
</Footer>
</Chat>
@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.
shellui add button, then use <Button> in any page.
<div class="flex w-full max-w-xl flex-col gap-4">
<ChatMessage Role="system" Text="The conversation started." />
<ChatMessage Role="user" Name="You" Timestamp="09:41" Text="How do I add a button?" />
<ChatMessage Role="assistant" Name="ShellUI" Timestamp="09:41">
<Avatar>
<span class="text-xs font-semibold">UI</span>
</Avatar>
<ChildContent>
Run <code class="rounded bg-background px-1 py-0.5 text-xs">shellui add button</code>, then use <code class="rounded bg-background px-1 py-0.5 text-xs"><Button></code> in any page.
</ChildContent>
<Actions>
<CopyButton Text="shellui add button" />
</Actions>
</ChatMessage>
<ChatMessage Role="assistant" Name="ShellUI" IsStreaming="true" />
</div>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.
Last sent: nothing yet
<div class="w-full max-w-xl space-y-3">
<ChatInput @bind-Value="draft" OnSubmit="Send" Placeholder="Ask a question..." Rows="2">
<Actions>
<Button Variant="ButtonVariant.Ghost" Size="ButtonSize.Sm" OnClick="@(() => draft = "Summarize this page")">Suggest</Button>
</Actions>
</ChatInput>
<p class="text-sm text-muted-foreground">Last sent: @(sent ?? "nothing yet")</p>
</div>
@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():
<ChatMessage Role="assistant" Text="@reply" IsStreaming="@streaming" />
@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<string?> |
- | Fires as the draft changes |
OnSubmit |
EventCallback<string> |
- | 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"witharia-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