File tree Expand file tree Collapse file tree 1 file changed +11
-3
lines changed
packages/docusaurus-theme-common/src/contexts Expand file tree Collapse file tree 1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import React, {
1313 useMemo ,
1414 type ReactNode ,
1515} from 'react' ;
16+ import useIsBrowser from '@docusaurus/useIsBrowser' ;
1617import { ReactContextError } from '../utils/reactUtils' ;
1718import { createStorageSlot } from '../utils/storageUtils' ;
1819import { useThemeConfig } from '../utils/useThemeConfig' ;
@@ -124,10 +125,17 @@ function useColorModeState() {
124125 const {
125126 colorMode : { defaultMode} ,
126127 } = useThemeConfig ( ) ;
128+ const isBrowser = useIsBrowser ( ) ;
127129
128- const [ colorMode , setColorModeState ] = useState < ColorMode > ( defaultMode ) ;
129- const [ colorModeChoice , setColorModeChoiceState ] =
130- useState < ColorModeChoice > ( null ) ;
130+ // Since the provider may unmount/remount on client navigation, we need to
131+ // reinitialize the state with the correct values to avoid visual glitches.
132+ // See also https://github.com/facebook/docusaurus/issues/11399#issuecomment-3279181314
133+ const [ colorMode , setColorModeState ] = useState < ColorMode > (
134+ isBrowser ? ColorModeAttribute . get ( ) : defaultMode ,
135+ ) ;
136+ const [ colorModeChoice , setColorModeChoiceState ] = useState < ColorModeChoice > (
137+ isBrowser ? ColorModeChoiceAttribute . get ( ) : null ,
138+ ) ;
131139
132140 useEffect ( ( ) => {
133141 setColorModeState ( ColorModeAttribute . get ( ) ) ;
You can’t perform that action at this time.
0 commit comments