Migration Guide
Migrating to Lexical Builder is designed to be seamless! Lexical Builder is a higher-level API on top of the existing functionality you are already using.
Generally speaking, the only thing that needs to change is how you create the editor. Everything else can be migrated (or not) at your own leisure, but the result will be simpler and more composable if you do!
Vanilla JS App
- Before
- After (minimal changes)
- After (all-in)
import {registerDragonSupport} from '@lexical/dragon';
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
import {HeadingNode, QuoteNode, registerRichText} from '@lexical/rich-text';
import {mergeRegister} from '@lexical/utils';
import {createEditor} from 'lexical';
import $prepopulatedRichText from './$prepopulatedRichText';
const editorRef = document.getElementById('lexical-editor');
const stateRef = document.getElementById(
'lexical-state',
) as HTMLTextAreaElement;
const editor = createEditor({
namespace: 'Vanilla JS Demo',
// Register nodes specific for @lexical/rich-text
nodes: [HeadingNode, QuoteNode],
onError: (error: Error) => {
throw error;
},
theme: {quote: 'PlaygroundEditorTheme__quote'},
});
editor.setRootElement(editorRef);
// Registering Plugins
mergeRegister(
registerRichText(editor),
registerDragonSupport(editor),
registerHistory(editor, createEmptyHistoryState(), 300),
);
editor.update(prepopulatedRichText, {tag: 'history-merge'});
import {registerDragonSupport} from '@lexical/dragon';
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
import {HeadingNode, QuoteNode, registerRichText} from '@lexical/rich-text';
import {mergeRegister} from '@lexical/utils';
import {buildEditorFromExtensions} from '@lexical/extension';
import $prepopulatedRichText from './$prepopulatedRichText';
const editorRef = document.getElementById('lexical-editor');
const stateRef = document.getElementById(
'lexical-state',
) as HTMLTextAreaElement;
const editor = buildEditorFromExtensions({
// Any string is suitable as long as it uniquely defines the Extension in the editor
name: '[root]',
namespace: 'Vanilla JS Demo (with Lexical Extension)',
// Register nodes specific for @lexical/rich-text
nodes: [HeadingNode, QuoteNode],
// onError boilerplate removed
theme: {quote: 'PlaygroundEditorTheme__quote'},
});
editor.setRootElement(editorRef);
// Registering Plugins
mergeRegister(
registerRichText(editor),
registerDragonSupport(editor),
registerHistory(editor, createEmptyHistoryState(), 300),
);
editor.update($prepopulatedRichText, {tag: 'history-merge'});
import {buildEditorFromExtensions} from '@lexical/extension';
import {HistoryExtension} from '@lexical/history';
import {RichTextExtension} from '@lexical/rich-text';
import prepopulatedRichText from './prepopulatedRichText';
const editor = buildEditorFromExtensions({
// This works similarly to LexicalComposer editorState
$initialEditorState: $prepopulatedRichText,
name: '[root]',
namespace: 'Vanilla JS Demo (all-in with Lexical Builder)',
// RichTextExtension has a nodes property to add QuoteNode and HeadingNode
// DragonExtension is a dependency of RichTextExtension
// All three extensions have register properties to add behavior to the editor,
// with defaults for all of the History configuration
dependencies: [RichTextExtension, HistoryExtension],
theme: {quote: 'PlaygroundEditorTheme__quote'},
});
editor.setRootElement(editorRef);