fix(diagram): Mermaid.js: dynamic loading, support dark theme, background color for edgeLabel paragraph elements

- Fixes dynamic loading of Mermaid.js
- Adds MutationObserver to handle light/dark theme switching.
- Sets the background-color for edgeLabel p elements to override a white background that obscures text in the dark theme.
pull/6079/head
Jason Stirnaman 2025-05-13 10:40:07 -05:00
parent 3c64c01f75
commit d92d253242
2 changed files with 67 additions and 14 deletions

View File

@ -1,17 +1,66 @@
export default function Diagram({ component }) {
// Only load mermaid when diagrams are present
const hasDiagrams = document.querySelectorAll('.mermaid').length > 0;
if (hasDiagrams) {
import("mermaid").then(({ mermaid }) => {
// Import mermaid.js module
import('mermaid').then(({ default: mermaid }) => {
// Configure mermaid with InfluxData theming
mermaid.initialize({
startOnLoad: true,
startOnLoad: false, // We'll manually call run()
theme: document.body.classList.contains('dark-theme') ? 'dark' : 'default',
themeVariables: {
fontFamily: "Proxima Nova",
fontSize: '18px',
fontFamily: 'Proxima Nova',
fontSize: '16px',
lineColor: '#22ADF6',
primaryColor: '#22ADF6',
primaryTextColor: '#545454',
secondaryColor: '#05CE78',
tertiaryColor: '#f4f5f5',
},
securityLevel: 'loose', // Required for interactive diagrams
logLevel: 'error'
});
// Process the specific diagram component
try {
mermaid.run({ nodes: [component] });
} catch (error) {
console.error('Mermaid diagram rendering error:', error);
}
// Store reference to mermaid for theme switching
if (!window.mermaidInstances) {
window.mermaidInstances = new Map();
}
window.mermaidInstances.set(component, mermaid);
}).catch(error => {
console.error('Failed to load Mermaid library:', error);
});
// Listen for theme changes to refresh diagrams
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.attributeName === 'class' &&
document.body.classList.contains('dark-theme') !== window.isDarkTheme) {
window.isDarkTheme = document.body.classList.contains('dark-theme');
// Reload this specific diagram with new theme
if (window.mermaidInstances?.has(component)) {
const mermaid = window.mermaidInstances.get(component);
mermaid.initialize({
theme: window.isDarkTheme ? 'dark' : 'default'
});
mermaid.run({ nodes: [component] });
}
}
});
mermaid.run();
});
// Watch for theme changes on body element
observer.observe(document.body, { attributes: true });
// Return cleanup function to be called when component is destroyed
return () => {
observer.disconnect();
if (window.mermaidInstances?.has(component)) {
window.mermaidInstances.delete(component);
}
};
}

View File

@ -16,6 +16,10 @@
background: $article-code-bg !important;
font-size: .85em;
font-weight: $medium;
p {
background: $article-bg !important;
}
}
.node {