Skip to content
Editor Editor

Editor

A rich text editor built on TipTap. The value is an HTML string; it loads from a separate file only on pages that have an editor, and static pages show the HTML read-only until it is ready.

On this page

Examples

Bound with menus

The value reaches .NET on blur and after 400 ms without typing, not on every key. The text below shows the bound HTML.

TextPlain paragraph
Heading 1Big section heading
Heading 2Medium section heading
Heading 3Small section heading
Bulleted listA simple list of points
Numbered listA list with numbers
QuoteQuote a passage
Code blockMonospaced code
DividerA horizontal line
Ada LovelaceMathematician
Alan TuringComputer scientist
Grace HopperRear admiral
grinning
smile
joy
slight_smile
wink
heart_eyes
thinking
sunglasses
cry
angry
thumbsup
thumbsdown
clap
pray
wave
muscle
heart
fire
star
sparkles
tada
rocket
bulb
white_check_mark
x
warning
bug
pushpin
paperclip
coffee
Hello Klyroo. Second paragraph.

Type / for blocks, @ to mention someone and : followed by a word for an emoji.

<p>Hello <strong>Klyroo</strong>.</p><p>Second paragraph.</p>

Razor
<div class="flex w-full max-w-2xl flex-col gap-3">
    <div class="ms-8">
        <KlyFormField Label="Release notes" Help="Type / for blocks, @@ to mention someone and : followed by a word for an emoji.">
            <KlyEditor @bind-Value="_html" Placeholder="Write something..." data-testid="editor">
                <KlyEditorToolbar />
                <KlyEditorDragHandle />
                <KlyEditorSuggestionMenu />
                <KlyEditorMentionMenu Items="People" />
                <KlyEditorEmojiMenu />
            </KlyEditor>
        </KlyFormField>
    </div>
    <p class="break-all font-mono text-xs text-(--klyroo-text-muted)" data-testid="editor-html">@(string.IsNullOrEmpty(_html) ? "(empty)" : _html)</p>
</div>

@code {
    private string? _html = "<p>Hello <strong>Klyroo</strong>.</p><p>Second paragraph.</p>";

    private static readonly EditorMentionItem[] People =
    [
        new("ada", "Ada Lovelace") { Description = "Mathematician" },
        new("alan", "Alan Turing") { Description = "Computer scientist" },
        new("grace", "Grace Hopper") { Description = "Rear admiral" },
    ];
}

In an EditForm

Posts the HTML under the field name, with validation and aria-invalid, also in static rendering. The posted HTML is user input: sanitize it on the server.

Posted as HTML, also without interactivity.

Draft
Razor
@using System.ComponentModel.DataAnnotations

<EditForm Model="Model" FormName="editor-notes" OnValidSubmit="Save" class="flex w-full max-w-2xl flex-col items-start gap-3">
    <DataAnnotationsValidator />
    <KlyFormField Label="Notes" Description="Posted as HTML, also without interactivity." Field="FieldIdentifier.Create(() => Model!.Notes)" class="w-full">
        <KlyEditor @bind-Value="Model!.Notes" Placeholder="Write a note..." data-testid="editor-form">
            <KlyEditorToolbar />
        </KlyEditor>
    </KlyFormField>
    <KlyButton Type="ButtonType.Submit" Label="Save" />
    @if (_saved is not null)
    {
        <p role="status" class="break-all font-mono text-xs text-(--klyroo-text-muted)" data-testid="saved">Saved: @_saved</p>
    }
</EditForm>

@code {
    [SupplyParameterFromForm(FormName = "editor-notes")]
    private Note? Model { get; set; }

    private string? _saved;

    protected override void OnInitialized() => Model ??= new();

    // The posted HTML is user input: sanitize it on the server before you store or show it anywhere else.
    private void Save() => _saved = Model!.Notes;

    private sealed class Note
    {
        [Required(ErrorMessage = "Write something first.")]
        public string? Notes { get; set; } = "<p>Draft</p>";
    }
}

Floating toolbar and read only

Unbound: the editor keeps its own state in the browser and posts through a hidden input.

Select a few of these words, then use the floating toolbar or press Ctrl+B.

Select some text to format it.

This text can be selected and copied, not edited.
Razor
<div class="flex w-full max-w-2xl flex-col gap-3">
    <KlyFormField Label="Summary" Help="Select some text to format it.">
        <KlyEditor Value="@("<p>Select a few of these words, then use the floating toolbar or press Ctrl+B.</p>")" Name="summary" data-testid="editor-bubble">
            <KlyEditorToolbar Layout="EditorToolbarLayout.Bubble" />
        </KlyEditor>
    </KlyFormField>
    <KlyFormField Label="Read only">
        <KlyEditor Value="@("<p>This text can be selected and copied, not edited.</p>")" ReadOnly="true" data-testid="editor-readonly" />
    </KlyFormField>
</div>

Length limit and accepted addresses

MaxLength stops a text that is too long from being sent (OnLimit says so); ImageSchemes and LinkSchemes refuse the addresses a server would not keep, and say why.

