diff --git a/docs/src/components/mdx/sdk-components.tsx b/docs/src/components/mdx/sdk-components.tsx index a894232b1..8a1e9e698 100644 --- a/docs/src/components/mdx/sdk-components.tsx +++ b/docs/src/components/mdx/sdk-components.tsx @@ -101,9 +101,13 @@ function ClickableCodeblock({ useEffect(() => { const updateHighlightedCode = async () => { try { + // Detect if we're in dark mode by checking CSS custom properties or document class + const isDarkMode = document.documentElement.classList.contains('dark') || + getComputedStyle(document.documentElement).getPropertyValue('--fd-background').includes('0 0% 3.9%'); + const html = await codeToHtml(code, { lang: language, - theme: 'github-dark', + theme: isDarkMode ? 'github-dark' : 'github-light-default', transformers: [{ pre(node) { // Remove background styles from pre element @@ -132,15 +136,34 @@ function ClickableCodeblock({ updateHighlightedCode().catch(error => { console.error('Error updating highlighted code:', error); }); + + // Listen for theme changes on the document element + const observer = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + if (mutation.type === 'attributes' && mutation.attributeName === 'class') { + updateHighlightedCode().catch(error => { + console.error('Error updating highlighted code on theme change:', error); + }); + } + }); + }); + + observer.observe(document.documentElement, { + attributes: true, + attributeFilter: ['class'] + }); + + return () => { + observer.disconnect(); + }; }, [code, language]); return (