Rich Text Editor

Beta

Rich text editor provides a WYSIWYG editing surface for formatted text, and a read-only renderer for displaying it.

Overview

The rich text editor lets users write and format text, such as notes, comments and descriptions, with headings, lists, emphasis and links. It’s built on Tiptap and comes with a read-only renderer for displaying the saved content elsewhere in an app.

It ships in its own package, @tylertech/forge-rich-text-editor, so apps that don’t need it don’t pay for Tiptap. It usually sits in a form alongside text fields, or in a card or dialog where users add a comment.

Usage

forge-rich-text-editor only supports the formatting of the feature elements slotted into it. Slot forge-rte-standard-tools for headings, bold, italic, underline, strikethrough, lists, alignment and undo, and add forge-rte-code and forge-rte-link when you need them. Read the content with toJSON() or the change event, and display it with forge-rich-text-renderer. For plain text, or a single line, use a text field instead.

Rules

Do
Start with forge-rte-standard-tools, and add the code and link tools only when users need them.Store the ProseMirror document from toJSON(), and display it with forge-rich-text-renderer.Give the editor a visible label, and set max-length with a character count when the content has a limit.Use read-only, not disabled, for content users may need to select or copy.
Don't
Don't use a rich text editor for plain text or a single line — use a text field.Don't pass an HTML string to forge-rich-text-renderer; it renders as escaped text.Don't rely on FormData to submit the editor's value; it isn't a native form control.Don't load content that uses formatting the editor's features don't support; that formatting is dropped.

Basic usage

Slot forge-rte-standard-tools into the editor. An editor with nothing slotted keeps text but supports no formatting.

A rich text editor with the standard formatting tools.

Composed toolbar

To place the toolbar apart from the content, for a fixed toolbar or a custom layout, wrap them in forge-rich-text-context instead of using forge-rich-text-editor. Put the tools in a forge-rich-text-toolbar, which gives them the toolbar role, an accessible name from its label, and a single tab stop, and add a forge-rich-text-content for the editing area. forge-rich-text-context takes the same properties and events as the editor.

A toolbar and content area laid out separately inside forge-rich-text-context.

Output formats

toJSON() returns the ProseMirror document, the same value the change event emits. Store it to save the content without a round trip through HTML. toHTML() returns sanitized HTML, and toMarkdown() returns Markdown.

The content property accepts either an HTML string or a document; the content attribute only accepts HTML. Documents are stricter: one mark the editor has no feature for discards the whole document, while HTML just loses that formatting. The editor reports a discarded document through its error event, with context: 'Invalid document content'. Load a document into an editor with the same features as the one that saved it.

The editor's content as a ProseMirror document, HTML and Markdown, updating as you type.

Renderer

forge-rich-text-renderer displays saved content read-only, with no toolbar. Its content property only takes a ProseMirror document. To display HTML, convert it first with toRichTextDocument(html).

Saved content displayed read-only with forge-rich-text-renderer.

The renderer has role="article" unless you set another role. Give it an aria-label or aria-labelledby when there’s more than one on the page.

forge-rte-link adds a link tool that opens a popover for the URL. Dangerous protocols such as javascript: are always rejected, in typed and pasted links. With auto-protocol, a URL with no protocol gets https://. Links open in a new tab with rel="noopener noreferrer nofollow".

An editor with the link tool. Select text, then use the link button to add a URL.

Character limits

max-length limits the number of characters, and each block boundary, like a new paragraph, counts as one. Typing and pasting stop at the limit, and a paste that would go over it is refused rather than cut short. show-character-count and show-word-count add counters below the editor.

Content set through content loads even when it’s over the limit. The editor then shows error-message and reports { isValid, errors } through its validation event, which you can use to block a form submission.

An editor with a 500 character limit and character and word counts.

Disabled

disabled dims the whole editor, and the content can’t be edited or selected.

A disabled rich text editor.

Read-only

readonly keeps the content selectable and copyable, and only dims the toolbar. Use it for content users may need to read or quote. In a composed layout, --forge-rich-text-toolbar-readonly-opacity sets how much forge-rich-text-toolbar is dimmed.

A read-only rich text editor.

Form integration

The editor isn’t a native form control, so it isn’t included in FormData and a form reset doesn’t clear it. Read its value on submit with toJSON() or toHTML(), and clear it on reset by setting content to an empty string. See Forms for validation patterns.

A form that reads the editor's content on submit and clears it on reset.

Pasting and sanitization

All content is sanitized before it reaches the editor, whether it’s set through content or pasted. Dangerous elements and on* handlers are removed, and every URL is protocol-checked. The text-align, color and background-color styles are kept, but class and data-* attributes are removed, so formatting that depends on them is lost.

