@octanejs/email 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Resend
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # @octanejs/email
2
+
3
+ Email-safe components from [React Email](https://github.com/resend/react-email), ported to Octane.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @octanejs/email
9
+ pnpm add @octanejs/email
10
+ ```
11
+
12
+ ```tsrx
13
+ import { Button, Html, render } from '@octanejs/email';
14
+
15
+ function Email(props: { url: string }) @{
16
+ <Html><Button href={props.url}>Open</Button></Html>
17
+ }
18
+
19
+ const html = await render(Email, { url: 'https://example.com' });
20
+ ```
21
+
22
+ The renderer accepts an Octane component and props because Octane server rendering uses component entry points. It returns XHTML Transitional email markup with no hydration markers.
23
+
24
+ Included primitives: `Body`, `Button`, `CodeInline`, `Column`, `Container`, `Font`, `Head`, `Heading`, `Hr`, `Html`, `Img`, `Link`, `Preview`, `Row`, `Section`, and `Text`.
25
+
26
+ Rich email authoring includes `Markdown`, Prism-powered `CodeBlock`, and `Tailwind`. Tailwind utilities are applied after static rendering, so classes inside ordinary nested `.tsrx` components are inlined correctly; responsive and pseudo rules are emitted into the document head.
27
+
28
+ Use `<Preview text="Inbox preview" />` and `<Markdown children={"# Hello"} />`. Octane's natural JSX children are render blocks, so text-inspecting components use explicit string props.
29
+
30
+ Template export and development tooling lives in `@octanejs/email-cli`.
31
+
32
+ Ported from React Email 6.9.2 at `ffe605819782b31d7f946e30f938b1b63e6b239c` (MIT).
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@octanejs/email",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=22.22.2"
8
+ },
9
+ "octane": {
10
+ "sourceEnvironment": "node"
11
+ },
12
+ "description": "React Email's email-safe component primitives and static renderer for Octane.",
13
+ "author": {
14
+ "name": "Dominic Gannaway",
15
+ "email": "dg@domgan.com"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/octanejs/octane.git",
23
+ "directory": "packages/email"
24
+ },
25
+ "main": "src/index.ts",
26
+ "module": "src/index.ts",
27
+ "types": "src/index.ts",
28
+ "files": [
29
+ "src",
30
+ "README.md",
31
+ "LICENSE.react-email"
32
+ ],
33
+ "exports": {
34
+ ".": "./src/index.ts"
35
+ },
36
+ "dependencies": {
37
+ "@types/css-tree": "2.3.10",
38
+ "@types/prismjs": "1.26.6",
39
+ "css-tree": "3.2.1",
40
+ "marked": "^15.0.12",
41
+ "prismjs": "^1.30.0",
42
+ "tailwindcss": "4.3.3"
43
+ },
44
+ "peerDependencies": {
45
+ "octane": "0.1.50"
46
+ },
47
+ "devDependencies": {
48
+ "@react-email/components": "1.0.12",
49
+ "@react-email/render": "2.0.6",
50
+ "@tsrx/react": "^0.2.61",
51
+ "esbuild": "^0.28.1",
52
+ "react": "^19.2.7",
53
+ "react-dom": "^19.2.7",
54
+ "vitest": "^4.1.10",
55
+ "octane": "0.1.50"
56
+ },
57
+ "scripts": {
58
+ "test": "vitest run"
59
+ }
60
+ }
@@ -0,0 +1,73 @@
1
+ import type { EmailStyle } from './types.ts';
2
+
3
+ type PaddingValue = string | number | undefined;
4
+
5
+ function toPixels(value: PaddingValue): number {
6
+ if (!value) return 0;
7
+ if (typeof value === 'number') return value;
8
+
9
+ const match = /^([\d.]+)(px|em|rem|%)$/.exec(value);
10
+ if (!match) return 0;
11
+
12
+ const amount = Number.parseFloat(match[1]!);
13
+ switch (match[2]) {
14
+ case 'em':
15
+ case 'rem':
16
+ return amount * 16;
17
+ case '%':
18
+ return (amount / 100) * 600;
19
+ default:
20
+ return amount;
21
+ }
22
+ }
23
+
24
+ function expandPadding(value: PaddingValue): PaddingValue[] {
25
+ if (typeof value === 'number') return [value, value, value, value];
26
+ if (typeof value !== 'string') return [];
27
+
28
+ const values = value.trim().split(/\s+/);
29
+ switch (values.length) {
30
+ case 1:
31
+ return [values[0], values[0], values[0], values[0]];
32
+ case 2:
33
+ return [values[0], values[1], values[0], values[1]];
34
+ case 3:
35
+ return [values[0], values[1], values[2], values[1]];
36
+ case 4:
37
+ return values;
38
+ default:
39
+ return [];
40
+ }
41
+ }
42
+
43
+ export function parseButtonPadding(style: EmailStyle) {
44
+ const expanded = expandPadding(style.padding as PaddingValue);
45
+ const values: PaddingValue[] = [
46
+ (style.paddingTop as PaddingValue) ?? expanded[0],
47
+ (style.paddingRight as PaddingValue) ?? expanded[1],
48
+ (style.paddingBottom as PaddingValue) ?? expanded[2],
49
+ (style.paddingLeft as PaddingValue) ?? expanded[3],
50
+ ];
51
+ return values.map((value) => (value === undefined ? undefined : toPixels(value))) as [
52
+ number | undefined,
53
+ number | undefined,
54
+ number | undefined,
55
+ number | undefined,
56
+ ];
57
+ }
58
+
59
+ export function computeOutlookSpacing(expectedWidth: number) {
60
+ if (expectedWidth === 0) return [0, 0] as const;
61
+
62
+ let spaceCount = 0;
63
+ let fontWidth = Number.POSITIVE_INFINITY;
64
+ while (fontWidth > 5) {
65
+ spaceCount++;
66
+ fontWidth = expectedWidth / spaceCount / 2;
67
+ }
68
+ return [fontWidth, spaceCount] as const;
69
+ }
70
+
71
+ export function pixelsToPoints(value: number | undefined) {
72
+ return value === undefined || Number.isNaN(value) ? undefined : (value * 3) / 4;
73
+ }
@@ -0,0 +1,368 @@
1
+ import type { OctaneNode } from 'octane';
2
+ import type { EmailElementProps, EmailStyle } from './types.ts';
3
+ import { computeOutlookSpacing, parseButtonPadding, pixelsToPoints } from './button-utils.ts';
4
+ import { computeMargins, splitPadding, withMargins } from './utils.ts';
5
+
6
+ function withRef<T>(ref: ((element: T | null) => void) | { current: T | null } | null | undefined) {
7
+ return ref === undefined ? {} : { ref };
8
+ }
9
+
10
+ export type BodyProps = EmailElementProps<HTMLBodyElement>;
11
+ export function Body({ children, style = {}, ref, ...props }: BodyProps) @{
12
+ const reset: EmailStyle = {
13
+ background: style.background,
14
+ backgroundColor: style.backgroundColor,
15
+ };
16
+ for (const key of [
17
+ 'margin',
18
+ 'marginTop',
19
+ 'marginRight',
20
+ 'marginBottom',
21
+ 'marginLeft',
22
+ 'padding',
23
+ 'paddingTop',
24
+ 'paddingRight',
25
+ 'paddingBottom',
26
+ 'paddingLeft',
27
+ ]) {
28
+ if ((style as Record<string, unknown>)[key] !== undefined) (reset as Record<
29
+ string,
30
+ unknown
31
+ >)[key] = 0;
32
+ }
33
+ <body
34
+ {...props}
35
+ {...withRef(ref)}
36
+ dir={props.dir as string | undefined ?? 'ltr'}
37
+ lang={props.lang as string | undefined ?? 'en'}
38
+ style={reset}
39
+ >
40
+ <table
41
+ border={0}
42
+ width="100%"
43
+ cellPadding="0"
44
+ cellSpacing="0"
45
+ role="presentation"
46
+ align="center"
47
+ >
48
+ <tbody>
49
+ <tr>
50
+ <td
51
+ dir={props.dir as string | undefined ?? 'ltr'}
52
+ lang={props.lang as string | undefined ?? 'en'}
53
+ style={style}
54
+ >{children}</td>
55
+ </tr>
56
+ </tbody>
57
+ </table>
58
+ </body>
59
+ }
60
+
61
+ export type HtmlProps = EmailElementProps<HTMLHtmlElement> & { lang?: string; dir?: string };
62
+ export function Html({ children, lang = 'en', dir = 'ltr', ref, ...props }: HtmlProps) @{
63
+ <html {...props} {...withRef(ref)} dir={dir} lang={lang}>{children}</html>
64
+ }
65
+
66
+ export type HeadProps = EmailElementProps<HTMLHeadElement>;
67
+ export function Head({ children }: HeadProps) @{
68
+ <>
69
+ <meta content="text/html; charset=UTF-8" httpEquiv="Content-Type" />
70
+ <meta name="x-apple-disable-message-reformatting" />
71
+ {/* Octane forbids authored <head>; render() extracts this temporary carrier. */}
72
+ <body data-octane-email-head="head">{children}</body>
73
+ </>
74
+ }
75
+
76
+ export type ContainerProps = EmailElementProps<HTMLTableElement>;
77
+ export function Container({ children, style = {}, ref, ...props }: ContainerProps) @{
78
+ const [cellStyle, tableStyle] = splitPadding(style);
79
+ <table
80
+ align="center"
81
+ width="100%"
82
+ {...props}
83
+ {...withRef(ref)}
84
+ border={0}
85
+ cellPadding="0"
86
+ cellSpacing="0"
87
+ role="presentation"
88
+ style={{ maxWidth: '37.5em', ...tableStyle }}
89
+ >
90
+ <tbody>
91
+ <tr style={{ width: '100%' }}>
92
+ <td style={cellStyle}>{children}</td>
93
+ </tr>
94
+ </tbody>
95
+ </table>
96
+ }
97
+
98
+ export type SectionProps = EmailElementProps<HTMLTableElement>;
99
+ export function Section({ children, style = {}, ref, ...props }: SectionProps) @{
100
+ const [cellStyle, tableStyle] = splitPadding(style);
101
+ <table
102
+ align="center"
103
+ width="100%"
104
+ border={0}
105
+ cellPadding="0"
106
+ cellSpacing="0"
107
+ role="presentation"
108
+ {...props}
109
+ {...withRef(ref)}
110
+ style={tableStyle}
111
+ >
112
+ <tbody>
113
+ <tr>
114
+ <td style={cellStyle}>{children}</td>
115
+ </tr>
116
+ </tbody>
117
+ </table>
118
+ }
119
+
120
+ export type RowProps = EmailElementProps<HTMLTableElement>;
121
+ export function Row({ children, style, ref, ...props }: RowProps) @{
122
+ <table
123
+ align="center"
124
+ width="100%"
125
+ border={0}
126
+ cellPadding="0"
127
+ cellSpacing="0"
128
+ role="presentation"
129
+ {...props}
130
+ {...withRef(ref)}
131
+ style={style}
132
+ >
133
+ <tbody style={{ width: '100%' }}>
134
+ <tr style={{ width: '100%' }}>{children}</tr>
135
+ </tbody>
136
+ </table>
137
+ }
138
+
139
+ export type ColumnProps = EmailElementProps<HTMLTableCellElement>;
140
+ export function Column({ children, style, ref, ...props }: ColumnProps) @{
141
+ <td {...props} {...withRef(ref)} data-id="__react-email-column" style={style}>{children}</td>
142
+ }
143
+
144
+ export type TextProps = EmailElementProps<HTMLParagraphElement>;
145
+ export function Text({ style = {}, ref, ...props }: TextProps) @{
146
+ const defaults: EmailStyle = {};
147
+ if (style.marginTop === undefined) defaults.marginTop = '16px';
148
+ if (style.marginBottom === undefined) defaults.marginBottom = '16px';
149
+ const margins = computeMargins({ ...defaults, ...style });
150
+ <p
151
+ {...props}
152
+ {...withRef(ref)}
153
+ style={{ fontSize: '14px', lineHeight: '24px', ...style, ...margins }}
154
+ />
155
+ }
156
+
157
+ export type LinkProps = EmailElementProps<HTMLAnchorElement> & { href?: string; target?: string };
158
+ export function Link({ children, target = '_blank', style, ref, ...props }: LinkProps) @{
159
+ <a
160
+ {...props}
161
+ {...withRef(ref)}
162
+ style={{ color: '#067df7', textDecorationLine: 'none', ...style }}
163
+ target={target}
164
+ >{children}</a>
165
+ }
166
+
167
+ export type ImgProps =
168
+ EmailElementProps<HTMLImageElement> & {
169
+ alt?: string;
170
+ src?: string;
171
+ width?: string | number;
172
+ height?: string | number;
173
+ };
174
+ export function Img({ alt = '', src, width, height, style, ref, ...props }: ImgProps) @{
175
+ <img
176
+ {...props}
177
+ {...withRef(ref)}
178
+ alt={alt}
179
+ height={height}
180
+ src={src}
181
+ style={{ display: 'block', outline: 'none', border: 'none', textDecoration: 'none', ...style }}
182
+ width={width}
183
+ />
184
+ }
185
+
186
+ export type HrProps = EmailElementProps<HTMLHRElement>;
187
+ export function Hr({ style, ref, ...props }: HrProps) @{
188
+ <hr
189
+ {...props}
190
+ {...withRef(ref)}
191
+ style={{
192
+ width: '100%',
193
+ border: 'none',
194
+ borderColor: 'transparent',
195
+ borderTop: '1px solid #eaeaea',
196
+ ...style,
197
+ }}
198
+ />
199
+ }
200
+
201
+ export type HeadingProps =
202
+ EmailElementProps<HTMLHeadingElement> & {
203
+ as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
204
+ m?: string | number;
205
+ mx?: string | number;
206
+ my?: string | number;
207
+ mt?: string | number;
208
+ mr?: string | number;
209
+ mb?: string | number;
210
+ ml?: string | number;
211
+ };
212
+ export function Heading({
213
+ as: Tag = 'h1',
214
+ children,
215
+ style,
216
+ ref,
217
+ m,
218
+ mx,
219
+ my,
220
+ mt,
221
+ mr,
222
+ mb,
223
+ ml,
224
+ ...props
225
+ }: HeadingProps) @{
226
+ <Tag
227
+ {...props}
228
+ {...withRef(ref)}
229
+ style={{ ...withMargins({ m, mx, my, mt, mr, mb, ml }), ...style }}
230
+ >{children}</Tag>
231
+ }
232
+
233
+ export interface FontProps {
234
+ fontFamily: string;
235
+ fallbackFontFamily: string | string[];
236
+ webFont?: {
237
+ url: string;
238
+ format: 'woff' | 'woff2' | 'truetype' | 'opentype' | 'embedded-opentype' | 'svg';
239
+ };
240
+ fontStyle?: string;
241
+ fontWeight?: string | number;
242
+ }
243
+ export function Font({
244
+ fontFamily,
245
+ fallbackFontFamily,
246
+ webFont,
247
+ fontStyle = 'normal',
248
+ fontWeight = 400,
249
+ }: FontProps) @{
250
+ const fallbacks = Array.isArray(fallbackFontFamily) ? fallbackFontFamily : [fallbackFontFamily];
251
+ const source = webFont ? `src: url(${webFont.url}) format('${webFont.format}');` : '';
252
+ const css = `\n @font-face {\n font-family: '${fontFamily}';\n font-style: ${fontStyle};\n font-weight: ${fontWeight};\n mso-font-alt: '${fallbacks[0]}';\n ${source}\n }\n\n * {\n font-family: '${fontFamily}', ${fallbacks.join(
253
+ ', ',
254
+ )};\n }\n `;
255
+ // A dynamic host keeps email CSS global instead of entering Octane's scoped-style pipeline.
256
+ const Style = 'style';
257
+ <Style dangerouslySetInnerHTML={{ __html: css }} />
258
+ }
259
+
260
+ const PREVIEW_MAX_LENGTH = 200;
261
+ const WHITE_SPACE = ' ‌​‍‎‏';
262
+ export type PreviewProps =
263
+ Readonly<Omit<EmailElementProps<HTMLDivElement>, 'children'>> & Readonly<{
264
+ text?: string;
265
+ children?: string | string[];
266
+ useTitleTag?: boolean;
267
+ }>;
268
+ export function Preview({
269
+ text: explicitText,
270
+ children = '',
271
+ useTitleTag = true,
272
+ ref,
273
+ ...props
274
+ }: PreviewProps) @{
275
+ const childText =
276
+ Array.isArray(children)
277
+ ? children.join('')
278
+ : typeof children === 'string'
279
+ ? children
280
+ : '';
281
+ const text = (explicitText ?? childText).substring(0, PREVIEW_MAX_LENGTH);
282
+ <>
283
+ @if (useTitleTag) {
284
+ <title>{text}</title>
285
+ }
286
+ <div
287
+ style={{
288
+ display: 'none',
289
+ overflow: 'hidden',
290
+ lineHeight: '1px',
291
+ opacity: 0,
292
+ maxHeight: 0,
293
+ maxWidth: 0,
294
+ }}
295
+ data-skip-in-text={true}
296
+ {...props}
297
+ {...withRef(ref)}
298
+ >
299
+ {text}
300
+ @if (text.length < PREVIEW_MAX_LENGTH) {
301
+ <div>{WHITE_SPACE.repeat(PREVIEW_MAX_LENGTH - text.length)}</div>
302
+ }
303
+ </div>
304
+ </>
305
+ }
306
+
307
+ export type CodeInlineProps = EmailElementProps<HTMLElement>;
308
+ export function CodeInline({ children, style, ref, ...props }: CodeInlineProps) @{
309
+ <code
310
+ {...props}
311
+ {...withRef(ref)}
312
+ style={{
313
+ display: 'inline',
314
+ background: 'rgba(135,131,120,0.15)',
315
+ padding: '2px 4px',
316
+ borderRadius: '4px',
317
+ ...style,
318
+ }}
319
+ >{children}</code>
320
+ }
321
+
322
+ export type ButtonProps = EmailElementProps<HTMLAnchorElement> & { href?: string; target?: string };
323
+ export function Button({ children, style = {}, target = '_blank', ref, ...props }: ButtonProps) @{
324
+ const [paddingTop, paddingRight, paddingBottom, paddingLeft] = parseButtonPadding(style);
325
+ const textRaise = pixelsToPoints((paddingTop ?? 0) + (paddingBottom ?? 0));
326
+ const [leftFontWidth, leftSpaceCount] = computeOutlookSpacing(paddingLeft ?? 0);
327
+ const [rightFontWidth, rightSpaceCount] = computeOutlookSpacing(paddingRight ?? 0);
328
+ const buttonStyle: EmailStyle = {
329
+ lineHeight: '100%',
330
+ textDecoration: 'none',
331
+ display: 'inline-block',
332
+ maxWidth: '100%',
333
+ msoPaddingAlt: '0px',
334
+ ...style,
335
+ paddingTop,
336
+ paddingRight,
337
+ paddingBottom,
338
+ paddingLeft,
339
+ };
340
+ const labelStyle: EmailStyle = {
341
+ maxWidth: '100%',
342
+ display: 'inline-block',
343
+ lineHeight: '120%',
344
+ msoPaddingAlt: '0px',
345
+ msoTextRaise: pixelsToPoints(paddingBottom),
346
+ };
347
+ <a {...props} {...withRef(ref)} style={buttonStyle} target={target}>
348
+ <span
349
+ dangerouslySetInnerHTML={{
350
+ __html: `<!--[if mso]><i style="mso-font-width:${
351
+ leftFontWidth * 100
352
+ }%;mso-text-raise:${textRaise}px" hidden>${'&#8202;'.repeat(
353
+ leftSpaceCount,
354
+ )}</i><![endif]-->`,
355
+ }}
356
+ />
357
+ <span style={labelStyle}>{children}</span>
358
+ <span
359
+ dangerouslySetInnerHTML={{
360
+ __html: `<!--[if mso]><i style="mso-font-width:${
361
+ rightFontWidth * 100
362
+ }%" hidden>${'&#8202;'.repeat(rightSpaceCount)}&#8203;</i><![endif]-->`,
363
+ }}
364
+ />
365
+ </a>
366
+ }
367
+
368
+ export type { OctaneNode };
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './components.tsrx';
2
+ export * from './rich-content.tsrx';
3
+ export * from './render.ts';
4
+ export * from './tailwind/index.ts';
5
+ export type { MarkdownStyles } from './markdown.ts';
6
+ export type { EmailElementProps, EmailStyle } from './types.ts';