Rich text editor
A dependency-free rich text editor with a formatting toolbar and sanitized HTML in and out.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsbeta
- 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
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | – | |
aria-labelledby | string | – | |
value | string | – | Controlled content as an HTML string. It is sanitized with `sanitizeHtml` before it is shown. |
defaultValue | string | – | Initial HTML when uncontrolled. Sanitized with `sanitizeHtml` before it is shown. |
onValueChange | ((html: string) => void) | – | Called with sanitized HTML after every edit. |
placeholder | string | – | Shown while the editor is empty (a data attribute and CSS, not a text node). |
disabled | boolean | – | Disables editing and the toolbar. |
readOnly | boolean | – | Shows the content without letting anyone change it; the toolbar is disabled. |
toolbarLabel | string | 'Text formatting' | Names the formatting toolbar. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[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
| Class | Stylesheet |
|---|---|
.toolbar | toolbar.css |
.toolbar-item | toolbar.css |
.toolbar-sep | toolbar.css |
.menu-content | popover.css |
.menu-indicator | popover.css |
.menu-item | popover.css |
.menu-item-check | popover.css |
.menu-label | popover.css |
.popover | popover.css |
.btn | 02-buttons.css |
.btn-block | 02-buttons.css |
.btn-danger | 02-buttons.css |
.btn-ghost | 02-buttons.css |
.btn-lg | 02-buttons.css |
.btn-line | 02-buttons.css |
.btn-sm | 02-buttons.css |
.btn-solid | 02-buttons.css |
.field | 03-form-fields.css |
.field-hint | 03-form-fields.css |
.input | 03-form-fields.css |
.input-bare | 03-form-fields.css |
.input-end | 03-form-fields.css |
.input-lg | 03-form-fields.css |
.input-sm | 03-form-fields.css |
.input-wrap | 03-form-fields.css |
.muted | 03-form-fields.css |
.seg | 03-form-fields.css |
.seg-opt | 03-form-fields.css |
.select | 03-form-fields.css |
.setting | 03-form-fields.css |
.switch | 03-form-fields.css |
.textarea | 03-form-fields.css |
.field-error | 11-field-states.css |
.field-ok | 11-field-states.css |
.field-required | 11-field-states.css |
.form-row | 11-field-states.css |
.input | 11-field-states.css |
.input-affix | 11-field-states.css |
.input-group | 11-field-states.css |
.select | 11-field-states.css |
.textarea | 11-field-states.css |
.rte | rich-text-editor.css |
.rte-content | rich-text-editor.css |
.rte-link | rich-text-editor.css |
.rte-link-actions | rich-text-editor.css |
.rte-link-error | rich-text-editor.css |
.rte-link-label | rich-text-editor.css |
.rte-toolbar | rich-text-editor.css |
.toolbar-item | rich-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
| Keys | Action |
|---|---|
| Escape | In the toolbar returns focus to the editor |