From 2419695c9a5b85dfa258d20d59fb0d1e0f59848c Mon Sep 17 00:00:00 2001 From: Gabriel Darbord <78592838+Gabriel-Darbord@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:15:00 +0100 Subject: [PATCH] Redraw diagrams in the page's colours after a theme switch Mermaid keeps the variables an earlier initialize set, so a diagram drawn dark and switched to light kept dark labels; each redraw now sets every variable, from the page's colours, in both themes. Co-Authored-By: Claude Opus 5.5 --- src/components/Mermaid.astro | 43 +++++++++++++++++------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/components/Mermaid.astro b/src/components/Mermaid.astro index af3768e..313e254 100644 --- a/src/components/Mermaid.astro +++ b/src/components/Mermaid.astro @@ -6,30 +6,27 @@ const { default: mermaid } = await import('mermaid'); const draw = async () => { const dark = document.documentElement.dataset.theme === 'dark'; - // In the dark, the page's own soft greys rather than Mermaid's high-contrast dark theme. + // Both themes draw with the page's own colours. Each redraw sets every variable, since + // Mermaid keeps what an earlier initialize set: a dark redraw would leak into the light one. const color = (name: string) => getComputedStyle(document.documentElement).getPropertyValue(name).trim(); - mermaid.initialize( - dark - ? { - startOnLoad: false, - theme: 'base', - themeVariables: { - darkMode: true, - background: color('--surface'), - primaryColor: color('--code-bg'), - primaryTextColor: color('--text'), - primaryBorderColor: color('--border'), - secondaryColor: color('--accent-soft'), - tertiaryColor: color('--surface'), - lineColor: color('--muted'), - textColor: color('--text'), - clusterBkg: color('--bg'), - clusterBorder: color('--border'), - edgeLabelBackground: color('--surface'), - }, - } - : { startOnLoad: false, theme: 'default' }, - ); + mermaid.initialize({ + startOnLoad: false, + theme: 'base', + themeVariables: { + darkMode: dark, + background: color('--surface'), + primaryColor: color('--code-bg'), + primaryTextColor: color('--text'), + primaryBorderColor: color('--muted'), + secondaryColor: color('--accent-soft'), + tertiaryColor: color('--surface'), + lineColor: color('--muted'), + textColor: color('--text'), + clusterBkg: color('--bg'), + clusterBorder: color('--border'), + edgeLabelBackground: color('--surface'), + }, + }); for (const diagram of diagrams) { diagram.removeAttribute('data-processed'); diagram.textContent = diagram.dataset.source!;