Have you read the Contributing Guidelines on issues?
Prerequisites
Description
The value returned by the useColorMode hook from @docusaurus/theme-common seems to have a strange behavior when:
- Building for production and/or using React in prod mode (
npm run build && npm run serve).
window.localStorage.theme is 'dark' while the themeConfig.colorMode.defaultMode value is 'light', or window.localStorage.theme is 'light' while the themeConfig.colorMode.defaultMode value is 'dark'.
Then trying to use the colorMode value set the value of DOM attributes ends up generating a DOM with incorrect attribute values:
import { useColorMode } from "@docusaurus/theme-common";
import React from "react";
export default function ColorModeTest() {
const { colorMode } = useColorMode();
return (
<div title={colorMode}>{colorMode}</div>
);
}
In the conditions described above, if the themeConfig.colorMode.defaultMode value is 'light' and the localStorage.theme value is 'dark':
- The generated HTML will be:
<div title="light">light</div>
- The hydrated DOM will be:
<div title="light">dark</div>
This is particularly troublesome when you have UI components that support both dark and light themes and rely on HTML attributes to set the correct color theme on the component itself (e.g. in order to display a light component in a dark context for visual emphasis).
Reproducible demo
https://github.com/fvsch/docusaurus-use-color-mode-stale-value
Steps to reproduce
- Check out https://github.com/fvsch/docusaurus-use-color-mode-stale-value
- Install dependencies, build for prod and serve that build:
npm install && npm run build && npm run serve
- The home page of the demo should be using a light theme.
- Click the theme switching button on the top right to switch to the dark theme.
- Reload the page.
Expected behavior
The values derived from the colorMode, whether they're text nodes or attribute nodes, should be in sync and reflect the localStorage.theme value.
In the last step, the visual result should be:

Actual behavior
Attribute values that are derived from the colorMode value seem to be outdated.
In the last step, the visual result is:

Your environment
Self-service
Have you read the Contributing Guidelines on issues?
Prerequisites
npm run clearoryarn clearcommand.rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages.Description
The value returned by the
useColorModehook from@docusaurus/theme-commonseems to have a strange behavior when:npm run build && npm run serve).window.localStorage.themeis'dark'while thethemeConfig.colorMode.defaultModevalue is'light', orwindow.localStorage.themeis'light'while thethemeConfig.colorMode.defaultModevalue is'dark'.Then trying to use the
colorModevalue set the value of DOM attributes ends up generating a DOM with incorrect attribute values:In the conditions described above, if the
themeConfig.colorMode.defaultModevalue is'light'and thelocalStorage.themevalue is'dark':<div title="light">light</div><div title="light">dark</div>This is particularly troublesome when you have UI components that support both dark and light themes and rely on HTML attributes to set the correct color theme on the component itself (e.g. in order to display a light component in a dark context for visual emphasis).
Reproducible demo
https://github.com/fvsch/docusaurus-use-color-mode-stale-value
Steps to reproduce
npm install && npm run build && npm run serveExpected behavior
The values derived from the
colorMode, whether they're text nodes or attribute nodes, should be in sync and reflect thelocalStorage.themevalue.In the last step, the visual result should be:
Actual behavior
Attribute values that are derived from the
colorModevalue seem to be outdated.In the last step, the visual result is:
Your environment
Self-service