@lexical/rich-text package provides everything needed to build a full-featured rich text editor with headings, quotes, lists, and text formatting.
Setup
import { createEditor } from 'lexical';
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
const editor = createEditor({
namespace: 'MyEditor',
nodes: [HeadingNode, QuoteNode],
onError: (error) => console.error(error),
});
React Integration
UseRichTextPlugin for React applications:
packages/lexical-react/src/LexicalRichTextPlugin.tsx for the plugin implementation.
Headings
Creating Headings
Heading Levels
Convert to Heading
HeadingNode Properties
packages/lexical-rich-text/src/index.ts:218-330 for HeadingNode implementation.
Quotes
Creating Quotes
Convert to Quote
QuoteNode Behavior
- Pressing Enter at the end creates a new paragraph
- Pressing Backspace at the start collapses to paragraph
- Supports nested content
packages/lexical-rich-text/src/index.ts:127-216 for QuoteNode implementation.
Text Formatting
Format Commands
Programmatic Formatting
Check Format State
Toolbar Example
Element Formatting
Alignment
Indentation
Keyboard Shortcuts
Rich text plugin registers these shortcuts:- Bold:
Ctrl/Cmd + B - Italic:
Ctrl/Cmd + I - Underline:
Ctrl/Cmd + U - Undo:
Ctrl/Cmd + Z - Redo:
Ctrl/Cmd + Shift + Z - Enter: Insert paragraph
- Shift + Enter: Insert line break
- Tab: Indent
- Shift + Tab: Outdent
- Backspace: Delete character/merge blocks
packages/lexical-rich-text/src/index.ts:346-1080 for keyboard command implementations.
Line Breaks vs Paragraphs
Insert Line Break
Insert Paragraph
Styling with Themes
Configure theme classes for rich text elements:Custom Block Types
Extend with additional block types:Best Practices
- Use Commands: Dispatch commands instead of direct node manipulation
- Update Listener: Track formatting state for toolbar UI
- Theme Classes: Use theme system for styling consistency
- Keyboard Shortcuts: Leverage built-in shortcuts
- Block Types: Use
$setBlocksType()to convert selections - Text Formats: Use
selection.formatText()for inline formatting
See Also
- Guide: Plain Text Editing
- Guide: Creating Custom Nodes
- API Reference: Rich Text Package
- API Reference: Commands
- Source:
packages/lexical-rich-text/src/