Short.

At most 200 characters of HTML. Links take https and mailto addresses, images https addresses.

Within the limit

<p>Short.</p>

Razor
<div class="flex w-full max-w-2xl flex-col gap-3">
    <KlyFormField Label="Short note" Help="At most 200 characters of HTML. Links take https and mailto addresses, images https addresses.">
        <KlyEditor @bind-Value="_html" MaxLength="200" OnLimit="OnLimit" ImageSchemes="_https" LinkSchemes="_links" Placeholder="Write a short note..." data-testid="limit-editor">
            <KlyEditorToolbar Items="_items" />
        </KlyEditor>
    </KlyFormField>
    <p class="text-sm text-(--klyroo-text-muted)" role="status" data-testid="limit-state">@_state</p>
    <p class="break-all font-mono text-xs text-(--klyroo-text-muted)" data-testid="limit-html">@(string.IsNullOrEmpty(_html) ? "(empty)" : _html)</p>
</div>

@code {
    private static readonly string[] _https = ["https"];
    private static readonly string[] _links = ["https", "mailto"];

    private static readonly IReadOnlyList<EditorToolbarItem> _items =
    [
        new(EditorAction.Bold), new(EditorAction.TaskList), new(EditorAction.Link), new(EditorAction.Image), new(EditorAction.Undo),
    ];

    private string? _html = "<p>Short.</p>";
    private string _state = "Within the limit";

    // The editor does not send a text that is over the limit: _html stays the last one that fit.
    private void OnLimit(KlyLimitEventArgs e) =>
        _state = e.Exceeded ? $"Too long: {e.Length} of {e.Max} characters, not kept" : "Within the limit";
}

API

KlyEditor

Rich text editor that binds to an HTML string, with formatting commands, links, images, task lists and slash, mention and emoji menus.

Parameters

Name Type Default Description
Class string Classes added to the root element, merged last so they win over the theme.
Color KlyColor "primary" Semantic color of the control: a built-in KlyColor or the name of a color you defined. When not set, Color applies, else primary. Values: primary, secondary, success, info, warning, error, neutral.
Disabled bool false Disables the control: it cannot be focused or changed and is not submitted.
Id string The id of the native control. Defaults to the one of the enclosing KlyFormField , else a generated one.
ImageSchemes IReadOnlyList<string> The schemes an image address may have: http and https (the default, null ) or one of them. An address with another scheme, no host, a user name or longer than 2,048 characters is refused when it is entered, with a message that names the schemes, and is not an image when it is pasted or loaded. List what the server keeps: a server that keeps only https images should say so here.
Label string Accessible name of the editable area when there is no enclosing KlyFormField label. Defaults to EditorLabel .
LinkSchemes IReadOnlyList<string> The schemes a link may have, as absolute addresses (for example https , http and mailto ). Without a list, the editor takes what TipTap takes: those schemes and a few more (, ftp:), and relative addresses. With one, an address of another scheme or a relative one is refused when it is entered, with a message that names the schemes, and is not a link when it is pasted, typed or loaded. List what the server keeps.
MaxLength int? The most characters of HTML the editor reports ( ValueChanged ), or null for no limit. A value longer than that is never sent to .NET: the editor keeps it on the page, marks the text invalid ( aria-invalid , and data-over-limit on the element) and raises OnLimit , so the bound value stays the last one that fit. Measured on the HTML the editor writes, in UTF-16 characters; a server that cleans or rewrites the HTML should measure again.
Name string Form field name outside an EditForm . Inside one, the name comes from the bound expression.
Placeholder string Shown while the editor is empty.
ReadOnly bool false The text can be selected and copied but not changed. The value still posts with the form.
Required bool false Marks the control as required, for the browser and for assistive technology.
Size KlySize? Defaults to the size of the enclosing KlyFormField , or medium. Values: Xs, Sm, Md, Lg, Xl.
Ui EditorUi Per-instance class overrides of the slots of the editor, see EditorUi . They are merged after the theme and the Class parameter wins on the root slot.
Value (@bind-Value) string The value of the control. Bind it with @bind-Value .
ValueExpression Expression<Func<string>> The expression of the bound field. The compiler sets it with @bind-Value , and validation uses it to find the field.
Variant FieldVariant FieldVariant.Outline Look of the editor frame, as a FieldVariant . Values: Outline, Soft, Subtle, Ghost, None.

Events

Name Type Description
OnLimit EventCallback<KlyLimitEventArgs> Raised when the HTML goes past MaxLength , while it keeps changing past it, and once more when it is back within it. Settles after 400 ms without typing, like the value.

Slots

Name Type Description
ChildContent RenderFragment Toolbars, drag handle and suggestion menus: components of the editor family placed inside it. They are wired to the editor by the browser.

Ui slots

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

Slot Styles
Root Classes merged into the root slot of KlyEditor , after the theme and the app configuration.
Content The area around the text.
Base The element the editable text is drawn in.
Fallback The HTML shown before the editor is ready.