To paste plain text only, set the allowPasteFormatting property to false. It’s a boolean attribute that defaults to on, so set the property rather than the attribute, and set it before the editor initializes: changing it on a live editor has no effect. Users can paste plain text at any time with Ctrl+Shift+V (Cmd+Shift+V on macOS).

const editor = document.createElement('forge-rich-text-editor');
editor.allowPasteFormatting = false;
editor.append(document.createElement('forge-rte-standard-tools'));
container.append(editor);

NPM

The rich text editor is in its own package, so apps that don’t use it never load Tiptap. @tylertech/forge is a peer dependency, which your app already has.

npm install @tylertech/forge-rich-text-editor

Registration

The core elements and the feature elements are registered separately, so an app only loads the tools it uses. Import both entry points to register everything:

import '@tylertech/forge-rich-text-editor';
import '@tylertech/forge-rich-text-editor/features';

Or call the define functions:

import { defineRichTextEditorComponents } from '@tylertech/forge-rich-text-editor';
import { defineRteFeatureComponents } from '@tylertech/forge-rich-text-editor/features';

defineRichTextEditorComponents();
defineRteFeatureComponents();

The package root registers forge-rich-text-editor, forge-rich-text-renderer, forge-rich-text-context, forge-rich-text-content and forge-rich-text-toolbar. The /features entry point registers the forge-rte-* tools.

React

npm install @tylertech/forge-rich-text-editor-react @tylertech/forge-rich-text-editor

Importing a component registers its element, so there’s no define call to make:

import { ForgeRichTextEditor, ForgeRteStandardTools } from '@tylertech/forge-rich-text-editor-react';

export const Editor = () => (
  <ForgeRichTextEditor maxLength={500} onChange={event => console.log(event.detail)}>
    <ForgeRteStandardTools />
  </ForgeRichTextEditor>
);

Angular

npm install @tylertech/forge-rich-text-editor-angular @tylertech/forge-rich-text-editor

Import ForgeRteModule for every element, or the individual modules you need. Importing a module registers its element.

import { ForgeRteModule } from '@tylertech/forge-rich-text-editor-angular';

@NgModule({
  imports: [ForgeRteModule]
})
export class AppModule {}
<forge-rich-text-editor [maxLength]="500" (change)="onChange($event)">
  <forge-rte-standard-tools></forge-rte-standard-tools>
</forge-rich-text-editor>

forge-rich-text-editor

A rich text editor component powered by TipTap that provides a flexible, component-based architecture for composing editing features.

import "@tylertech/forge-rich-text-editor";
Properties
PropertyAttributeTypeDefaultDescription
contentcontentstring''The HTML content of the editor. Pass a ProseMirror document through the property instead; an attribute cannot carry an object.
disableddisabledbooleanfalseWhether the editor is disabled.
readOnly—booleanfalseWhether the editor is in readonly mode.
maxLengthmax-lengthnumber0Maximum character length allowed. 0 means no limit.
errorMessageerror-messagestring''Error message to display when validation fails.
showCharacterCountshow-character-countbooleanfalseWhether to show character count below the editor.
showWordCountshow-word-countbooleanfalseWhether to show word count below the editor.
allowPasteFormattingallow-paste-formattingbooleantrueWhether to allow pasted content to retain formatting.
allowPasteImagesallow-paste-imagesbooleanfalseWhether to allow images to be pasted into the editor.
isInitialized—boolean—Returns whether the editor has been successfully initialized.
initializationError—string|null—Returns the initialization error message, if any.
—readonlyboolean—Whether the editor is in readonly mode.
Events
NameDetailDescription
changeRichTextEditorChangeEventDetailFired when the content of the editor changes. The detail contains the editor content in ProseMirror JSON format.
validationRichTextEditorValidationEventDetailFired when validation state changes. The detail contains validation status and error messages.
initializedvoidFired when the editor has been successfully initialized.
initialization-errorRichTextEditorInitializationErrorEventDetailFired when editor initialization fails. The detail contains the error message.
errorRichTextEditorErrorEventDetailFired when a non-fatal error occurs during editor operation. The detail contains context and error message. A `context` of `Invalid document content` means a ProseMirror document was rejected by the schema and discarded.
Slots
NameDescription
(default)The default slot is reserved for feature components (toolbar buttons like forge-rte-standard-tools, forge-rte-code, forge-rte-link, etc.).
CSS Custom Properties
NameDescription
--forge-rich-text-editor-disabled-opacityThe opacity of the editor when it is disabled, and of the toolbar when it is readonly.
--forge-rich-text-editor-content-paddingThe padding around the editable content.
Methods
NameReturnsDescription
toJSON()—Returns the editor content as JSON in ProseMirror format. Returns undefined if the editor is not initialized or if an error occurs.
toHTML()—Returns the editor content as an HTML string. Returns an empty string if the editor is not initialized or if an error occurs.
toMarkdown()—Returns the editor content as a Markdown string. Returns an empty string if the editor is not initialized or if an error occurs.

