What is Lexical? Lexical is an open-source text editor framework designed with extensibility, reliability, and performance in mind. Unlike traditional rich text editors that operate directly on the DOM, Lexical uses a virtual DOM-like approach for text editing, making it more predictable and easier to extend.
Key advantages of Lexical include:
Extensibility : Easy to add custom features through plugins
Performance : Optimized for speed and efficiency
Reliability : More predictable behavior compared to traditional ContentEditable implementations
Framework-agnostic : Works with React, Vue, or vanilla JavaScript
Customizable : Full control over the editor’s appearance and behavior
Core Features Lexical provides several core features that make it a compelling choice for developers:
Rich Text Formatting : Support for basic formatting like bold, italic, underline
Lists : Ordered and unordered lists
Tables : Built-in table support
Markdown Support : Can be extended to handle Markdown input/output
Collaborative Editing : Can be integrated with collaborative editing solutions
Theming : Customizable appearance
History Management : Built-in undo/redo functionality
Resources
Quick Start Example Step reate a React app 1 pnpm create vite@latest lexical-demo --template react-ts
Install Lib 1 pnpm install --save lexical @lexical/react
Basic Example (Offical Doc Code) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 import {$getRoot, $getSelection} from 'lexical' ;import {useEffect} from 'react' ;import {AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin' ;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 {LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary' ;const theme = { }function onError (error ) { console .error (error); }function Editor ( ) { const initialConfig = { namespace : 'MyEditor' , theme, onError, }; return ( <LexicalComposer initialConfig ={initialConfig} > <RichTextPlugin contentEditable ={ <ContentEditable aria-placeholder ={ 'Enter some text... '} placeholder ={ <div > Enter some text...</div > } /> } ErrorBoundary={LexicalErrorBoundary} /> <HistoryPlugin /> <AutoFocusPlugin /> </LexicalComposer > ); }
Out of the box Lexical doesn’t provide any type of UI as it’s not a ready to use editor but rather a framework for creation of your own editor.
Add 2 new plugins
ToolbarPlugin
- renders UI to control text formatting
TreeViewPlugin
- renders debug view below the editor so we can see its state in real time
The code is an official case, I only use it for learning purposes.
Install Lib 1 pnpm install @lexical/utils
Plugins
Click to expand/collapse code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext' ;import {mergeRegister} from '@lexical/utils' ;import { $getSelection, $isRangeSelection, CAN_REDO_COMMAND , CAN_UNDO_COMMAND , FORMAT_ELEMENT_COMMAND , FORMAT_TEXT_COMMAND , REDO_COMMAND , SELECTION_CHANGE_COMMAND , UNDO_COMMAND , } from 'lexical' ;import {useCallback, useEffect, useRef, useState} from 'react' ;const LowPriority = 1 ;function Divider ( ) { return <div className ="divider" /> ; }export default function ToolbarPlugin ( ) { const [editor] = useLexicalComposerContext (); const toolbarRef = useRef (null ); const [canUndo, setCanUndo] = useState (false ); const [canRedo, setCanRedo] = useState (false ); const [isBold, setIsBold] = useState (false ); const [isItalic, setIsItalic] = useState (false ); const [isUnderline, setIsUnderline] = useState (false ); const [isStrikethrough, setIsStrikethrough] = useState (false ); const $updateToolbar = useCallback (() => { const selection = $getSelection(); if ($isRangeSelection(selection)) { setIsBold (selection.hasFormat ('bold' )); setIsItalic (selection.hasFormat ('italic' )); setIsUnderline (selection.hasFormat ('underline' )); setIsStrikethrough (selection.hasFormat ('strikethrough' )); } }, []); useEffect (() => { return mergeRegister ( editor.registerUpdateListener (({editorState} ) => { editorState.read (() => { $updateToolbar(); }); }), editor.registerCommand ( SELECTION_CHANGE_COMMAND , (_payload, _newEditor ) => { $updateToolbar(); return false ; }, LowPriority , ), editor.registerCommand ( CAN_UNDO_COMMAND , (payload ) => { setCanUndo (payload); return false ; }, LowPriority , ), editor.registerCommand ( CAN_REDO_COMMAND , (payload ) => { setCanRedo (payload); return false ; }, LowPriority , ), ); }, [editor, $updateToolbar]); return ( <div className ="toolbar" ref ={toolbarRef} > <button disabled ={!canUndo} onClick ={() => { editor.dispatchCommand(UNDO_COMMAND, undefined); }} className="toolbar-item spaced" aria-label="Undo"> <i className ="format undo" /> </button > <button disabled ={!canRedo} onClick ={() => { editor.dispatchCommand(REDO_COMMAND, undefined); }} className="toolbar-item" aria-label="Redo"> <i className ="format redo" /> </button > <Divider /> <button onClick ={() => { editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'bold'); }} className={'toolbar-item spaced ' + (isBold ? 'active' : '')} aria-label="Format Bold"> <i className ="format bold" /> </button > <button onClick ={() => { editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'italic'); }} className={'toolbar-item spaced ' + (isItalic ? 'active' : '')} aria-label="Format Italics"> <i className ="format italic" /> </button > <button onClick ={() => { editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'underline'); }} className={'toolbar-item spaced ' + (isUnderline ? 'active' : '')} aria-label="Format Underline"> <i className ="format underline" /> </button > <button onClick ={() => { editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'strikethrough'); }} className={'toolbar-item spaced ' + (isStrikethrough ? 'active' : '')} aria-label="Format Strikethrough"> <i className ="format strikethrough" /> </button > <Divider /> <button onClick ={() => { editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'left'); }} className="toolbar-item spaced" aria-label="Left Align"> <i className ="format left-align" /> </button > <button onClick ={() => { editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'center'); }} className="toolbar-item spaced" aria-label="Center Align"> <i className ="format center-align" /> </button > <button onClick ={() => { editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'right'); }} className="toolbar-item spaced" aria-label="Right Align"> <i className ="format right-align" /> </button > <button onClick ={() => { editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, 'justify'); }} className="toolbar-item" aria-label="Justify Align"> <i className ="format justify-align" /> </button > {' '} </div > ); }
File src/plugins/TreeViewPlugin.tsx
Click to expand/collapse code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import type {JSX } from 'react' ;import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext' ;import {TreeView } from '@lexical/react/LexicalTreeView' ;export default function TreeViewPlugin ( ): JSX .Element { const [editor] = useLexicalComposerContext (); return ( <TreeView viewClassName ="tree-view-output" treeTypeButtonClassName ="debug-treetype-button" timeTravelPanelClassName ="debug-timetravel-panel" timeTravelButtonClassName ="debug-timetravel-button" timeTravelPanelSliderClassName ="debug-timetravel-panel-slider" timeTravelPanelButtonClassName ="debug-timetravel-panel-button" editor ={editor} /> ); }
File src/App.tsx
Click to expand/collapse code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin' ;import { LexicalComposer } from '@lexical/react/LexicalComposer' ;import { ContentEditable } from '@lexical/react/LexicalContentEditable' ;import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary' ;import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin' ;import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin' ;import { $isTextNode, DOMConversionMap , DOMExportOutput , DOMExportOutputMap , isHTMLElement, Klass , LexicalEditor , LexicalNode , ParagraphNode , TextNode , } from 'lexical' ;import ExampleTheme from './ExampleTheme' ;import ToolbarPlugin from './plugins/ToolbarPlugin' ;import TreeViewPlugin from './plugins/TreeViewPlugin' ;import { parseAllowedColor, parseAllowedFontSize } from './styleConfig' ;const placeholder = 'Enter some rich text...' ;const removeStylesExportDOM = ( editor : LexicalEditor , target : LexicalNode , ): DOMExportOutput => { const output = target.exportDOM (editor); if (output && isHTMLElement (output.element )) { for (const el of [ output.element , ...output.element .querySelectorAll ('[style],[class],[dir="ltr"]' ), ]) { el.removeAttribute ('class' ); el.removeAttribute ('style' ); if (el.getAttribute ('dir' ) === 'ltr' ) { el.removeAttribute ('dir' ); } } } return output; };const exportMap : DOMExportOutputMap = new Map < Klass <LexicalNode >, (editor: LexicalEditor, target: LexicalNode ) => DOMExportOutput >([ [ParagraphNode , removeStylesExportDOM], [TextNode , removeStylesExportDOM], ]);const getExtraStyles = (element : HTMLElement ): string => { let extraStyles = '' ; const fontSize = parseAllowedFontSize (element.style .fontSize ); const backgroundColor = parseAllowedColor (element.style .backgroundColor ); const color = parseAllowedColor (element.style .color ); if (fontSize !== '' && fontSize !== '15px' ) { extraStyles += `font-size: ${fontSize} ;` ; } if (backgroundColor !== '' && backgroundColor !== 'rgb(255, 255, 255)' ) { extraStyles += `background-color: ${backgroundColor} ;` ; } if (color !== '' && color !== 'rgb(0, 0, 0)' ) { extraStyles += `color: ${color} ;` ; } return extraStyles; };const constructImportMap = (): DOMConversionMap => { const importMap : DOMConversionMap = {}; for (const [tag, fn] of Object .entries (TextNode .importDOM () || {})) { importMap[tag] = (importNode ) => { const importer = fn (importNode); if (!importer) { return null ; } return { ...importer, conversion : (element ) => { const output = importer.conversion (element); if ( output === null || output.forChild === undefined || output.after !== undefined || output.node !== null ) { return output; } const extraStyles = getExtraStyles (element); if (extraStyles) { const { forChild } = output; return { ...output, forChild : (child, parent ) => { const textNode = forChild (child, parent); if ($isTextNode(textNode)) { textNode.setStyle (textNode.getStyle () + extraStyles); } return textNode; }, }; } return output; }, }; }; } return importMap; };const editorConfig = { html : { export : exportMap, import : constructImportMap (), }, namespace : 'React.js Demo' , nodes : [ParagraphNode , TextNode ], onError (error: Error ) { throw error; }, theme : ExampleTheme , };export default function App ( ) { return ( <LexicalComposer initialConfig ={editorConfig} > <div className ="editor-container" > <ToolbarPlugin /> <div className ="editor-inner" > <RichTextPlugin contentEditable ={ <ContentEditable className ="editor-input" aria-placeholder ={placeholder} placeholder ={ <div className ="editor-placeholder" > {placeholder}</div > } /> } ErrorBoundary={LexicalErrorBoundary} /> <HistoryPlugin /> <AutoFocusPlugin /> <TreeViewPlugin /> </div > </div > </LexicalComposer > ); }
File src/ExampleTheme.ts
Click to expand/collapse code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 export default { code : 'editor-code' , heading : { h1 : 'editor-heading-h1' , h2 : 'editor-heading-h2' , h3 : 'editor-heading-h3' , h4 : 'editor-heading-h4' , h5 : 'editor-heading-h5' , }, image : 'editor-image' , link : 'editor-link' , list : { listitem : 'editor-listitem' , nested : { listitem : 'editor-nested-listitem' , }, ol : 'editor-list-ol' , ul : 'editor-list-ul' , }, ltr : 'ltr' , paragraph : 'editor-paragraph' , placeholder : 'editor-placeholder' , quote : 'editor-quote' , rtl : 'rtl' , text : { bold : 'editor-text-bold' , code : 'editor-text-code' , hashtag : 'editor-text-hashtag' , italic : 'editor-text-italic' , overflowed : 'editor-text-overflowed' , strikethrough : 'editor-text-strikethrough' , underline : 'editor-text-underline' , underlineStrikethrough : 'editor-text-underlineStrikethrough' , }, };
File src/styleConfig.ts
Click to expand/collapse code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const MIN_ALLOWED_FONT_SIZE = 8 ;const MAX_ALLOWED_FONT_SIZE = 72 ;export const parseAllowedFontSize = (input : string): string => { const match = input.match (/^(\d+(?:\.\d+)?)px$/ ); if (match) { const n = Number (match[1 ]); if (n >= MIN_ALLOWED_FONT_SIZE && n <= MAX_ALLOWED_FONT_SIZE ) { return input; } } return '' ; };export function parseAllowedColor (input: string ) { return /^rgb\(\d+, \d+, \d+\)$/ .test (input) ? input : '' ; }
Although the code runs normally, there is no icon, interface, etc. because there is no style control.
File src/styles.css
Click to expand/collapse code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 body { margin : 0 ; background : #eee ; font-family : system-ui, -apple-system, BlinkMacSystemFont, '.SFNSText-Regular' , sans-serif; font-weight : 500 ; -webkit-font-smoothing : antialiased; -moz-osx-font-smoothing : grayscale; } .other h2 { font-size : 18px ; color : #444 ; margin-bottom : 7px ; } .other a { color : #777 ; text-decoration : underline; font-size : 14px ; } .other ul { padding : 0 ; margin : 0 ; list-style-type : none; } .App { font-family : sans-serif; text-align : center; } h1 { font-size : 24px ; color : #333 ; } .ltr { text-align : left; } .rtl { text-align : right; } .editor-container { margin : 20px auto 20px auto; border-radius : 2px ; max-width : 600px ; color : #000 ; position : relative; line-height : 20px ; font-weight : 400 ; text-align : left; border-top-left-radius : 10px ; border-top-right-radius : 10px ; } .editor-inner { background : #fff ; position : relative; } .editor-input { min-height : 150px ; resize : none; font-size : 15px ; caret-color : rgb (5 , 5 , 5 ); position : relative; tab-size : 1 ; outline : 0 ; padding : 15px 10px ; caret-color : #444 ; } .editor-placeholder { color : #999 ; overflow : hidden; position : absolute; text-overflow : ellipsis; top : 15px ; left : 10px ; font-size : 15px ; user-select : none; display : inline-block; pointer-events : none; } .editor-text-bold { font-weight : bold; } .editor-text-italic { font-style : italic; } .editor-text-underline { text-decoration : underline; } .editor-text-strikethrough { text-decoration : line-through; } .editor-text-underlineStrikethrough { text-decoration : underline line-through; } .editor-text-code { background-color : rgb (240 , 242 , 245 ); padding : 1px 0.25rem ; font-family : Menlo, Consolas, Monaco, monospace; font-size : 94% ; } .editor-link { color : rgb (33 , 111 , 219 ); text-decoration : none; } .tree-view-output { display : block; background : #222 ; color : #fff ; padding : 5px ; font-size : 12px ; white-space : pre-wrap; margin : 1px auto 10px auto; max-height : 250px ; position : relative; border-bottom-left-radius : 10px ; border-bottom-right-radius : 10px ; overflow : auto; line-height : 14px ; } .editor-code { background-color : rgb (240 , 242 , 245 ); font-family : Menlo, Consolas, Monaco, monospace; display : block; padding : 8px 8px 8px 52px ; line-height : 1.53 ; font-size : 13px ; margin : 0 ; margin-top : 8px ; margin-bottom : 8px ; tab-size : 2 ; overflow-x : auto; position : relative; } .editor-code :before { content : attr (data-gutter); position : absolute; background-color : #eee ; left : 0 ; top : 0 ; border-right : 1px solid #ccc ; padding : 8px ; color : #777 ; white-space : pre-wrap; text-align : right; min-width : 25px ; } .editor-code :after { content : attr (data-highlight-language); top : 0 ; right : 3px ; padding : 3px ; font-size : 10px ; text-transform : uppercase; position : absolute; color : rgba (0 , 0 , 0 , 0.5 ); } .editor-tokenComment { color : slategray; } .editor-tokenPunctuation { color : #999 ; } .editor-tokenProperty { color : #905 ; } .editor-tokenSelector { color : #690 ; } .editor-tokenOperator { color : #9a6e3a ; } .editor-tokenAttr { color : #07a ; } .editor-tokenVariable { color : #e90 ; } .editor-tokenFunction { color : #dd4a68 ; } .editor-paragraph { margin : 0 ; margin-bottom : 8px ; position : relative; } .editor-paragraph :last-child { margin-bottom : 0 ; } .editor-heading-h1 { font-size : 24px ; color : rgb (5 , 5 , 5 ); font-weight : 400 ; margin : 0 ; margin-bottom : 12px ; padding : 0 ; } .editor-heading-h2 { font-size : 15px ; color : rgb (101 , 103 , 107 ); font-weight : 700 ; margin : 0 ; margin-top : 10px ; padding : 0 ; text-transform : uppercase; } .editor-quote { margin : 0 ; margin-left : 20px ; font-size : 15px ; color : rgb (101 , 103 , 107 ); border-left-color : rgb (206 , 208 , 212 ); border-left-width : 4px ; border-left-style : solid; padding-left : 16px ; } .editor-list-ol { padding : 0 ; margin : 0 ; margin-left : 16px ; } .editor-list-ul { padding : 0 ; margin : 0 ; margin-left : 16px ; } .editor-listitem { margin : 8px 32px 8px 32px ; } .editor-nested-listitem { list-style-type : none; } pre::-webkit-scrollbar { background : transparent; width : 10px ; } pre::-webkit-scrollbar-thumb { background : #999 ; } .debug-timetravel-panel { overflow : hidden; padding : 0 0 10px 0 ; margin : auto; display : flex; } .debug-timetravel-panel-slider { padding : 0 ; flex : 8 ; } .debug-timetravel-panel-button { padding : 0 ; border : 0 ; background : none; flex : 1 ; color : #fff ; font-size : 12px ; } .debug-timetravel-panel-button :hover { text-decoration : underline; } .debug-timetravel-button { border : 0 ; padding : 0 ; font-size : 12px ; top : 10px ; right : 15px ; position : absolute; background : none; color : #fff ; } .debug-timetravel-button :hover { text-decoration : underline; } .toolbar { display : flex; margin-bottom : 1px ; background : #fff ; padding : 4px ; border-top-left-radius : 10px ; border-top-right-radius : 10px ; vertical-align : middle; } .toolbar button .toolbar-item { border : 0 ; display : flex; background : none; border-radius : 10px ; padding : 8px ; cursor : pointer; vertical-align : middle; } .toolbar button .toolbar-item :disabled { cursor : not-allowed; } .toolbar button .toolbar-item .spaced { margin-right : 2px ; } .toolbar button .toolbar-item i .format { background-size : contain; display : inline-block; height : 18px ; width : 18px ; margin-top : 2px ; vertical-align : -0.25em ; display : flex; opacity : 0.6 ; } .toolbar button .toolbar-item :disabled i .format { opacity : 0.2 ; } .toolbar button .toolbar-item .active { background-color : rgba (223 , 232 , 250 , 0.3 ); } .toolbar button .toolbar-item .active i { opacity : 1 ; } .toolbar .toolbar-item :hover :not ([disabled] ) { background-color : #eee ; } .toolbar .divider { width : 1px ; background-color : #eee ; margin : 0 4px ; } .toolbar .toolbar-item .text { display : flex; line-height : 20px ; width : 200px ; vertical-align : middle; font-size : 14px ; color : #777 ; text-overflow : ellipsis; width : 70px ; overflow : hidden; height : 20px ; text-align : left; } .toolbar .toolbar-item .icon { display : flex; width : 20px ; height : 20px ; user-select : none; margin-right : 8px ; line-height : 16px ; background-size : contain; } i .undo { background-image : url (icons/arrow-counterclockwise.svg ); } i .redo { background-image : url (icons/arrow-clockwise.svg ); } i .bold { background-image : url (icons/type-bold.svg ); } i .italic { background-image : url (icons/type-italic.svg ); } i .underline { background-image : url (icons/type-underline.svg ); } i .strikethrough { background-image : url (icons/type-strikethrough.svg ); } i .left-align { background-image : url (icons/text-left.svg ); } i .center-align { background-image : url (icons/text-center.svg ); } i .right-align { background-image : url (icons/text-right.svg ); } i .justify-align { background-image : url (icons/justify.svg ); }
File src/main.tsx
Import css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import './styles.css' ;import { StrictMode } from 'react' import { createRoot } from 'react-dom/client' import App from './App.tsx' createRoot (document .getElementById ('root' )!).render ( <StrictMode > <div className ="App" > <App /> </div > </StrictMode > )
We found that it is working properly.
Let’s create a simple Lexical editor with React to demonstrate the basics. We’ll implement a basic editor with formatting toolbar.
How the Example Works Let’s break down the key parts of our simple Lexical editor:
LexicalComposer : The root component that initializes the editor with configuration
RichTextPlugin : Handles rendering the editable content
HistoryPlugin : Provides undo/redo functionality
ListPlugin : Enables list formatting
ToolbarPlugin : Our custom component for basic formatting controls
The key aspects to understand in Lexical are:
The editor state is managed through updates via editor.update()
Nodes represent different content types (paragraphs, headings, lists, etc.)
Plugins extend functionality without modifying core code
Core Concepts Editor State Lexical uses an immutable state model. Every change to the editor happens through an update cycle:
1 2 3 editor.update (() => { });
Nodes Everything in Lexical is represented as a node - paragraphs, tables, list items, etc. The framework comes with built-in nodes and allows for custom nodes.
Selection Lexical abstracts DOM selection with its own selection model, making it easier to work with text selection across different browsers.
Using transformers, you can convert between Lexical’s internal format and other formats like HTML or Markdown.
Advanced Features While our example is simple, Lexical supports advanced features like:
Collaborative editing : Integration with libraries like Yjs
Custom nodes : Create specialized nodes for unique content types
Internationalization : Support for different languages and text directions
Accessibility : Built with accessibility in mind
Autocompletion : Can be extended for mentions, hashtags, etc.
Conclusion Lexical makes it easier to build reliable editors without fighting against the DOM’s quirks.
Our simple example just scratches the surface of what’s possible with Lexical. As you grow more comfortable with the framework, you can explore more complex features like tables, code blocks, or even collaborative editing.