Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(email): dark styles never reach Outlook's light reading pane

Sidwebworksbbf73d9b0a6a
diff --git a/.claude/rules/email-design.md b/.claude/rules/email-design.md
index 4551792b00..1d6b775891 100644
--- a/.claude/rules/email-design.md
+++ b/.claude/rules/email-design.md
@@ -47,8 +47,8 @@ are label/value details. Every piece below is load-bearing:
## Rules that are not style
- **No background images, and no page colour in light mode.** Gmail's iOS app ignores the dark-mode media queries and inverts the email's colours itself, but never a background image: a `linear-gradient` card stayed white while its text turned white. `.nf-body` has a colour only in dark mode, and `.nf-card` is a bordered box on the mail app's own background in light mode.
-- **The dark palette is Gmail's inversion of the light one.** `emailTheme.dark` (`components/Theme.tsx`) holds the colours Gmail's iOS app produced from the light palette (page `#131314`, callout `#242527`, button `#f3f3f3` on `#202124`, muted `#9fa7b6`), measured from a real screenshot, so Apple Mail (which runs the dark CSS) shows the same email Gmail does. In dark mode the card is the page's colour, set off by its border. `notificationStyles` and `WeeklyReminderEmail` read the palette, never hex literals. Change a light colour and its dark twin changes in Gmail on its own: re-measure and update `emailTheme.dark` to match.
-- **Every dark rule sits inside a `prefers-color-scheme: dark` media query.** Outlook (web and the new app) strips that query and recolours the email itself, tagging elements `data-ogsc` / `data-ogsb` as it does. Rules keyed on those tags (and the folklore `.gmail_dark` / `[data-darkmode]` selectors) also fired with the message shown on a light background: a purchase order's panel went dark and its heading and greeting turned white on white. Let Outlook and Gmail do their own dark mode.
+- **The dark palette is Gmail's inversion of the light one.** `emailTheme.dark` (`components/Theme.tsx`) holds the colours Gmail's iOS app produced from the light palette (page `#131314`, callout `#242527`, button `#f3f3f3` on `#202124`, muted `#9fa7b6`), measured from a real screenshot, so Apple Mail (which runs the dark CSS) shows the same email Gmail does. Outlook recolours on its own and never gets this palette. In dark mode the card is the page's colour, set off by its border. `notificationStyles` and `WeeklyReminderEmail` read the palette, never hex literals. Change a light colour and its dark twin changes in Gmail on its own: re-measure and update `emailTheme.dark` to match.
+- **Every dark rule goes through `darkModeMedia` (`components/Theme.tsx`).** Outlook.com and the new Outlook run `@media (prefers-color-scheme: dark)` from the operating system's theme, not their own (Can I email lists Outlook.com as supporting the query; HTeuMeuLeu: "if your email is displayed in Outlook.com's light mode preview pane, but your operating system is set up in dark mode, all of your dark mode styles will still apply"). A purchase order on Outlook's light pane under a dark Windows got a dark panel and a heading white on white. Outlook prefixes every class with `x_` in the markup and the CSS, so `darkModeMedia` adds `:not([class^="x_"])` to each selector's first class and refuses a selector with no class; Outlook then does its own dark mode. Outlook.com also drops any rule containing `:not()` outright (Can I email's `:not()` test, 2024-01: neither cell styled, where stripping only `:not()` would have styled both), so the rule cannot reach it either way. Never write a raw `@media (prefers-color-scheme: dark)` block, and never key a rule on Outlook's `data-ogsc` / `data-ogsb` or the `.gmail_dark` / `[data-darkmode]` selectors: no Gmail client runs the query at all. No Outlook honours the `color-scheme` meta tag, so `light only` cannot opt out there.
- **Dark mode is done by CLASS, not inline style.** Backgrounds go through `nf-*` classes so the `!important` media-query overrides in `notificationStyles` can flip them; an inline `background-color` beats them and renders a white card on a black Gmail. Text colors use `getEmailThemeClasses()` (`email-text`, `email-muted`, …). Off-black `#0e0e0e` and off-white `#fefefe`, never pure `#000`/`#fff` — pure values get auto-inverted by some clients.
- **No sample defaults on content props.** `NotificationEmail` deliberately gives `heading`/`message` no defaults: fabricated data must never reach a real recipient. Sample data lives in the preview fixture only. (Some older templates — `VerificationEmail` — still default their props; don't copy that, or their pre-card layout.)
- **Always send a `text` alternative** alongside `html` — the job builds it from the same content (see `entryEmailContent` in `packages/jobs/src/inngest/functions/tasks/changelog-dispatch.feed.ts`).
diff --git a/packages/documents/src/email/WeeklyReminderEmail.tsx b/packages/documents/src/email/WeeklyReminderEmail.tsx
index 847d12a14b..e6683d0922 100644
--- a/packages/documents/src/email/WeeklyReminderEmail.tsx
+++ b/packages/documents/src/email/WeeklyReminderEmail.tsx
@@ -15,6 +15,7 @@ import { Button } from "./components/Button";
import { Logo } from "./components/Logo";
import { flatCardOnPhones } from "./components/notificationStyles";
import {
+ darkModeMedia,
EmailThemeProvider,
emailTheme,
getEmailThemeClasses,
@@ -83,7 +84,7 @@ const weeklyReminderStyles = `
color: #6b7280 !important;
}
- @media (prefers-color-scheme: dark) {
+ ${darkModeMedia(`
.wr-body {
background-color: ${dark.background} !important;
}
@@ -110,7 +111,7 @@ const weeklyReminderStyles = `
.wr-fallback {
color: ${dark.muted} !important;
}
- }
+ `)}
${flatCardOnPhones("wr-card")}
`;
diff --git a/packages/documents/src/email/components/Theme.test.ts b/packages/documents/src/email/components/Theme.test.ts
new file mode 100644
index 0000000000..a2e5a98ba3
--- /dev/null
+++ b/packages/documents/src/email/components/Theme.test.ts
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+import { describe, expect, it } from "vitest";
+import { darkModeMedia, getEmailDarkModeCSS } from "./Theme";
+
+describe("darkModeMedia", () => {
+ it("scopes each selector's first class away from Outlook's x_ classes", () => {
+ const css = darkModeMedia(`
+ .a, .b > table > tbody > tr > td { color: red !important; }
+ .c .d { border-color: blue; }
+ `);
+ expect(css).toContain("@media (prefers-color-scheme: dark)");
+ expect(css).toContain('.a:not([class^="x_"])');
+ expect(css).toContain('.b:not([class^="x_"]) > table > tbody > tr > td');
+ expect(css).toContain('.c:not([class^="x_"]) .d');
+ expect(css).toContain("color: red !important;");
+ });
+
+ it("refuses a selector with no class, which Outlook would still run", () => {
+ expect(() => darkModeMedia("td { color: red; }")).toThrow(/has no class/);
+ });
+
+ it("leaves no dark rule in the theme unscoped", () => {
+ const media = getEmailDarkModeCSS().split("@media").slice(1).join("@media");
+ for (const [, selectors] of media.matchAll(/([^{}]+)\{[^{}]*\}/g)) {
+ for (const selector of (selectors ?? "").split(",")) {
+ expect(selector).toContain(':not([class^="x_"])');
+ }
+ }
+ });
+});
diff --git a/packages/documents/src/email/components/Theme.tsx b/packages/documents/src/email/components/Theme.tsx
index eab45a3c4b..c45ee8bcb4 100644
--- a/packages/documents/src/email/components/Theme.tsx
+++ b/packages/documents/src/email/components/Theme.tsx
@@ -36,6 +36,35 @@ export const emailTheme = {
}
} as const;
+/**
+ * A `prefers-color-scheme: dark` block that skips Outlook.com and the new
+ * Outlook. Those run the query from the operating system's theme, not their
+ * own: on a dark OS, a message on Outlook's light reading pane took every dark
+ * rule, so a purchase order's panel went dark and its heading turned white on
+ * white (HTeuMeuLeu, "Making Emails React to Outlook.com's Dark Mode"; Can I
+ * email, `@media (prefers-color-scheme)`). Outlook prefixes every class with
+ * `x_`, in the markup and the CSS alike, so `:not([class^="x_"])` on each
+ * selector's first class matches everywhere but there, and Outlook does its
+ * own dark mode. Outlook.com also drops any rule containing `:not()` (Can I
+ * email, `:not()`: not supported, tested 2024-01, on a test where stripping
+ * only the `:not()` would have shown), so the rule never reaches it either
+ * way. A selector with no class would still reach Outlook, so it is refused.
+ */
+export function darkModeMedia(rules: string) {
+ const scoped = rules
+ .replace(/\/\*[\s\S]*?\*\//g, "")
+ .replace(/([^{}]+)\{/g, (_, selectors: string) => {
+ const list = selectors.split(",").map((selector) => {
+ if (!/\.[A-Za-z]/.test(selector)) {
+ throw new Error(`darkModeMedia: "${selector.trim()}" has no class`);
+ }
+ return selector.replace(/\.([A-Za-z][\w-]*)/, '.$1:not([class^="x_"])');
+ });
+ return `${list.join(",")}{`;
+ });
+ return `@media (prefers-color-scheme: dark) {${scoped}}`;
+}
+
// Industry-standard dark mode CSS for email clients
export const getEmailDarkModeCSS = () => {
return `
@@ -45,8 +74,8 @@ export const getEmailDarkModeCSS = () => {
supported-color-schemes: light dark;
}
- /* Apple Mail, iOS Mail, and some webview clients */
- @media (prefers-color-scheme: dark) {
+ /* Apple Mail, iOS Mail and other clients that run the theme's dark CSS */
+ ${darkModeMedia(`
.email-body,
.email-body > table > tbody > tr > td {
background-color: ${emailTheme.dark.background} !important;
@@ -91,85 +120,7 @@ export const getEmailDarkModeCSS = () => {
.dark-mode-show {
display: block !important;
}
- }
-
- /* Gmail Desktop Dark Mode - Multiple targeting approaches */
- @media (prefers-color-scheme: dark) {
- /* Gmail specific selectors */
- .gmail_dark .email-body,
- .gmail_dark .email-body > table > tbody > tr > td,
- .gmail_dark_theme .email-body,
- .gmail_dark_theme .email-body > table > tbody > tr > td,
- [data-darkmode="true"] .email-body,
- [data-darkmode="true"] .email-body > table > tbody > tr > td {
- background-color: ${emailTheme.dark.background} !important;
- color: ${emailTheme.dark.foreground} !important;
- }
- .gmail_dark .email-container,
- .gmail_dark_theme .email-container,
- [data-darkmode="true"] .email-container {
- border-color: ${emailTheme.dark.border} !important;
- }
- .gmail_dark .email-text,
- .gmail_dark_theme .email-text,
- [data-darkmode="true"] .email-text {
- color: ${emailTheme.dark.foreground} !important;
- }
- .gmail_dark .email-muted,
- .gmail_dark_theme .email-muted,
- [data-darkmode="true"] .email-muted {
- color: ${emailTheme.dark.muted} !important;
- }
- .gmail_dark .email-accent,
- .gmail_dark_theme .email-accent,
- [data-darkmode="true"] .email-accent {
- color: ${emailTheme.dark.accent} !important;
- border-color: ${emailTheme.dark.accent} !important;
- }
- .gmail_dark .email-button,
- .gmail_dark_theme .email-button,
- [data-darkmode="true"] .email-button {
- background-color: ${emailTheme.dark.accent} !important;
- border-color: ${emailTheme.dark.accent} !important;
- color: ${emailTheme.dark.accentForeground} !important;
- }
- }
-
- /* Gmail Desktop conditional dark mode targeting */
- @media screen and (prefers-color-scheme: dark) {
- /* More aggressive Gmail desktop targeting */
- div[style*="background"] .email-body,
- div[style*="background"] .email-body > table > tbody > tr > td,
- .ii .email-body,
- .ii .email-body > table > tbody > tr > td {
- background-color: ${emailTheme.dark.background} !important;
- color: ${emailTheme.dark.foreground} !important;
- }
- div[style*="background"] .email-container,
- .ii .email-container {
- border-color: ${emailTheme.dark.border} !important;
- }
- div[style*="background"] .email-text,
- .ii .email-text {
- color: ${emailTheme.dark.foreground} !important;
- }
- div[style*="background"] .email-muted,
- .ii .email-muted {
- color: ${emailTheme.dark.muted} !important;
- }
- div[style*="background"] .email-accent,
- .ii .email-accent {
- color: ${emailTheme.dark.accent} !important;
- border-color: ${emailTheme.dark.accent} !important;
- }
- }
-
- /* Nothing outside a dark media query. Outlook (web and the new app) and
- Gmail's apps recolour an email for dark mode themselves. Outlook tags
- the email with data-ogsc / data-ogsb when it does, and rules keyed on
- those tags also fired with the message shown on a light background: a
- purchase order's panel went dark and its heading turned white on
- white. */
+ `)}
`;
};
diff --git a/packages/documents/src/email/components/notificationStyles.ts b/packages/documents/src/email/components/notificationStyles.ts
index acf31418dc..5519823964 100644
--- a/packages/documents/src/email/components/notificationStyles.ts
+++ b/packages/documents/src/email/components/notificationStyles.ts
@@ -2,7 +2,7 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
-import { emailTheme } from "./Theme";
+import { darkModeMedia, emailTheme } from "./Theme";
const dark = emailTheme.dark;
@@ -63,7 +63,7 @@ export const notificationStyles = `
color: #6b7280 !important;
}
- @media (prefers-color-scheme: dark) {
+ ${darkModeMedia(`
.nf-body,
.nf-body > table > tbody > tr > td {
background-color: ${dark.background} !important;
@@ -88,5 +88,5 @@ export const notificationStyles = `
.nf-fallback {
color: ${dark.muted} !important;
}
- }
+ `)}
${flatCardOnPhones("nf-card")}`;