File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,21 @@ export default function noFlashColorModePlugin(context) {
3131 document.documentElement.setAttribute('data-theme', theme);
3232 document.documentElement.setAttribute('data-theme-choice', storedTheme || (respectPrefersColorScheme ? 'system' : defaultMode));
3333 document.documentElement.style.colorScheme = theme;
34+ if (document.body) {
35+ document.body.style.colorScheme = theme;
36+ }
37+
38+ var observer = new MutationObserver(function() {
39+ var newTheme = document.documentElement.getAttribute('data-theme');
40+ if (newTheme) {
41+ document.documentElement.style.colorScheme = newTheme;
42+ if (document.body) {
43+ document.body.style.colorScheme = newTheme;
44+ }
45+ }
46+ });
47+
48+ observer.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
3449 })();` ,
3550 } ,
3651 ] ,
Original file line number Diff line number Diff line change 1+ import React , { type PropsWithChildren , useEffect } from 'react' ;
2+ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment' ;
3+
4+ function applyColorScheme ( nextTheme : string | null | undefined ) {
5+ if ( ! ExecutionEnvironment . canUseDOM || ! nextTheme ) {
6+ return ;
7+ }
8+
9+ const resolvedTheme = nextTheme === 'dark' ? 'dark' : 'light' ;
10+ document . documentElement . style . colorScheme = resolvedTheme ;
11+ if ( document . body ) {
12+ document . body . style . colorScheme = resolvedTheme ;
13+ }
14+ }
15+
16+ export default function Root ( { children} : PropsWithChildren ) : JSX . Element {
17+ useEffect ( ( ) => {
18+ if ( ! ExecutionEnvironment . canUseDOM ) {
19+ return undefined ;
20+ }
21+
22+ applyColorScheme ( document . documentElement . getAttribute ( 'data-theme' ) ) ;
23+
24+ const observer = new MutationObserver ( ( ) => {
25+ applyColorScheme ( document . documentElement . getAttribute ( 'data-theme' ) ) ;
26+ } ) ;
27+
28+ observer . observe ( document . documentElement , {
29+ attributes : true ,
30+ attributeFilter : [ 'data-theme' ] ,
31+ } ) ;
32+
33+ return ( ) => observer . disconnect ( ) ;
34+ } , [ ] ) ;
35+
36+ return < > { children } </ > ;
37+ }
You can’t perform that action at this time.
0 commit comments