Skip to content

Commit a2ce25e

Browse files
committed
Make the Ask AI widget follow the site's colour mode
The widget defaulted to theme auto, which tracks the OS setting, so it disagreed with the site whenever the Docusaurus toggle differed from the OS. Root sits above the theme providers, so read the html data-theme attribute Docusaurus maintains and watch it for changes.
1 parent 47fa123 commit a2ce25e

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/theme/Root.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
1-
import React, {type ReactNode} from 'react';
1+
import React, {useEffect, useState, type ReactNode} from 'react';
22
import {AskAIWidget} from '@0gfoundation/ask-ai-widget';
33
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
44
import '@0gfoundation/ask-ai-widget/styles.css';
55

6+
type SiteTheme = 'light' | 'dark' | 'auto';
7+
8+
// Docusaurus writes the chosen colour mode to <html data-theme="...">. Root
9+
// sits above the theme's providers, so useColorMode() isn't available here;
10+
// watching the attribute is what keeps the widget in step with the site's
11+
// toggle rather than the OS setting.
12+
function useSiteTheme(): SiteTheme {
13+
const [theme, setTheme] = useState<SiteTheme>('auto');
14+
useEffect(() => {
15+
const root = document.documentElement;
16+
const read = () => {
17+
const value = root.getAttribute('data-theme');
18+
setTheme(value === 'dark' || value === 'light' ? value : 'auto');
19+
};
20+
read();
21+
const observer = new MutationObserver(read);
22+
observer.observe(root, {attributes: true, attributeFilter: ['data-theme']});
23+
return () => observer.disconnect();
24+
}, []);
25+
return theme;
26+
}
27+
628
// Docusaurus Root wrapper — renders once around every page, so each page gets
729
// the floating "Ask AI" chat button (bottom-right). The widget talks to the
830
// Ask Zed chat backend at 0g.ai/zed (0G Compute, same RAG knowledge index
931
// as build.0g.ai/ask); docs.0g.ai is in that backend's CORS allowlist.
1032
export default function Root({children}: {children: ReactNode}): ReactNode {
1133
const {siteConfig} = useDocusaurusContext();
1234
const turnstileSiteKey = siteConfig.customFields?.turnstileSiteKey as string;
35+
const theme = useSiteTheme();
1336
return (
1437
<>
1538
{children}
1639
<AskAIWidget
1740
apiUrl="https://0g.ai/zed/api/chat"
1841
turnstileSiteKey={turnstileSiteKey}
42+
theme={theme}
1943
/>
2044
</>
2145
);

0 commit comments

Comments
 (0)