What is @lexical/react?
@lexical/react is a powerful and flexible text editor framework for React applications. It provides a set of tools and components to build rich text editors with ease, offering features like collaborative editing, custom formatting, and extensibility.
What are @lexical/react's main functionalities?
Basic Editor Setup
This code sets up a basic Lexical editor with rich text capabilities and history support. The LexicalComposer component initializes the editor with a configuration, and the RichTextPlugin and HistoryPlugin add rich text editing and undo/redo functionality.
import React from 'react';
import { LexicalComposer } from '@lexical/react/LexicalComposer';
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
const editorConfig = {
namespace: 'MyEditor',
theme: {},
onError: (error) => {
console.error(error);
},
};
function MyEditor() {
return (
<LexicalComposer initialConfig={editorConfig}>
<RichTextPlugin
contentEditable={<ContentEditable />}
placeholder={<div>Enter some text...</div>}
/>
<HistoryPlugin />
</LexicalComposer>
);
}
export default MyEditor;
Custom Formatting
This code adds a toolbar to the Lexical editor, allowing users to apply custom formatting to their text. The ToolbarPlugin provides a set of formatting options like bold, italic, and underline.
import React from 'react';
import { LexicalComposer } from '@lexical/react/LexicalComposer';
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
import { ToolbarPlugin } from '@lexical/react/LexicalToolbarPlugin';
import { $createTextNode } from 'lexical';
const editorConfig = {
namespace: 'MyEditor',
theme: {},
onError: (error) => {
console.error(error);
},
};
function MyEditor() {
return (
<LexicalComposer initialConfig={editorConfig}>
<ToolbarPlugin />
<RichTextPlugin
contentEditable={<ContentEditable />}
placeholder={<div>Enter some text...</div>}
/>
<HistoryPlugin />
</LexicalComposer>
);
}
export default MyEditor;
Collaborative Editing
This code enables collaborative editing in the Lexical editor. The CollaborationPlugin allows multiple users to edit the same document in real-time, identified by a unique room ID.
import React from 'react';
import { LexicalComposer } from '@lexical/react/LexicalComposer';
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
import { CollaborationPlugin } from '@lexical/react/LexicalCollaborationPlugin';
const editorConfig = {
namespace: 'MyEditor',
theme: {},
onError: (error) => {
console.error(error);
},
};
function MyEditor() {
return (
<LexicalComposer initialConfig={editorConfig}>
<RichTextPlugin
contentEditable={<ContentEditable />}
placeholder={<div>Enter some text...</div>}
/>
<HistoryPlugin />
<CollaborationPlugin id="my-collab-room" />
</LexicalComposer>
);
}
export default MyEditor;
Other packages similar to @lexical/react
draft-js
Draft.js is a framework for building rich text editors in React. It provides a set of immutable models and helper functions to manage editor state and content. Compared to @lexical/react, Draft.js is more mature but may require more boilerplate code for advanced features.
slate
Slate is a highly customizable framework for building rich text editors in React. It offers a more flexible and extensible architecture compared to @lexical/react, allowing developers to define their own data models and editor behaviors. However, this flexibility can come with a steeper learning curve.
quill
Quill is a modern WYSIWYG editor built for the web. It provides a rich set of features out of the box, including formatting, themes, and modules for extending functionality. Quill is less focused on React specifically but can be integrated with React applications using wrappers like react-quill.