Skip to content

Commit 17ba800

Browse files
committed
Pass unlayer instance to onLoad and onReady callbacks
Closes #201 Closes #180
1 parent 30c5dde commit 17ba800

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

demo/src/example/index.tsx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import React, { useRef, useState } from 'react';
22
import styled from 'styled-components';
33

44
import packageJson from '../../../package.json';
5-
6-
import EmailEditor from '../../../src';
7-
import { EditorRef } from '../../../src/types';
5+
import EmailEditor, { EditorRef, EmailEditorProps } from '../../../src'; // use react-email-editor instead
86
import sample from './sample.json';
97

108
const Container = styled.div`
@@ -75,19 +73,14 @@ const Example = () => {
7573
console.log('onDesignLoad', data);
7674
};
7775

78-
const onLoad = () => {
79-
console.log('onLoad');
80-
81-
emailEditorRef.current?.editor?.addEventListener(
82-
'design:loaded',
83-
onDesignLoad
84-
);
85-
86-
emailEditorRef.current?.editor?.loadDesign(sample);
76+
const onLoad: EmailEditorProps['onLoad'] = (unlayer) => {
77+
console.log('onLoad', unlayer);
78+
unlayer.addEventListener('design:loaded', onDesignLoad);
79+
unlayer.loadDesign(sample);
8780
};
8881

89-
const onReady = () => {
90-
console.log('onReady');
82+
const onReady: EmailEditorProps['onReady'] = (unlayer) => {
83+
console.log('onReady', unlayer);
9184
};
9285

9386
return (

src/EmailEditor.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const EmailEditor = React.forwardRef<EditorRef, EmailEditorProps>(
7070
useEffect(() => {
7171
if (!editor) return;
7272

73-
onLoad?.();
73+
onLoad?.(editor);
7474

7575
// All properties starting with on[Name] are registered as event listeners.
7676
methodProps.forEach((methodProp) => {
@@ -84,7 +84,11 @@ export const EmailEditor = React.forwardRef<EditorRef, EmailEditorProps>(
8484
}
8585
});
8686

87-
if (onReady) editor.addEventListener('editor:ready', onReady);
87+
if (onReady) {
88+
editor.addEventListener('editor:ready', () => {
89+
onReady(editor);
90+
});
91+
}
8892
}, [editor, Object.keys(methodProps).join(',')]);
8993

9094
return (

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export interface EditorRef {
1717
export interface EmailEditorProps {
1818
editorId?: string | undefined;
1919
minHeight?: number | string | undefined;
20-
onLoad?(): void;
21-
onReady?(): void;
20+
onLoad?(unlayer: Editor): void;
21+
onReady?(unlayer: Editor): void;
2222
options?: UnlayerOptions | undefined;
2323
scriptUrl?: string | undefined;
2424
style?: CSSProperties | undefined;

0 commit comments

Comments
 (0)