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,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.
|
||||
|
||||
Reference in New Issue
Block a user