Rich text editor
Loading the editor
npx shadcn add https://ui.klarhimmel.com/r/rich-text-editor.jsonCopy for AIDetailsAbout, usage, props and more
Short formatted text edited in place: headings, bold, italic, bullet and task lists (GFM checkboxes) and links, Markdown in and out, with a toolbar that floats while it has focus.
When to use
Use for
Editing a section of a document in place: a proposal, a brief, a page's text, where bold, a list and a link are enough.
Not for
Plain one-paragraph text. Use Textarea. Long documents with headings, tables and images: that needs a document editor.
Pairs with
A section heading, a Save button, Banner for a conflict.
Preview
Scope
Loading the editor
Markdown: 94 characters
Import
Import
- Files
- rich-text-editor.tsx, in components/klarhimmel/ui/. With it: icon-button, theme, utils. npm: hast-util-from-dom and 9 more.
- Render in
- Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | required | Markdown. A new value from outside replaces the text; the editor's own changes come back through onChange. |
onChange | (value: string) => void | required | Required. Markdown. A new value from outside replaces the text; edits come back as Markdown. |
label | string | required | Required: names the text box (and, after the toolbar label, its toolbar). |
placement | 'auto' | 'above' | 'below' | 'auto' | Where the toolbar floats while the editor has focus. auto: above, or below when there is no room above it in the window (the top inset is the page's scroll-padding-top, so a sticky header counts). It never takes room in the page. |
maxLength | number | 8000 | The longest Markdown accepted, in characters; an edit past it is undone. 0 for no limit. |
labels | Partial<RichTextEditorLabels> | - | The toolbar's and the buttons' names. English by default: pass them translated. |
className | string | - | Extra classes, merged after the component's own. |
Accessibility
| Key | Does |
|---|---|
Type | Edits the text; Markdown comes back through onChange |
Cmd or Ctrl + B / I | Bold, italic |
Cmd or Ctrl + Z, Shift + Cmd or Ctrl + Z | Undo, redo |
Shift + Tab from the text | Reaches the toolbar |
Left / Right / Home / End in the toolbar | Move between its buttons; Tab leaves it |
A named multi-line textbox. The toolbar has one tab stop, Bold, Italic and List say whether they are on, and it floats only while the editor has focus, so the page never moves.
Accessibility