Lexical 0.24 with Vanilla JS: Getting started

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

Tutorial Tutorial

1. Create a Vite project 1. Create a Vite project

First, run:

console
$ bun run create vite lexical-example

Some questions will be asked. Here are my responses as an example:

console
✔ 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:

console
$ 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:

ts
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 is contenteidtable.
  • 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…😂

The page at localhost:5173 in a browser: an empty input box with a blue border. Typing does not change it yet.

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.

console
$ bun add @lexical/plain-text @lexical/history

Edit src/main.ts as below:

diff
  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

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 !!!

The same page after the editor is implemented: the box now holds the typed text “Hello, world.”

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

Scqr [secureal]

Scqr [secureal]: the engineering practice of Reflerd Inc. (opens in a new tab)

Comments and feedback are welcome.