Rich text editor: formatted text, headings, lists, tasks, quotes, highlighted code blocks, links, images and mentions.
33 exports
Live example
Preview
Weekly note
This editor supports bold, italic, underline, strikethrough, inline code and highlight. There are links too.
Finish the design
Write the tests
Mobile
Try the Markdown shortcuts: type ## , - , 1. , [ ] , > or ```ts at the start of a line.
export function Hello({ name }: { name: string }) {
return <p>Hello {name}!</p>;
}
Type / on an empty line to add a block, mention someone with @.
Value (html)
<h2>Weekly note</h2>
<p>This editor supports <strong>bold</strong>, <em>italic</em>, <u>underline</u>, <s>strikethrough</s>, <code>inline code</code> and <mark>highlight</mark>. There are <a href="https://github.com/yasinsensoy/ui">links</a> too.</p>
<ul data-type="task"><li data-checked="true">Finish the design</li><li data-checked="false">Write the tests<ul data-type="task"><li data-checked="false">Mobile</li></ul></li></ul>
<blockquote>Try the Markdown shortcuts: type <code>## </code>, <code>- </code>, <code>1. </code>, <code>[ ] </code>, <code>> </code> or <code>```ts</code> at the start of a line.</blockquote>
<pre data-language="tsx"><code>export function Hello({ name }: { name: string }) {
return <p>Hello {name}!</p>;
}</code></pre>
<p>Type <code>/</code> on an empty line to add a block, mention someone with <code>@</code>.</p>
Code
TSX
import { RichTextEditor, RichTextView } from "@yasinsensoy/web";<RichTextEditor placeholder="Write something…" mentions={[{ trigger: "@", items: personSearch }, { trigger: "#", items: tags }]} onImageUpload={async (file) => (await upload(file)).url} onValueChange={setContent}/>// To display it (server component, code blocks with CodeBlock):<RichTextView value={content} />
Events
Events appear here when you interact with the preview.
Control with a ref and RichTextView
Control with a ref
Try the buttons.
Form field (required, 280 characters, Markdown)
Read-only display (RichTextView)
Weekly note
This editor supports bold, italic, underline, strikethrough, inline code and highlight. There are links too.
Finish the design
Write the tests
Mobile
Try the Markdown shortcuts: type ## , - , 1. , [ ] , > or ```ts at the start of a line.
TSX
export function Hello({ name }: { name: string }) { return <p>Hello {name}!</p>;}
Type / on an empty line to add a block, mention someone with @.
Rich text editor: formatted text, headings, lists, tasks, quotes, highlighted code blocks, links, images and mentions. Uses no external library.
Value: HTML, Markdown or JSON with format. All pasted and given content is sanitized; only known structures and safe URLs remain (safe against XSS).
Shortcuts: Mod+B/I/U, Mod+E code, Mod+Shift+X strike, Mod+Shift+H highlight, Mod+K link, Mod+Alt+1/2/3 headings, Mod+Shift+7/8/9 lists, Tab/Shift+Tab indent, Mod+Z/Mod+Shift+Z undo/redo, Mod+Shift+V paste without formatting. For Markdown shortcuts see markdownShortcuts.
Code blocks: The language can be picked; highlighted while typing (without touching the DOM, with the CSS Custom Highlight API); Tab indents, Mod+Enter exits the block. RichTextView draws code blocks with CodeBlock.
Paste: From Word, Google Docs and web pages with formatting; Markdown text is parsed; a URL pasted over selected text becomes a link.
Form: Submitted with the form via name; required and maxLength join browser validation. For label and error see RichTextField.
Mobile: The toolbar scrolls horizontally; task checkboxes are finger-sized; suggestion and link panels stay above the keyboard.
Server: The initial content is rendered on the server; no layout shift when the page opens.
Shows rich text (HTML, Markdown or JSON). Uses the same typography as RichTextEditor; what is written and what is published look the same.
Server component: Needs no client JavaScript; HTML and Markdown are parsed on the server. Only code blocks run on the client with CodeBlock (highlighting and copying).
Safe:dangerouslySetInnerHTML isn't used; content is converted to React elements. Unknown tags, scripts and unsafe URLs are dropped.
External links open in a new tab with rel="noopener noreferrer nofollow".
Converts rich text to HTML. The input is sanitized; the output contains only known tags and safe URLs, so it can be shown with dangerouslySetInnerHTML. Also runs on the server.
Look: outline (bordered), filled, underline or ghost (plain). If omitted, the setting of the nearest data-field-variant container is used, otherwise outline. See FieldVariant.
effect?
EffectProp
Effect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProviderappearance.defaults.field.effect. See BuiltinEffect.
value?
RichTextValue
Controlled value. Its format is set by format.
defaultValue?
RichTextValue
Uncontrolled initial value.
onValueChange?
(value: RichTextValue) => void
Called with the new value when the content changes. An empty document arrives as "" (an empty document in json).
format?
RichTextFormat
"html"
Format of the value: "html" (sanitized HTML), "markdown" or "json". See RichTextFormat.
placeholder?
string
Placeholder shown while the document is empty.
toolbar?
boolean | readonly RichTextToolbarItem[]
true
Toolbar: true default tools, false hides it, an array shows only those tools (in order). "|" is a separator; for your own button pass a RichTextCustomTool.
bubbleMenu?
boolean
true
Small format menu that opens above the selection when text is selected (bold, italic, link…). Allows formatting even when the toolbar is hidden.
markdownShortcuts?
boolean
true
Markdown shortcuts: # heading, - list, 1. numbered list, [ ] task, > quote, `````` code block, --- rule, bold, italic, code, ~~strike~~, ==highlight==`. Typed URLs turn into links. Backspace right after a shortcut undoes it.
slashCommands?
boolean | readonly RichTextSlashCommand[]
true
Slash (/) menu: typing / at line start or after a space opens the block insert menu. true built-in commands; an array adds your own commands after the built-in ones. false turns it off.
Mention sources: e.g. person suggestions on @, tag suggestions on #. See RichTextMentionSource.
onImageUpload?
(file: File) => Promise<string>
Image uploader. If given, images can be pasted, dragged and dropped, and picked from files. Upload the file to your server and return the image URL. The image looks faded while uploading; it is removed on error.
maxLength?
number
Maximum characters. Typing and pasting beyond it is blocked.
showCount?
boolean | "words"
Counter on the bottom row: true characters (and the limit if maxLength is set), "words" word count. Turns on automatically if maxLength is given.
readOnly?
boolean
Read-only: content can be selected and copied but not changed; the toolbar is hidden.
disabled?
boolean
Disabled.
autoFocus?
boolean
Focus when the page opens.
name?
string
Name submitted with the form; the value goes as text in the format format (json: JSON text).
required?
boolean
Can't be left empty in a form.
form?
string
Id of the form it belongs to.
id?
string
Field id (for label linking; RichTextField passes it itself).
aria-label?
string
Screen reader name (if there is no visible label).
aria-describedby?
string
Ids of the description elements.
aria-invalid?
boolean | "true" | "false"
Mark as invalid.
spellCheck?
boolean
true
Spell check.
onFocus?
(event: FocusEvent<HTMLDivElement>) => void
On focus.
onBlur?
(event: FocusEvent<HTMLDivElement>) => void
On blur.
messages?
Partial<RichTextMessages>
Texts of this editor; for app-wide texts use the dictionary's ui.richText namespace. See RichTextMessages.
className?
string
Classes added to the container.
classNames?
RichTextEditorClassNames
Classes added to the inner parts.
ref?
Ref<RichTextEditorHandle>
For controlling the editor. See RichTextEditorHandle.
Look: outline (bordered), filled, underline or ghost (plain). If omitted, the setting of the nearest data-field-variant container is used, otherwise outline. See FieldVariant.
effect?
EffectProp
Effect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProviderappearance.defaults.field.effect. See BuiltinEffect.
value?
RichTextValue
Controlled value. Its format is set by format.
defaultValue?
RichTextValue
Uncontrolled initial value.
onValueChange?
(value: RichTextValue) => void
Called with the new value when the content changes. An empty document arrives as "" (an empty document in json).
format?
RichTextFormat
"html"
Format of the value: "html" (sanitized HTML), "markdown" or "json". See RichTextFormat.
placeholder?
string
Placeholder shown while the document is empty.
toolbar?
boolean | readonly RichTextToolbarItem[]
true
Toolbar: true default tools, false hides it, an array shows only those tools (in order). "|" is a separator; for your own button pass a RichTextCustomTool.
bubbleMenu?
boolean
true
Small format menu that opens above the selection when text is selected (bold, italic, link…). Allows formatting even when the toolbar is hidden.
markdownShortcuts?
boolean
true
Markdown shortcuts: # heading, - list, 1. numbered list, [ ] task, > quote, `````` code block, --- rule, bold, italic, code, ~~strike~~, ==highlight==`. Typed URLs turn into links. Backspace right after a shortcut undoes it.
slashCommands?
boolean | readonly RichTextSlashCommand[]
true
Slash (/) menu: typing / at line start or after a space opens the block insert menu. true built-in commands; an array adds your own commands after the built-in ones. false turns it off.
Mention sources: e.g. person suggestions on @, tag suggestions on #. See RichTextMentionSource.
onImageUpload?
(file: File) => Promise<string>
Image uploader. If given, images can be pasted, dragged and dropped, and picked from files. Upload the file to your server and return the image URL. The image looks faded while uploading; it is removed on error.
maxLength?
number
Maximum characters. Typing and pasting beyond it is blocked.
showCount?
boolean | "words"
Counter on the bottom row: true characters (and the limit if maxLength is set), "words" word count. Turns on automatically if maxLength is given.
readOnly?
boolean
Read-only: content can be selected and copied but not changed; the toolbar is hidden.
disabled?
boolean
Disabled.
autoFocus?
boolean
Focus when the page opens.
name?
string
Name submitted with the form; the value goes as text in the format format (json: JSON text).
required?
boolean
Can't be left empty in a form.
form?
string
Id of the form it belongs to.
id?
string
Field id (for label linking; RichTextField passes it itself).
aria-label?
string
Screen reader name (if there is no visible label).
aria-describedby?
string
Ids of the description elements.
aria-invalid?
boolean | "true" | "false"
Mark as invalid.
spellCheck?
boolean
true
Spell check.
onFocus?
(event: FocusEvent<HTMLDivElement>) => void
On focus.
onBlur?
(event: FocusEvent<HTMLDivElement>) => void
On blur.
ref?
Ref<RichTextEditorHandle>
For controlling the editor. See RichTextEditorHandle.
editorMessages?
Partial<RichTextMessages>
Texts of the editor (messages is the field's validation messages). See RichTextMessages.
Suggestions: an array or a function called with the typed query (server search). The function can cancel old searches with signal. An array is filtered by name.
<Formaction={save}><RichTextFieldlabel="Description"name="description"format="markdown"requiredmaxLength={5000}description="Markdown shortcuts are supported."messages={{ valueMissing:"Description is required"}}/><SubmitButton>Save</SubmitButton></Form>
RichTextView({ value, format, linkTarget, mentionHref, codeLineNumbers, empty, className,...props}:RichTextViewProps):JSX.Element