Skip to main content

React Reference

The @pexelize/react-editor package provides a React component and hook for embedding the Pexelize editor.

Installation

npm install @pexelize/react-editor @pexelize/editor-types

Quick Start

import { PexelizeEditor } from '@pexelize/react-editor';
import type { DesignJson, EditorOptions } from '@pexelize/editor-types';

function App() {
const options: EditorOptions = {
user: { id: 'user-1', name: 'Jane', email: 'jane@example.com' },
appearance: { theme: 'light' },
};

const handleReady = () => console.log('Editor ready');
const handleChange = (design: DesignJson) => console.log('Changed', design);

return (
<PexelizeEditor
options={options}
style={{ height: '100vh' }}
onReady={handleReady}
onChange={handleChange}
/>
);
}

PexelizeEditor Component Props

PropTypeRequiredDescription
optionsEditorOptionsYesEditor configuration. See Configuration.
designDesignJsonNoInitial design JSON to load.
styleReact.CSSPropertiesNoInline styles for the container div.
classNamestringNoCSS class for the container div.
onReady() => voidNoFires when the editor is fully initialized.
onLoad(design: DesignJson) => voidNoFires when a design is loaded.
onChange(design: DesignJson) => voidNoFires on every design change.
onError(error: EditorError) => voidNoFires on editor errors.
onModuleSave(data: ModuleSaveData) => Promise<void>NoFires when a module is saved. Blocking.
onPreview(data: PreviewData) => voidNoFires when the user clicks preview.
onContentDialog(data: ContentDialogData) => Promise<DesignJson>NoFires when the content dialog opens. Blocking.

usePexelizeEditor Hook

For imperative control, use the usePexelizeEditor hook:

import { PexelizeEditor, usePexelizeEditor } from '@pexelize/react-editor';
import type { DesignJson, ExportHtmlData } from '@pexelize/editor-types';

function App() {
const { ref, isReady, loadDesign, exportHtml, undo, redo } = usePexelizeEditor();

const handleExport = async () => {
const html: ExportHtmlData = await exportHtml();
console.log(html);
};

return (
<div>
<button onClick={handleExport} disabled={!isReady}>Export HTML</button>
<button onClick={undo} disabled={!isReady}>Undo</button>
<button onClick={redo} disabled={!isReady}>Redo</button>
<PexelizeEditor
ref={ref}
options={{ user: { id: '1', name: 'Jane', email: 'jane@example.com' } }}
style={{ height: '100vh' }}
/>
</div>
);
}

Hook Return Values

PropertyTypeDescription
refRefObject<PexelizeEditorRef>Pass to the ref prop of <PexelizeEditor>.
isReadybooleantrue once the editor is initialized.
loadDesign(design: DesignJson) => voidLoad a design programmatically.
getDesign() => DesignJsonGet the current design JSON.
exportHtml() => Promise<ExportHtmlData>Export design as HTML.
exportImage(options?: ExportImageOptions) => Promise<ExportImageData>Export design as image.
exportPdf(options?: ExportPdfOptions) => Promise<ExportPdfData>Export design as PDF.
undo() => voidUndo last action.
redo() => voidRedo last action.
setViewMode(mode: ViewMode) => voidSwitch between desktop/tablet/mobile view.
setTheme(theme: ThemeMode) => voidSwitch light/dark theme.
showPreview() => voidOpen the preview modal.
hidePreview() => voidClose the preview modal.

Ref Methods

If you prefer using refs directly instead of the hook:

import { useRef } from 'react';
import { PexelizeEditor, type PexelizeEditorRef } from '@pexelize/react-editor';

function App() {
const editorRef = useRef<PexelizeEditorRef>(null);

const handleSave = async () => {
const design = editorRef.current?.getDesign();
const html = await editorRef.current?.exportHtml();
// save to your backend
};

return (
<PexelizeEditor
ref={editorRef}
options={{ user: { id: '1', name: 'Jane', email: 'jane@example.com' } }}
style={{ height: '100vh' }}
onReady={() => console.log('Ready')}
/>
);
}

PexelizeEditorRef exposes all the same methods as the hook (except ref and isReady).

TypeScript

All types are re-exported from @pexelize/editor-types for convenience:

import type {
DesignJson,
EditorOptions,
ExportHtmlData,
ExportImageData,
ExportPdfData,
ViewMode,
ThemeMode,
} from '@pexelize/react-editor';
tip

Use the usePexelizeEditor hook for the most ergonomic API. It manages the ref internally and provides reactive isReady state.

warning

Always set an explicit height on the editor container (via style or className). The editor fills its parent and won't render without a defined height.