Skip to content

fix(xl-email-exporter): better defaults, customize textStyles, output inline styles #1856

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions docs/content/docs/features/export/email.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,33 @@ See the [full example](/examples/interoperability/converting-blocks-to-react-ema
- **header**: Add content to the top of the email (must be a React-Email compatible component)
- **footer**: Add content to the bottom of the email (must be a React-Email compatible component)
- **head**: Inject elements into the [Head element](https://react.email/docs/components/head)
- **container**: Customize the container element (A component which will wrap the email content including the header and footer)

Example usage:

```tsx
import { Text } from "@react-email/components";
```tsx twoslash
import React from "react";
import {
ReactEmailExporter,
reactEmailDefaultSchemaMappings,
} from "@blocknote/xl-email-exporter";
import { BlockNoteEditor } from "@blocknote/core";
import { Text, Container } from "@react-email/components";

const editor = BlockNoteEditor.create();

// ---cut---
const exporter = new ReactEmailExporter(
editor.schema,
reactEmailDefaultSchemaMappings,
);

const html = await exporter.toReactEmailDocument(editor.document, {
preview: "This is a preview of the email content",
header: <Text>Header</Text>,
footer: <Text>Footer</Text>,
head: <title>My email</title>,
container: ({ children }) => <Container>{children}</Container>,
});
```

Expand Down Expand Up @@ -106,3 +123,41 @@ const defaultOptions = {
colors: COLORS_DEFAULT, // defaults from @blocknote/core
};
```

### Custom styles

Want to tweak the default styles of the email? You can use `reactEmailDefaultSchemaMappingsWithStyles` to create a custom mapping with your own styles.

```tsx
import {
ReactEmailExporter,
reactEmailDefaultSchemaMappingsWithStyles,
} from "@blocknote/xl-email-exporter";
import { Text } from "@react-email/components";

const { blockMapping, inlineContentMapping, styleMapping } =
reactEmailDefaultSchemaMappingsWithStyles({
textStyles: {
paragraph: {
style: {
fontSize: 16,
lineHeight: 1.5,
margin: 3,
minHeight: 24,
},
},
},
});

new ReactEmailExporter(schema, {
// You can still use the default block mapping, but you can also overwrite it
blockMapping: {
...blockMapping,
audio: (block, exporter) => {
return <Text>Audio block</Text>;
},
},
inlineContentMapping,
styleMapping,
});
```
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"fumadocs-typescript": "^4.0.6",
"fumadocs-ui": "^15.5.4",
"import-in-the-middle": "^1.14.2",
"next": "^15.4.1",
"next": "^15.4.3",
"nodemailer": "^6.10.1",
"partykit": "^0.0.115",
"pg": "^8.15.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
BlockNoteSchema,
COLORS_DARK_MODE_DEFAULT,
COLORS_DEFAULT,
combineByGroup,
filterSuggestionItems,
withPageBreak,
Expand All @@ -10,7 +12,9 @@ import "@blocknote/mantine/style.css";
import {
SuggestionMenuController,
getDefaultReactSlashMenuItems,
useBlockNoteContext,
useCreateBlockNote,
usePrefersColorScheme,
} from "@blocknote/react";
import {
ReactEmailExporter,
Expand Down Expand Up @@ -314,13 +318,22 @@ export default function App() {
],
});

const existingContext = useBlockNoteContext();
const systemColorScheme = usePrefersColorScheme();
const colorScheme =
existingContext?.colorSchemePreference || systemColorScheme;

const onChange = async () => {
if (!editor || !editor.document) {
return;
}
const exporter = new ReactEmailExporter(
editor.schema,
reactEmailDefaultSchemaMappings,
{
colors:
colorScheme === "dark" ? COLORS_DARK_MODE_DEFAULT : COLORS_DEFAULT,
},
);
const emailHtml = await exporter.toReactEmailDocument(editor.document);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

.email {
min-height: 500px;
display: flex;
align-items: stretch;
}

/* hack to get react-email to show on website */
Expand Down

Large diffs are not rendered by default.

Loading
Loading