diff --git a/assets/js/components/diagram.js b/assets/js/components/diagram.js index 9348b94e1..5da4382c4 100644 --- a/assets/js/components/diagram.js +++ b/assets/js/components/diagram.js @@ -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 }) => { - mermaid.initialize({ - startOnLoad: true, - themeVariables: { - fontFamily: "Proxima Nova", - fontSize: '18px', - } - }); - mermaid.run(); + // Import mermaid.js module + import('mermaid').then(({ default: mermaid }) => { + // Configure mermaid with InfluxData theming + mermaid.initialize({ + startOnLoad: false, // We'll manually call run() + theme: document.body.classList.contains('dark-theme') ? 'dark' : 'default', + themeVariables: { + 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] }); + } + } + }); + }); + + // 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); + } + }; } diff --git a/assets/styles/layouts/article/_diagrams.scss b/assets/styles/layouts/article/_diagrams.scss index 4e3c1694e..f6c3e1b07 100644 --- a/assets/styles/layouts/article/_diagrams.scss +++ b/assets/styles/layouts/article/_diagrams.scss @@ -16,6 +16,10 @@ background: $article-code-bg !important; font-size: .85em; font-weight: $medium; + + p { + background: $article-bg !important; + } } .node {