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.
@lexical/react
This package provides a set of components and hooks for Lexical that allow for text editing in React applications.
Getting started
Install lexical
and @lexical/react
:
npm install --save lexical @lexical/react
Below is an example of a basic plain text editor using lexical
and @lexical/react
(try it yourself).
import {$getRoot, $getSelection} from 'lexical';
import {useEffect} from 'react';
import {LexicalComposer} from '@lexical/react/LexicalComposer';
import {PlainTextPlugin} from '@lexical/react/LexicalPlainTextPlugin';
import {ContentEditable} from '@lexical/react/LexicalContentEditable';
import {HistoryPlugin} from '@lexical/react/LexicalHistoryPlugin';
import {OnChangePlugin} from '@lexical/react/LexicalOnChangePlugin';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
const theme = {
...
}
function onChange(editorState) {
editorState.read(() => {
const root = $getRoot();
const selection = $getSelection();
console.log(root, selection);
});
}
function MyCustomAutoFocusPlugin() {
const [editor] = useLexicalComposerContext();
useEffect(() => {
editor.focus();
}, [editor]);
return null;
}
function onError(error) {
throw error;
}
function Editor() {
const initialConfig = {
namespace: 'MyEditor',
theme,
onError,
};
return (
<LexicalComposer initialConfig={initialConfig}>
<PlainTextPlugin
contentEditable={<ContentEditable />}
placeholder={<div>Enter some text...</div>}
/>
<OnChangePlugin onChange={onChange} />
<HistoryPlugin />
<MyCustomAutoFocusPlugin />
</LexicalComposer>
);
}