From 7c6c47cdb206d6f005d3cb2ddae464182d6a0d99 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sat, 28 Nov 2026 16:15:48 -0500 Subject: [PATCH] feat(mailer): emails follow a light system theme, and stay dark otherwise Every email is still drawn dark inline, so a client that can't tell the reader's theme shows the app's own look. A stylesheet gives each coloured element the design system's light colours when the reader's system is light. A test checks that every element drawn dark carries the class that lightens it. Co-Authored-By: Claude Opus 5.5 --- internal/mailer/layout.go | 83 +++++++++++++++++-- internal/mailer/layout_test.go | 61 +++++++++++++- internal/mailer/templates/layout.html | 21 +++-- internal/mailer/templates/password_reset.html | 6 +- internal/notifications/templates/digest.html | 8 +- 5 files changed, 155 insertions(+), 24 deletions(-) diff --git a/internal/mailer/layout.go b/internal/mailer/layout.go index bcc18e49..c240e14f 100644 --- a/internal/mailer/layout.go +++ b/internal/mailer/layout.go @@ -6,6 +6,7 @@ import ( "fmt" htmltemplate "html/template" "io/fs" + "sort" "strings" texttemplate "text/template" ) @@ -16,16 +17,20 @@ import ( // template and an HTML template, plus a Frame; Render puts the content in the // frame. A plain-text body goes out beside the HTML for clients that won't // show HTML. Colours and type come from the Minstrel -// design system's dark theme, the app's own look, so an email reads as part -// of the same product on every client. +// design system, so an email reads as part of the same product. An email is +// dark, the app's own look, wherever the client can't say which the reader +// prefers; a client that says they use a light theme shows the design +// system's light colours instead (lightTheme). //go:embed templates/layout.html templates/layout.txt var layoutFS embed.FS var ( layoutText = texttemplate.Must(texttemplate.ParseFS(layoutFS, "templates/layout.txt")) - layoutHTML = htmltemplate.Must(htmltemplate.New("layout.html").Funcs(htmltemplate.FuncMap{"style": style}). - ParseFS(layoutFS, "templates/layout.html")) + layoutHTML = htmltemplate.Must(htmltemplate.New("layout.html").Funcs(htmltemplate.FuncMap{ + "style": style, "class": class, "lightTheme": lightTheme, + }). + ParseFS(layoutFS, "templates/layout.html")) ) // The Minstrel design system's values, as an email client needs them: inline, @@ -40,9 +45,19 @@ const ( colourLink = "#A1AEA0" // --fs-accent-fg: the accent, readable as text on a dark card colourAction = "#4A5D3F" // --fs-action-primary colourOnAction = "#E8E4D8" // --fs-text-on-action - fontBody = "'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif" - fontDisplay = "'Fraunces', Georgia, 'Times New Roman', serif" - fontMono = "'JetBrains Mono', ui-monospace, Menlo, Consolas, monospace" + + // The light theme's values of the same tokens. The action colour and the + // text on it are one value in both themes. + lightPage = "#F5F1E8" // --fs-surface-page + lightCard = "#FBF8F0" // --fs-surface-raised + lightBorder = "#D9D6CE" // --fs-border-color + lightText = "#14171A" // --fs-text-primary + lightSecondary = "#5A5852" // --fs-text-secondary + lightTertiary = "#9A9890" // --fs-text-tertiary + lightLink = "#2C3D38" // --fs-accent-fg, mixed with the light text colour + fontBody = "'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif" + fontDisplay = "'Fraunces', Georgia, 'Times New Roman', serif" + fontMono = "'JetBrains Mono', ui-monospace, Menlo, Consolas, monospace" ) // styles are the named inline styles an email's HTML content uses, written @@ -66,6 +81,26 @@ var styles = map[string]string{ "code": "font-family:" + fontMono + "; font-size:13px; word-break:break-all; color:" + colourSecondary + ";", } +// lightStyles are the colours each style takes in a light theme. A style +// with a colour has an entry, and an element using it carries its class, +// written `class="{{class "link"}}"`, for the light colours to reach it. +// Inline styles can't follow the reader's theme, so the light colours are +// a stylesheet that overrides them; a client that drops the stylesheet or +// can't tell the theme keeps the inline dark colours. +var lightStyles = map[string]string{ + "page": "background:" + lightPage, + "wrap": "background:" + lightPage, + "brand": "color:" + lightText, + "heading": "color:" + lightText, + "card": "background:" + lightCard + "; border-color:" + lightBorder + "; color:" + lightText, + "footer": "color:" + lightTertiary, + "link": "color:" + lightLink, + "quiet": "color:" + lightSecondary, + "faint": "color:" + lightTertiary, + "group": "color:" + lightText, + "code": "color:" + lightSecondary, +} + // style returns a named style for an inline style attribute. An unknown name // fails the render, so a typo shows in the email's test, not in an inbox. func style(name string) (htmltemplate.CSS, error) { @@ -76,6 +111,40 @@ func style(name string) (htmltemplate.CSS, error) { return htmltemplate.CSS(s), nil //nolint:gosec // fixed strings from the table above, never user input } +// class returns the class names that give the named styles their light +// colours. An unknown name, or one with no light colours, fails the render. +func class(names ...string) (string, error) { + out := make([]string, 0, len(names)) + for _, n := range names { + if _, ok := lightStyles[n]; !ok { + return "", fmt.Errorf("mailer: no light colours for email style %q", n) + } + out = append(out, "m-"+n) + } + return strings.Join(out, " "), nil +} + +// lightTheme is the stylesheet that gives each class its light colours when +// the reader's system uses a light theme. +func lightTheme() htmltemplate.CSS { + names := make([]string, 0, len(lightStyles)) + for n := range lightStyles { + names = append(names, n) + } + sort.Strings(names) + var b strings.Builder + b.WriteString("@media (prefers-color-scheme: light) {\n") + for _, n := range names { + decls := strings.Split(lightStyles[n], "; ") + for i, d := range decls { + decls[i] = d + " !important" + } + fmt.Fprintf(&b, ".m-%s { %s; }\n", n, strings.Join(decls, "; ")) + } + b.WriteString("}") + return htmltemplate.CSS(b.String()) //nolint:gosec // built from the fixed table above, never user input +} + // Frame is what the layout says around an email's content. type Frame struct { // Heading is the line at the top of the card, saying what the email is. diff --git a/internal/mailer/layout_test.go b/internal/mailer/layout_test.go index df327b17..bf30e38e 100644 --- a/internal/mailer/layout_test.go +++ b/internal/mailer/layout_test.go @@ -1,6 +1,7 @@ package mailer import ( + "regexp" "strings" "testing" "testing/fstest" @@ -23,7 +24,7 @@ func TestEveryEmailIsFramedByTheLayout(t *testing.T) { } for _, want := range []string{ `">Minstrel`, - "

x

`); len(got) != 1 { + t.Errorf("an element without its class went unnoticed: %v", got) + } +} + +// unthemed lists the tags in html whose inline style uses a dark colour that +// has a light one, but which carry no m- class. A button is the action +// colour with its own text colour, the same in both themes. +func unthemed(html string) []string { + dark := []string{colourPage, colourCard, colourBorder, colourText, colourSecondary, colourTertiary, colourLink} + var out []string + for _, tag := range regexp.MustCompile(`<[a-z0-9]+\s[^>]*style="[^"]*"[^>]*>`).FindAllString(html, -1) { + if strings.Contains(tag, colourAction) { + continue + } + for _, c := range dark { + if strings.Contains(tag, c) && !strings.Contains(tag, `class="m-`) { + out = append(out, tag) + break + } + } + } + return out +} diff --git a/internal/mailer/templates/layout.html b/internal/mailer/templates/layout.html index 603d55b3..be38cdb2 100644 --- a/internal/mailer/templates/layout.html +++ b/internal/mailer/templates/layout.html @@ -3,22 +3,25 @@ - - + + + - - + +