Rich Text Editor

@allem-ui/rich-text

Lightweight WYSIWYG editor with formatting toolbar, keyboard shortcuts, and dark mode. Zero dependencies — built on native contentEditable.

Installation

npm install @allem-ui/rich-text

Add the @source directive to your CSS so Tailwind generates the component classes:

@source "@allem-ui/rich-text";

Editor

Full editor with toolbar. Try the formatting buttons and keyboard shortcuts.

Read-Only

Display formatted content without editing. Toolbar is automatically hidden.

Bubble Menu

Select text to reveal a floating toolbar with inline formatting options. The bubble menu appears automatically — hide the static toolbar for a distraction-free writing experience.

Hidden Toolbar

Use hideToolbar for a minimal writing experience. Keyboard shortcuts still work.

RichTextEditor Props

PropTypeDefaultDescription
initialValuestring""Initial HTML content
onChange(html: string) => void—Called on content change with HTML string
placeholderstring"Start writing..."Placeholder text
readOnlybooleanfalseRead-only mode (hides toolbar)
minHeightnumber120Minimum editor height in pixels
maxHeightnumber400Maximum editor height in pixels
hideToolbarbooleanfalseHide the formatting toolbar
hideBubbleMenubooleanfalseHide the floating bubble menu on text selection
classNamestring—Additional CSS classes

useRichText

Hook for building a custom editor UI. Use this when you need full control over the toolbar and layout.

See the code example for composing a custom editor with useRichText.

Options

PropTypeDefaultDescription
initialValuestring—Initial HTML content
onChange(html: string) => void—Called when content changes

Return Value

PropTypeDefaultDescription
editorRefRefObject<HTMLDivElement>—Ref to attach to the editable element
htmlstring—Current HTML content
activeFormatsActiveFormats—Object of currently active formatting states
selectionRectSelectionRect | null—Bounding rect of the current text selection
hasSelectionboolean—Whether text is currently selected
handleInput() => void—Input event handler
toggleBold() => void—Toggle bold formatting
toggleItalic() => void—Toggle italic formatting
toggleUnderline() => void—Toggle underline formatting
toggleStrikethrough() => void—Toggle strikethrough formatting
toggleHeading(level: 1 | 2 | 3) => void—Toggle heading level
toggleOrderedList() => void—Toggle ordered list
toggleUnorderedList() => void—Toggle unordered list
toggleBlockquote() => void—Toggle blockquote
toggleCodeBlock() => void—Toggle code block
insertLink(url: string) => void—Insert a link at selection
removeLink() => void—Remove link from selection
insertHorizontalRule() => void—Insert a horizontal rule
undo() => void—Undo last action
redo() => void—Redo last undone action

BubbleMenu Props

Floating inline toolbar that appears on text selection. Built into RichTextEditor by default — use standalone with useRichText for custom layouts.

PropTypeDefaultDescription
editorRef*RefObject<HTMLDivElement>—Ref to the editable element
selectionRect*SelectionRect | null—Bounding rect of the current selection
hasSelection*boolean—Whether text is currently selected
activeFormats*ActiveFormats—Currently active formatting states
onBold*() => void—Toggle bold
onItalic*() => void—Toggle italic
onUnderline*() => void—Toggle underline
onStrikethrough*() => void—Toggle strikethrough
onLink*(url: string) => void—Insert a link
onRemoveLink*() => void—Remove a link

RichTextToolbar Props

Standalone toolbar component for custom editor layouts via useRichText.

PropTypeDefaultDescription
activeFormats*ActiveFormats—Current formatting state
onBold*() => void—Toggle bold
onItalic*() => void—Toggle italic
onUnderline*() => void—Toggle underline
onStrikethrough*() => void—Toggle strikethrough
onHeading*(level: 1 | 2 | 3) => void—Toggle heading level
onOrderedList*() => void—Toggle ordered list
onUnorderedList*() => void—Toggle unordered list
onBlockquote*() => void—Toggle blockquote
onCodeBlock*() => void—Toggle code block
onLink*(url: string) => void—Insert link
onRemoveLink*() => void—Remove link
onUndo() => void—Undo action
onRedo() => void—Redo action
classNamestring—Additional CSS classes

RichTextContent Props

The editable content area. Use with useRichText for custom layouts.

PropTypeDefaultDescription
editorRef*RefObject<HTMLDivElement>—Ref from useRichText
initialValuestring""Initial HTML content
placeholderstring"Start writing..."Placeholder text
readOnlybooleanfalseDisable editing
minHeightnumber120Minimum height in pixels
maxHeightnumber400Maximum height in pixels
onInput*() => void—Input handler from useRichText
onKeyDown(e: KeyboardEvent) => void—Keyboard event handler
classNamestring—Additional CSS classes
childrenReactNode—Overlay elements like BubbleMenu

ActiveFormats Type

PropTypeDefaultDescription
boldboolean—Bold is active
italicboolean—Italic is active
underlineboolean—Underline is active
strikethroughboolean—Strikethrough is active
orderedListboolean—Ordered list is active
unorderedListboolean—Unordered list is active
blockquoteboolean—Blockquote is active
codeboolean—Code block is active
heading1boolean—Heading 1 is active
heading2boolean—Heading 2 is active
heading3boolean—Heading 3 is active

SelectionRect Type

PropTypeDefaultDescription
topnumber—Top position in pixels
leftnumber—Left position in pixels
widthnumber—Selection width in pixels
heightnumber—Selection height in pixels

Keyboard Shortcuts

FormatShortcut
Bold⌘B
Italic⌘I
Underline⌘U
Undo⌘Z
Redo⌘⇧Z

Markdown Shortcuts

Type these at the start of a new line to auto-convert into formatted blocks.

InputResult
# Heading 1
## Heading 2
### Heading 3
> Blockquote
- or * Unordered list
1. Ordered list
```Code block
--- Horizontal rule

Toolbar Formats

The toolbar supports: Bold, Italic, Underline, Strikethrough, Heading 1–3, Ordered List, Unordered List, Blockquote, Code Block, Link, Undo, and Redo.