Forms
TwTextEditor
TwTextEditor is a Blazor rich text editor that binds HTML. Add headings, bold text, lists, quotes and links from a toolbar or with keyboard shortcuts.
Last updated
Please be aware this component is new, may have bugs and/or be subject to change. Please report any bugs at
https://github.com/twblazor/twblazor/issues.
Basic usage
Bind the content with
@bind-Value to a string of HTML. The default Standard toolbar has a text style list, bold, italic, underline, strikethrough, lists and links. The value only ever holds the tags the editor produces, and an empty editor binds as an empty string.Value:
<h2>Release notes</h2><p>This release adds a <strong>rich text editor</strong> with <em>headings</em>, lists and links.</p><ul><li>Bind the content as HTML</li><li>Use keyboard shortcuts</li></ul>
Toolbar presets
Toolbar chooses the controls, and each preset holds every control of the one before it. Simple has bold, italic, underline and lists. Standard adds the text style list, strikethrough and links. Full adds quotes, code blocks, clear formatting, undo, redo and an HTML source view.RAZOR
<div class="flex flex-col gap-6">
<TwTextEditor Label="Simple" Toolbar="TextEditorToolbar.Simple" MinHeight="80px" Value="@presetHtml" />
<TwTextEditor Label="Full" Toolbar="TextEditorToolbar.Full" MinHeight="80px" Value="@presetHtml" />
</div>No toolbar
With
TextEditorToolbar.None there is no toolbar, but the keyboard shortcuts still work: Ctrl + B, Ctrl + I and Ctrl + U for bold, italic and underline, and Ctrl + Shift + 8 for a bulleted list. Use it for inline editing, or with your own controls calling the editor's methods.Custom toolbar
ToolbarContent adds controls to the end of the toolbar, or makes up the whole toolbar with TextEditorToolbar.Custom. Its context is the editor, so a control can call FormatAsync and show its state with IsActive. Here each control is a TwToggle.RAZOR
<TwTextEditor Label="Headline" Toolbar="TextEditorToolbar.Custom" MinHeight="80px">
<ToolbarContent>
<TwToggle Icon="Icon.Type_H1" Size="Size.Sm" AriaLabel="Heading" Pressed="@context.IsActive(TextEditorFormat.Heading1)" PressedChanged="@(_ => context.FormatAsync(context.IsActive(TextEditorFormat.Heading1) ? TextEditorFormat.Paragraph : TextEditorFormat.Heading1))" />
<TwToggle Icon="Icon.Type_Bold" Size="Size.Sm" AriaLabel="Bold" Pressed="@context.IsActive(TextEditorFormat.Bold)" PressedChanged="@(_ => context.FormatAsync(TextEditorFormat.Bold))" />
<TwToggle Icon="Icon.Quote" Size="Size.Sm" AriaLabel="Quote" Pressed="@context.IsActive(TextEditorFormat.Blockquote)" PressedChanged="@(_ => context.FormatAsync(TextEditorFormat.Blockquote))" />
</ToolbarContent>
</TwTextEditor>Height
MinHeight (150px by default) sets where the editing area starts. With MaxHeight it grows with its text up to that height and then scrolls, and Height fixes its size.RAZOR
<div class="grid gap-6 md:grid-cols-2">
<TwTextEditor Label="Grows up to 160px" Toolbar="TextEditorToolbar.Simple" MinHeight="80px" MaxHeight="160px" Value="@longHtml" />
<TwTextEditor Label="Fixed at 120px" Toolbar="TextEditorToolbar.Simple" Height="120px" Value="@longHtml" />
</div>Links
The link button, or
Ctrl + K, opens a bar under the toolbar. Type an address and press Enter to link the selected text, or to insert the address as a link when nothing is selected. With the caret in a link, the bar changes or removes it. An address without a scheme gets https://, an email address gets mailto:, and an address such as javascript: is refused.Events and character count
OnTextChange receives the content as Html, plain Text and its Length, and OnSelectionChange the formats at the selection. OnFocus and OnBlur report focus. While the user types, changes are reported once typing pauses for DebounceInterval (250 milliseconds), and always when focus leaves the editor.0 of 140 characters
Press Alt+F10 to move to the toolbar.Formats at the caret: none
Methods
Use
@ref to call the editor from code: SetHtmlAsync and GetHtmlAsync, GetTextAsync and GetLengthAsync, FormatAsync, InsertLinkAsync and RemoveLinkAsync, UndoAsync, RedoAsync, ClearFormattingAsync and FocusAsync.Plain text appears here.
Export
Set
ExportFormats to add an export menu to the end of the toolbar, offering the formats in the order you list them, and ExportFileName for the saved file's name and the document's title. The formats are Markdown, Word (.docx), OpenDocument (.odt), a web page, plain text and Rich Text Format. Headings, lists, links, quotes and code blocks keep their meaning in each one: a Word document gets real heading styles and numbered lists. The files are written in .NET with no other package, and saved by the browser. Call ExportAsync to export from your own button; it also returns the file.Converting on the server
TwTextEditorExport holds the same converters as static methods, so stored HTML can be turned into a file on the server, for example to email a document or to serve a download from an API: ToMarkdown, ToPlainText, ToHtmlDocument, ToRtf, ToDocx and ToOdt, or Export, which returns the bytes with a file name and media type. They read HTML with the editor's own rules, so HTML from anywhere converts safely.Choose a format above.
Validation
Inside an
EditForm or a TwForm the editor reports every change, so validation attributes on the bound property run for it and their message is shown under the editor. An empty editor binds as an empty string, so [Required] works as it does for a text field.Variants
Variant works as it does on TwTextfield, so the editor lines up with the other inputs on a form.RAZOR
<div class="flex flex-col gap-6">
<TwTextEditor Label="Outlined" Variant="InputVariant.Outlined" Toolbar="TextEditorToolbar.Simple" MinHeight="60px" />
<TwTextEditor Label="Filled" Variant="InputVariant.Filled" Toolbar="TextEditorToolbar.Simple" MinHeight="60px" />
<TwTextEditor Label="Default" Variant="InputVariant.Default" Toolbar="TextEditorToolbar.Simple" MinHeight="60px" />
</div>Disabled and readonly
Disabled dims the editor and its toolbar, and it cannot be focused. ReadOnly keeps the text readable, selectable and in the Tab order, but it cannot be changed.RAZOR
<div class="grid gap-6 md:grid-cols-2">
<TwTextEditor Label="Disabled" Toolbar="TextEditorToolbar.Simple" MinHeight="80px" Value="@presetHtml" Disabled />
<TwTextEditor Label="Readonly" Toolbar="TextEditorToolbar.Simple" MinHeight="80px" Value="@presetHtml" ReadOnly />
</div>Security
Everything that enters the editor, including a value set from code and pasted or dropped content, is rebuilt from the tags the editor supports, and a link keeps only an http, https, mailto, tel or relative address. Scripts, styles, images, event handlers and every other attribute are removed. This cleaning runs in the browser, so treat HTML posted to your server as untrusted and sanitize it again there, for example with a library such as HtmlSanitizer, before you store it or show it on another page.
Accessibility
The editing area is a multiline
textbox named after the Label, with HelperText, any error message and a hint about the toolbar linked through aria-describedby. The toolbar is a toolbar named "Formatting": its toggle groups are each a single Tab stop with the arrow keys moving between their buttons, every button says whether its format is applied at the caret, and each one lists its shortcut in aria-keyshortcuts and its tooltip. Alt + F10 moves from the text to the toolbar, and a format applied from the toolbar with the keyboard leaves focus on the toolbar, so several can be applied in a row. Tab always leaves the editor rather than typing a tab character, so keyboard users are never trapped. All of the editor's own text is translated into the app's language.
Theme configuration
Styling is controlled by
TwBlazorTheme.Components (a TwTextEditorTheme), configured via the
AddTwBlazor() options callback described on the Get Started page.
Every property is a set of Tailwind classes, so changes are picked up by your own Tailwind build.
| Property | Type | Controls |
|---|---|---|
Root |
string | |
Disabled |
string | |
Toolbar |
string | |
ToolbarGroup |
string | |
ToolbarSeparator |
string | |
BlockSelect |
string | |
Content |
string | |
ContentDefaultVariant |
string | |
Typography |
string | |
LinkPanel |
string | |
LinkInput |
string | |
LinkInputField |
string | |
Source |
string | |
SourceInput |
string | |
HelperText |
string |
Full reference: TwTextEditorTheme on the API docs.