Skip to content
Klarhimmel UI

Rich text editor

Loading the editor

npx shadcn add https://ui.klarhimmel.com/r/rich-text-editor.json
DetailsAbout, 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

rich-text-editor.tsx
import { Theme } from '@/components/klarhimmel/theme/style';
import { RichTextEditor } from '@/components/klarhimmel/ui/rich-text-editor';

export default function Example() {
  return (
    <Theme theme="brand">
      <RichTextEditor
        label="Scope"
        value="We **redesign** the booking flow."
        onChange={(markdown) => localStorage.setItem('scope', markdown)}
      />
    </Theme>
  );
}

Import

Import

import { RichTextEditor } from '@/components/klarhimmel/ui/rich-text-editor';
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

PropTypeDefaultDescription
valuestringrequiredMarkdown. A new value from outside replaces the text; the editor's own changes come back through onChange.
onChange(value: string) => voidrequiredRequired. Markdown. A new value from outside replaces the text; edits come back as Markdown.
labelstringrequiredRequired: 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.
maxLengthnumber8000The longest Markdown accepted, in characters; an edit past it is undone. 0 for no limit.
labelsPartial<RichTextEditorLabels>-The toolbar's and the buttons' names. English by default: pass them translated.
classNamestring-Extra classes, merged after the component's own.

Accessibility

KeyDoes
TypeEdits the text; Markdown comes back through onChange
Cmd or Ctrl + B / IBold, italic
Cmd or Ctrl + Z, Shift + Cmd or Ctrl + ZUndo, redo
Shift + Tab from the textReaches the toolbar
Left / Right / Home / End in the toolbarMove 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