forge-rich-text-renderer

Renders rich text content in a read-only format for display purposes.

import "@tylertech/forge-rich-text-editor";
Properties
PropertyAttributeTypeDefaultDescription
content—RichTextRendererContent—The content to render in ProseMirror JSON format. Must be set as a property; it is not settable via attribute.
CSS Custom Properties
NameDescription
--forge-rich-text-renderer-paddingThe padding around the rendered content.

forge-rich-text-context

Provides the editor context for composed rich text editor layouts where toolbar and content are separated.

import "@tylertech/forge-rich-text-editor";
Properties
PropertyAttributeTypeDefaultDescription
editorIdeditor-idstring'editor'The ID of the element to instantiate the editor against.
contentcontentstring''The HTML content of the editor. Pass a ProseMirror document through the property instead; an attribute cannot carry an object.
disableddisabledbooleanfalseWhether the editor is disabled.
readOnly—booleanfalseWhether the editor is in readonly mode.
maxLengthmax-lengthnumber0Maximum character length allowed. 0 means no limit.
errorMessageerror-messagestring''Error message to display when validation fails.
showCharacterCountshow-character-countbooleanfalseWhether to show character count below the editor.
showWordCountshow-word-countbooleanfalseWhether to show word count below the editor.
allowPasteFormattingallow-paste-formattingbooleantrueWhether to allow pasted content to retain formatting.
allowPasteImagesallow-paste-imagesbooleanfalseWhether to allow images to be pasted into the editor.
editorContext—EditorContext{ editor: null, disabled: false, readOnly: false, content: '', isActive(identifier: string | Record<string, unknown>, attributes?: Record<string, unknown>) { return this.editor?.isActive(identifier as any, attributes as any) ?? false; }, isEditable() { return !this.disabled && !this.readOnly && !!this.editor; }, setEditorElement: this.#setEditorElement.bind(this), registerFeature: this.#registerFeature.bind(this), announce: this.#announce.bind(this), controlsElement: null }Provide the editor context to child components.
isInitialized—boolean—Returns whether the editor has been successfully initialized.
initializationError—string|null—Returns the initialization error message, if any.
—readonlyboolean—Whether the editor is in readonly mode.
Events
NameDetailDescription
validationRichTextEditorValidationEventDetailFired when validation state changes. The detail contains validation status and error messages.
changeRichTextEditorChangeEventDetailFired when the content of the editor changes. The detail contains the editor content in ProseMirror JSON format.
initializedvoidFired when the editor has been successfully initialized.
initialization-errorRichTextEditorInitializationErrorEventDetailFired when editor initialization fails. The detail contains the error message.
errorRichTextEditorErrorEventDetailFired when a non-fatal error occurs during editor operation. The detail contains context and error message. A `context` of `Invalid document content` means a ProseMirror document was rejected by the schema and discarded.
Slots
NameDescription
(default)The default slot for toolbar components and forge-rich-text-content.
Methods
NameReturnsDescription
toJSON()object|undefinedReturns the editor content as JSON. Returns undefined if editor is not initialized or if an error occurs.
toHTML()stringReturns the editor content as HTML. Returns empty string if editor is not initialized or if an error occurs. The output is sanitized to prevent XSS attacks.
toMarkdown()stringReturns the editor content as Markdown. Returns empty string if editor is not initialized or if an error occurs.

forge-rich-text-content

Renders the editable content area for composed rich text editor layouts.

import "@tylertech/forge-rich-text-editor";
CSS Custom Properties
NameDescription
--forge-rich-text-content-disabled-opacityThe opacity of the content area when the surrounding editor is disabled.
--forge-rich-text-content-paddingThe padding around the editable content.

forge-rich-text-toolbar

Groups rich text editor tools into an accessible toolbar with a single tab stop.

import "@tylertech/forge-rich-text-editor";
Properties
PropertyAttributeTypeDefaultDescription
labellabelstring'Rich text formatting toolbar'The accessible name of the toolbar.
Slots
NameDescription
(default)Feature components, such as `forge-rte-standard-tools`, `forge-rte-link` and `forge-rte-divider`.
CSS Custom Properties
NameDescription
--forge-rich-text-toolbar-readonly-opacityThe opacity of the toolbar when the editor is readonly. Falls back to `--forge-rich-text-editor-disabled-opacity`.

Feature elements

Each feature element adds one tool to the toolbar and the formatting it supports to the editor.

import "@tylertech/forge-rich-text-editor/features";
ElementAttributesDescription
forge-rte-standard-tools—A convenience component that bundles common text formatting features into a single toolbar.
forge-rte-headingh1-labelh2-labelh3-labelProvides heading formatting buttons (H1, H2, H3) for the rich text editor.
forge-rte-boldlabelProvides a bold text formatting button for the rich text editor.
forge-rte-italiclabelProvides an italic text formatting button for the rich text editor.
forge-rte-underlinelabelProvides an underline text formatting button for the rich text editor.
forge-rte-strikelabelProvides a strikethrough text formatting button for the rich text editor.
forge-rte-bullet-listlabelProvides a bulleted (unordered) list button for the rich text editor.
forge-rte-ordered-listlabelProvides a numbered (ordered) list button for the rich text editor.
forge-rte-alignleft-labelcenter-labelright-labeljustify-labelProvides text alignment buttons (left, center, right, justify) for the rich text editor.
forge-rte-undo-redoundo-labelredo-labelProvides undo and redo buttons for the rich text editor history management.
forge-rte-codelabelProvides an inline code formatting button for the rich text editor.
forge-rte-linklabelauto-protocolProvides a link creation and editing button for the rich text editor.
forge-rte-divider—A vertical divider for separating groups of buttons in the rich text editor toolbar.

Keyboard interaction

The toolbar follows the WAI-ARIA toolbar pattern and is a single tab stop. forge-rich-text-editor does this for you. In a composed layout, wrap the tools in forge-rich-text-toolbar to get the same behavior; tools in a plain container are each their own tab stop.

Key Action
Tab Moves focus into the toolbar, on the last tool used, then into the content.
Right Arrow / Left Arrow Moves to the next or previous tool, reversed in right-to-left layouts. Focus stops at the first and last tool.
Home / End Moves to the first or last tool.
Enter / Space Activates the focused tool.
Enter (link popover) Applies the link, if the URL is valid.
Escape (link popover) Closes the popover without changing the link.

Disabled tools are skipped.

Formatting shortcuts

Each shortcut works in the content while the feature that provides it is slotted. Use Cmd in place of Ctrl on macOS.

Shortcut Action Feature
Ctrl+B Bold forge-rte-bold
Ctrl+I Italic forge-rte-italic
Ctrl+U Underline forge-rte-underline
Ctrl+Shift+S Strikethrough forge-rte-strike
Ctrl+E Inline code forge-rte-code
Ctrl+Alt+1 to Ctrl+Alt+3 Heading 1 to 3 forge-rte-heading
Ctrl+Shift+8 Bulleted list forge-rte-bullet-list
Ctrl+Shift+7 Numbered list forge-rte-ordered-list
Ctrl+Shift+L / E / R / J Align left, center, right or justify forge-rte-align
Ctrl+Z Undo forge-rte-undo-redo
Ctrl+Shift+Z or Ctrl+Y Redo forge-rte-undo-redo
Ctrl+Shift+V Paste as plain text Always available

Screen reader behavior

  • The editable area is a multi-line textbox. Tiptap builds its own contenteditable element inside the content area, and that inner element is the real control: it receives focus and is where assistive technology lands. Attributes on the outer forge-rich-text-editor don’t reach it.
  • Formatting tools are toggle buttons that expose their state with aria-pressed and are disabled with the editor. Undo and redo are plain buttons with no pressed state. Bold, italic, underline, undo and redo also list their shortcut in aria-keyshortcuts.
  • Applying formatting is announced politely through Forge’s live announcer, for example “Bold applied”, so it isn’t a silent action.
  • forge-rich-text-renderer has role="article" unless you set another role.

Labeling

  • The editable area is always named “Rich text editor content”, and there’s currently no way to change that name or point it at your own label. Put a visible label next to the editor so sighted users know what it’s for, and describe its purpose in the surrounding content.
  • In a composed layout, give forge-rich-text-toolbar a label so the toolbar has an accessible name.
  • Give each forge-rich-text-renderer an aria-label or aria-labelledby when there’s more than one on a page.
  • Every tool has a default label, which you can change for localization: label on most tools, h1-label to h3-label on forge-rte-heading, left-label to justify-label on forge-rte-align, and undo-label and redo-label on forge-rte-undo-redo.

Disabled and read-only

Both states stop editing and disable the tools. disabled also dims the content and blocks selecting it, and the content area reports aria-disabled. readonly leaves the content at full contrast and selectable, so users can still read and copy it, and reports aria-readonly. Prefer readonly for content users need to read.

Type to search the docs.

NavigateSelectClose
Loading Forge chat…