Skip to content
Chat ChatMessage

ChatMessage

One message of a conversation, with an avatar, a surface, a header, attachments and actions. It renders the markup or text you give it: markdown is the app's job (render it with Markdig and pass the HTML as content).

On this page

Examples

Roles and variants

A user message sits on the right with a soft surface, the others on the left with none. A message that is still arriving is marked busy, so it is announced once it is complete.

Today, 9:41

How do I stream a reply in Blazor?

Await an IAsyncEnumerable<string> and append each token to the message.

Variant Solid

Variant Outline

Variant Soft

Variant Subtle

Variant Naked

A compact, solid message on the left.

Razor
@* Rendering markdown is up to the app: render the text with Markdig (Markdown.ToHtml) and give the result as ChildContent
   with a MarkupString. Here the content is plain markup. *@
<div class="flex w-full max-w-xl flex-col gap-4" data-testid="chat-message-example">
    <KlyChatMessage Role="ChatRole.System" Text="Today, 9:41" />
    <KlyChatMessage Role="ChatRole.User" Text="How do I stream a reply in Blazor?" data-testid="chat-message-user" />
    <KlyChatMessage Role="ChatRole.Assistant" AvatarText="B" AvatarAlt="Klyroo assistant" data-testid="chat-message-assistant">
        <p>Await an <code>IAsyncEnumerable&lt;string&gt;</code> and append each token to the message.</p>
    </KlyChatMessage>
    <div class="flex flex-col gap-2">
        @foreach (var variant in Enum.GetValues<ChatMessageVariant>())
        {
            <KlyChatMessage Role="ChatRole.Assistant" Variant="variant" Text="@($"Variant {variant}")" />
        }
    </div>
    <KlyChatMessage Role="ChatRole.User" Side="ChatSide.Left" Variant="ChatMessageVariant.Solid" Color="KlyColor.Primary" Compact Text="A compact, solid message on the left." />
</div>

Files and actions

Header and Files above the text, Actions below it.

report.pdfPDF · 242.2 KB
notes.txtTXT · 1.2 KB

Can you summarize these?

Klyroo assistant · 9:42

The report covers three themes: cost, speed and accessibility.

Razor
@* Actions sit under the message, Files above it. Copying needs interactivity, so the buttons wait for it. *@
<div class="flex w-full max-w-xl flex-col gap-4" data-testid="chat-message-actions-example">
    <KlyChatMessage Role="ChatRole.User" Text="Can you summarize these?">
        <Files>
            <KlyChatAttachment Name="report.pdf" Type="application/pdf" FileSize="248000" Size="KlySize.Sm" />
            <KlyChatAttachment Name="notes.txt" Type="text/plain" FileSize="1200" Size="KlySize.Sm" />
        </Files>
    </KlyChatMessage>
    <KlyChatMessage Role="ChatRole.Assistant" Text="The report covers three themes: cost, speed and accessibility." AvatarText="B" AvatarAlt="Klyroo assistant">
        <Header>Klyroo assistant · 9:42</Header>
        <Actions>
            <KlyButton Square Size="KlySize.Xs" Variant="ButtonVariant.Ghost" Color="KlyColor.Neutral" aria-label="Copy" OnClick="Copy"
                      LeadingIcon="@KlyIcons.Named("copy", "size-4")" data-testid="chat-message-copy" />
            <KlyButton Square Size="KlySize.Xs" Variant="ButtonVariant.Ghost" Color="KlyColor.Neutral" aria-label="Regenerate" OnClick="Regenerate"
                      LeadingIcon="@KlyIcons.Named("refresh-cw", "size-4")" data-testid="chat-message-regenerate" />
            @if (_note is not null)
            {
                <span role="status" class="text-xs" data-testid="chat-message-note">@_note</span>
            }
        </Actions>
    </KlyChatMessage>
</div>

@code {
    private string? _note;

    private void Copy() => _note = "Copied";

    private void Regenerate() => _note = "Regenerating";
}

API

KlyChatMessage

One message of a conversation, aligned by role, with an avatar, actions and a streaming state.

Parameters

Name Type Default Description
AvatarAlt string Name of the author for the avatar. Defaults to the name of the role.
AvatarSrc string Image of the author; the initials of AvatarAlt show when there is none.
AvatarText string Initials of the avatar when there is no picture.
Class string Classes added to the root element, merged last so they win over the theme.
Color KlyColor "neutral" Semantic color of the chat message: a built-in KlyColor or the name of a color you defined. Values: primary, secondary, success, info, warning, error, neutral.
Compact bool false Tighter spacing, for narrow panels such as KlyChatPalette .
Role ChatRole ChatRole.Assistant Who wrote it: tints nothing, but sets the default side and look, and the accessible name of the message. Values: User, Assistant, System.
Side ChatSide? The side of the conversation. A user message sits on the right, the others on the left. Values: Left, Right.
Streaming bool false Marks the message as still arriving. Pair it with the changing content.
Text string The message as plain text, line breaks kept. Ignored when ChildContent is set.
Ui ChatMessageUi Per-instance class overrides of the slots of the chat message, see ChatMessageUi . They are merged after the theme and the Class parameter wins on the root slot.
Variant ChatMessageVariant? The surface of the message. A user message is soft, the others have none. Values: Solid, Outline, Soft, Subtle, Naked.

Slots

Name Type Description
Actions RenderFragment Below the message: copy, retry, thumbs.
Files RenderFragment Attachments of the message, above the text: KlyChatAttachment items.
Header RenderFragment Above the message: a name, a time.
Icon RenderFragment An icon in place of the avatar picture.
Leading RenderFragment Replaces the avatar altogether. Without any of the avatar parameters nothing is shown.
ChildContent RenderFragment The message, as markup. Rendering markdown is the app's job; nothing here parses it.

Ui slots

Ui takes a ChatMessageUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyChatMessage , after the theme and the app configuration.
Container Classes merged into the container slot of KlyChatMessage , after the theme and the app configuration.
Leading Classes merged into the leading slot of KlyChatMessage , after the theme and the app configuration.
Wrapper Classes merged into the wrapper slot of KlyChatMessage , after the theme and the app configuration.
Header Classes merged into the header slot of KlyChatMessage , after the theme and the app configuration.
Files Classes merged into the files slot of KlyChatMessage , after the theme and the app configuration.
Content Classes merged into the content slot of KlyChatMessage , after the theme and the app configuration.
Text Classes merged into the text slot of KlyChatMessage , after the theme and the app configuration.
Actions Classes merged into the actions slot of KlyChatMessage , after the theme and the app configuration.