Dark mode
HTML text and borders switch cleanly through CSS custom properties, whereas compiled static SVGs (charts, flowcharts, math frames) bake fixed colors into the output file. Here is how site theme toggling pairs with three ways to handle SVG colors.
Theme toggle
Combine system preference detection with a manual override in localStorage and :root[data-theme="dark"]. Run a tiny inline blocking script in <head> before styles load so dark-mode visitors do not see a flash of the light theme:
:root {
color-scheme: light;
--bg: #f7f8f7;
--fg: #0f1f1a;
--border: #d4ddd9;
}
:root[data-theme="dark"] {
color-scheme: dark;
--bg: #1b1e1d;
--fg: #e5e7e9;
--border: #34393b;
}// Runs before CSS to set data-theme prior to first paint
#html.script("
(function () {
var saved = localStorage.getItem('theme');
var dark = saved ? saved === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches;
if (dark) document.documentElement.dataset.theme = 'dark';
})();
")Standalone SVGs
Embedding dozens of kilobytes of inline SVG directly into HTML inflates page weight and defeats browser caching. Instead, record lightweight #metadata(...) markers while rendering pages, emit <img> tags, and publish deduplicated .svg files once at the Bundle root via query and asset(...):
#import "@tola/address:0.0.0": output-url
#import "@tola/hash:0.0.0": blake3, bytes-to-hex
#let svg-id(src) = bytes-to-hex(blake3(bytes(src))).slice(0, 8)
#let chart-block(src, alt: "") = {
let id = svg-id(src)
let out = "charts/" + id + ".svg"
html.figure[
#metadata((id: id, src: src, out: out)) <site-chart>
#html.img(src: output-url(out), alt: alt, loading: "lazy")
]
}
// Call once at top level of site.typ after all document(...) calls
#let publish-charts() = context {
let seen = (:)
for item in query(<site-chart>) {
let v = item.value
if v.id in seen { continue }
seen.insert(v.id, true)
asset(v.out, render-chart-svg(v.src))
}
}Balanced contrast
If you emit a single monochrome SVG used over both light and dark backgrounds, linearize both background sRGB values to WCAG 2 relative luminance and take their geometric mean rather than guessing a mid-grey:
The resulting grey has identical contrast against both surfaces. You can compute it directly in Typst at build time:
#let _linear(v) = if v <= 0.04045 { v / 12.92 } else { calc.pow((v + 0.055) / 1.055, 2.4) }
#let _encoded(v) = if v <= 0.0031308 { 12.92 * v } else { 1.055 * calc.pow(v, 1 / 2.4) - 0.055 }
#let _luminance(color) = {
let hex = color.to-hex()
let ch(i) = int(hex.slice(1 + i * 2, 3 + i * 2), base: 16) / 255
0.2126 * _linear(ch(0)) + 0.7152 * _linear(ch(1)) + 0.0722 * _linear(ch(2))
}
#let balanced-grey(light-bg, dark-bg) = {
let l1 = _luminance(light-bg)
let l2 = _luminance(dark-bg)
let target = calc.sqrt((calc.max(l1, l2) + 0.05) * (calc.min(l1, l2) + 0.05)) - 0.05
let pct = _encoded(target) * 100%
rgb(pct, pct, pct)
}
#let ink = balanced-grey(rgb("#f7f8f7"), rgb("#1b1e1d")).to-hex() // #7b7b7bThere is a hard mathematical ceiling: even between pure white and pure black, one shared grey reaches at most 4.58:1 contrast; against softer surfaces such as #f7f8f7 and #1b1e1d, #7b7b7b achieves 3.97:1. That clears WCAG 3:1 for graphics and icons, but falls below 4.5:1 for text labels—so line-art graphics can use one shared SVG, whereas labeled diagrams need per-theme colors.
Dual SVGs
When diagrams carry labels or multiple colors, compare the two options:
| Approach | Best fit | Trade-offs |
|---|---|---|
| Compile-time dual SVGs | External <img src="...svg"> diagrams that must follow manual data-theme switches | Writes two SVG files per figure and toggles two <img> elements via CSS |
CSS light-dark() | Inline <svg> or page CSS when you want one shared definition | Newer CSS Color 5 feature; does not respond to host data-theme inside external <img> SVGs |
With dual SVGs, emit both light and dark variants in publish-charts and toggle visibility via CSS (see Math & diagrams for the Mermaid setup):
.diagram-dark { display: none; }
:root[data-theme="dark"] .diagram-light { display: none; }
:root[data-theme="dark"] .diagram-dark { display: block; }For inline <svg> elements inside modern browsers, light-dark(light, dark) avoids duplicate files:
:root { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }
.inline-chart text {
fill: light-dark(#0f1f1a, #e5e7e9);
}
.inline-chart path {
stroke: light-dark(#007a63, #35c3a1);
}