@tonycodes-ui/mortar 0.14.0 → 0.15.0
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.
- package/CHANGELOG.md +3 -2
- package/catalog.json +119 -119
- package/dist/agent-data.json +1 -1
- package/dist/email-components.d.ts +115 -0
- package/dist/email-components.js +221 -0
- package/dist/email-templates.d.ts +108 -0
- package/dist/email-templates.js +57 -0
- package/dist/email.d.ts +4 -98
- package/dist/email.js +4 -151
- package/docs/auth-flows.md +1 -1
- package/docs/email.md +34 -19
- package/package.json +1 -1
- package/src/{email.tsx → email-components.tsx} +208 -93
- package/src/email-templates.tsx +355 -0
- package/src/email.ts +47 -0
package/dist/email.js
CHANGED
|
@@ -1,152 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
which renders the table layout and inline styles email clients need; these
|
|
4
|
-
components give it Mortar's tokens, brand and copy. Nothing here uses
|
|
5
|
-
Mortar's stylesheet or app components, which email clients cannot run.
|
|
6
|
-
Requires the optional peer dependency @react-email/components. */
|
|
7
|
-
import { createContext, useContext, } from "react";
|
|
8
|
-
import { Body, Button, Container, Head, Heading, Html, Img, Link, Preview, Text, render, } from "@react-email/components";
|
|
9
|
-
import { emailTheme } from "./email-theme.js";
|
|
1
|
+
/* @tonycodes-ui/mortar/email: transactional email built on React Email
|
|
2
|
+
(the optional peer dependency @react-email/components). See docs/email.md. */
|
|
10
3
|
export { emailTheme } from "./email-theme.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/** The theme from the enclosing EmailShell, or Mortar's neutral theme. */
|
|
14
|
-
function useTheme() {
|
|
15
|
-
return useContext(ThemeContext) ?? (neutral ??= emailTheme());
|
|
16
|
-
}
|
|
17
|
-
/* Links leave the mail client, so only web and mail addresses are allowed. */
|
|
18
|
-
function assertHref(href, component, mail = false) {
|
|
19
|
-
if (!(mail ? /^(?:https?:|mailto:)/i : /^https?:\/\//i).test(href.trim()))
|
|
20
|
-
throw new Error(`Mortar ${component} needs an absolute ${mail ? "http(s) or mailto" : "http(s)"} URL: "${href}"`);
|
|
21
|
-
}
|
|
22
|
-
/* A one-cell layout table. Outlook for Windows ignores padding, margins and
|
|
23
|
-
backgrounds on tables, so spacing and fills go on the cell. */
|
|
24
|
-
function Cell({ style, children, }) {
|
|
25
|
-
return (_jsx("table", { role: "presentation", width: "100%", border: 0, cellPadding: 0, cellSpacing: 0, children: _jsx("tbody", { children: _jsx("tr", { children: _jsx("td", { style: style, children: children }) }) }) }));
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* The whole email: a card up to 600px wide on the sunken backdrop, with an
|
|
29
|
-
* optional logo above it and footer below. It sets the theme for the email
|
|
30
|
-
* components inside, and marks the email light-only so mail apps don't
|
|
31
|
-
* recolour the brand.
|
|
32
|
-
*/
|
|
33
|
-
export function EmailShell({ preview, theme: given, logo, footer, lang = "en", dir = "ltr", children, }) {
|
|
34
|
-
const theme = given ?? emailTheme();
|
|
35
|
-
if (logo)
|
|
36
|
-
assertHref(logo.src, "EmailShell logo");
|
|
37
|
-
return (_jsx(ThemeContext.Provider, { value: theme, children: _jsxs(Html, { lang: lang, dir: dir, children: [_jsxs(Head, { children: [_jsx("meta", { name: "color-scheme", content: "light" }), _jsx("meta", { name: "supported-color-schemes", content: "light" })] }), _jsx(Preview, { children: preview }), _jsx(Body, { style: {
|
|
38
|
-
margin: 0,
|
|
39
|
-
padding: "32px 12px",
|
|
40
|
-
backgroundColor: theme.sunken,
|
|
41
|
-
color: theme.ink,
|
|
42
|
-
fontFamily: theme.font,
|
|
43
|
-
}, children: _jsxs(Container, { style: { width: "100%", maxWidth: "600px" }, children: [logo && (_jsx(Cell, { style: { padding: "0 0 24px" }, children: _jsx(Img, { src: logo.src, alt: logo.alt, width: logo.width, height: logo.height }) })), _jsx(Cell, { style: {
|
|
44
|
-
backgroundColor: theme.surface,
|
|
45
|
-
border: `1px solid ${theme.line}`,
|
|
46
|
-
borderRadius: "12px",
|
|
47
|
-
padding: "32px",
|
|
48
|
-
}, children: children }), footer && (_jsx(Cell, { style: { padding: "24px 32px 0" }, children: _jsx(Text, { style: {
|
|
49
|
-
margin: 0,
|
|
50
|
-
fontFamily: theme.font,
|
|
51
|
-
fontSize: "12px",
|
|
52
|
-
lineHeight: "18px",
|
|
53
|
-
color: theme.muted,
|
|
54
|
-
}, children: footer }) }))] }) })] }) }));
|
|
55
|
-
}
|
|
56
|
-
export function EmailHeading({ level = 1, children }) {
|
|
57
|
-
const theme = useTheme();
|
|
58
|
-
return (_jsx(Heading, { as: level === 1 ? "h1" : "h2", style: {
|
|
59
|
-
margin: "0 0 16px",
|
|
60
|
-
fontFamily: theme.fontHeading,
|
|
61
|
-
fontSize: level === 1 ? "24px" : "18px",
|
|
62
|
-
lineHeight: level === 1 ? "32px" : "26px",
|
|
63
|
-
fontWeight: 600,
|
|
64
|
-
letterSpacing: "-0.02em",
|
|
65
|
-
color: theme.ink,
|
|
66
|
-
}, children: children }));
|
|
67
|
-
}
|
|
68
|
-
/** A paragraph. 16px by default: email is read on phones, at arm's length. */
|
|
69
|
-
export function EmailText({ tone = "default", size = "md", children, }) {
|
|
70
|
-
const theme = useTheme();
|
|
71
|
-
return (_jsx(Text, { style: {
|
|
72
|
-
margin: "0 0 16px",
|
|
73
|
-
fontFamily: theme.font,
|
|
74
|
-
fontSize: size === "md" ? "16px" : "14px",
|
|
75
|
-
lineHeight: size === "md" ? "24px" : "20px",
|
|
76
|
-
color: tone === "muted" ? theme.muted : theme.ink,
|
|
77
|
-
}, children: children }));
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* The email's main action, on its own line. Padding is applied in a way
|
|
81
|
-
* Outlook for Windows keeps, so the whole button is clickable there too.
|
|
82
|
-
* Follow it with the URL as an EmailLink for clients that block buttons.
|
|
83
|
-
*/
|
|
84
|
-
export function EmailButton({ href, children }) {
|
|
85
|
-
const theme = useTheme();
|
|
86
|
-
assertHref(href, "EmailButton");
|
|
87
|
-
return (_jsx(Cell, { style: { padding: "8px 0 24px" }, children: _jsx(Button, { href: href, style: {
|
|
88
|
-
display: "inline-block",
|
|
89
|
-
backgroundColor: theme.primary,
|
|
90
|
-
color: theme.onPrimary,
|
|
91
|
-
fontFamily: theme.font,
|
|
92
|
-
fontSize: "16px",
|
|
93
|
-
lineHeight: "24px",
|
|
94
|
-
fontWeight: 600,
|
|
95
|
-
textDecoration: "none",
|
|
96
|
-
padding: "12px 24px",
|
|
97
|
-
borderRadius: "8px",
|
|
98
|
-
}, children: children }) }));
|
|
99
|
-
}
|
|
100
|
-
export function EmailLink({ href, children }) {
|
|
101
|
-
const theme = useTheme();
|
|
102
|
-
assertHref(href, "EmailLink", true);
|
|
103
|
-
return (_jsx(Link, { href: href, style: {
|
|
104
|
-
color: theme.link,
|
|
105
|
-
textDecoration: "underline",
|
|
106
|
-
wordBreak: "break-all",
|
|
107
|
-
}, children: children ?? href }));
|
|
108
|
-
}
|
|
109
|
-
export function EmailCode({ code }) {
|
|
110
|
-
const theme = useTheme();
|
|
111
|
-
return (_jsx(Cell, { style: { padding: "8px 0 24px" }, children: _jsx(Cell, { style: {
|
|
112
|
-
backgroundColor: theme.sunken,
|
|
113
|
-
borderRadius: "8px",
|
|
114
|
-
padding: "16px",
|
|
115
|
-
textAlign: "center",
|
|
116
|
-
}, children: _jsx(Text, { style: {
|
|
117
|
-
margin: 0,
|
|
118
|
-
fontFamily: theme.mono,
|
|
119
|
-
fontSize: "28px",
|
|
120
|
-
lineHeight: "36px",
|
|
121
|
-
fontWeight: 600,
|
|
122
|
-
letterSpacing: "0.2em",
|
|
123
|
-
color: theme.ink,
|
|
124
|
-
}, children: code }) }) }));
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* An email element as HTML and plain text, ready for a sending service
|
|
128
|
-
* (Resend, SES, Postmark…). Keep the HTML under about 100 KB: Gmail clips
|
|
129
|
-
* longer messages.
|
|
130
|
-
*/
|
|
131
|
-
export async function renderEmail(element) {
|
|
132
|
-
const [html, text] = await Promise.all([
|
|
133
|
-
render(element),
|
|
134
|
-
render(element, { plainText: true }),
|
|
135
|
-
]);
|
|
136
|
-
return { html, text };
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Confirms an email address with a code, a link or both: the email for the
|
|
140
|
-
* auth flow's verify-email step.
|
|
141
|
-
*/
|
|
142
|
-
export function VerifyEmail({ productName, code, href, recipient, expiresIn, theme, logo, footer, lang, }) {
|
|
143
|
-
if (!code && !href)
|
|
144
|
-
throw new Error("Mortar VerifyEmail needs a `code`, an `href` or both.");
|
|
145
|
-
const address = recipient ?? "your email address";
|
|
146
|
-
return (_jsxs(EmailShell, { preview: `Confirm your email address for ${productName}`, theme: theme, logo: logo, lang: lang, footer: footer ??
|
|
147
|
-
`You received this email because this address was used to sign up for ${productName}. If that wasn't you, you can ignore it.`, children: [_jsx(EmailHeading, { children: "Confirm your email address" }), code && (_jsxs(_Fragment, { children: [_jsxs(EmailText, { children: ["Enter this code in ", productName, " to confirm ", address, ":"] }), _jsx(EmailCode, { code: code })] })), href && (_jsxs(_Fragment, { children: [_jsx(EmailText, { children: code
|
|
148
|
-
? "Or confirm with one click:"
|
|
149
|
-
: `Confirm ${address} to finish setting up your ${productName} account.` }), _jsx(EmailButton, { href: href, children: "Confirm email address" }), _jsxs(EmailText, { tone: "muted", size: "sm", children: ["If the button doesn't work, paste this link into your browser:", " ", _jsx(EmailLink, { href: href })] })] })), expiresIn && (_jsx(EmailText, { tone: "muted", size: "sm", children: code && href
|
|
150
|
-
? `This code and link expire in ${expiresIn}.`
|
|
151
|
-
: `This ${code ? "code" : "link"} expires in ${expiresIn}.` }))] }));
|
|
152
|
-
}
|
|
4
|
+
export { EmailShell, EmailHeading, EmailText, EmailButton, EmailLink, EmailCode, EmailCallout, EmailDivider, EmailSummary, renderEmail, } from "./email-components.js";
|
|
5
|
+
export { VerifyEmail, MagicLink, PasswordReset, Invite, Receipt, Notification, } from "./email-templates.js";
|
package/docs/auth-flows.md
CHANGED
|
@@ -27,7 +27,7 @@ The playground has a page template for each step (`?recipe=<id>`).
|
|
|
27
27
|
| Verify email | `verify-email` | AuthLayout, CodeInput, Button, a resend Button with a countdown, a "Change your email" link | `CodeInput` (`one-time-code`, `inputmode="numeric"`) |
|
|
28
28
|
| Two-factor | `two-factor` | AuthLayout, CodeInput, Button, a ghost Button that switches to a recovery code | Authenticator: `CodeInput`. Recovery: `CodeInput alphanumeric length={8}` |
|
|
29
29
|
|
|
30
|
-
The
|
|
30
|
+
The emails for these steps come from `@tonycodes-ui/mortar/email`: `VerifyEmail` for verify email, `PasswordReset` for forgot password, and `MagicLink` for a passwordless sign-in with SignInPanel. See [Transactional email](email.md).
|
|
31
31
|
|
|
32
32
|
## Validation and errors
|
|
33
33
|
|
package/docs/email.md
CHANGED
|
@@ -58,38 +58,53 @@ Emails render light. Most clients either ignore a dark design or recolour it the
|
|
|
58
58
|
|
|
59
59
|
## Components
|
|
60
60
|
|
|
61
|
-
| Component | Use
|
|
62
|
-
| -------------- |
|
|
63
|
-
| `EmailShell` | The whole email: the inbox `preview` line, an optional `logo`, a card up to 600px wide and a `footer`. It sets the theme for the components inside it.
|
|
64
|
-
| `EmailHeading` | `level={1}` is the email's title (once per email); `level={2}` is a section.
|
|
65
|
-
| `EmailText` | A paragraph, 16px. `tone="muted"` and `size="sm"` are for details such as an expiry time.
|
|
66
|
-
| `EmailButton` | The main action. It keeps its padding in Outlook for Windows. Follow it with the URL as an `EmailLink`, for clients that block buttons.
|
|
67
|
-
| `EmailLink` | An inline link. Without children it shows the URL itself.
|
|
68
|
-
| `EmailCode` | A one-time code, large and spaced.
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
| Component | Use |
|
|
62
|
+
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
63
|
+
| `EmailShell` | The whole email: the inbox `preview` line, an optional `logo`, a card up to 600px wide and a `footer`. It sets the theme for the components inside it. |
|
|
64
|
+
| `EmailHeading` | `level={1}` is the email's title (once per email); `level={2}` is a section. |
|
|
65
|
+
| `EmailText` | A paragraph, 16px. `tone="muted"` and `size="sm"` are for details such as an expiry time. |
|
|
66
|
+
| `EmailButton` | The main action. It keeps its padding in Outlook for Windows. Follow it with the URL as an `EmailLink`, for clients that block buttons. |
|
|
67
|
+
| `EmailLink` | An inline link. Without children it shows the URL itself. |
|
|
68
|
+
| `EmailCode` | A one-time code, large and spaced. |
|
|
69
|
+
| `EmailCallout` | A highlighted block with an optional `title`. `tone` (`info`, `success`, `warning`, `danger`) colours its edge; `neutral`, the default, is an aside such as a quoted message. |
|
|
70
|
+
| `EmailDivider` | A rule between sections. |
|
|
71
|
+
| `EmailSummary` | A receipt's `items` (name, preformatted `price`, optional `quantity` and `meta`), cost `lines` and `total`, as OrderSummary takes them. In the plain-text part it reads as rows. |
|
|
72
|
+
|
|
73
|
+
`EmailShell` takes `dir="rtl"`, and the components that place things at the start or end follow it: the callout's edge and the summary's amounts.
|
|
74
|
+
|
|
75
|
+
Links and the logo need absolute `http://` or `https://` URLs (`EmailLink` also takes `mailto:`), because the email is read away from your site. Anything else, such as a relative or `javascript:` URL, throws.
|
|
71
76
|
|
|
72
77
|
Compose your own email from these:
|
|
73
78
|
|
|
74
79
|
```tsx
|
|
75
80
|
<EmailShell
|
|
76
|
-
preview="
|
|
81
|
+
preview="3 tasks are due this week"
|
|
77
82
|
theme={theme}
|
|
78
|
-
footer="
|
|
83
|
+
footer="You get this summary every Monday."
|
|
79
84
|
>
|
|
80
|
-
<EmailHeading>
|
|
81
|
-
<EmailText>
|
|
82
|
-
<
|
|
85
|
+
<EmailHeading>Your week in Hive</EmailHeading>
|
|
86
|
+
<EmailText>3 tasks are due this week.</EmailText>
|
|
87
|
+
<EmailCallout tone="warning" title="Overdue">
|
|
88
|
+
Launch checklist, due Friday
|
|
89
|
+
</EmailCallout>
|
|
90
|
+
<EmailButton href={tasksUrl}>Open your tasks</EmailButton>
|
|
83
91
|
</EmailShell>
|
|
84
92
|
```
|
|
85
93
|
|
|
86
94
|
## Templates
|
|
87
95
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
|
96
|
+
Every template takes `productName`, and optionally `theme`, `logo`, `footer` (replacing its "why you got this" line), `lang` and `dir`. Times and amounts are preformatted strings, so you choose the locale.
|
|
97
|
+
|
|
98
|
+
| Template | Use | Props |
|
|
99
|
+
| --------------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
|
100
|
+
| `VerifyEmail` | The [verify-email step](auth-flows.md#steps) | A `code`, an `href` or both; optional `recipient`, `expiresIn` |
|
|
101
|
+
| `MagicLink` | A passwordless sign-in link, as SignInPanel sends | `href`; optional `expiresIn`, `requestedFrom` (e.g. "Chrome on macOS, near London") |
|
|
102
|
+
| `PasswordReset` | The [reset-password step](auth-flows.md#steps) | `href`; optional `recipient`, `expiresIn`, `requestedFrom` |
|
|
103
|
+
| `Invite` | An invitation to a workspace | `inviterName`, `href`; optional `workspaceName`, `message` (the inviter's note, quoted), `expiresIn` |
|
|
104
|
+
| `Receipt` | An order confirmation or receipt | `orderNumber`, `date`, `items`, `total`; optional `lines`, `paymentMethod`, `href` ("View order"), `title` |
|
|
105
|
+
| `Notification` | A product notification | `title`, `children` (a string becomes a paragraph); optional `action` (`{ label, href }`), `preview`, `preferencesHref` |
|
|
91
106
|
|
|
92
|
-
|
|
107
|
+
Send a password reset only to an address that has an account, and show the same "check your email" page either way, so the form never reveals whether an account exists.
|
|
93
108
|
|
|
94
109
|
## Rules
|
|
95
110
|
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*
|
|
1
|
+
/* Email components for @tonycodes-ui/mortar/email. Built on React Email,
|
|
2
2
|
which renders the table layout and inline styles email clients need; these
|
|
3
3
|
components give it Mortar's tokens, brand and copy. Nothing here uses
|
|
4
4
|
Mortar's stylesheet or app components, which email clients cannot run.
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
Container,
|
|
17
17
|
Head,
|
|
18
18
|
Heading,
|
|
19
|
+
Hr,
|
|
19
20
|
Html,
|
|
20
21
|
Img,
|
|
21
22
|
Link,
|
|
@@ -23,20 +24,29 @@ import {
|
|
|
23
24
|
Text,
|
|
24
25
|
render,
|
|
25
26
|
} from "@react-email/components";
|
|
27
|
+
import type { Tone } from "./primitives.js";
|
|
26
28
|
import { emailTheme, type EmailTheme } from "./email-theme.js";
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const ThemeContext = createContext<EmailTheme | undefined>(undefined);
|
|
30
|
+
const ShellContext = createContext<
|
|
31
|
+
{ theme: EmailTheme; dir: "ltr" | "rtl" } | undefined
|
|
32
|
+
>(undefined);
|
|
32
33
|
let neutral: EmailTheme | undefined;
|
|
33
34
|
/** The theme from the enclosing EmailShell, or Mortar's neutral theme. */
|
|
34
35
|
function useTheme(): EmailTheme {
|
|
35
|
-
return useContext(
|
|
36
|
+
return useContext(ShellContext)?.theme ?? (neutral ??= emailTheme());
|
|
37
|
+
}
|
|
38
|
+
/* Email clients don't support logical properties, so the inline start and
|
|
39
|
+
end are worked out from the shell's direction. */
|
|
40
|
+
function useSides() {
|
|
41
|
+
const rtl = useContext(ShellContext)?.dir === "rtl";
|
|
42
|
+
return {
|
|
43
|
+
start: rtl ? "right" : "left",
|
|
44
|
+
end: rtl ? "left" : "right",
|
|
45
|
+
} as const;
|
|
36
46
|
}
|
|
37
47
|
|
|
38
48
|
/* Links leave the mail client, so only web and mail addresses are allowed. */
|
|
39
|
-
function assertHref(href: string, component: string, mail = false) {
|
|
49
|
+
export function assertHref(href: string, component: string, mail = false) {
|
|
40
50
|
if (!(mail ? /^(?:https?:|mailto:)/i : /^https?:\/\//i).test(href.trim()))
|
|
41
51
|
throw new Error(
|
|
42
52
|
`Mortar ${component} needs an absolute ${mail ? "http(s) or mailto" : "http(s)"} URL: "${href}"`,
|
|
@@ -107,7 +117,7 @@ export function EmailShell({
|
|
|
107
117
|
const theme = given ?? emailTheme();
|
|
108
118
|
if (logo) assertHref(logo.src, "EmailShell logo");
|
|
109
119
|
return (
|
|
110
|
-
<
|
|
120
|
+
<ShellContext.Provider value={{ theme, dir }}>
|
|
111
121
|
<Html lang={lang} dir={dir}>
|
|
112
122
|
<Head>
|
|
113
123
|
<meta name="color-scheme" content="light" />
|
|
@@ -162,7 +172,7 @@ export function EmailShell({
|
|
|
162
172
|
</Container>
|
|
163
173
|
</Body>
|
|
164
174
|
</Html>
|
|
165
|
-
</
|
|
175
|
+
</ShellContext.Provider>
|
|
166
176
|
);
|
|
167
177
|
}
|
|
168
178
|
|
|
@@ -270,7 +280,8 @@ export function EmailLink({ href, children }: EmailLinkProps) {
|
|
|
270
280
|
style={{
|
|
271
281
|
color: theme.link,
|
|
272
282
|
textDecoration: "underline",
|
|
273
|
-
|
|
283
|
+
// A bare URL may break anywhere so it can't widen the email.
|
|
284
|
+
wordBreak: children === undefined ? "break-all" : undefined,
|
|
274
285
|
}}
|
|
275
286
|
>
|
|
276
287
|
{children ?? href}
|
|
@@ -312,6 +323,180 @@ export function EmailCode({ code }: EmailCodeProps) {
|
|
|
312
323
|
);
|
|
313
324
|
}
|
|
314
325
|
|
|
326
|
+
export interface EmailCalloutProps {
|
|
327
|
+
/** Colours the edge: a status, or `neutral` for an aside such as a quoted message. */
|
|
328
|
+
tone?: Tone;
|
|
329
|
+
title?: string;
|
|
330
|
+
children: ReactNode;
|
|
331
|
+
}
|
|
332
|
+
/** A highlighted block: a warning, a status or a quoted personal message. */
|
|
333
|
+
export function EmailCallout({
|
|
334
|
+
tone = "neutral",
|
|
335
|
+
title,
|
|
336
|
+
children,
|
|
337
|
+
}: EmailCalloutProps) {
|
|
338
|
+
const theme = useTheme();
|
|
339
|
+
const { start } = useSides();
|
|
340
|
+
const edge = tone === "neutral" ? theme.line : theme[tone];
|
|
341
|
+
return (
|
|
342
|
+
<Cell style={{ padding: "8px 0 24px" }}>
|
|
343
|
+
<Cell
|
|
344
|
+
style={{
|
|
345
|
+
backgroundColor: theme.sunken,
|
|
346
|
+
[start === "left" ? "borderLeft" : "borderRight"]:
|
|
347
|
+
`4px solid ${edge}`,
|
|
348
|
+
padding: "16px 20px",
|
|
349
|
+
fontFamily: theme.font,
|
|
350
|
+
fontSize: "15px",
|
|
351
|
+
lineHeight: "22px",
|
|
352
|
+
color: theme.ink,
|
|
353
|
+
}}
|
|
354
|
+
>
|
|
355
|
+
{title && (
|
|
356
|
+
<Text
|
|
357
|
+
style={{
|
|
358
|
+
margin: "0 0 4px",
|
|
359
|
+
fontFamily: theme.font,
|
|
360
|
+
fontSize: "15px",
|
|
361
|
+
lineHeight: "22px",
|
|
362
|
+
fontWeight: 600,
|
|
363
|
+
color: theme.ink,
|
|
364
|
+
}}
|
|
365
|
+
>
|
|
366
|
+
{title}
|
|
367
|
+
</Text>
|
|
368
|
+
)}
|
|
369
|
+
{children}
|
|
370
|
+
</Cell>
|
|
371
|
+
</Cell>
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** A rule between sections of an email. */
|
|
376
|
+
export function EmailDivider() {
|
|
377
|
+
const theme = useTheme();
|
|
378
|
+
return (
|
|
379
|
+
<Hr
|
|
380
|
+
style={{
|
|
381
|
+
margin: "8px 0 24px",
|
|
382
|
+
border: "none",
|
|
383
|
+
borderTop: `1px solid ${theme.line}`,
|
|
384
|
+
}}
|
|
385
|
+
/>
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export interface EmailSummaryItem {
|
|
390
|
+
name: string;
|
|
391
|
+
/** Preformatted, e.g. "£42.00". */
|
|
392
|
+
price: string;
|
|
393
|
+
quantity?: number;
|
|
394
|
+
/** Chosen options, e.g. "Natural · Small". */
|
|
395
|
+
meta?: string;
|
|
396
|
+
}
|
|
397
|
+
export interface EmailSummaryProps {
|
|
398
|
+
/** Line items, each with its preformatted price. */
|
|
399
|
+
items?: EmailSummaryItem[];
|
|
400
|
+
/** Cost lines under the items, as OrderSummary takes them: subtotal, shipping, tax. */
|
|
401
|
+
lines?: { label: string; value: string }[];
|
|
402
|
+
total: { label: string; value: string };
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* A receipt's items, cost lines and total, as a two-column table: labels at
|
|
406
|
+
* the start, amounts at the end. Amounts are preformatted strings, as in
|
|
407
|
+
* Mortar's commerce components.
|
|
408
|
+
*/
|
|
409
|
+
export function EmailSummary({
|
|
410
|
+
items = [],
|
|
411
|
+
lines = [],
|
|
412
|
+
total,
|
|
413
|
+
}: EmailSummaryProps) {
|
|
414
|
+
const theme = useTheme();
|
|
415
|
+
const { start, end } = useSides();
|
|
416
|
+
const cell = (side: "start" | "end", extra: CSSProperties = {}) => ({
|
|
417
|
+
padding: "8px 0",
|
|
418
|
+
textAlign: side === "start" ? start : end,
|
|
419
|
+
verticalAlign: "top",
|
|
420
|
+
fontFamily: theme.font,
|
|
421
|
+
fontSize: "15px",
|
|
422
|
+
lineHeight: "22px",
|
|
423
|
+
color: theme.ink,
|
|
424
|
+
...extra,
|
|
425
|
+
});
|
|
426
|
+
const rule = `1px solid ${theme.line}`;
|
|
427
|
+
return (
|
|
428
|
+
<Cell style={{ padding: "8px 0 24px" }}>
|
|
429
|
+
<table
|
|
430
|
+
data-summary=""
|
|
431
|
+
role="presentation"
|
|
432
|
+
width="100%"
|
|
433
|
+
border={0}
|
|
434
|
+
cellPadding={0}
|
|
435
|
+
cellSpacing={0}
|
|
436
|
+
>
|
|
437
|
+
<tbody>
|
|
438
|
+
{items.map((item, i) => (
|
|
439
|
+
<tr key={i}>
|
|
440
|
+
<td style={cell("start", { borderBottom: rule })}>
|
|
441
|
+
{item.name}
|
|
442
|
+
{item.quantity !== undefined && item.quantity !== 1 && (
|
|
443
|
+
<span style={{ color: theme.muted }}> × {item.quantity}</span>
|
|
444
|
+
)}
|
|
445
|
+
{item.meta && (
|
|
446
|
+
<>
|
|
447
|
+
<br />
|
|
448
|
+
<span style={{ fontSize: "13px", color: theme.muted }}>
|
|
449
|
+
{item.meta}
|
|
450
|
+
</span>
|
|
451
|
+
</>
|
|
452
|
+
)}
|
|
453
|
+
</td>
|
|
454
|
+
<td
|
|
455
|
+
style={cell("end", {
|
|
456
|
+
borderBottom: rule,
|
|
457
|
+
whiteSpace: "nowrap",
|
|
458
|
+
[start === "left" ? "paddingLeft" : "paddingRight"]: "16px",
|
|
459
|
+
})}
|
|
460
|
+
>
|
|
461
|
+
{item.price}
|
|
462
|
+
</td>
|
|
463
|
+
</tr>
|
|
464
|
+
))}
|
|
465
|
+
{lines.map((line, i) => (
|
|
466
|
+
<tr key={`line-${i}`}>
|
|
467
|
+
<td style={cell("start", { color: theme.secondary })}>
|
|
468
|
+
{line.label}
|
|
469
|
+
</td>
|
|
470
|
+
<td
|
|
471
|
+
style={cell("end", {
|
|
472
|
+
color: theme.secondary,
|
|
473
|
+
whiteSpace: "nowrap",
|
|
474
|
+
})}
|
|
475
|
+
>
|
|
476
|
+
{line.value}
|
|
477
|
+
</td>
|
|
478
|
+
</tr>
|
|
479
|
+
))}
|
|
480
|
+
<tr>
|
|
481
|
+
<td style={cell("start", { borderTop: rule, fontWeight: 600 })}>
|
|
482
|
+
{total.label}
|
|
483
|
+
</td>
|
|
484
|
+
<td
|
|
485
|
+
style={cell("end", {
|
|
486
|
+
borderTop: rule,
|
|
487
|
+
fontWeight: 600,
|
|
488
|
+
whiteSpace: "nowrap",
|
|
489
|
+
})}
|
|
490
|
+
>
|
|
491
|
+
{total.value}
|
|
492
|
+
</td>
|
|
493
|
+
</tr>
|
|
494
|
+
</tbody>
|
|
495
|
+
</table>
|
|
496
|
+
</Cell>
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
315
500
|
export interface RenderedEmail {
|
|
316
501
|
html: string;
|
|
317
502
|
/** The plain-text alternative: send it with the HTML. */
|
|
@@ -325,89 +510,19 @@ export interface RenderedEmail {
|
|
|
325
510
|
export async function renderEmail(element: ReactNode): Promise<RenderedEmail> {
|
|
326
511
|
const [html, text] = await Promise.all([
|
|
327
512
|
render(element),
|
|
328
|
-
render(element, {
|
|
513
|
+
render(element, {
|
|
514
|
+
plainText: true,
|
|
515
|
+
htmlToTextOptions: {
|
|
516
|
+
// A summary reads as rows of label and amount, not run together.
|
|
517
|
+
selectors: [
|
|
518
|
+
{
|
|
519
|
+
selector: "table[data-summary]",
|
|
520
|
+
format: "dataTable",
|
|
521
|
+
options: { maxColumnWidth: 48 },
|
|
522
|
+
},
|
|
523
|
+
],
|
|
524
|
+
},
|
|
525
|
+
}),
|
|
329
526
|
]);
|
|
330
527
|
return { html, text };
|
|
331
528
|
}
|
|
332
|
-
|
|
333
|
-
/* Templates */
|
|
334
|
-
|
|
335
|
-
export interface VerifyEmailProps {
|
|
336
|
-
/** Your product, as the person knows it. */
|
|
337
|
-
productName: string;
|
|
338
|
-
/** A one-time code to type in, shown large. */
|
|
339
|
-
code?: string;
|
|
340
|
-
/** A confirmation link, shown as a button. Give `code`, `href` or both. */
|
|
341
|
-
href?: string;
|
|
342
|
-
/** The address being confirmed. */
|
|
343
|
-
recipient?: string;
|
|
344
|
-
/** Preformatted, e.g. "10 minutes". */
|
|
345
|
-
expiresIn?: string;
|
|
346
|
-
theme?: EmailTheme;
|
|
347
|
-
logo?: EmailLogo;
|
|
348
|
-
/** Replaces the default "if this wasn't you" footer. */
|
|
349
|
-
footer?: ReactNode;
|
|
350
|
-
lang?: string;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Confirms an email address with a code, a link or both: the email for the
|
|
354
|
-
* auth flow's verify-email step.
|
|
355
|
-
*/
|
|
356
|
-
export function VerifyEmail({
|
|
357
|
-
productName,
|
|
358
|
-
code,
|
|
359
|
-
href,
|
|
360
|
-
recipient,
|
|
361
|
-
expiresIn,
|
|
362
|
-
theme,
|
|
363
|
-
logo,
|
|
364
|
-
footer,
|
|
365
|
-
lang,
|
|
366
|
-
}: VerifyEmailProps) {
|
|
367
|
-
if (!code && !href)
|
|
368
|
-
throw new Error("Mortar VerifyEmail needs a `code`, an `href` or both.");
|
|
369
|
-
const address = recipient ?? "your email address";
|
|
370
|
-
return (
|
|
371
|
-
<EmailShell
|
|
372
|
-
preview={`Confirm your email address for ${productName}`}
|
|
373
|
-
theme={theme}
|
|
374
|
-
logo={logo}
|
|
375
|
-
lang={lang}
|
|
376
|
-
footer={
|
|
377
|
-
footer ??
|
|
378
|
-
`You received this email because this address was used to sign up for ${productName}. If that wasn't you, you can ignore it.`
|
|
379
|
-
}
|
|
380
|
-
>
|
|
381
|
-
<EmailHeading>Confirm your email address</EmailHeading>
|
|
382
|
-
{code && (
|
|
383
|
-
<>
|
|
384
|
-
<EmailText>
|
|
385
|
-
Enter this code in {productName} to confirm {address}:
|
|
386
|
-
</EmailText>
|
|
387
|
-
<EmailCode code={code} />
|
|
388
|
-
</>
|
|
389
|
-
)}
|
|
390
|
-
{href && (
|
|
391
|
-
<>
|
|
392
|
-
<EmailText>
|
|
393
|
-
{code
|
|
394
|
-
? "Or confirm with one click:"
|
|
395
|
-
: `Confirm ${address} to finish setting up your ${productName} account.`}
|
|
396
|
-
</EmailText>
|
|
397
|
-
<EmailButton href={href}>Confirm email address</EmailButton>
|
|
398
|
-
<EmailText tone="muted" size="sm">
|
|
399
|
-
If the button doesn't work, paste this link into your browser:{" "}
|
|
400
|
-
<EmailLink href={href} />
|
|
401
|
-
</EmailText>
|
|
402
|
-
</>
|
|
403
|
-
)}
|
|
404
|
-
{expiresIn && (
|
|
405
|
-
<EmailText tone="muted" size="sm">
|
|
406
|
-
{code && href
|
|
407
|
-
? `This code and link expire in ${expiresIn}.`
|
|
408
|
-
: `This ${code ? "code" : "link"} expires in ${expiresIn}.`}
|
|
409
|
-
</EmailText>
|
|
410
|
-
)}
|
|
411
|
-
</EmailShell>
|
|
412
|
-
);
|
|
413
|
-
}
|