Skip to content

Changing the system theme resets the Docusaurus theme to the default #8938

Closed

Description

@MageJohn

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

When the system colour theme changes (the prefers-color-scheme media query changes, that is), a Docusaurus site with respectPrefersColorScheme off sets the theme back to the default, regardless of the system theme or the current site theme as set by the user.

Reproducible demo

https://codesandbox.io/p/sandbox/github/facebook/docusaurus/tree/main/examples/classic

Steps to reproduce

A fresh installation of Docusaurus should work, or the codesandbox.io link above.

  1. Put the system in light mode (steps vary based on OS; in essence, make sure the media query prefers-color-scheme: light is true)
  2. Click the theme change button to set the Docusaurus site to dark mode
  3. Put the system into dark mode (prefers-color-scheme: dark is true)

Expected behavior

Given that respectPrefersColorScheme is off, I'd expect the theme not to change when the system colour scheme changes.

Actual behavior

The theme is reset to the default – light mode in the case of the repro link.

Your environment

  • Public source code: See the repro link above
  • Public site URL: In addition to the repro, I first discovered this issue when using docs.cypress.io
  • Docusaurus version used: 2.4.0
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): Firefox 112.0.2
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): macOS 13.3.1

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on Apr 27, 2023
  2. vennilamahalingam commented on Jul 15, 2023

    @vennilamahalingam

    From initial debugging - Irrespective of respectPrefersColorScheme value, every time the system theme is changed, the localstorage value is deleted and so default value(light theme) is set.

    May I work on this ?

  3. vennilamahalingam commented on Jul 15, 2023

    @vennilamahalingam

    On further deep dive in the codebase, this proves to be a conscious and expected behavior.
    It seems to have been agreed by the team that the OS triggered theme changes overrides the user choice - mentioned by @Josh-Cena
    More on #7200
    This issue is contrary to the above discussion, hence this issue could be closed.

  4. Josh-Cena commented on Jul 15, 2023

    @Josh-Cena
    Collaborator

    The problem is that there exists a user action here (setting the mode to dark), so I'd expect it to persist. It's the exact opposite situation as in #7199 (where the button is disabled but respectPrefersColorScheme is on)

  5. vennilamahalingam commented on Jul 15, 2023

    @vennilamahalingam

    The problem is that there exists a user action here (setting the mode to dark), so I'd expect it to persist.

    #7200 (comment) - this thread here seemed to have agreed on with prioritizing the OS triggered color even when user action is setting to dark mode and also about persist option being unused.
    What am I missing here ?

    As per what I understand from code, if and when there is a change in the system color mode, window.matchMedia onchange is triggered and the setColorMode(null); resets to default mode which is light.

  6. MageJohn commented on Jul 16, 2023

    @MageJohn
    Author

    I think there's scope for improving the behaviour, even if it was intended. To me it doesn't make sense that changing the system theme at all should always put a site into light mode.

  7. Josh-Cena commented on Jul 16, 2023

    @Josh-Cena
    Collaborator

    Mmm, I think the key is respectPrefersColorScheme is off here, so regardless we shouldn't do anything about system changes. I don't really know though—it's been a while since I've handled this and my memory is all a bit blurry.

  8. added
    domain: theme
    and removed
    status: needs triage
    on Jul 19, 2024
  9. slorber commented on Oct 16, 2026

    @slorber
    Collaborator

    This no longer reproduces with Docusaurus 4.0.0-canary-6975, nor with the latest stable 3.10.2.

    It was fixed by #10987, released in Docusaurus 3.8.0. The site now only follows OS color scheme changes when respectPrefersColorScheme: true and the user hasn't picked a color mode, and these changes are never persisted. Before that, every OS change called setColorMode(null), which reset the site to defaultMode and deleted the stored choice.

    Repro: https://github.com/slorber/docusaurus-repros/tree/issue-8938

    • A minimal site with the issue's config (respectPrefersColorScheme: false, defaultMode: 'light').
    • With the OS in light mode, pick dark with the toggle, then switch the OS to dark and back to light: the site stays dark, localStorage keeps the choice, and it's still dark after a reload.
    • The README shows how to see the original bug with 3.7.0. I reproduced it there and on 2.4.0: the first OS change turns the site light and removes the stored theme key.
    • With respectPrefersColorScheme: true, an explicit light/dark choice also survives OS changes. Only the "system" toggle state follows the OS live.

    I checked this in headless Chromium, emulating prefers-color-scheme changes with Playwright.

    (AI-assisted)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdomain: theme

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions