From 14dab47fd538f774f10e8d823198c726aef548da Mon Sep 17 00:00:00 2001 From: Madison Date: Wed, 2 Jul 2025 00:49:22 -0500 Subject: [PATCH] Immediate hover response on clickableTOC, and light mode support --- docs/src/components/mdx/sdk-components.tsx | 31 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) 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 (