Repository navigation
feat(editor): add EmailEditor high-level component #3115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joaopcm
merged 6 commits into
feat/ai-oss-editor
from
feature/product-1690-work-on-the-one-line-editor
Mar 26, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9fff1e6
feat(editor): add EmailEditor high-level component
joaopcm ef269d2
chore(editor): bump version to 0.0.0-experimental.32
joaopcm 130a67c
feat(editor): add one-line editor examples
joaopcm 39b6c4c
merge feat/ai-oss-editor, resolve conflicts
joaopcm b24238f
chore(editor): bump version to 0.0.0-experimental.37
joaopcm bb8ac67
refactor(editor): use arrow function in EmailEditor forwardRef
joaopcm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/editor/examples/src/examples/one-line-editor-full.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { EmailEditor, type EmailEditorRef } from '@react-email/editor'; | ||
| import { useRef, useState } from 'react'; | ||
| import { ExampleShell } from '../example-shell'; | ||
|
|
||
| const content = { | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'heading', | ||
| attrs: { level: 1 }, | ||
| content: [{ type: 'text', text: 'Welcome to the Newsletter' }], | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'Edit this content, then use the buttons below to export or inspect the editor output. Try switching themes to see how the editor adapts.', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| export function OneLineEditorFull() { | ||
| const editorRef = useRef<EmailEditorRef>(null); | ||
| const [theme, setTheme] = useState<'basic' | 'minimal'>('basic'); | ||
| const [output, setOutput] = useState(''); | ||
|
|
||
| const handleExportHtml = async () => { | ||
| if (!editorRef.current) return; | ||
| const result = await editorRef.current.export(); | ||
| setOutput(result.html); | ||
| }; | ||
|
|
||
| const handleGetJson = () => { | ||
| if (!editorRef.current) return; | ||
| setOutput(JSON.stringify(editorRef.current.getJSON(), null, 2)); | ||
| }; | ||
|
|
||
| return ( | ||
| <ExampleShell | ||
| title="One-Line Editor — Full Features" | ||
| description="Theme switching, ref methods (export, getJSON), and callbacks — all with a single component." | ||
| > | ||
| <div className="flex gap-2 mb-4"> | ||
| <button | ||
| type="button" | ||
| onClick={() => setTheme(theme === 'basic' ? 'minimal' : 'basic')} | ||
| className="px-3 py-1.5 border border-(--re-border) rounded-lg bg-(--re-bg) text-(--re-text) cursor-pointer text-[0.8125rem] hover:bg-(--re-hover)" | ||
| > | ||
| Theme: {theme} | ||
| </button> | ||
| <button | ||
| type="button" | ||
| onClick={handleExportHtml} | ||
| className="px-3 py-1.5 border border-(--re-border) rounded-lg bg-(--re-bg) text-(--re-text) cursor-pointer text-[0.8125rem] hover:bg-(--re-hover)" | ||
| > | ||
| Export HTML | ||
| </button> | ||
| <button | ||
| type="button" | ||
| onClick={handleGetJson} | ||
| className="px-3 py-1.5 border border-(--re-border) rounded-lg bg-(--re-bg) text-(--re-text) cursor-pointer text-[0.8125rem] hover:bg-(--re-hover)" | ||
| > | ||
| Get JSON | ||
| </button> | ||
| </div> | ||
|
|
||
| <EmailEditor ref={editorRef} content={content} theme={theme} /> | ||
|
|
||
| {output && ( | ||
| <textarea | ||
| readOnly | ||
| value={output} | ||
| className="mt-4 w-full h-64 p-3 font-mono text-xs bg-(--re-bg) text-(--re-text) border border-(--re-border) rounded-lg resize-y" | ||
| /> | ||
| )} | ||
| </ExampleShell> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { EmailEditor } from '@react-email/editor'; | ||
| import { ExampleShell } from '../example-shell'; | ||
|
|
||
| const content = { | ||
| type: 'doc', | ||
| content: [ | ||
| { | ||
| type: 'paragraph', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'This is the simplest way to use the editor — a single component with everything pre-wired. Try selecting text to see the bubble menu, or type "/" for slash commands.', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| export function OneLineEditor() { | ||
| return ( | ||
| <ExampleShell | ||
| title="One-Line Editor" | ||
| description="The simplest setup — one component with everything included." | ||
| > | ||
| <EmailEditor content={content} /> | ||
| </ExampleShell> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import type { Content, Editor, Extensions, JSONContent } from '@tiptap/core'; | ||
| import { UndoRedo } from '@tiptap/extensions'; | ||
| import { | ||
| EditorProvider, | ||
| type UseEditorOptions, | ||
| useCurrentEditor, | ||
| } from '@tiptap/react'; | ||
| import { forwardRef, type Ref, useImperativeHandle, useMemo } from 'react'; | ||
| import { createDropHandler } from '../core/create-drop-handler'; | ||
| import { | ||
| createPasteHandler, | ||
| type UploadImageHandler, | ||
| } from '../core/create-paste-handler'; | ||
| import { composeReactEmail } from '../core/serializer/compose-react-email'; | ||
| import { StarterKit } from '../extensions'; | ||
| import { EmailTheming } from '../plugins/email-theming/extension'; | ||
| import { BubbleMenuDefault } from '../ui/bubble-menu/default'; | ||
| import { ButtonBubbleMenuDefault } from '../ui/button-bubble-menu/default'; | ||
| import { ImageBubbleMenuDefault } from '../ui/image-bubble-menu/default'; | ||
| import { LinkBubbleMenuDefault } from '../ui/link-bubble-menu/default'; | ||
| import { SlashCommandRoot } from '../ui/slash-command/root'; | ||
| import '../ui/themes/default.css'; | ||
|
|
||
| export interface EmailEditorRef { | ||
| export: () => Promise<{ html: string; text: string }>; | ||
| getJSON: () => JSONContent; | ||
| getHTML: () => string; | ||
| editor: Editor | null; | ||
| } | ||
|
|
||
| export interface EmailEditorProps { | ||
| content?: Content; | ||
| onChange?: (editor: Editor) => void; | ||
| onUploadImage?: UploadImageHandler; | ||
| onReady?: (editor: Editor) => void; | ||
| theme?: 'basic' | 'minimal'; | ||
| editable?: boolean; | ||
| placeholder?: string; | ||
| bubbleMenu?: { | ||
| excludeNodes?: string[]; | ||
| excludeMarks?: string[]; | ||
| }; | ||
| extensions?: Extensions; | ||
| className?: string; | ||
| } | ||
|
|
||
| function RefBridge({ editorRef }: { editorRef: Ref<EmailEditorRef> }) { | ||
| const { editor } = useCurrentEditor(); | ||
|
|
||
| useImperativeHandle( | ||
| editorRef, | ||
| () => ({ | ||
| export: async () => { | ||
| if (!editor) { | ||
| return { html: '', text: '' }; | ||
| } | ||
| return composeReactEmail({ editor }); | ||
| }, | ||
| getJSON: () => editor?.getJSON() ?? { type: 'doc', content: [] }, | ||
| getHTML: () => editor?.getHTML() ?? '', | ||
| editor, | ||
| }), | ||
| [editor], | ||
| ); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export const EmailEditor = forwardRef<EmailEditorRef, EmailEditorProps>( | ||
| ( | ||
| { | ||
| content, | ||
| onChange, | ||
| onUploadImage, | ||
| onReady, | ||
| theme = 'basic', | ||
| editable = true, | ||
| placeholder, | ||
| bubbleMenu, | ||
| extensions: extensionsProp, | ||
| className, | ||
| }, | ||
| ref, | ||
| ) => { | ||
| const extensions = useMemo(() => { | ||
| if (extensionsProp) { | ||
| return extensionsProp; | ||
| } | ||
|
|
||
| return [ | ||
| StarterKit.configure({ | ||
| Placeholder: placeholder ? { placeholder } : undefined, | ||
| }), | ||
| UndoRedo, | ||
| EmailTheming.configure({ theme }), | ||
| ]; | ||
| }, [extensionsProp, theme, placeholder]); | ||
|
|
||
| const editorProps: UseEditorOptions['editorProps'] = useMemo( | ||
| () => ({ | ||
| handlePaste: createPasteHandler({ | ||
| onUploadImage, | ||
| extensions, | ||
| }), | ||
| handleDrop: createDropHandler({ | ||
| onUploadImage, | ||
| }), | ||
| }), | ||
| [onUploadImage, extensions], | ||
| ); | ||
|
|
||
| return ( | ||
| <EditorProvider | ||
|
joaopcm marked this conversation as resolved.
|
||
| key={theme} | ||
| extensions={extensions} | ||
| content={content} | ||
| editable={editable} | ||
| immediatelyRender={false} | ||
| editorProps={editorProps} | ||
| editorContainerProps={{ className }} | ||
| onCreate={({ editor }) => onReady?.(editor)} | ||
| onUpdate={({ editor }) => onChange?.(editor)} | ||
| > | ||
| <RefBridge editorRef={ref} /> | ||
| <BubbleMenuDefault | ||
| excludeNodes={bubbleMenu?.excludeNodes ?? ['button']} | ||
| excludeMarks={bubbleMenu?.excludeMarks ?? ['link']} | ||
| /> | ||
| <LinkBubbleMenuDefault /> | ||
| <ButtonBubbleMenuDefault /> | ||
| <ImageBubbleMenuDefault /> | ||
| <SlashCommandRoot /> | ||
| </EditorProvider> | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| EmailEditor.displayName = 'EmailEditor'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export { | ||
| EmailEditor, | ||
| type EmailEditorProps, | ||
| type EmailEditorRef, | ||
| } from './email-editor'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export { | ||
| EmailEditor, | ||
| type EmailEditorProps, | ||
| type EmailEditorRef, | ||
| } from './email-editor'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.