Skip to content

Commit 17e3c29

Browse files
authored
fix(xl-email-exporter): better defaults, customize textStyles, output inline styles (#1856)
1 parent 8f18ba1 commit 17e3c29

File tree

11 files changed

+533
-144
lines changed

11 files changed

+533
-144
lines changed

docs/content/docs/features/export/email.mdx

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,33 @@ See the [full example](/examples/interoperability/converting-blocks-to-react-ema
5555
- **header**: Add content to the top of the email (must be a React-Email compatible component)
5656
- **footer**: Add content to the bottom of the email (must be a React-Email compatible component)
5757
- **head**: Inject elements into the [Head element](https://react.email/docs/components/head)
58+
- **container**: Customize the container element (A component which will wrap the email content including the header and footer)
5859

5960
Example usage:
6061

61-
```tsx
62-
import { Text } from "@react-email/components";
62+
```tsx twoslash
63+
import React from "react";
64+
import {
65+
ReactEmailExporter,
66+
reactEmailDefaultSchemaMappings,
67+
} from "@blocknote/xl-email-exporter";
68+
import { BlockNoteEditor } from "@blocknote/core";
69+
import { Text, Container } from "@react-email/components";
70+
71+
const editor = BlockNoteEditor.create();
72+
73+
// ---cut---
74+
const exporter = new ReactEmailExporter(
75+
editor.schema,
76+
reactEmailDefaultSchemaMappings,
77+
);
78+
6379
const html = await exporter.toReactEmailDocument(editor.document, {
6480
preview: "This is a preview of the email content",
6581
header: <Text>Header</Text>,
6682
footer: <Text>Footer</Text>,
6783
head: <title>My email</title>,
84+
container: ({ children }) => <Container>{children}</Container>,
6885
});
6986
```
7087

@@ -106,3 +123,41 @@ const defaultOptions = {
106123
colors: COLORS_DEFAULT, // defaults from @blocknote/core
107124
};
108125
```
126+
127+
### Custom styles
128+
129+
Want to tweak the default styles of the email? You can use `reactEmailDefaultSchemaMappingsWithStyles` to create a custom mapping with your own styles.
130+
131+
```tsx
132+
import {
133+
ReactEmailExporter,
134+
reactEmailDefaultSchemaMappingsWithStyles,
135+
} from "@blocknote/xl-email-exporter";
136+
import { Text } from "@react-email/components";
137+
138+
const { blockMapping, inlineContentMapping, styleMapping } =
139+
reactEmailDefaultSchemaMappingsWithStyles({
140+
textStyles: {
141+
paragraph: {
142+
style: {
143+
fontSize: 16,
144+
lineHeight: 1.5,
145+
margin: 3,
146+
minHeight: 24,
147+
},
148+
},
149+
},
150+
});
151+
152+
new ReactEmailExporter(schema, {
153+
// You can still use the default block mapping, but you can also overwrite it
154+
blockMapping: {
155+
...blockMapping,
156+
audio: (block, exporter) => {
157+
return <Text>Audio block</Text>;
158+
},
159+
},
160+
inlineContentMapping,
161+
styleMapping,
162+
});
163+
```

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"fumadocs-typescript": "^4.0.6",
8585
"fumadocs-ui": "^15.5.4",
8686
"import-in-the-middle": "^1.14.2",
87-
"next": "^15.4.1",
87+
"next": "^15.4.3",
8888
"nodemailer": "^6.10.1",
8989
"partykit": "^0.0.115",
9090
"pg": "^8.15.5",

examples/05-interoperability/08-converting-blocks-to-react-email/src/App.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
BlockNoteSchema,
3+
COLORS_DARK_MODE_DEFAULT,
4+
COLORS_DEFAULT,
35
combineByGroup,
46
filterSuggestionItems,
57
withPageBreak,
@@ -10,7 +12,9 @@ import "@blocknote/mantine/style.css";
1012
import {
1113
SuggestionMenuController,
1214
getDefaultReactSlashMenuItems,
15+
useBlockNoteContext,
1316
useCreateBlockNote,
17+
usePrefersColorScheme,
1418
} from "@blocknote/react";
1519
import {
1620
ReactEmailExporter,
@@ -314,13 +318,22 @@ export default function App() {
314318
],
315319
});
316320

321+
const existingContext = useBlockNoteContext();
322+
const systemColorScheme = usePrefersColorScheme();
323+
const colorScheme =
324+
existingContext?.colorSchemePreference || systemColorScheme;
325+
317326
const onChange = async () => {
318327
if (!editor || !editor.document) {
319328
return;
320329
}
321330
const exporter = new ReactEmailExporter(
322331
editor.schema,
323332
reactEmailDefaultSchemaMappings,
333+
{
334+
colors:
335+
colorScheme === "dark" ? COLORS_DARK_MODE_DEFAULT : COLORS_DEFAULT,
336+
},
324337
);
325338
const emailHtml = await exporter.toReactEmailDocument(editor.document);
326339

examples/05-interoperability/08-converting-blocks-to-react-email/src/styles.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
.email {
2323
min-height: 500px;
24-
display: flex;
25-
align-items: stretch;
2624
}
2725

2826
/* hack to get react-email to show on website */

packages/xl-email-exporter/src/react-email/__snapshots__/reactEmailExporter.test.tsx.snap

Lines changed: 17 additions & 17 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)