@12-apps/notifications 4.8.0 → 4.10.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.
Files changed (114) hide show
  1. package/ADOPTING.md +173 -1
  2. package/README.md +138 -0
  3. package/dist/catalog-BOVZ2b-b.d.ts +132 -0
  4. package/dist/{chunk-M5XTROYM.js → chunk-5Y7QRORV.js} +8 -4
  5. package/dist/chunk-5Y7QRORV.js.map +1 -0
  6. package/dist/{chunk-BGI4BQ66.js → chunk-6W7INOYQ.js} +1 -1
  7. package/dist/chunk-6W7INOYQ.js.map +1 -0
  8. package/dist/chunk-EKUSNUBT.js +222 -0
  9. package/dist/chunk-EKUSNUBT.js.map +1 -0
  10. package/dist/chunk-FTFLU6KH.js +114 -0
  11. package/dist/chunk-FTFLU6KH.js.map +1 -0
  12. package/dist/chunk-JCVRQ42B.js +226 -0
  13. package/dist/chunk-JCVRQ42B.js.map +1 -0
  14. package/dist/chunk-RTURLH5U.js +33 -0
  15. package/dist/chunk-RTURLH5U.js.map +1 -0
  16. package/dist/{chunk-GGAZOLNN.js → chunk-SWOWHIFE.js} +2 -2
  17. package/dist/{chunk-7234ROOP.js → chunk-WVRODNXQ.js} +28 -6
  18. package/dist/chunk-WVRODNXQ.js.map +1 -0
  19. package/dist/chunk-ZFIYBNZ7.js +502 -0
  20. package/dist/chunk-ZFIYBNZ7.js.map +1 -0
  21. package/dist/{chunk-KU4FVOFA.js → chunk-ZIR3ILFH.js} +2 -2
  22. package/dist/{create-api-notifications-WaexrZnn.d.ts → create-api-notifications-CcPYrM3p.d.ts} +35 -4
  23. package/dist/{create-web-notifications-DrgqtU4Q.d.ts → create-web-notifications-_NVYmlvy.d.ts} +89 -4
  24. package/dist/email/index.d.ts +26 -0
  25. package/dist/email/index.js +20 -0
  26. package/dist/email/index.js.map +1 -0
  27. package/dist/email/locales.d.ts +43 -0
  28. package/dist/email/locales.js +29 -0
  29. package/dist/email/locales.js.map +1 -0
  30. package/dist/email/previews/hono.d.ts +46 -0
  31. package/dist/email/previews/hono.js +33 -0
  32. package/dist/email/previews/hono.js.map +1 -0
  33. package/dist/email/previews/index.d.ts +67 -0
  34. package/dist/email/previews/index.js +12 -0
  35. package/dist/email/previews/index.js.map +1 -0
  36. package/dist/email/previews/react/index.d.ts +43 -0
  37. package/dist/email/previews/react/index.js +58 -0
  38. package/dist/email/previews/react/index.js.map +1 -0
  39. package/dist/{generators-CH5m8Ucj.d.ts → generators-qAD4fNPq.d.ts} +1 -1
  40. package/dist/hono/index.d.ts +6 -5
  41. package/dist/hono/index.js +5 -3
  42. package/dist/hono/index.js.map +1 -1
  43. package/dist/index.d.ts +4 -3
  44. package/dist/index.js +15 -5
  45. package/dist/{jobs-CcsSA0YR.d.ts → jobs--fex87-q.d.ts} +1 -1
  46. package/dist/live-DYxEFO49.d.ts +158 -0
  47. package/dist/manifest/index.d.ts +39 -1
  48. package/dist/manifest/index.js +9 -0
  49. package/dist/manifest/index.js.map +1 -1
  50. package/dist/manifest/server.d.ts +32 -7
  51. package/dist/manifest/server.js +16 -4
  52. package/dist/manifest/server.js.map +1 -1
  53. package/dist/manifest/web.d.ts +19 -4
  54. package/dist/manifest/web.js +9 -1
  55. package/dist/manifest/web.js.map +1 -1
  56. package/dist/{panel-UFXNO4AF.js → panel-T36JEMO3.js} +37 -13
  57. package/dist/panel-T36JEMO3.js.map +1 -0
  58. package/dist/{preferences-screen-V3ZBWP7A.js → preferences-screen-SXUIGECY.js} +2 -2
  59. package/dist/preview-screen-DYJRAnAY.d.ts +83 -0
  60. package/dist/react/index.d.ts +55 -6
  61. package/dist/react/index.js +16 -10
  62. package/dist/server/index.d.ts +8 -7
  63. package/dist/server/index.js +6 -4
  64. package/dist/template-Dlt5S54z.d.ts +196 -0
  65. package/dist/{types-CYJN1xCM.d.ts → types-BlqZkCWZ.d.ts} +11 -2
  66. package/dist/web-push/index.d.ts +2 -2
  67. package/dist/{web-push-iER5StBm.d.ts → web-push-Dnyaha2z.d.ts} +21 -1
  68. package/dist/{wire-CmRrB3_s.d.ts → wire-BG1kuoXX.d.ts} +1 -1
  69. package/package.json +22 -2
  70. package/src/email/chrome.en-US.ts +8 -0
  71. package/src/email/chrome.pt-BR.ts +21 -0
  72. package/src/email/index.ts +43 -0
  73. package/src/email/locales.ts +29 -0
  74. package/src/email/previews/catalog.ts +208 -0
  75. package/src/email/previews/hono.ts +69 -0
  76. package/src/email/previews/index.ts +26 -0
  77. package/src/email/previews/react/copy.en-US.ts +24 -0
  78. package/src/email/previews/react/copy.pt-BR.ts +24 -0
  79. package/src/email/previews/react/copy.ts +42 -0
  80. package/src/email/previews/react/index.tsx +14 -0
  81. package/src/email/previews/react/loadable.tsx +83 -0
  82. package/src/email/previews/react/message-list.tsx +133 -0
  83. package/src/email/previews/react/message-view.tsx +167 -0
  84. package/src/email/previews/react/preview-screen.tsx +342 -0
  85. package/src/email/previews/react/transport.ts +60 -0
  86. package/src/email/previews/routes.ts +111 -0
  87. package/src/email/template.ts +365 -0
  88. package/src/email/text.ts +59 -0
  89. package/src/email/theme.ts +77 -0
  90. package/src/index.ts +10 -0
  91. package/src/live.ts +179 -0
  92. package/src/manifest/index.ts +37 -0
  93. package/src/manifest/server.ts +23 -0
  94. package/src/manifest/web.ts +13 -0
  95. package/src/react/create-web-notifications.tsx +15 -1
  96. package/src/react/index.ts +29 -0
  97. package/src/react/live-card.tsx +279 -0
  98. package/src/react/live-config.ts +79 -0
  99. package/src/react/live-section.tsx +141 -0
  100. package/src/react/panel-lazy.tsx +3 -0
  101. package/src/react/panel.tsx +57 -10
  102. package/src/react/relative-time.ts +14 -2
  103. package/src/server/transports/email.ts +62 -0
  104. package/src/server/transports/web-push.ts +22 -0
  105. package/src/types.ts +10 -1
  106. package/dist/chunk-7234ROOP.js.map +0 -1
  107. package/dist/chunk-BGI4BQ66.js.map +0 -1
  108. package/dist/chunk-M5XTROYM.js.map +0 -1
  109. package/dist/chunk-O5BVUXPO.js +0 -22
  110. package/dist/chunk-O5BVUXPO.js.map +0 -1
  111. package/dist/panel-UFXNO4AF.js.map +0 -1
  112. /package/dist/{chunk-GGAZOLNN.js.map → chunk-SWOWHIFE.js.map} +0 -0
  113. /package/dist/{chunk-KU4FVOFA.js.map → chunk-ZIR3ILFH.js.map} +0 -0
  114. /package/dist/{preferences-screen-V3ZBWP7A.js.map → preferences-screen-SXUIGECY.js.map} +0 -0
