feat(mailer): emails follow a light system theme, and stay dark otherwise
release / go (push) Successful in 2m0s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m16s
release / android (push) Successful in 4m23s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 2m0s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m16s
release / android (push) Successful in 4m23s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
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 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
htmltemplate "html/template"
|
||||
"io/fs"
|
||||
"sort"
|
||||
"strings"
|
||||
texttemplate "text/template"
|
||||
)
|
||||
@@ -16,15 +17,19 @@ 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}).
|
||||
layoutHTML = htmltemplate.Must(htmltemplate.New("layout.html").Funcs(htmltemplate.FuncMap{
|
||||
"style": style, "class": class, "lightTheme": lightTheme,
|
||||
}).
|
||||
ParseFS(layoutFS, "templates/layout.html"))
|
||||
)
|
||||
|
||||
@@ -40,6 +45,16 @@ 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
|
||||
|
||||
// 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"
|
||||
@@ -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.
|
||||
|
||||
@@ -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</td>`,
|
||||
"<h1 style=",
|
||||
`<h1 class="m-heading" style=`,
|
||||
"Sent by the Minstrel server at https://music.example because ",
|
||||
"— Minstrel",
|
||||
} {
|
||||
@@ -63,3 +64,61 @@ func TestUnknownStyleFailsTheRender(t *testing.T) {
|
||||
t.Fatal("want an error for an unknown style")
|
||||
}
|
||||
}
|
||||
|
||||
// An email is dark unless the reader's system is light. Every element drawn
|
||||
// in a dark colour carries the class that gives it the light one, and every
|
||||
// style with a colour has light colours to give.
|
||||
func TestEveryColouredElementFollowsALightTheme(t *testing.T) {
|
||||
for name, s := range styles {
|
||||
if strings.Contains(s, "#") && name != "button" {
|
||||
if _, ok := lightStyles[name]; !ok {
|
||||
t.Errorf("style %q has a colour and no light colours", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
reset, err := func() (string, error) {
|
||||
_, html, err := RenderResetEmail(ResetEmailVars{
|
||||
Username: "alice", ResetURL: "https://music.example/reset/abc", ExpiryHours: 1, Server: "https://music.example",
|
||||
})
|
||||
return html, err
|
||||
}()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, smtpTest, err := RenderSMTPTestEmail("https://music.example/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, html := range map[string]string{"reset": reset, "smtp test": smtpTest} {
|
||||
if !strings.Contains(html, "@media (prefers-color-scheme: light)") ||
|
||||
!strings.Contains(html, ".m-card { background:"+lightCard+" !important;") {
|
||||
t.Errorf("%s html has no light theme", name)
|
||||
}
|
||||
for _, tag := range unthemed(html) {
|
||||
t.Errorf("%s: %s is drawn dark with no light class", name, tag)
|
||||
}
|
||||
}
|
||||
if got := unthemed(`<p style="color:` + colourText + `;">x</p>`); 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
|
||||
}
|
||||
|
||||
@@ -3,22 +3,25 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="color-scheme" content="dark">
|
||||
<meta name="supported-color-schemes" content="dark">
|
||||
<meta name="color-scheme" content="dark light">
|
||||
<meta name="supported-color-schemes" content="dark light">
|
||||
<style>
|
||||
{{lightTheme}}
|
||||
</style>
|
||||
</head>
|
||||
<body style="{{style "page"}}">
|
||||
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="{{style "wrap"}}">
|
||||
<body class="{{class "page"}}" style="{{style "page"}}">
|
||||
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" class="{{class "wrap"}}" style="{{style "wrap"}}">
|
||||
<tr><td align="center" style="padding:32px 16px;">
|
||||
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:560px;">
|
||||
<tr><td style="{{style "brand"}}">Minstrel</td></tr>
|
||||
<tr><td style="{{style "card"}}">
|
||||
<h1 style="{{style "heading"}}">{{.Frame.Heading}}</h1>
|
||||
<tr><td class="{{class "brand"}}" style="{{style "brand"}}">Minstrel</td></tr>
|
||||
<tr><td class="{{class "card"}}" style="{{style "card"}}">
|
||||
<h1 class="{{class "heading"}}" style="{{style "heading"}}">{{.Frame.Heading}}</h1>
|
||||
{{template "content" .Content}}
|
||||
</td></tr>
|
||||
<tr><td style="{{style "footer"}}">
|
||||
<tr><td class="{{class "footer"}}" style="{{style "footer"}}">
|
||||
Sent by {{.Frame.ServerName}} because {{.Frame.Reason}}.<br>
|
||||
{{- if .Frame.NotificationSettings}}
|
||||
{{with .Frame.SettingsURL}}<a href="{{.}}" style="{{style "faint"}}">Choose which notifications are emailed to you</a>{{else}}Choose which notifications are emailed to you in Minstrel, under Settings → Notifications.{{end}}<br>
|
||||
{{with .Frame.SettingsURL}}<a href="{{.}}" class="{{class "faint"}}" style="{{style "faint"}}">Choose which notifications are emailed to you</a>{{else}}Choose which notifications are emailed to you in Minstrel, under Settings → Notifications.{{end}}<br>
|
||||
{{- end}}
|
||||
— Minstrel
|
||||
</td></tr>
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
<p style="{{style "p"}}">A password reset was requested for your Minstrel account. To set a new
|
||||
password, use the button below within <strong>{{.ExpiryHours}}</strong> hours.</p>
|
||||
<p style="margin:24px 0;"><a href="{{.ResetURL}}" style="{{style "button"}}">Reset password</a></p>
|
||||
<p style="{{style "p"}} {{style "quiet"}}">Or copy this address into your browser:<br>
|
||||
<span style="{{style "code"}}">{{.ResetURL}}</span></p>
|
||||
<p style="margin:0; {{style "quiet"}}">If you didn't ask for this, you can ignore this email. Your password won't change.</p>
|
||||
<p class="{{class "quiet"}}" style="{{style "p"}} {{style "quiet"}}">Or copy this address into your browser:<br>
|
||||
<span class="{{class "code"}}" style="{{style "code"}}">{{.ResetURL}}</span></p>
|
||||
<p class="{{class "quiet"}}" style="margin:0; {{style "quiet"}}">If you didn't ask for this, you can ignore this email. Your password won't change.</p>
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
<p style="{{style "p"}}">Hello <strong>{{.Name}}</strong>,</p>
|
||||
<p style="margin:0;">{{.Intro}}</p>
|
||||
{{range .Artists}}
|
||||
<p style="{{style "group"}}">{{if .URL}}<a href="{{.URL}}" style="{{style "link"}}">{{.Name}}</a>{{else}}{{.Name}}{{end}}</p>
|
||||
<p class="{{class "group"}}" style="{{style "group"}}">{{if .URL}}<a href="{{.URL}}" class="{{class "link"}}" style="{{style "link"}}">{{.Name}}</a>{{else}}{{.Name}}{{end}}</p>
|
||||
{{if .Titles}}<ul style="{{style "list"}}">
|
||||
{{range .Titles}}<li style="{{style "item"}}">{{if .URL}}<a href="{{.URL}}" style="{{style "link"}}">{{.Title}}</a>{{else}}{{.Title}}{{end}}</li>
|
||||
{{range .Titles}}<li style="{{style "item"}}">{{if .URL}}<a href="{{.URL}}" class="{{class "link"}}" style="{{style "link"}}">{{.Title}}</a>{{else}}{{.Title}}{{end}}</li>
|
||||
{{end}}</ul>{{end}}
|
||||
{{end}}{{if .Items}}
|
||||
<ul style="margin:16px 0 0; padding-left:20px;">
|
||||
{{range .Items}}<li style="margin:8px 0;">
|
||||
{{if .URL}}<a href="{{.URL}}" style="{{style "link"}} font-weight:600;">{{.Title}}</a>{{else}}<strong>{{.Title}}</strong>{{end}}
|
||||
{{if .Body}}<br><span style="{{style "quiet"}}">{{.Body}}</span>{{end}}
|
||||
{{if .URL}}<a href="{{.URL}}" class="{{class "link"}}" style="{{style "link"}} font-weight:600;">{{.Title}}</a>{{else}}<strong>{{.Title}}</strong>{{end}}
|
||||
{{if .Body}}<br><span class="{{class "quiet"}}" style="{{style "quiet"}}">{{.Body}}</span>{{end}}
|
||||
</li>
|
||||
{{end}}</ul>
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user