|
1 | 1 | import React, { useEffect } from 'react'; |
2 | | -import { useColorMode } from '@docusaurus/theme-common'; |
3 | | - |
4 | | -export default function Root({ children }) { |
5 | | - const { colorMode } = useColorMode(); |
| 2 | +import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'; |
6 | 3 |
|
| 4 | +// Client-side only component |
| 5 | +function ThemeMessenger({ children }) { |
7 | 6 | useEffect(() => { |
8 | | - // Function to send theme to all iframes |
9 | | - const sendThemeToIframes = () => { |
10 | | - const iframes = document.querySelectorAll('iframe'); |
11 | | - iframes.forEach((iframe) => { |
12 | | - try { |
13 | | - iframe.contentWindow.postMessage( |
| 7 | + // Dynamic import to avoid SSR issues |
| 8 | + import('@docusaurus/theme-common').then(({ useColorMode: getColorMode }) => { |
| 9 | + // Get the current color mode from the DOM |
| 10 | + const getCurrentTheme = () => { |
| 11 | + return document.documentElement.getAttribute('data-theme') || 'light'; |
| 12 | + }; |
| 13 | + |
| 14 | + // Function to send theme to all iframes |
| 15 | + const sendThemeToIframes = () => { |
| 16 | + const theme = getCurrentTheme(); |
| 17 | + const iframes = document.querySelectorAll('iframe'); |
| 18 | + iframes.forEach((iframe) => { |
| 19 | + try { |
| 20 | + iframe.contentWindow.postMessage( |
| 21 | + { |
| 22 | + type: 'themeChange', |
| 23 | + theme: theme, |
| 24 | + }, |
| 25 | + '*' |
| 26 | + ); |
| 27 | + } catch (e) { |
| 28 | + // Ignore errors |
| 29 | + } |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + // Send theme initially |
| 34 | + sendThemeToIframes(); |
| 35 | + |
| 36 | + // Send theme on a slight delay to catch lazy-loaded iframes |
| 37 | + const timer = setTimeout(sendThemeToIframes, 100); |
| 38 | + |
| 39 | + // Listen for theme requests from iframes |
| 40 | + const handleMessage = (event) => { |
| 41 | + if (event.data && event.data.type === 'requestTheme') { |
| 42 | + const theme = getCurrentTheme(); |
| 43 | + event.source.postMessage( |
14 | 44 | { |
15 | 45 | type: 'themeChange', |
16 | | - theme: colorMode, |
| 46 | + theme: theme, |
17 | 47 | }, |
18 | 48 | '*' |
19 | 49 | ); |
20 | | - } catch (e) { |
21 | | - // Ignore errors |
22 | 50 | } |
| 51 | + }; |
| 52 | + |
| 53 | + window.addEventListener('message', handleMessage); |
| 54 | + |
| 55 | + // Watch for theme changes using MutationObserver |
| 56 | + const observer = new MutationObserver((mutations) => { |
| 57 | + mutations.forEach((mutation) => { |
| 58 | + if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') { |
| 59 | + sendThemeToIframes(); |
| 60 | + } |
| 61 | + }); |
23 | 62 | }); |
24 | | - }; |
25 | | - |
26 | | - // Send theme when it changes |
27 | | - sendThemeToIframes(); |
28 | | - |
29 | | - // Also send theme on a slight delay to catch lazy-loaded iframes |
30 | | - const timer = setTimeout(sendThemeToIframes, 100); |
31 | | - |
32 | | - // Listen for theme requests from iframes |
33 | | - const handleMessage = (event) => { |
34 | | - if (event.data && event.data.type === 'requestTheme') { |
35 | | - event.source.postMessage( |
36 | | - { |
37 | | - type: 'themeChange', |
38 | | - theme: colorMode, |
39 | | - }, |
40 | | - '*' |
41 | | - ); |
42 | | - } |
43 | | - }; |
44 | | - |
45 | | - window.addEventListener('message', handleMessage); |
46 | | - |
47 | | - return () => { |
48 | | - clearTimeout(timer); |
49 | | - window.removeEventListener('message', handleMessage); |
50 | | - }; |
51 | | - }, [colorMode]); |
| 63 | + |
| 64 | + observer.observe(document.documentElement, { |
| 65 | + attributes: true, |
| 66 | + attributeFilter: ['data-theme'], |
| 67 | + }); |
| 68 | + |
| 69 | + return () => { |
| 70 | + clearTimeout(timer); |
| 71 | + window.removeEventListener('message', handleMessage); |
| 72 | + observer.disconnect(); |
| 73 | + }; |
| 74 | + }); |
| 75 | + }, []); |
52 | 76 |
|
53 | 77 | return <>{children}</>; |
54 | 78 | } |
55 | 79 |
|
| 80 | +export default function Root({ children }) { |
| 81 | + // Only run on client side |
| 82 | + if (!ExecutionEnvironment.canUseDOM) { |
| 83 | + return <>{children}</>; |
| 84 | + } |
| 85 | + |
| 86 | + return <ThemeMessenger>{children}</ThemeMessenger>; |
| 87 | +} |
| 88 | + |
0 commit comments