@@ -0,0 +1,111 @@
1
+ import {
2
+ createEmailPreviews,
3
+ type ApiEmailPreviews,
4
+ type EmailPreviewsConfig,
5
+ } from './catalog';
6
+
7
+ /**
8
+ * The two endpoints over the catalogue, as descriptors.
9
+ *
10
+ * `GET /` answers the list — one row per message, a subject each, no rendered
11
+ * bytes — and `GET /:id` answers one whole document. Two calls rather than one
12
+ * because a rendered mail is 6-10 KB and a catalogue of twenty would be twenty
13
+ * times that on a screen where the operator reads one.
14
+ *
15
+ * ## Nothing here can send anything
16
+ *
17
+ * The surface holds no driver, no transport and no address. `render` is pure by
18
+ * the contract `EmailPreviewMessage` states, so the one mistake a preview
19
+ * surface must be incapable of — putting a sample in somebody's inbox — is not
20
+ * reachable from this code at all rather than merely not done.
21
+ *
22
+ * ## The gate is the HOST's — but the POSTURE is declared here
23
+ *
24
+ * Every route carries `kind: 'authenticated'`, the wiring contract's own word
25
+ * for "behind the host's session resolution and its RBAC". A package cannot
26
+ * know WHICH gate a host uses, and this one does not try: it names no
27
+ * permission id, because the ids belong to whichever host mounts it.
28
+ *
29
+ * What it must not do is stay silent. `public` is the contract's word for
30
+ * anonymous-by-design, and the contract's DEFAULT is `authenticated` — so a
31
+ * descriptor that declared nothing would still read as authenticated to a
32
+ * host's gates, while saying nothing to the person adopting it. This surface
33
+ * publishes a host's whole transactional-mail inventory and the exact wording
34
+ * and link shape of its verification and password-reset mails, which is the
35
+ * reference somebody writing a convincing phishing mail would want. That is
36
+ * worth one word in the descriptor rather than a sentence in a document.
37
+ *
38
+ * `ADOPTING.md` states the obligation in prose as well, because a host still
39
+ * has to supply the gate this word only asks for.
40
+ *
41
+ * An unknown LOCALE is a 400 rather than a silent fall back to the default.
42
+ * This is a diagnostic surface: quietly answering the default language to
43
+ * `?locale=es-AR` looks exactly like a product with no Spanish, which is the
44
+ * question the operator was asking.
45
+ */
46
+
47
+ /** Twin of the wiring contract's request; no import, so this package stays liftable. */
48
+ export interface EmailPreviewRequest {
49
+ /** The path segment after the mount, when the caller asked for one message. */
50
+ params?: Readonly<Record<string, string | undefined>>;
51
+ query?: Readonly<Record<string, string | undefined>>;
52
+ }
53
+
54
+ export interface EmailPreviewResponse {
55
+ status: number;
56
+ body: unknown;
57
+ }
58
+
59
+ export interface EmailPreviewRoute {
60
+ method: 'GET';
61
+ path: string;
62
+ /**
63
+ * The wiring contract's `WireRouteKind`, restated. Always `authenticated`
64
+ * here: the host supplies the gate, and this is the descriptor asking for
65
+ * one — see the docblock above for why silence was not an option.
66
+ */
67
+ kind: 'authenticated';
68
+ handle(request: EmailPreviewRequest): Promise<EmailPreviewResponse>;
69
+ }
70
+
71
+ const ok = (body: unknown): EmailPreviewResponse => ({ status: 200, body: { data: body } });
72
+ const fail = (status: number, error: string): EmailPreviewResponse => ({ status, body: { error } });
73
+
74
+ /** `undefined` passes through as "not asked"; anything unknown is refused. */
75
+ function readLocale(
76
+ previews: ApiEmailPreviews,
77
+ request: EmailPreviewRequest,
78
+ ): { ok: true; locale?: string } | { ok: false; tag: string } {
79
+ const tag = request.query?.locale;
80
+ if (tag === undefined || tag === '') return { ok: true };
81
+ return previews.supportsLocale(tag) ? { ok: true, locale: tag } : { ok: false, tag };
82
+ }
83
+
84
+ export function emailPreviewRoutes(config: EmailPreviewsConfig): EmailPreviewRoute[] {
85
+ const previews = createEmailPreviews(config);
86
+
87
+ return [
88
+ {
89
+ method: 'GET',
90
+ path: '/',
91
+ kind: 'authenticated',
92
+ handle: (request) => {
93
+ const locale = readLocale(previews, request);
94
+ if (!locale.ok) return Promise.resolve(fail(400, `Unknown locale "${locale.tag}".`));
95
+ return Promise.resolve(ok(previews.index(locale.locale)));
96
+ },
97
+ },
98
+ {
99
+ method: 'GET',
100
+ path: '/:id',
101
+ kind: 'authenticated',
102
+ handle: (request) => {
103
+ const locale = readLocale(previews, request);
104
+ if (!locale.ok) return Promise.resolve(fail(400, `Unknown locale "${locale.tag}".`));
105
+ const id = request.params?.id ?? '';
106
+ const detail = previews.render(id, locale.locale);
107
+ return Promise.resolve(detail ? ok(detail) : fail(404, `No e-mail preview named "${id}".`));
108
+ },
109
+ },
110
+ ];
111
+ }
@@ -0,0 +1,365 @@
1
+ import { renderEmailText } from './text';
2
+ import {
3
+ EMAIL_CONTENT_WIDTH,
4
+ EMAIL_FONT_STACK,
5
+ NEUTRAL_EMAIL_THEME,
6
+ type EmailTheme,
7
+ } from './theme';
8
+
9
+ /**
10
+ * THE transactional-mail layout: one document model, one HTML renderer, one
11
+ * plain-text twin.
12
+ *
13
+ * ## The problem it exists for
14
+ *
15
+ * A host that sends mail from more than one place ends up rendering it in more
16
+ * than one way — the notification pipeline emits a bold paragraph and a bare
17
+ * anchor, the sign-in flow emits a `div` with a max-width, the receipt builds
18
+ * its own list — and each grows its own `escapeHtml` and its own text twin. The
19
+ * three look like three different products, because a paragraph with no
20
+ * document around it inherits whatever the client decides: 13px Arial in Gmail,
21
+ * Times New Roman in Outlook, no centring anywhere.
22
+ *
23
+ * That is a LAYOUT problem rather than a wording one, which is what makes it a
24
+ * package's to solve. The words stay the host's; the document does not.
25
+ *
26
+ * ## Why the document is a MODEL and not a string
27
+ *
28
+ * Every caller hands over structure — a heading, some paragraphs, an optional
29
+ * facts table, at most one call to action — and never markup. Three things fall
30
+ * out of that, and each is a real defect in the hand-rolled renderers this
31
+ * replaces:
32
+ *
33
+ * 1. **Escaping cannot be forgotten.** Every field is escaped here, once. Of
34
+ * the three renderers this was extracted from, two escaped the body and
35
+ * interpolated the LINK raw.
36
+ * 2. **The plain-text twin cannot drift.** Both halves render from the same
37
+ * object, so a line added to one is in the other. A `text/html` part with no
38
+ * `text/plain` twin is scored by every major spam filter, and it is what a
39
+ * watch, a terminal client and a screen reader in plain-text mode show.
40
+ * 3. **A preview is honest.** `./server`'s catalogue renders exactly this, from
41
+ * exactly these inputs, so a preview cannot be right about a mail nobody
42
+ * gets.
43
+ *
44
+ * ## The client constraints this encodes
45
+ *
46
+ * Stated once here, so no caller has to know them:
47
+ *
48
+ * - **Tables, not divs.** Outlook 2016+ on Windows lays HTML out with Word,
49
+ * which supports no `flex`, no `grid` and no reliable `max-width` on a block.
50
+ * A centred 600px table is the one construction every client agrees on.
51
+ * - **Inline styles.** Gmail strips `<style>` from the document it renders in
52
+ * several contexts, and always in the Gmail app for a non-Gmail account. The
53
+ * `<style>` block below therefore carries only the mobile media query —
54
+ * progressive enhancement, never anything the layout depends on.
55
+ * - **No web fonts, no `color-mix()`, no CSS variables.** See `./theme`.
56
+ * - **A preheader.** The inbox list shows the first text in the body after the
57
+ * subject; without one it shows the footer's legal line.
58
+ * - **`role="presentation"`** on every layout table, so a screen reader reads
59
+ * the message rather than announcing a five-column grid.
60
+ */
61
+
62
+ /** The words the LAYOUT itself needs — never the message's own sentences. */
63
+ export interface EmailChromeCopy {
64
+ /** "If the button does not work, paste this address into your browser:" */
65
+ readonly fallbackHint: string;
66
+ /** "This is an automated message — please do not reply." */
67
+ readonly automated: string;
68
+ /** The footer's one line about the product, given the brand name. */
69
+ readonly tagline: (brand: string) => string;
70
+ }
71
+
72
+ /** The one call to action a message may carry. More than one dilutes both. */
73
+ export interface EmailAction {
74
+ readonly label: string;
75
+ readonly href: string;
76
+ }
77
+
78
+ /** One row of the facts panel — a receipt's total, an order's reference. */
79
+ export interface EmailFact {
80
+ readonly label: string;
81
+ readonly value: string;
82
+ /** Renders bolder and larger — the one number the reader came for. */
83
+ readonly emphasis?: boolean;
84
+ }
85
+
86
+ /** Everything a message says, with no opinion about how it looks. */
87
+ export interface EmailDocument {
88
+ /** The subject line. Also the document `<title>`. */
89
+ readonly subject: string;
90
+ /**
91
+ * The inbox-list preview, ~90 chars. Falls back to the first paragraph,
92
+ * which is right far more often than it is wrong.
93
+ */
94
+ readonly preheader?: string;
95
+ /** The `<h1>`. Usually a restatement of the subject in the reader's terms. */
96
+ readonly heading: string;
97
+ /** The body, one entry per paragraph. Plain sentences — never markup. */
98
+ readonly paragraphs?: readonly string[];
99
+ /** An optional label/value panel under the body. */
100
+ readonly facts?: readonly EmailFact[];
101
+ /** At most one CTA. */
102
+ readonly action?: EmailAction;
103
+ /** Small print under the CTA — a deadline, a "you can ignore this". */
104
+ readonly notes?: readonly string[];
105
+ /**
106
+ * The layout's own words, in the recipient's language.
107
+ *
108
+ * REQUIRED, with no default in any language — the copy-portability doctrine.
109
+ * `@12-apps/notifications/email/locales` ships a pack per language and a host
110
+ * passes one BY
111
+ * NAME, which is a decision rather than a silence.
112
+ */
113
+ readonly chrome: EmailChromeCopy;
114
+ /**
115
+ * The product name in the header and the footer. REQUIRED: a package that
116
+ * defaulted this would sign another company's mail.
117
+ */
118
+ readonly brand: string;
119
+ /** The recipient's language, for the document's `lang` attribute. */
120
+ readonly locale: string;
121
+ /** Defaults to {@link NEUTRAL_EMAIL_THEME} — see `./theme` for why this one may. */
122
+ readonly theme?: EmailTheme;
123
+ }
124
+
125
+ /**
126
+ * Escape everything that reaches the HTML body.
127
+ *
128
+ * A store name, a buyer's display name and a product title are all user input
129
+ * that reaches a mail, and the ONE thing a shared layout must guarantee is that
130
+ * none of them can close a tag. Single quotes are escaped too: attribute values
131
+ * below are double-quoted, but a caller reading this should not have to check.
132
+ */
133
+ function escapeHtml(value: string): string {
134
+ return value
135
+ .replaceAll("&", "&amp;")
136
+ .replaceAll("<", "&lt;")
137
+ .replaceAll(">", "&gt;")
138
+ .replaceAll('"', "&quot;")
139
+ .replaceAll("'", "&#39;");
140
+ }
141
+
142
+ /**
143
+ * The schemes a link in a mail may use.
144
+ *
145
+ * `javascript:` is inert in every mail client, so this is not the XSS guard it
146
+ * would be on a page — it is the guard for the PREVIEW, which renders the same
147
+ * HTML in a browser, and for the day some part of this markup is reused on a
148
+ * screen. A rejected href drops to `#`, which fails visibly rather than
149
+ * silently linking somewhere unexpected.
150
+ */
151
+ const SAFE_LINK_SCHEMES = new Set(["http:", "https:", "mailto:"]);
152
+
153
+ /** A URL as an attribute value, or `#` when it is not one this layout will emit. */
154
+ export function safeHref(href: string): string {
155
+ const trimmed = href.trim();
156
+ // A relative link is fine and common — every caller that has an origin
157
+ // resolves it before it gets here, and one that does not is better off with
158
+ // a path than with nothing.
159
+ if (trimmed.startsWith("/")) return escapeHtml(trimmed);
160
+ try {
161
+ const url = new URL(trimmed);
162
+ return SAFE_LINK_SCHEMES.has(url.protocol) ? escapeHtml(url.toString()) : "#";
163
+ } catch {
164
+ return "#";
165
+ }
166
+ }
167
+
168
+ /** The message's own first sentence, when the caller named no preheader. */
169
+ function preheaderOf(document: EmailDocument): string {
170
+ return document.preheader ?? document.paragraphs?.[0] ?? document.heading;
171
+ }
172
+
173
+ /** A `<td>` of body copy. One paragraph, at the layout's own rhythm. */
174
+ function paragraph(text: string, theme: EmailTheme): string {
175
+ return `<p style="margin:0 0 16px;font-size:16px;line-height:1.6;color:${theme.ink}">${escapeHtml(text)}</p>`;
176
+ }
177
+
178
+ /**
179
+ * The facts panel — a receipt's four lines, an order's reference.
180
+ *
181
+ * Two cells per row rather than a definition list: `<dl>` is styled
182
+ * inconsistently across clients and collapses to a single column in Outlook,
183
+ * which is the one place these are read side by side.
184
+ */
185
+ function facts(rows: readonly EmailFact[], theme: EmailTheme): string {
186
+ if (rows.length === 0) return "";
187
+ const cells = rows
188
+ .map((row, index) => {
189
+ const divider =
190
+ index === 0 ? "" : `border-top:1px solid ${theme.border};`;
191
+ const value = row.emphasis
192
+ ? `font-size:20px;font-weight:700;color:${theme.ink}`
193
+ : `font-size:15px;color:${theme.ink}`;
194
+ return [
195
+ `<tr>`,
196
+ `<td style="${divider}padding:12px 0 12px 16px;font-size:13px;line-height:1.4;color:${theme.muted};text-transform:uppercase;letter-spacing:.4px;white-space:nowrap">${escapeHtml(row.label)}</td>`,
197
+ `<td align="right" style="${divider}padding:12px 16px 12px 0;line-height:1.4;${value}">${escapeHtml(row.value)}</td>`,
198
+ `</tr>`,
199
+ ].join("");
200
+ })
201
+ .join("");
202
+ return [
203
+ `<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:separate;background:${theme.page};border:1px solid ${theme.border};border-radius:10px;margin:0 0 24px">`,
204
+ cells,
205
+ `</table>`,
206
+ ].join("");
207
+ }
208
+
209
+ /**
210
+ * The CTA.
211
+ *
212
+ * A single-cell table with `bgcolor` AND a background style: Outlook reads the
213
+ * attribute and ignores the property, every other client does the reverse, and
214
+ * a button that loses its fill is invisible ink on white. `border-radius` is
215
+ * simply dropped by Word — a square button is the accepted degradation, and the
216
+ * alternative (a VML rounded rectangle) is forty lines of conditional comment
217
+ * that then has to be kept in step with the colour.
218
+ */
219
+ function action(cta: EmailAction, theme: EmailTheme): string {
220
+ return [
221
+ `<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="margin:8px 0 24px">`,
222
+ `<tr>`,
223
+ `<td align="center" bgcolor="${theme.accent}" style="border-radius:8px;background:${theme.accent}">`,
224
+ `<a href="${safeHref(cta.href)}" style="display:inline-block;padding:14px 32px;font-family:${EMAIL_FONT_STACK};font-size:16px;font-weight:600;line-height:1;color:${theme.onAccent};text-decoration:none;border-radius:8px">${escapeHtml(cta.label)}</a>`,
225
+ `</td>`,
226
+ `</tr>`,
227
+ `</table>`,
228
+ ].join("");
229
+ }
230
+
231
+ /**
232
+ * The "paste this address" fallback, printed whenever there is a CTA.
233
+ *
234
+ * Not optional, and the reason is measured elsewhere in this repo: corporate
235
+ * mail gateways rewrite link targets, and a reader whose gateway mangles the
236
+ * button has no other way to reach a verification or reset link. `word-break`
237
+ * because a signed URL is longer than the card.
238
+ */
239
+ function fallback(cta: EmailAction, chrome: EmailChromeCopy, theme: EmailTheme): string {
240
+ return [
241
+ `<p style="margin:0 0 8px;font-size:13px;line-height:1.5;color:${theme.muted}">${escapeHtml(chrome.fallbackHint)}</p>`,
242
+ `<p style="margin:0 0 24px;font-size:13px;line-height:1.5;word-break:break-all"><a href="${safeHref(cta.href)}" style="color:${theme.accent};text-decoration:underline">${escapeHtml(cta.href)}</a></p>`,
243
+ ].join("");
244
+ }
245
+
246
+ /** The brand header: wordmark over the ramp's rule. */
247
+ function header(document: EmailDocument, theme: EmailTheme): string {
248
+ return [
249
+ `<tr>`,
250
+ `<td style="padding:32px 40px 0">`,
251
+ `<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">`,
252
+ `<tr><td style="font-size:20px;font-weight:700;letter-spacing:-.2px;color:${theme.ink}">${escapeHtml(document.brand)}</td></tr>`,
253
+ `<tr><td style="padding-top:12px"><div style="height:3px;width:44px;background:${theme.rule};border-radius:2px;font-size:0;line-height:0">&nbsp;</div></td></tr>`,
254
+ `</table>`,
255
+ `</td>`,
256
+ `</tr>`,
257
+ ].join("");
258
+ }
259
+
260
+ /** The footer: the tagline, the "do not reply" line, and nothing else. */
261
+ function footer(document: EmailDocument, theme: EmailTheme): string {
262
+ const { chrome, brand } = document;
263
+ return [
264
+ `<tr>`,
265
+ `<td style="padding:0 40px 40px">`,
266
+ `<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">`,
267
+ `<tr><td style="border-top:1px solid ${theme.border};padding-top:20px">`,
268
+ `<p style="margin:0 0 6px;font-size:13px;line-height:1.5;color:${theme.muted}">${escapeHtml(chrome.tagline(brand))}</p>`,
269
+ `<p style="margin:0;font-size:12px;line-height:1.5;color:${theme.muted}">${escapeHtml(chrome.automated)}</p>`,
270
+ `</td></tr>`,
271
+ `</table>`,
272
+ `</td>`,
273
+ `</tr>`,
274
+ ].join("");
275
+ }
276
+
277
+ /**
278
+ * The mobile media query — the one thing in a `<style>` block.
279
+ *
280
+ * Everything it does is also correct without it: the card is already
281
+ * `width:100%` up to 600px, so a client that strips this renders a slightly
282
+ * roomier mail rather than a broken one. That is the test for whether a rule
283
+ * belongs here at all.
284
+ */
285
+ function styleBlock(): string {
286
+ return [
287
+ `<style>`,
288
+ `@media only screen and (max-width:620px){`,
289
+ `.fp-card{width:100%!important;border-radius:0!important;border-left:0!important;border-right:0!important}`,
290
+ `.fp-pad{padding-left:24px!important;padding-right:24px!important}`,
291
+ `.fp-h1{font-size:22px!important}`,
292
+ `}`,
293
+ `</style>`,
294
+ ].join("");
295
+ }
296
+
297
+ /**
298
+ * Render the document as the HTML half of the message.
299
+ *
300
+ * A complete document rather than a fragment: a `<!DOCTYPE>` is what puts
301
+ * Outlook into standards mode, and `x-apple-disable-message-reformatting` is
302
+ * what stops iOS Mail re-flowing the card to the screen width and shrinking the
303
+ * type with it.
304
+ */
305
+ export function renderEmailHtml(document: EmailDocument): string {
306
+ const theme = document.theme ?? NEUTRAL_EMAIL_THEME;
307
+ const body = [
308
+ `<h1 class="fp-h1" style="margin:0 0 16px;font-size:26px;line-height:1.25;font-weight:700;letter-spacing:-.3px;color:${theme.ink}">${escapeHtml(document.heading)}</h1>`,
309
+ ...(document.paragraphs ?? []).map((text) => paragraph(text, theme)),
310
+ facts(document.facts ?? [], theme),
311
+ ...(document.action ? [action(document.action, theme)] : []),
312
+ ...(document.action ? [fallback(document.action, document.chrome, theme)] : []),
313
+ ...(document.notes ?? []).map(
314
+ (note) =>
315
+ `<p style="margin:0 0 8px;font-size:13px;line-height:1.5;color:${theme.muted}">${escapeHtml(note)}</p>`,
316
+ ),
317
+ ].join("\n ");
318
+
319
+ return `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
320
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="${escapeHtml(document.locale)}">
321
+ <head>
322
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
323
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
324
+ <meta name="x-apple-disable-message-reformatting" />
325
+ <meta name="color-scheme" content="light" />
326
+ <meta name="supported-color-schemes" content="light" />
327
+ <title>${escapeHtml(document.subject)}</title>
328
+ ${styleBlock()}
329
+ </head>
330
+ <body style="margin:0;padding:0;background:${theme.page};font-family:${EMAIL_FONT_STACK};-webkit-font-smoothing:antialiased">
331
+ <div style="display:none;max-height:0;overflow:hidden;opacity:0;mso-hide:all">${escapeHtml(preheaderOf(document))}</div>
332
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background:${theme.page}">
333
+ <tr>
334
+ <td align="center" style="padding:32px 12px">
335
+ <table role="presentation" class="fp-card" width="${EMAIL_CONTENT_WIDTH}" cellpadding="0" cellspacing="0" border="0" style="width:${EMAIL_CONTENT_WIDTH}px;max-width:100%;background:${theme.surface};border:1px solid ${theme.border};border-radius:14px">
336
+ ${header(document, theme)}
337
+ <tr>
338
+ <td class="fp-pad" style="padding:24px 40px 8px">
339
+ ${body}
340
+ </td>
341
+ </tr>
342
+ ${footer(document, theme)}
343
+ </table>
344
+ </td>
345
+ </tr>
346
+ </table>
347
+ </body>
348
+ </html>`;
349
+ }
350
+
351
+ /** Both halves plus the subject — what every driver in this repo is handed. */
352
+ export interface RenderedEmail {
353
+ readonly subject: string;
354
+ readonly html: string;
355
+ readonly text: string;
356
+ }
357
+
358
+ /** Render one document into the message shape `EmailDriver.send` takes. */
359
+ export function renderEmail(document: EmailDocument): RenderedEmail {
360
+ return {
361
+ subject: document.subject,
362
+ html: renderEmailHtml(document),
363
+ text: renderEmailText(document),
364
+ };
365
+ }
@@ -0,0 +1,59 @@
1
+ import type { EmailDocument } from './template';
2
+
3
+ /**
4
+ * The plain-text half.
5
+ *
6
+ * Not a courtesy. Every major spam filter scores a `text/html` part with no
7
+ * `text/plain` twin, and a watch, a terminal client and a screen reader in
8
+ * plain-text mode show this and nothing else.
9
+ *
10
+ * It renders from the SAME `EmailDocument` the HTML half does, which is the
11
+ * property that matters: a line added to one is in the other, so the two cannot
12
+ * drift. Its own module only because the HTML renderer is already a file's
13
+ * worth of client workarounds — the seam is the output format, not the content.
14
+ */
15
+
16
+ /**
17
+ * Render the document as the plain-text half.
18
+ *
19
+ * Not a courtesy. Every major spam filter scores a `text/html` part with no
20
+ * `text/plain` twin, and a watch, a terminal client and a screen reader in
21
+ * plain-text mode show this and nothing else. It is rendered from the SAME
22
+ * object, so it cannot say less than the HTML does.
23
+ */
24
+ export function renderEmailText(document: EmailDocument): string {
25
+ const blocks = [
26
+ document.heading,
27
+ "",
28
+ ...textParagraphs(document),
29
+ ...textFacts(document),
30
+ ...textAction(document),
31
+ ...textNotes(document),
32
+ "--",
33
+ document.chrome.tagline(document.brand),
34
+ document.chrome.automated,
35
+ ];
36
+ return `${blocks.join("\n").replace(/\n{3,}/g, "\n\n").trimEnd()}\n`;
37
+ }
38
+
39
+ /** One blank line after each paragraph, as prose reads. */
40
+ function textParagraphs(document: EmailDocument): string[] {
41
+ return (document.paragraphs ?? []).flatMap((text) => [text, ""]);
42
+ }
43
+
44
+ /** `Label: value` per row, as a receipt is quoted back over the phone. */
45
+ function textFacts(document: EmailDocument): string[] {
46
+ const rows = document.facts ?? [];
47
+ return rows.length === 0 ? [] : [...rows.map((f) => `${f.label}: ${f.value}`), ""];
48
+ }
49
+
50
+ /** The CTA as label + URL: a plain-text reader has no button to press. */
51
+ function textAction(document: EmailDocument): string[] {
52
+ return document.action ? [`${document.action.label}: ${document.action.href}`, ""] : [];
53
+ }
54
+
55
+ function textNotes(document: EmailDocument): string[] {
56
+ const notes = document.notes ?? [];
57
+ return notes.length === 0 ? [] : [...notes, ""];
58
+ }
59
+
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The layout's design tokens — the nine colours a mail is drawn from.
3
+ *
4
+ * ## Why a theme is a VALUE here and copy is required config
5
+ *
6
+ * The copy-portability doctrine is that a package ships no user-facing
7
+ * sentences: a default in the origin host's language reads as finished to the
8
+ * next host right up until somebody's customer gets mail in a language they do
9
+ * not speak. Colour is not that. {@link NEUTRAL_EMAIL_THEME} is not anybody's
10
+ * brand — it is grey on white, which reads as *unstyled but tidy* rather than
11
+ * as *somebody else's product*. A host that passes nothing gets a mail that
12
+ * looks plain, not a mail that looks like a different company.
13
+ *
14
+ * So this one has a default and the copy does not, and the difference is
15
+ * exactly whether getting it wrong is invisible. A missing theme is visible in
16
+ * the first preview; a wrong-language default is invisible until a customer
17
+ * complains.
18
+ *
19
+ * ## What a host should know before overriding it
20
+ *
21
+ * Two rungs of most brand palettes cannot carry light text, and a mail has no
22
+ * theme to fall back on when a client ignores a colour. {@link accent} is the
23
+ * one that MUST clear 4.5:1 against white — it is the CTA fill and every link.
24
+ * A warm mid-tone that looks fine on a screen at 2.6:1 is unreadable in an
25
+ * inbox, and nothing in the pipeline will say so.
26
+ */
27
+ export interface EmailTheme {
28
+ /** The page behind the card. Never pure white: a white card needs a ground. */
29
+ readonly page: string;
30
+ /** The card itself. */
31
+ readonly surface: string;
32
+ /** A quieter panel inside the card — the facts table. */
33
+ readonly panel: string;
34
+ /** Every hairline. */
35
+ readonly border: string;
36
+ /** Body text. */
37
+ readonly ink: string;
38
+ /** Secondary text: the footer, the fallback link, a fact's label. */
39
+ readonly muted: string;
40
+ /** The action colour — the CTA fill and every link. Must clear 4.5:1 on white. */
41
+ readonly accent: string;
42
+ /** Ink ON {@link accent}. */
43
+ readonly onAccent: string;
44
+ /** The rule under the wordmark. Decorative — nothing is written on it. */
45
+ readonly rule: string;
46
+ }
47
+
48
+ /**
49
+ * A brand-free default: greys, one blue, nothing anybody would mistake for a
50
+ * product. Every value clears its contrast requirement against its own ground.
51
+ */
52
+ export const NEUTRAL_EMAIL_THEME: EmailTheme = {
53
+ page: '#F5F6F8',
54
+ surface: '#FFFFFF',
55
+ panel: '#F5F6F8',
56
+ border: '#E1E4E9',
57
+ ink: '#1A1D23',
58
+ muted: '#5C6470',
59
+ accent: '#1F5EDB',
60
+ onAccent: '#FFFFFF',
61
+ rule: '#1F5EDB',
62
+ };
63
+
64
+ /**
65
+ * The font stack, as one string.
66
+ *
67
+ * System fonts only, and no web font: a `@font-face` is stripped by Gmail and
68
+ * by Outlook, so a mail that depends on one renders in whatever the client
69
+ * substitutes — never the fallback the designer chose. The stack ends in the
70
+ * emoji faces so a status glyph in a subject line does not drop to a tofu box
71
+ * on Windows.
72
+ */
73
+ export const EMAIL_FONT_STACK =
74
+ "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'";
75
+
76
+ /** The card's width. 600px is the width every client lays out without scroll. */
77
+ export const EMAIL_CONTENT_WIDTH = 600;
package/src/index.ts CHANGED
@@ -65,6 +65,16 @@ export {
65
65
  type NotificationMessages,
66
66
  } from './messages';
67
67
 
68
+ export {
69
+ LIVE_PUSH_TAG_PREFIX,
70
+ LIVE_SUBJECT_KEY,
71
+ liveActivityLane,
72
+ livePushTag,
73
+ type LiveActivity,
74
+ type LiveActivityLane,
75
+ type LiveActivityStep,
76
+ } from './live';
77
+
68
78
  export {
69
79
  inboxWire,
70
80
  type InboxNotification,