// On the server, on every save
import { sanitizeRichText, isEmptyRichText, richTextPlain, richTextWords } from "capsomer/behavior/rich-text-field";
const title = sanitizeRichText(form.get("title"), ["em"]);
A body: italic, superscript and subscript, with a word count
<div data-mount="body"></div>
With help, required
<div data-mount="help"></div>
Invalid
<div data-mount="error"></div>
Every tag a field may allow
<div data-mount="all"></div>
API reference
RichTextField
Prop
Type
Required
Does
name
string
Yes
The form field's name: a hidden input carries the sanitized value.
label
ReactNode
Yes
tags
readonly RichTextTag[]
Yes
The inline tags this field allows, in toolbar order. Italic only for a title.
initialHTML
string
The stored value; sanitized again before it is shown.
help
ReactNode
Help, between the label and the field, read as its description.
error
ReactNode
What is wrong and what to do; marks the field invalid.
required
boolean
lines
"single" | "multi"
"multi" wraps over several lines and starts taller (an abstract's body); "single" is one line's height (a title). Enter never adds a line break: the value holds inline text only.
wordCount
boolean
A live word count under the field.
describedBy
string
Another description's id (a limit the page states).
onValueChange
(value: string) => void
Every change, with the sanitized value.
Functions
Function
Signature
Does
decodeEntities
(text: string): string
text with its character references read (numeric, and the names above); an unknown name stays as written.
input (HTML, or plain text with stray < and &) as a tree of plain text and the allowed tags only: any other tag is dropped and its text kept, attributes and comments are dropped, <i> reads as em and <b> as strong, and the tags are balanced (one closed out of order closes and reopens the ones inside it). Text in <script> and <style> is dropped with them.
Toggles tag over the selection inside root: where the selection sits inside that tag, the tag is taken off (its text stays); otherwise the selected text is wrapped in it and stays selected. A collapsed selection, or one outside root, does nothing. Returns whether anything changed.
Puts nodes where the selection is inside root, replacing what is selected, and the caret after them.
Parts and data attributes
Class
Is
.cap-rich-text-bar
the root
Attribute
Values
data-lines
multi
Accessibility
Key
Does
Ctrl or Cmd + I
Italic on or off (when the field allows em)
Ctrl or Cmd + B
Bold on or off (when it allows strong); otherwise nothing, so the browser writes no tag
Ctrl or Cmd + .
Superscript on or off (sup)
Ctrl or Cmd + ,
Subscript on or off (sub)
Ctrl or Cmd + E
Code on or off (code)
Enter
Nothing: no line break
Tab
From the toolbar to the box, then on
Left, Right, Home, End in the toolbar
Move between its buttons (one tab stop)
The box is role="textbox" named by the label (aria-labelledby), described by the help, the error and the word count, with aria-multiline for a multi-line field, aria-invalid and aria-required.
Each button is named in words ("Italic"), its glyph hidden, with aria-keyshortcuts and aria-pressed.
The word count is a polite live region.
Screen readers: no manual NVDA or VoiceOver run, by decision (2026-11-07); the automated browser checks and a front-end review stand in. Automated: see the site's Tests page.