import { composeReactEmail } from '@react-email/editor/core';
import { StarterKit } from '@react-email/editor/extensions';
import { EmailTheming } from '@react-email/editor/plugins';
import {
BubbleMenu,
defaultSlashCommands,
SlashCommand,
} from '@react-email/editor/ui';
import { EditorProvider, useCurrentEditor } from '@tiptap/react';
import { useState } from 'react';
type EditorTheme = 'basic' | 'minimal';
function ControlPanel() {
const { editor } = useCurrentEditor();
const [html, setHtml] = useState('');
const [exporting, setExporting] = useState(false);
const handleExport = async () => {
if (!editor) return;
setExporting(true);
const result = await composeReactEmail({ editor, preview: null });
setHtml(result.html);
setExporting(false);
};
return (
<div>
<button onClick={handleExport} disabled={exporting}>
{exporting ? 'Exporting...' : 'Export HTML'}
</button>
{html && (
<textarea
readOnly
value={html}
rows={16}
style={{ width: '100%', fontFamily: 'monospace', fontSize: '12px' }}
/>
)}
</div>
);
}
export function FullEmailBuilder() {
const [theme, setTheme] = useState<EditorTheme>('basic');
const extensions = [StarterKit, EmailTheming.configure({ theme })];
const content = `
<h1>Weekly Newsletter</h1>
<p>Edit this content, then click <strong>Export HTML</strong> to see the generated email markup.</p>
`;
return (
<div>
<div style={{ display: 'flex', gap: '8px', marginBottom: '16px' }}>
<button onClick={() => setTheme('basic')}>Basic Theme</button>
<button onClick={() => setTheme('minimal')}>Minimal Theme</button>
</div>
<EditorProvider key={theme} extensions={extensions} content={content}>
<BubbleMenu.Default hideWhenActiveNodes={['button']} hideWhenActiveMarks={['link']} />
<BubbleMenu.LinkDefault />
<BubbleMenu.ButtonDefault />
<SlashCommand.Root items={defaultSlashCommands} />
<ControlPanel />
</EditorProvider>
</div>
);
}