This guide adds VersaEdit to a React application with TypeScript and Vite: installation, a complete example that captures the edited HTML, the essential properties and how to send the content to your backend. It is based on the published beta, 0.5.0-beta.4.
Requirements: React 18.3 or 19 and a bundler that imports CSS files, such as Vite or Next.js.
Installation
npm install versaedit@betanpm install versaedit@0.5.0-beta.4versaedit@beta installs the most recent beta and records it in package.json with a range (^0.5.0-beta.4), so a later npm install or npm update may bring a newer beta. versaedit@0.5.0-beta.4 installs exactly this version; add --save-exact to record it without a range, so every installation gets the same version until you change it. While the API may still change before 1.0, pinning the version is the safer choice for production.
No project yet? Create one with Vite (React + TypeScript), then install VersaEdit inside it:
npm create vite@latest my-editor -- --template react-ts
cd my-editor
npm install
npm install versaedit@beta
Complete example
Replace src/App.tsx with the code below. The editor starts with an HTML document, every change updates a React state through onChange, and Save content reads the HTML with getHTML() and shows it on the page.
import { useRef, useState } from 'react'
import { VersaEdit, type VersaEditHandle } from 'versaedit'
// Editor styles as a file (used with injectStyles={false}; see "Styles")
import 'versaedit/versaedit.css'
const INITIAL_HTML =
'<h2>Hello, VersaEdit</h2><p>Edit this text and click <strong>Save content</strong>.</p>'
export default function App() {
const editorRef = useRef<VersaEditHandle>(null)
// Latest HTML produced by the editor (updated on every change)
const [html, setHtml] = useState(INITIAL_HTML)
// HTML captured by the "Save content" button
const [savedHtml, setSavedHtml] = useState('')
function handleSave() {
// getHTML() returns the current content (the same HTML as the onChange state)
setSavedHtml(editorRef.current?.getHTML() ?? html)
}
return (
<main style={{ maxWidth: 820, margin: '32px auto', padding: '0 16px' }}>
<h1>VersaEdit Quick Start</h1>
<VersaEdit
ref={editorRef}
initialContent={INITIAL_HTML}
onChange={setHtml}
injectStyles={false}
width="100%"
height={360}
locale="en"
/>
<p>
<button type="button" onClick={handleSave}>
Save content
</button>{' '}
<small>{html.length} characters of HTML</small>
</p>
<h2>Captured HTML</h2>
{/* Shown as text: React escapes it, nothing is executed */}
<pre style={{ whiteSpace: 'pre-wrap', overflowWrap: 'anywhere' }}>
{savedHtml || 'Click "Save content" to capture the HTML.'}
</pre>
</main>
)
}
In a project created with the Vite template, also remove import './index.css' from src/main.tsx (or empty that file): the template centers the page text, and the editor would inherit that alignment.
Run npm run dev and open the address shown by Vite. Type in the editor and click Save content: the HTML appears below the editor, displayed as text.
This is the code of the quick-start example, compiled and tested in a browser in continuous integration with the packed npm package, and after each release with the package from the npm registry.
Styles
By default the editor injects its own stylesheet, so no import is needed. The example turns injection off (injectStyles={false}) and imports versaedit/versaedit.css instead, which is required on sites with a strict Content Security Policy. To display saved content on your site, wrap it in an element with the versaedit-content class and import versaedit/versaedit-content.css.
Essential properties
All properties are optional. The most used ones:
| Property | Type | Required | Default | Description | Example |
|---|---|---|---|---|---|
initialContent | string | No | '' | HTML loaded into the editor (sanitized). Changing it from outside reloads the content and resets undo. | initialContent={html} |
onChange | (html: string) => void | No | — | Called whenever the HTML changes. The editor ignores the echo of its own changes, so it can feed a React state. | onChange={setHtml} |
ref | Ref<VersaEditHandle> | No | — | Methods to read and control the content: getHTML(), setHTML(), clear(), focus(), undo(), redo(). | ref={editorRef} |
onSave | (html: string) => void | No | downloads a .html file | The toolbar Save button hands you the HTML instead of downloading a file. | onSave={saveContent} |
width | number | string | No | '100%' | Total width: a number is px, a string is any CSS length. Never wider than the container. | width="100%" |
height | number | No | 500 | Height of the editing area in px (frame included, toolbar not). | height={360} |
locale | 'en' | 'pt-BR' | 'es' | No | 'en' | Interface language (toolbar, menus, messages, dictation). | locale="pt-BR" |
placeholder | string | No | language text | Text shown while the editor is empty. | placeholder="Write your post" |
toolbar | readonly ToolbarGroup[] | No | all groups | Toolbar groups to show, always in the toolbar’s fixed order; [] hides the toolbar (unless settingsPanel is set). The groups are listed in TOOLBAR_GROUPS. | toolbar={['history', 'format', 'list', 'link']} |
showFileButtons | boolean | No | true | Shows the Save and Open file buttons. | showFileButtons={false} |
injectStyles | boolean | No | true | Injects the editor CSS into <head>. With false, import versaedit/versaedit.css. | injectStyles={false} |
onImageUpload | (file: File) => Promise<string> | No | image stored as base64 | Uploads an inserted image and returns its public URL (recommended). | onImageUpload={uploadImage} |
onError | (error: VersaEditError) => void | No | alert() | Errors and warnings with a stable code (invalid file, truncated content, failed upload). | onError={(e) => toast(e.message)} |
accentColor | string | No | '#2563eb' | Color of selection outlines, text boxes and handles. | accentColor="#0f766e" |
labels | Partial<VersaEditLabels> | No | — | Replaces any interface text. | labels={{ placeholder: 'Start here' }} |
The complete list (frame, fonts, colors, dictation, paste and overflow options, resizable area, settings panel, Layout Mirror and object margin) is in the README.
Ref methods
With ref, the application reads and controls the editor at any time:
| Method | Description |
|---|---|
getHTML(): string | Current HTML (with the paper color, when set). |
setHTML(html: string): void | Replaces the content (resets undo). |
clear(): void | Erases all content. |
focus(): void | Puts the cursor back in the editor. |
undo() / redo() | Undo and redo, as with the toolbar buttons. |
getSize() / setSize(size) | Visible size of the editing area; setSize works only with resizable. |
Capturing and saving the HTML
- The user edits
- VersaEdit produces HTML
- React captures it
- Your application saves it
React captures the HTML in two ways: onChange on every change (for a state, a draft or a character count) and getHTML() through the ref when you need it (for example, when saving). VersaEdit does not replace your backend or your database: it only hands the HTML to the application, which decides where to store it (Supabase, Firebase, a REST API…).
A function that sends the HTML to an API with fetch. It is optional: the example above runs without any backend.
// Sends the HTML to your API (optional: the example above does not call it).
// Adjust the URL, the method and the authentication to your backend.
export async function saveContent(html: string): Promise<void> {
const response = await fetch('/api/documents/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ html }),
})
if (!response.ok) {
throw new Error(`Could not save: HTTP ${response.status}`)
}
}
To save to your API from the example, call it in handleSave:
import { saveContent } from './saveContent'
async function handleSave() {
const current = editorRef.current?.getHTML() ?? html
setSavedHtml(current)
await saveContent(current)
}
Security
The editor sanitizes everything it receives (DOMPurify and its own allow-lists), but it runs in the user’s browser, where it can be bypassed. Your server must validate and sanitize the HTML again before storing or publishing it, according to your application’s security policy. The integration guide includes a tested server-side recipe.
Next steps
- Try every tool in the Playground.
- All properties, error codes and toolbar groups: README.
- Image upload, server-side sanitization and display: integration guide.
- More examples (Vite and Next.js): examples folder.