@lexical/plain-text package provides a lightweight editor for plain text input without formatting, ideal for comments, code editors, or simple text fields.
Setup
React Integration
UsePlainTextPlugin for React applications:
packages/lexical-react/src/LexicalPlainTextPlugin.tsx for the plugin implementation.
Features
Plain text mode provides:- Text Input: Basic text editing without formatting
- Line Breaks: Support for multi-line text
- Copy/Paste: Plain text clipboard operations
- Keyboard Navigation: Arrow keys, Home, End, etc.
- Selection: Text selection and manipulation
- Undo/Redo: History support
Supported Commands
Plain text plugin handles these commands:Text Insertion
Line Breaks
Deletion
Clipboard
packages/lexical-plain-text/src/index.ts:118-423 for command implementations.
Keyboard Shortcuts
Plain text plugin supports:- Enter: Insert line break
- Backspace: Delete backward
- Delete: Delete forward
- Ctrl/Cmd + A: Select all
- Ctrl/Cmd + C: Copy
- Ctrl/Cmd + X: Cut
- Ctrl/Cmd + V: Paste
- Ctrl/Cmd + Z: Undo
- Ctrl/Cmd + Shift + Z: Redo
- Arrow Keys: Navigate text
- Home/End: Navigate to line start/end
Getting Text Content
Read Text
Listen to Changes
Setting Text Content
Validation Example
Limit text length:Textarea Replacement
Replace a textarea with a plain text editor:Code Editor Example
Build a simple code editor:Plain Text vs Rich Text
| Feature | Plain Text | Rich Text |
|---|---|---|
| Text Formatting | None | Bold, Italic, etc. |
| Headings | No | Yes |
| Lists | No | Yes |
| Links | No | Yes |
| Images | No | Yes |
| File Size | Smaller | Larger |
| Complexity | Simple | Complex |
| Use Cases | Comments, Code | Documents, Articles |
Styling
Style the plain text editor:Differences from Textarea
Advantages over<textarea>:
- Extensibility: Add plugins for autocomplete, mentions, etc.
- Styling: Rich CSS styling capabilities
- Performance: Better for large text content
- Features: Built-in undo/redo, history
- Mobile: Better mobile editing experience
- Complexity: More setup than textarea
- Bundle Size: Larger JavaScript bundle
- Accessibility: Need to ensure proper ARIA attributes
Best Practices
- Spell Check: Set
spellCheckattribute on ContentEditable - Accessibility: Provide proper
aria-labelattributes - Performance: Use
registerUpdateListenerwith debouncing for large text - Validation: Implement validation in update listeners
- Mobile: Test on mobile devices for touch input
- Cleanup: Always cleanup listeners on unmount
See Also
- Guide: Rich Text Editing
- API Reference: React Plugins
- API Reference: Plain Text Package
- Source:
packages/lexical-plain-text/src/ - Examples:
packages/lexical-playground/src/