page.mdx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/api/react-email")
  3. # @json-render/react-email
  4. React Email renderer. Turn JSON specs into HTML or plain-text emails using `@react-email/components` and `@react-email/render`.
  5. ## Install
  6. ```bash
  7. npm install @json-render/core @json-render/react-email @react-email/components @react-email/render
  8. ```
  9. See the [React Email example](https://github.com/vercel-labs/json-render/tree/main/examples/react-email) for a full working example.
  10. ## schema
  11. The email element schema for specs. Use with `defineCatalog` from core.
  12. ```typescript
  13. import { defineCatalog } from '@json-render/core';
  14. import { schema, standardComponentDefinitions } from '@json-render/react-email';
  15. const catalog = defineCatalog(schema, {
  16. components: standardComponentDefinitions,
  17. });
  18. ```
  19. ## Render Functions
  20. Server-side functions for producing email output. All accept a spec and optional `RenderOptions`.
  21. ```typescript
  22. import { renderToHtml, renderToPlainText } from '@json-render/react-email';
  23. const html = await renderToHtml(spec);
  24. const plainText = await renderToPlainText(spec);
  25. ```
  26. ### RenderOptions
  27. ```typescript
  28. interface RenderOptions {
  29. registry?: ComponentRegistry;
  30. includeStandard?: boolean; // default: true
  31. state?: Record<string, unknown>;
  32. }
  33. ```
  34. <table>
  35. <thead>
  36. <tr>
  37. <th>Option</th>
  38. <th>Description</th>
  39. </tr>
  40. </thead>
  41. <tbody>
  42. <tr>
  43. <td><code>registry</code></td>
  44. <td>Custom component map (merged with standard components)</td>
  45. </tr>
  46. <tr>
  47. <td><code>includeStandard</code></td>
  48. <td>Include built-in standard components (default: <code>true</code>)</td>
  49. </tr>
  50. <tr>
  51. <td><code>state</code></td>
  52. <td>Initial state for <code>$state</code> / <code>$cond</code> dynamic prop resolution</td>
  53. </tr>
  54. </tbody>
  55. </table>
  56. ## defineRegistry
  57. Create a type-safe component registry from a catalog. Components receive `{ props, children, emit, bindings, loading }`.
  58. ```tsx
  59. import { defineRegistry } from '@json-render/react-email';
  60. import { Container, Heading, Text } from '@react-email/components';
  61. const { registry } = defineRegistry(catalog, {
  62. components: {
  63. Card: ({ props, children }) => (
  64. <Container style={{ padding: 16, backgroundColor: '#fff' }}>
  65. <Heading>{props.title}</Heading>
  66. {children}
  67. </Container>
  68. ),
  69. },
  70. });
  71. const html = await renderToHtml(spec, { registry });
  72. ```
  73. ## createRenderer
  74. Create a standalone renderer component wired to state, actions, and validation (for interactive previews in the browser).
  75. ```typescript
  76. import { createRenderer } from '@json-render/react-email';
  77. const EmailRenderer = createRenderer(catalog, components);
  78. ```
  79. ## Renderer
  80. The main component that renders a spec to React Email elements. Use inside `JSONUIProvider` when you need state, actions, or visibility.
  81. ```typescript
  82. interface RendererProps {
  83. spec: Spec | null;
  84. registry?: ComponentRegistry;
  85. includeStandard?: boolean; // default: true
  86. loading?: boolean;
  87. fallback?: ComponentRenderer;
  88. }
  89. ```
  90. ## Standard Components
  91. ### Document structure
  92. <table>
  93. <thead>
  94. <tr>
  95. <th>Component</th>
  96. <th>Description</th>
  97. </tr>
  98. </thead>
  99. <tbody>
  100. <tr>
  101. <td><code>Html</code></td>
  102. <td>Top-level email wrapper. Must be the root element.</td>
  103. </tr>
  104. <tr>
  105. <td><code>Head</code></td>
  106. <td>Email head section. Place inside Html.</td>
  107. </tr>
  108. <tr>
  109. <td><code>Body</code></td>
  110. <td>Email body wrapper. Place inside Html.</td>
  111. </tr>
  112. </tbody>
  113. </table>
  114. ### Layout
  115. <table>
  116. <thead>
  117. <tr>
  118. <th>Component</th>
  119. <th>Description</th>
  120. </tr>
  121. </thead>
  122. <tbody>
  123. <tr>
  124. <td><code>Container</code></td>
  125. <td>Constrains content width (e.g. max-width 600px).</td>
  126. </tr>
  127. <tr>
  128. <td><code>Section</code></td>
  129. <td>Groups related content.</td>
  130. </tr>
  131. <tr>
  132. <td><code>Row</code></td>
  133. <td>Horizontal layout row.</td>
  134. </tr>
  135. <tr>
  136. <td><code>Column</code></td>
  137. <td>Column within a Row.</td>
  138. </tr>
  139. </tbody>
  140. </table>
  141. ### Content
  142. <table>
  143. <thead>
  144. <tr>
  145. <th>Component</th>
  146. <th>Description</th>
  147. </tr>
  148. </thead>
  149. <tbody>
  150. <tr>
  151. <td><code>Heading</code></td>
  152. <td>Heading text (h1-h6).</td>
  153. </tr>
  154. <tr>
  155. <td><code>Text</code></td>
  156. <td>Body text paragraph.</td>
  157. </tr>
  158. <tr>
  159. <td><code>Link</code></td>
  160. <td>Hyperlink with text and href.</td>
  161. </tr>
  162. <tr>
  163. <td><code>Button</code></td>
  164. <td>Call-to-action button (link styled as button).</td>
  165. </tr>
  166. <tr>
  167. <td><code>Image</code></td>
  168. <td>Image from URL.</td>
  169. </tr>
  170. <tr>
  171. <td><code>Hr</code></td>
  172. <td>Horizontal rule separator.</td>
  173. </tr>
  174. </tbody>
  175. </table>
  176. ### Utility
  177. <table>
  178. <thead>
  179. <tr>
  180. <th>Component</th>
  181. <th>Description</th>
  182. </tr>
  183. </thead>
  184. <tbody>
  185. <tr>
  186. <td><code>Preview</code></td>
  187. <td>Preview text for inbox (inside Html).</td>
  188. </tr>
  189. <tr>
  190. <td><code>Markdown</code></td>
  191. <td>Renders markdown content as email-safe HTML.</td>
  192. </tr>
  193. </tbody>
  194. </table>
  195. ## Server-Safe Import
  196. Import schema and catalog definitions without pulling in React or `@react-email/components`:
  197. ```typescript
  198. import { schema, standardComponentDefinitions } from '@json-render/react-email/server';
  199. ```
  200. ## Sub-path Exports
  201. <table>
  202. <thead>
  203. <tr>
  204. <th>Export</th>
  205. <th>Description</th>
  206. </tr>
  207. </thead>
  208. <tbody>
  209. <tr>
  210. <td><code>@json-render/react-email</code></td>
  211. <td>Full package: schema, renderer, components, render functions</td>
  212. </tr>
  213. <tr>
  214. <td><code>@json-render/react-email/server</code></td>
  215. <td>Schema and catalog definitions only (no React)</td>
  216. </tr>
  217. <tr>
  218. <td><code>@json-render/react-email/catalog</code></td>
  219. <td>Standard component definitions and types</td>
  220. </tr>
  221. <tr>
  222. <td><code>@json-render/react-email/render</code></td>
  223. <td>Server-side render functions only</td>
  224. </tr>
  225. </tbody>
  226. </table>
  227. ## Types
  228. <table>
  229. <thead>
  230. <tr>
  231. <th>Export</th>
  232. <th>Description</th>
  233. </tr>
  234. </thead>
  235. <tbody>
  236. <tr>
  237. <td><code>ReactEmailSchema</code></td>
  238. <td>Schema type for email specs</td>
  239. </tr>
  240. <tr>
  241. <td><code>ReactEmailSpec</code></td>
  242. <td>Spec type for email documents</td>
  243. </tr>
  244. <tr>
  245. <td><code>RenderOptions</code></td>
  246. <td>Options for render functions</td>
  247. </tr>
  248. <tr>
  249. <td><code>ComponentContext</code></td>
  250. <td>Typed component render function context</td>
  251. </tr>
  252. <tr>
  253. <td><code>ComponentFn</code></td>
  254. <td>Component render function type</td>
  255. </tr>
  256. <tr>
  257. <td><code>StandardComponentDefinitions</code></td>
  258. <td>Type of the standard component definitions object</td>
  259. </tr>
  260. <tr>
  261. <td><code>StandardComponentProps&lt;K&gt;</code></td>
  262. <td>Inferred props type for a standard component by name</td>
  263. </tr>
  264. </tbody>
  265. </table>