Skip to content
Chat ChatPrompt

ChatPrompt

A message form with a textarea that grows with its text. Enter sends, Shift+Enter adds a line, an IME composition never sends. It works in static server rendering as a form post.

On this page

Examples

Send a message

The same code runs in every mode: interactive modes report the text through the browser, static rendering posts the form under FormName.

Razor
@* Enter sends, Shift+Enter adds a line, and an IME composition never sends. The same code works in every render mode:
   interactive, the text reaches OnSubmit through the browser; in static rendering the form posts under FormName and the page
   renders the reply (the list then holds only that exchange, since nothing remembers the earlier ones). *@
<div class="flex w-full max-w-xl flex-col gap-3" data-testid="chat-prompt-example">
    @foreach (var (text, reply) in _exchanges)
    {
        <KlyChatMessage Role="ChatRole.User" Text="@text" data-testid="chat-prompt-sent" />
        <KlyChatMessage Role="ChatRole.Assistant" Text="@reply" data-testid="chat-prompt-reply" />
    }
    <KlyChatPrompt FormName="chat-prompt-ssr" OnSubmit="Send" Placeholder="Write a message..." data-testid="chat-prompt" />
</div>

@code {
    private readonly List<(string Text, string Reply)> _exchanges = [];

    private void Send(string text) => _exchanges.Add((text, ChatDemo.Reply(text)));
}

With attachments

Attachments above the field; a message may be sent with no text while some are attached.

report.pdfPDF · 242.2 KB
budget.xlsxXLSX · 31.1 KB
Nothing sent yet
Razor
@* Attachments are yours to manage: show a chip per file and take it out of the list when it is removed. A message with
   attachments may be sent with no text. Removing needs interactivity. *@
<div class="w-full max-w-xl" data-testid="chat-prompt-attachments-example">
    <KlyChatPrompt OnSubmit="Send" Placeholder="Add a note..." Variant="FieldVariant.Subtle" data-testid="chat-prompt-attachments">
        <Attachments>
            @foreach (var file in _files)
            {
                var f = file;
                <KlyChatAttachment @key="f.Name" Name="@f.Name" Type="@f.Type" FileSize="f.Size" Size="KlySize.Sm" Removable OnRemove="() => Remove(f)" />
            }
        </Attachments>
        <Footer>
            <span class="text-xs text-(--klyroo-text-muted)" data-testid="chat-prompt-attachments-last">@(_last ?? "Nothing sent yet")</span>
        </Footer>
    </KlyChatPrompt>
</div>

@code {
    private sealed record Attached(string Name, string Type, long Size);

    private readonly List<Attached> _files = [new("report.pdf", "application/pdf", 248000), new("budget.xlsx", "application/vnd.ms-excel", 31800)];
    private string? _last;

    private void Remove(Attached file) => _files.Remove(file);

    private void Send(string text)
    {
        _last = $"Sent \"{text}\" with {_files.Count} file(s)";
        _files.Clear();
    }
}

API

KlyChatPrompt

Chat message form with a growing textarea that submits on Enter, plus slots for actions and a submit button.

Parameters

Name Type Default Description
Autofocus bool false Focuses the field when the page loads.
Class string Classes added to the root element, merged last so they win over the theme.
Color KlyColor "primary" Semantic color of the chat prompt: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else the default of the component. Values: primary, secondary, success, info, warning, error, neutral.
Disabled bool false Disables the text area and the submit button.
Enhance bool false Enhanced form handling in static server rendering: the post updates the page without a reload.
FormName string Name of the form. Required to send in static server rendering, where the page receives the form post and calls OnSubmit ; one prompt per page and name. Posting is for static rendering only: under an interactive render mode (prerendering included) the form never posts, and Enter does nothing until the page is interactive.
Label string Accessible name of the form and the field. Defaults to the built-in text.
MaxRows int 6 The field grows with its text up to this many rows, then scrolls.
Placeholder string Hint shown in the text area while it is empty. Defaults to the built-in text.
Rows int 1 Rows of an empty field.
Status ChatStatus ChatStatus.Idle Where the conversation is. While Submitted or Streaming a message cannot be sent (the submit button becomes a stop button); the field stays editable so the next message can be drafted. Values: Idle, Submitted, Streaming, Error.
SubmitOnEnter bool true Enter sends and Shift+Enter adds a line. Set it to false and Ctrl or Cmd plus Enter sends instead.
Ui ChatPromptUi Per-instance class overrides of the slots of the chat prompt, see ChatPromptUi . They are merged after the theme and the Class parameter wins on the root slot.
Value string Text the field starts with: a draft, or the message to show again after a post.
Variant FieldVariant FieldVariant.Outline Look of the prompt, as a FieldVariant . Values: Outline, Soft, Subtle, Ghost, None.

Events

Name Type Description
OnReload EventCallback Called when the retry button of the default submit button is pressed.
OnStop EventCallback Called when the stop button of the default submit button is pressed.
OnSubmit EventCallback<string> Called with the text of the message, when the user sends it. Without a handler, nothing leaves the browser.

Slots

Name Type Description
Attachments RenderFragment Above the field: KlyChatAttachment items. A message may be sent with no text while this is set.
Footer RenderFragment Below the field: buttons to attach files, a model picker.
Leading RenderFragment Before the field, in its row.
Trailing RenderFragment Replaces the submit button, after the field. An empty fragment hides it.

Ui slots

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

Slot Styles
Root Classes merged into the root slot of KlyChatPrompt , after the theme and the app configuration.
Form Classes merged into the form slot of KlyChatPrompt , after the theme and the app configuration.
Attachments Classes merged into the attachments slot of KlyChatPrompt , after the theme and the app configuration.
Row Classes merged into the row slot of KlyChatPrompt , after the theme and the app configuration.
Textarea Classes merged into the textarea slot of KlyChatPrompt , after the theme and the app configuration.
Trailing Classes merged into the trailing slot of KlyChatPrompt , after the theme and the app configuration.
Footer Classes merged into the footer slot of KlyChatPrompt , after the theme and the app configuration.