Playground

Rich text editor

A dependency-free rich text editor with a formatting toolbar and sanitized HTML in and out.

  • Built on native elements
  • 3 parts, named exports
  • The editing region has role="textbox", aria-multiline and an accessible name; the toolbar is a named toolbar with one tab stop and arrow-key movement.
  • Toggle buttons (bold, italic, underline, lists, headings, quote) carry aria-pressed that follows the selection.
  • Cmd/Ctrl+B, I, U and K apply bold, italic, underline and open the link prompt; Escape in the toolbar returns focus to the editor.
  • Styles in one file: toolbar.css

Import

import '@monoline/css';
import {RichTextEditor} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add rich-text-editor

Usage

import {RichTextEditor} from '@monoline/react';

<RichTextEditor aria-label="Notes" placeholder="Write something…" />

Anatomy

Import the parts you need and nest them like this.

import {RichTextEditor, sanitizeHtml, isSafeUrl} from '@monoline/react';

<RichTextEditor>
  <sanitizeHtml />
  <isSafeUrl />
</RichTextEditor>

Examples

Basic

Controlled, with the HTML shown

Read only

Disabled

Props

RichTextEditorProps

PropTypeDefaultDescription
aria-labelstring–
aria-labelledbystring–
valuestring–Controlled content as an HTML string. It is sanitized with `sanitizeHtml` before it is shown.
defaultValuestring–Initial HTML when uncontrolled. Sanitized with `sanitizeHtml` before it is shown.
onValueChange((html: string) => void)–Called with sanitized HTML after every edit.
placeholderstring–Shown while the editor is empty (a data attribute and CSS, not a text node).
disabledboolean–Disables editing and the toolbar.
readOnlyboolean–Shows the content without letting anyone change it; the toolbar is disabled.
toolbarLabelstring'Text formatting'Names the formatting toolbar.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-pressed="true"]toolbar.css
[data-state="on"]toolbar.css
[data-disabled]popover.css
[data-highlighted]popover.css
[data-variant="danger"]popover.css
[aria-checked="true"]03-form-fields.css
[aria-invalid="true"]03-form-fields.css
[aria-pressed="true"]03-form-fields.css
[data-valid="true"]11-field-states.css
[aria-pressed="true"]rich-text-editor.css
[data-disabled]rich-text-editor.css
[data-empty]rich-text-editor.css
[data-readonly]rich-text-editor.css

CSS classes

ClassStylesheet
.toolbartoolbar.css
.toolbar-itemtoolbar.css
.toolbar-septoolbar.css
.menu-contentpopover.css
.menu-indicatorpopover.css
.menu-itempopover.css
.menu-item-checkpopover.css
.menu-labelpopover.css
.popoverpopover.css
.btn02-buttons.css
.btn-block02-buttons.css
.btn-danger02-buttons.css
.btn-ghost02-buttons.css
.btn-lg02-buttons.css
.btn-line02-buttons.css
.btn-sm02-buttons.css
.btn-solid02-buttons.css
.field03-form-fields.css
.field-hint03-form-fields.css
.input03-form-fields.css
.input-bare03-form-fields.css
.input-end03-form-fields.css
.input-lg03-form-fields.css
.input-sm03-form-fields.css
.input-wrap03-form-fields.css
.muted03-form-fields.css
.seg03-form-fields.css
.seg-opt03-form-fields.css
.select03-form-fields.css
.setting03-form-fields.css
.switch03-form-fields.css
.textarea03-form-fields.css
.field-error11-field-states.css
.field-ok11-field-states.css
.field-required11-field-states.css
.form-row11-field-states.css
.input11-field-states.css
.input-affix11-field-states.css
.input-group11-field-states.css
.select11-field-states.css
.textarea11-field-states.css
.rterich-text-editor.css
.rte-contentrich-text-editor.css
.rte-linkrich-text-editor.css
.rte-link-actionsrich-text-editor.css
.rte-link-errorrich-text-editor.css
.rte-link-labelrich-text-editor.css
.rte-toolbarrich-text-editor.css
.toolbar-itemrich-text-editor.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.toolbar {
  border-color: var(--accent);
}
.toolbar[aria-pressed="true"] {
  background: var(--wash);
}

Accessibility

  • The editing region has role="textbox", aria-multiline and an accessible name; the toolbar is a named toolbar with one tab stop and arrow-key movement.
  • Toggle buttons (bold, italic, underline, lists, headings, quote) carry aria-pressed that follows the selection.
  • Cmd/Ctrl+B, I, U and K apply bold, italic, underline and open the link prompt; Escape in the toolbar returns focus to the editor.
  • The link prompt is a labelled popover; an invalid address is announced as an alert and links are limited to http, https and mailto.
  • Pasted content is sanitized to a small tag allowlist; disabled and read-only set aria-disabled and aria-readonly and turn the toolbar off.

Keyboard

KeysAction
EscapeIn the toolbar returns focus to the editor