Lexical 0.24 with Vanilla JS: Getting started
- Web and back-end development
- Published
- 3 min read
- @nabbisen
Table of contents
Summary Summary
Lexical (opens in a new tab) is a modern JavaScript (JS) text editor framework designed for extensibility and high performance. They adopt reactive state model, which ensures optimized updates and predictable behavior, and also have nodes as flexible data model.
Lexical is an open source project and considered the successor of Draft.js (opens in a new tab). It is primarily developed by Meta (opens in a new tab), licensed under MIT (opens in a new tab). It is not restricted to React (opens in a new tab), but supports Vanilla JS, too. The flexibility enables us to integrate it with other JS libraries such as Svelte (opens in a new tab) and Vue (opens in a new tab).
This post shows how to embed the editor in a Bun (opens in a new tab)-powered Vite (opens in a new tab) project handled by Vanilla JS.
Environment Environment
- Web Editor: Lexical 0.24
- Builder / Bundler: Vite 6 (opens in a new tab).1.0
- JavaScript runtime: Bun 1.2 (opens in a new tab).2
- OS: CathyOS (opens in a new tab) (Linux (opens in a new tab) distribution)
Tutorial Tutorial
1. Create a Vite project 1. Create a Vite project
First, run:
$ bun run create vite lexical-example
Some questions will be asked. Here are my responses as an example:
✔ Select a framework: › Vanilla
✔ Select a variant: › TypeScript
Scaffolding project in /(...)/lexical-example...
Done. Now run:
cd lexical-example
bun install
bun run dev
Let’s come in:
$ cd lexical-example
$ bun install
2. Install the editor framework 2. Install the editor framework
Add the dependency:
$ bun add lexical
It printed out installed lexical@0.24.0.
3. Embed it into app 3. Embed it into app
Rewrite src/main.ts as below:
import { createEditor, type LexicalEditor } from 'lexical'
document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
<div id="myeditor" contenteditable></div>
`
let editor: LexicalEditor
document.addEventListener('DOMContentLoaded', () => {
const editorElement = document.getElementById('myeditor')
editor = createEditor()
editor.setRootElement(editorElement)
})There are three points:
- Prepare
<div>which iscontenteidtable. - Create
LexicalEditor. - Set the
<div>as root element in the editor.
4. Test it to fail 4. Test it to fail
Open http://localhost:5173/ with your web browser.
Typing in it must not bring any update…😂

5. Implement an actual editor 5. Implement an actual editor
The reason around the failure is that there is LexicalEditor instance but are not any actual editors activated.
We can fix it by registering packages such as plain text editor (opens in a new tab). Optionally, history module (opens in a new tab) useful to manage input history is also available.
$ bun add @lexical/plain-text @lexical/history
Edit src/main.ts as below:
import { createEditor, type LexicalEditor } from "lexical"
+ import { createEmptyHistoryState, registerHistory } from '@lexical/history'
+ import { registerPlainText } from '@lexical/plain-text'
(...)
document.addEventListener("DOMContentLoaded", () => {
(...)
+ registerPlainText(editor)
+ registerHistory(editor, createEmptyHistoryState(), 300)
})
Complete src/main.ts Complete src/main.ts
import { createEditor, type LexicalEditor } from "lexical"
import { createEmptyHistoryState, registerHistory } from '@lexical/history';
import { registerPlainText } from "@lexical/plain-text";
document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
<div id="myeditor" contenteditable></div>
`
let editor: LexicalEditor;
document.addEventListener("DOMContentLoaded", () => {
const editorElement = document.getElementById("myeditor")
editor = createEditor()
editor.setRootElement(editorElement)
registerPlainText(editor)
registerHistory(editor, createEmptyHistoryState(), 300);
})6. Test it to succeed 6. Test it to succeed
Now we can type to dive in !!!

Conclusion Conclusion
Lexical is an open source text editor framework designed for speed, reliability, and flexibility. It enables developers to create not only plain text editors but also rich text or WYSIWYG editors with structured content, plugins, and collaborative features.
Happy development :)
More on: Web and back-end development
-
The latest Svelte 5 update brings runes, a fresh approach to reactivity which replaced $:.
-
Quarkus is one of Java frameworks for microservices development and cloud-native deployment.
-
Apache Maven is a popular open source software to build and manage projects of Java (and also other programming languages), licensed under Apache-2.0.
Scqr [secureal]
Scqr [secureal]: the engineering practice of Reflerd Inc. (opens in a new tab)
Comments and feedback are welcome.