diff --git a/public/mdbase-murmuration.js b/public/mdbase-murmuration.js index b5163e5..7bccb6e 100644 --- a/public/mdbase-murmuration.js +++ b/public/mdbase-murmuration.js @@ -47,29 +47,29 @@ }; function syncPalette() { - const styles = getComputedStyle(document.documentElement); + const probe = document.createElement("span"); + probe.hidden = true; + document.body.append(probe); + const styles = getComputedStyle(probe); const dark = styles.colorScheme === "dark"; - palette.paper = styles.getPropertyValue("--color-surface").trim() || palette.paper; - palette.surface = styles.getPropertyValue("--color-surface").trim() || palette.surface; - palette.surfaceSubtle = - styles.getPropertyValue("--color-surface-subtle").trim() || palette.surfaceSubtle; - palette.ink = styles.getPropertyValue("--color-text").trim() || palette.ink; - palette.soft = styles.getPropertyValue("--color-text-soft").trim() || palette.soft; - palette.muted = styles.getPropertyValue("--color-text-muted").trim() || palette.muted; - palette.line = dark - ? styles.getPropertyValue("--color-text-faint").trim() || palette.muted - : styles.getPropertyValue("--color-text-muted").trim() || palette.muted; - palette.lineSoft = styles.getPropertyValue("--color-border").trim() || palette.lineSoft; - palette.accent = styles.getPropertyValue("--color-accent").trim() || palette.accent; - palette.particle = dark - ? styles.getPropertyValue("--color-text-muted").trim() || palette.muted - : styles.getPropertyValue("--color-text").trim() || palette.ink; - palette.particleIntro = dark - ? styles.getPropertyValue("--color-text-soft").trim() || palette.soft - : styles.getPropertyValue("--color-text").trim() || palette.ink; - palette.warning = styles.getPropertyValue("--color-warning-text").trim() || palette.warning; - palette.replica = - styles.getPropertyValue("--color-text-muted").trim() || palette.muted; + const resolveColor = (token, fallback) => { + probe.style.color = `var(${token}, ${fallback})`; + return styles.color; + }; + palette.surface = resolveColor("--color-surface", palette.surface); + palette.paper = palette.surface; + palette.surfaceSubtle = resolveColor("--color-surface-subtle", palette.surfaceSubtle); + palette.ink = resolveColor("--color-text", palette.ink); + palette.soft = resolveColor("--color-text-soft", palette.soft); + palette.muted = resolveColor("--color-text-muted", palette.muted); + palette.line = dark ? resolveColor("--color-text-faint", palette.muted) : palette.muted; + palette.lineSoft = resolveColor("--color-border", palette.lineSoft); + palette.accent = resolveColor("--color-accent", palette.accent); + palette.particle = dark ? palette.muted : palette.ink; + palette.particleIntro = dark ? palette.soft : palette.ink; + palette.warning = resolveColor("--color-warning-text", palette.warning); + palette.replica = palette.muted; + probe.remove(); } class Particle { diff --git a/scripts/home-animation.test.mjs b/scripts/home-animation.test.mjs index 173c17d..7717dc3 100644 --- a/scripts/home-animation.test.mjs +++ b/scripts/home-animation.test.mjs @@ -9,10 +9,16 @@ const scenes = [...homepage.matchAll(/data-scene="([^"]+)"/g)].map(match => matc // Exercise the real scene builders with a deterministic canvas/browser stub. // Real font metrics and screenshots are checked separately in Chromium. -function harness(width, height, reducedMotion = true) { +function harness(width, height, { theme = 'light', tokens = {} } = {}) { + const listeners = new Map(); + const paints = { dots: new Set(), labels: new Set(), outlines: new Set() }; const context = new Proxy({ globalAlpha: 1, font: '14px sans-serif', + clearRect() { Object.values(paints).forEach(colors => colors.clear()); }, + fillRect() { paints.dots.add(this.fillStyle); }, + fillText() { paints.labels.add(this.fillStyle); }, + stroke() { paints.outlines.add(this.strokeStyle); }, measureText(text) { return { width: text.length * Number.parseFloat(this.font) * 0.5 }; }, @@ -28,14 +34,25 @@ function harness(width, height, reducedMotion = true) { }, querySelectorAll: () => chapters, documentElement: { style: { setProperty() {} }, addEventListener() {}, scrollHeight: height * 8 }, + body: { append(element) { element.attached = true; } }, + createElement: () => ({ style: {}, remove() { this.attached = false; } }), fonts: { ready: { then() {} } }, addEventListener() {}, }; const sandbox = { document, innerWidth: width, innerHeight: height, devicePixelRatio: 1, - scrollY: 0, matchMedia: () => ({ matches: reducedMotion, addEventListener() {} }), - getComputedStyle: () => ({ colorScheme: 'light', getPropertyValue: () => '' }), - requestAnimationFrame: () => 1, cancelAnimationFrame() {}, addEventListener() {}, + scrollY: 0, matchMedia: () => ({ matches: true, addEventListener() {} }), + getComputedStyle: element => ({ + colorScheme: theme, + getPropertyValue: name => tokens[name] ? `light-dark(${tokens[name].join(', ')})` : '', + get color() { + assert.ok(element.attached); + const [, name, fallback] = /^var\((--[\w-]+), (.*)\)$/.exec(element.style.color); + return tokens[name]?.[theme === 'dark' ? 1 : 0] ?? fallback; + }, + }), + requestAnimationFrame: () => 1, cancelAnimationFrame() {}, + addEventListener: (name, callback) => listeners.set(name, callback), performance: { now: () => 0 }, }; sandbox.window = sandbox; @@ -45,9 +62,34 @@ function harness(width, height, reducedMotion = true) { globalThis.fitLabel = fitNodeLabel; syncPalette(); resize();`), sandbox); + sandbox.paints = paints; + sandbox.setTheme = value => { + theme = value; + listeners.get('mdbase:themechange')(); + }; return sandbox; } +test('canvas paints resolved theme colors on startup and after toggling the theme', () => { + const app = harness(1440, 1000, { theme: 'dark', tokens: { + '--color-text': ['#222222', '#eeeeee'], + '--color-text-soft': ['#555555', '#cccccc'], + '--color-text-muted': ['#777777', '#aaaaaa'], + } }); + assert.ok(app.paints.dots.has('#cccccc')); + + app.select('collection'); + app.setTheme('light'); + assert.ok(app.paints.dots.has('#222222')); + assert.deepEqual([...app.paints.labels], ['#222222']); + assert.ok(app.paints.outlines.has('#777777')); + + app.setTheme('dark'); + assert.ok(app.paints.dots.has('#aaaaaa')); + assert.deepEqual([...app.paints.labels], ['#eeeeee']); + assert.ok(app.paints.outlines.has('#aaaaaa')); +}); + for (const [width, height] of [[320, 640], [390, 844], [820, 1000], [821, 900], [1024, 768], [1440, 1000]]) { test(`all homepage scenes have bounded labels and particle targets at ${width}px`, () => { const app = harness(width, height);