React

Embed the Dragble editor in a React application.

The Dragble editor can be embedded in React using the CDN-based SDK.

Adding the SDK

Add the script tag to your index.html:

html
<script src="https://sdk.dragble.com/latest/dragble-sdk.min.js"></script>

Usage with React

Since React runs on the client side, use useEffect to initialize the editor:

tsx

export default function EmailEditor() {
  const containerRef = useRef<HTMLDivElement>(null);
  const [editor, setEditor] = useState<DragbleEditorInstance | null>(null);

  useEffect(() => {
    if (!containerRef.current) return;

    const pexEditor = dragble.createEditor({
      containerId: "editor-container",
      user: { id: "user-1", name: "Jane", email: "jane@example.com" },
      appearance: { theme: "light" },
    });

    pexEditor.addEventListener("ready", () => {
      console.log("Editor ready");
    });

    pexEditor.addEventListener("change", (design: any) => {
      console.log("Changed", design);
    });

    setEditor(pexEditor);

    return () => {
      pexEditor.destroy();
    };
  }, []);

  return (
    <div id="editor-container" ref={containerRef} style={{ height: "100vh" }} />
  );
}

Editor Instance Methods

MethodSignatureDescription
loadDesign(design: DesignJson) => voidLoad a design.
getDesign() => DesignJsonGet the current design JSON.
exportHtml() => Promise<ExportHtmlData>Export as HTML.
exportImage(options?: ExportImageOptions) => Promise<ExportImageData>Export as image.
exportPdf(options?: ExportPdfOptions) => Promise<ExportPdfData>Export as PDF.
undo() => voidUndo last action.
redo() => voidRedo last action.
setViewMode(mode: ViewMode) => voidSwitch desktop/tablet/mobile.
setTheme(theme: ThemeMode) => voidSwitch light/dark theme.
showPreview() => voidOpen preview modal.
hidePreview() => voidClose preview modal.
destroy() => voidDestroy the editor instance.
addEventListener(event: string, handler: Function) => voidSubscribe to events.
removeEventListener(event: string, handler: Function) => voidUnsubscribe from events.

Event Types

EventHandlerDescription
ready() => voidEditor is fully initialized.
change(design: DesignJson) => voidDesign changed.
load(design: DesignJson) => voidDesign loaded.
error(error: any) => voidError occurred.

TypeScript

To use with TypeScript, declare the global:

ts
declare const dragble: {
  createEditor(config: DragbleConfig): DragbleEditorInstance;
};

interface DragbleConfig {
  containerId: string;
  editorKey?: string;
  editorMode?: "email" | "web";
  user?: { id: string; name: string; email: string };
  appearance?: { theme?: "light" | "dark"; accentColor?: string };
}

interface DragbleEditorInstance {
  loadDesign(design: DesignJson): void;
  getDesign(): DesignJson;
  exportHtml(): Promise<ExportHtmlData>;
  // ... other methods
  addEventListener(event: string, handler: Function): void;
  destroy(): void;
}
WARNING

Always set an explicit height on the editor container. The editor fills its parent and won't render without a defined height.