@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.
@@ -0,0 +1,355 @@
1
+ /* Email templates for @tonycodes-ui/mortar/email: the transactional emails
2
+ most products send, composed only from the email components. Copy follows
3
+ docs/auth-flows.md: say why the email came and what to do if it wasn't
4
+ expected, and never reveal more than the link or code needs. */
5
+
6
+ import type { ReactNode } from "react";
7
+ import type { EmailTheme } from "./email-theme.js";
8
+ import {
9
+ EmailButton,
10
+ EmailCallout,
11
+ EmailCode,
12
+ EmailHeading,
13
+ EmailLink,
14
+ EmailShell,
15
+ EmailSummary,
16
+ EmailText,
17
+ type EmailLogo,
18
+ type EmailSummaryItem,
19
+ } from "./email-components.js";
20
+
21
+ /** Props every template takes. */
22
+ export interface EmailTemplateProps {
23
+ /** Your product, as the person knows it. */
24
+ productName: string;
25
+ /** From `emailTheme(brand)`; Mortar's neutral theme by default. */
26
+ theme?: EmailTheme;
27
+ logo?: EmailLogo;
28
+ /** Replaces the template's footer, which says why the email was sent. */
29
+ footer?: ReactNode;
30
+ lang?: string;
31
+ dir?: "ltr" | "rtl";
32
+ }
33
+
34
+ /* The main action, then its URL for clients that block buttons. */
35
+ function Action({ href, label }: { href: string; label: string }) {
36
+ return (
37
+ <>
38
+ <EmailButton href={href}>{label}</EmailButton>
39
+ <EmailText tone="muted" size="sm">
40
+ If the button doesn't work, paste this link into your browser:{" "}
41
+ <EmailLink href={href} />
42
+ </EmailText>
43
+ </>
44
+ );
45
+ }
46
+
47
+ function Detail({ children }: { children: ReactNode }) {
48
+ return (
49
+ <EmailText tone="muted" size="sm">
50
+ {children}
51
+ </EmailText>
52
+ );
53
+ }
54
+
55
+ export interface VerifyEmailProps extends EmailTemplateProps {
56
+ /** A one-time code to type in, shown large. */
57
+ code?: string;
58
+ /** A confirmation link, shown as a button. Give `code`, `href` or both. */
59
+ href?: string;
60
+ /** The address being confirmed. */
61
+ recipient?: string;
62
+ /** Preformatted, e.g. "10 minutes". */
63
+ expiresIn?: string;
64
+ }
65
+ /**
66
+ * Confirms an email address with a code, a link or both: the email for the
67
+ * auth flow's verify-email step.
68
+ */
69
+ export function VerifyEmail({
70
+ productName,
71
+ code,
72
+ href,
73
+ recipient,
74
+ expiresIn,
75
+ footer,
76
+ ...shell
77
+ }: VerifyEmailProps) {
78
+ if (!code && !href)
79
+ throw new Error("Mortar VerifyEmail needs a `code`, an `href` or both.");
80
+ const address = recipient ?? "your email address";
81
+ return (
82
+ <EmailShell
83
+ {...shell}
84
+ preview={`Confirm your email address for ${productName}`}
85
+ footer={
86
+ footer ??
87
+ `You received this email because this address was used to sign up for ${productName}. If that wasn't you, you can ignore it.`
88
+ }
89
+ >
90
+ <EmailHeading>Confirm your email address</EmailHeading>
91
+ {code && (
92
+ <>
93
+ <EmailText>
94
+ Enter this code in {productName} to confirm {address}:
95
+ </EmailText>
96
+ <EmailCode code={code} />
97
+ </>
98
+ )}
99
+ {href && (
100
+ <>
101
+ <EmailText>
102
+ {code
103
+ ? "Or confirm with one click:"
104
+ : `Confirm ${address} to finish setting up your ${productName} account.`}
105
+ </EmailText>
106
+ <Action href={href} label="Confirm email address" />
107
+ </>
108
+ )}
109
+ {expiresIn && (
110
+ <Detail>
111
+ {code && href
112
+ ? `This code and link expire in ${expiresIn}.`
113
+ : `This ${code ? "code" : "link"} expires in ${expiresIn}.`}
114
+ </Detail>
115
+ )}
116
+ </EmailShell>
117
+ );
118
+ }
119
+
120
+ export interface MagicLinkProps extends EmailTemplateProps {
121
+ /** The sign-in link. */
122
+ href: string;
123
+ /** Preformatted, e.g. "15 minutes". */
124
+ expiresIn?: string;
125
+ /** Where the request came from, e.g. "Chrome on macOS, near London". */
126
+ requestedFrom?: string;
127
+ }
128
+ /** A passwordless sign-in link: the email behind SignInPanel. */
129
+ export function MagicLink({
130
+ productName,
131
+ href,
132
+ expiresIn,
133
+ requestedFrom,
134
+ footer,
135
+ ...shell
136
+ }: MagicLinkProps) {
137
+ return (
138
+ <EmailShell
139
+ {...shell}
140
+ preview={`Your sign-in link for ${productName}`}
141
+ footer={
142
+ footer ??
143
+ `You received this email because someone asked to sign in to ${productName} with this address. If that wasn't you, you can ignore it.`
144
+ }
145
+ >
146
+ <EmailHeading>Sign in to {productName}</EmailHeading>
147
+ <EmailText>Use the link below to sign in to {productName}.</EmailText>
148
+ <Action href={href} label={`Sign in to ${productName}`} />
149
+ {expiresIn && <Detail>This link expires in {expiresIn}.</Detail>}
150
+ {requestedFrom && <Detail>Requested from {requestedFrom}.</Detail>}
151
+ </EmailShell>
152
+ );
153
+ }
154
+
155
+ export interface PasswordResetProps extends EmailTemplateProps {
156
+ /** The reset link. */
157
+ href: string;
158
+ /** The account's address, shown so the person knows which account. */
159
+ recipient?: string;
160
+ /** Preformatted, e.g. "1 hour". */
161
+ expiresIn?: string;
162
+ /** Where the request came from, e.g. "Firefox on Windows". */
163
+ requestedFrom?: string;
164
+ }
165
+ /**
166
+ * The link for the auth flow's reset-password step. Send it only to an
167
+ * address that has an account, and show the same "check your email" page
168
+ * either way, so the form never reveals whether an account exists.
169
+ */
170
+ export function PasswordReset({
171
+ productName,
172
+ href,
173
+ recipient,
174
+ expiresIn,
175
+ requestedFrom,
176
+ footer,
177
+ ...shell
178
+ }: PasswordResetProps) {
179
+ return (
180
+ <EmailShell
181
+ {...shell}
182
+ preview={`Reset your ${productName} password`}
183
+ footer={
184
+ footer ??
185
+ "If you didn't ask to reset your password, you can ignore this email. Your password won't change."
186
+ }
187
+ >
188
+ <EmailHeading>Reset your password</EmailHeading>
189
+ <EmailText>
190
+ We received a request to reset the password for{" "}
191
+ {recipient
192
+ ? `the ${productName} account ${recipient}`
193
+ : `your ${productName} account`}
194
+ . To choose a new password, use the link below.
195
+ </EmailText>
196
+ <Action href={href} label="Reset password" />
197
+ {expiresIn && <Detail>This link expires in {expiresIn}.</Detail>}
198
+ {requestedFrom && <Detail>Requested from {requestedFrom}.</Detail>}
199
+ </EmailShell>
200
+ );
201
+ }
202
+
203
+ export interface InviteProps extends EmailTemplateProps {
204
+ /** Who sent the invitation. */
205
+ inviterName: string;
206
+ /** The team or workspace; the product when omitted. */
207
+ workspaceName?: string;
208
+ /** The link to accept. */
209
+ href: string;
210
+ /** The inviter's own note, shown quoted. */
211
+ message?: string;
212
+ /** Preformatted, e.g. "7 days". */
213
+ expiresIn?: string;
214
+ }
215
+ /** An invitation to join a workspace. */
216
+ export function Invite({
217
+ productName,
218
+ inviterName,
219
+ workspaceName,
220
+ href,
221
+ message,
222
+ expiresIn,
223
+ footer,
224
+ ...shell
225
+ }: InviteProps) {
226
+ const target = workspaceName ?? productName;
227
+ return (
228
+ <EmailShell
229
+ {...shell}
230
+ preview={`${inviterName} invited you to ${target}`}
231
+ footer={
232
+ footer ??
233
+ `You received this email because ${inviterName} invited this address to ${productName}. If you weren't expecting it, you can ignore it.`
234
+ }
235
+ >
236
+ <EmailHeading>
237
+ {inviterName} invited you to {target}
238
+ </EmailHeading>
239
+ <EmailText>
240
+ {workspaceName
241
+ ? `Join the ${workspaceName} workspace on ${productName} to work with ${inviterName}.`
242
+ : `Join ${inviterName} on ${productName}.`}
243
+ </EmailText>
244
+ {message && (
245
+ <EmailCallout title={`Message from ${inviterName}`}>
246
+ {message}
247
+ </EmailCallout>
248
+ )}
249
+ <Action href={href} label="Accept invitation" />
250
+ {expiresIn && <Detail>This invitation expires in {expiresIn}.</Detail>}
251
+ </EmailShell>
252
+ );
253
+ }
254
+
255
+ export interface ReceiptProps extends EmailTemplateProps {
256
+ orderNumber: string;
257
+ /** Preformatted, e.g. "6 November 2026". */
258
+ date: string;
259
+ items: EmailSummaryItem[];
260
+ /** Subtotal, shipping, tax, discounts. */
261
+ lines?: { label: string; value: string }[];
262
+ total: { label: string; value: string };
263
+ /** e.g. "Visa ending 4242". */
264
+ paymentMethod?: string;
265
+ /** The order in your app. */
266
+ href?: string;
267
+ /** Defaults to "Thanks for your order". */
268
+ title?: string;
269
+ }
270
+ /** An order confirmation or receipt, with items, costs and total. */
271
+ export function Receipt({
272
+ productName,
273
+ orderNumber,
274
+ date,
275
+ items,
276
+ lines,
277
+ total,
278
+ paymentMethod,
279
+ href,
280
+ title = "Thanks for your order",
281
+ footer,
282
+ ...shell
283
+ }: ReceiptProps) {
284
+ return (
285
+ <EmailShell
286
+ {...shell}
287
+ preview={`Your ${productName} receipt for order ${orderNumber}`}
288
+ footer={
289
+ footer ??
290
+ `You received this receipt for order ${orderNumber} from ${productName}. Keep it for your records.`
291
+ }
292
+ >
293
+ <EmailHeading>{title}</EmailHeading>
294
+ <Detail>
295
+ Order {orderNumber}, {date}
296
+ </Detail>
297
+ <EmailSummary items={items} lines={lines} total={total} />
298
+ {paymentMethod && <Detail>Paid with {paymentMethod}.</Detail>}
299
+ {href && <EmailButton href={href}>View order</EmailButton>}
300
+ </EmailShell>
301
+ );
302
+ }
303
+
304
+ export interface NotificationProps extends EmailTemplateProps {
305
+ title: string;
306
+ /** The inbox preview line; the title when omitted. */
307
+ preview?: string;
308
+ children: ReactNode;
309
+ /** One main action. */
310
+ action?: { label: string; href: string };
311
+ /** Where the person can change which emails they get. */
312
+ preferencesHref?: string;
313
+ }
314
+ /** A product notification: a title, a message and at most one action. */
315
+ export function Notification({
316
+ productName,
317
+ title,
318
+ preview,
319
+ children,
320
+ action,
321
+ preferencesHref,
322
+ footer,
323
+ ...shell
324
+ }: NotificationProps) {
325
+ return (
326
+ <EmailShell
327
+ {...shell}
328
+ preview={preview ?? title}
329
+ footer={
330
+ footer ?? (
331
+ <>
332
+ You received this email because of your notification settings in{" "}
333
+ {productName}.
334
+ {preferencesHref && (
335
+ <>
336
+ {" "}
337
+ <EmailLink href={preferencesHref}>
338
+ Manage email preferences
339
+ </EmailLink>
340
+ </>
341
+ )}
342
+ </>
343
+ )
344
+ }
345
+ >
346
+ <EmailHeading>{title}</EmailHeading>
347
+ {typeof children === "string" ? (
348
+ <EmailText>{children}</EmailText>
349
+ ) : (
350
+ children
351
+ )}
352
+ {action && <EmailButton href={action.href}>{action.label}</EmailButton>}
353
+ </EmailShell>
354
+ );
355
+ }
package/src/email.ts ADDED
@@ -0,0 +1,47 @@
1
+ /* @tonycodes-ui/mortar/email: transactional email built on React Email
2
+ (the optional peer dependency @react-email/components). See docs/email.md. */
3
+
4
+ export { emailTheme } from "./email-theme.js";
5
+ export type { EmailTheme } from "./email-theme.js";
6
+ export {
7
+ EmailShell,
8
+ EmailHeading,
9
+ EmailText,
10
+ EmailButton,
11
+ EmailLink,
12
+ EmailCode,
13
+ EmailCallout,
14
+ EmailDivider,
15
+ EmailSummary,
16
+ renderEmail,
17
+ } from "./email-components.js";
18
+ export type {
19
+ EmailLogo,
20
+ EmailShellProps,
21
+ EmailHeadingProps,
22
+ EmailTextProps,
23
+ EmailButtonProps,
24
+ EmailLinkProps,
25
+ EmailCodeProps,
26
+ EmailCalloutProps,
27
+ EmailSummaryItem,
28
+ EmailSummaryProps,
29
+ RenderedEmail,
30
+ } from "./email-components.js";
31
+ export {
32
+ VerifyEmail,
33
+ MagicLink,
34
+ PasswordReset,
35
+ Invite,
36
+ Receipt,
37
+ Notification,
38
+ } from "./email-templates.js";
39
+ export type {
40
+ EmailTemplateProps,
41
+ VerifyEmailProps,
42
+ MagicLinkProps,
43
+ PasswordResetProps,
44
+ InviteProps,
45
+ ReceiptProps,
46
+ NotificationProps,
47
+ } from "./email-templates.js";