-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I'm using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
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.themeis'dark'while thethemeConfig.colorMode.defaultModevalue is'light', orwindow.localStorage.themeis'light'while thethemeConfig.colorMode.defaultModevalue 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
- Public source code: https://github.com/fvsch/docusaurus-use-color-mode-stale-value/blob/main/src/components/ColorModeTest/index.js
- Public site URL: n.a. (but currently affects https://developer.stackblitz.com/ too)
- Docusaurus version used: 2.0.1
- React version used: 17.0.2
- Environment name and version (e.g. Chrome 89, Node.js 16.4): all browsers tested (Firefox, Chrome and Safari); Node 16.17.0.
- Operating system and version (e.g. Ubuntu 20.04.2 LTS): macOS 12.5, probably happens on Linux too (happens when building our site on CI); haven't tried on Windows but I don't expect a difference here.
Self-service
- I'd be willing to fix this bug myself.

