docusaurus icon indicating copy to clipboard operation
docusaurus copied to clipboard

Glitches on navbar when clicking on links and non default theme mode is used (dark / light)

Open jhereck opened this issue 1 year ago • 9 comments

Have you read the Contributing Guidelines on issues?

Prerequisites

  • [x] I'm using the latest version of Docusaurus.
  • [ ] I have tried the npm run clear or yarn clear command.
  • [ ] I have tried rm -rf node_modules yarn.lock package-lock.json and 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

Hi,

Just notice a problem in my local / staging env, and tried on the official Docusaurus page (url : https://docusaurus.io/) to be sure that it was not a pb on my end.

As you can see on the video below, pb is there's glitches when navigating in navbar when using non default theme (dark or light) : when letting default theme to light mode, when you select dark mode you can see glitches in navbar when clicking on links. If you set default to dark mode, you will experience the same when using light mode.

The video was taken from the official page so you can do the test very easily : i assume default theme mode was not set or set to light cause you've got glitch in dark mode (or system mode when system is dark too) !

Thanks for your help !

https://github.com/user-attachments/assets/286bd1ef-b5c7-457e-8dbb-c677dc81cee6

Reproducible demo

No response

Steps to reproduce

  1. Go to Docusaurus official page : https://docusaurus.io/
  2. Set dark mode if it's not your system default
  3. Click on links in navbar -> see glitches
  4. Set light mode
  5. Click on links in navbar -> no glitches

Expected behavior

No glitches at all, default theme mode selected or not.

Actual behavior

https://github.com/user-attachments/assets/286bd1ef-b5c7-457e-8dbb-c677dc81cee6

Your environment

  • Public source code:
  • Public site URL:
  • Docusaurus version used:
  • Environment name and version (e.g. Chrome 89, Node.js 16.4):
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS):

Self-service

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

jhereck avatar Sep 06 '25 18:09 jhereck

The glitch is due to the logo reloading, and the <img> tag using an initial size of 0, producing a layout shift when the logo SVG loads.

It is related to layout unmounts/remounts (tracked in https://github.com/facebook/docusaurus/issues/2891), and you will notice it doesn't happen when you navigate from/to Docs <-> API (this is because both are docs, and the docs layout is preserved)

Thanks for reporting that the glitch only happens in dark mode. I didn't notice, and this gives me new ideas what could be the cause.

In https://github.com/facebook/docusaurus/issues/8188 I thought specifying an explicit width/height on the logo config would prevent the glitch from happening, but apparently it's still happening 😅

slorber avatar Sep 08 '25 13:09 slorber

Hi @slorber,

Dark mode is not always the guilty, i tried on my dev environnent and if you change default mode in themeConfig (let say you set it light), then dark mode will have glitches when clicking.

If you set default to dark, then you will have glitches on light mode.

Hope it's clear ! :)

jhereck avatar Sep 09 '25 17:09 jhereck

Ah, thanks, that should help us troubleshoot the problem!

slorber avatar Sep 10 '25 07:09 slorber

Image

I'm pausing JavaScript immediately after page load. The problem is that the logo gets themedComponent--light first, and only changes to themedComponent--dark after JS hydration:

Image

So, my hypothesis is that useColorMode() returns "light" on page transitions. I've verified this by adding console.log(colorMode); in ThemedComponent in a local build. As you can see, on route transitions, colorMode changes three times, from dark to light to dark again.

Image

Josh-Cena avatar Sep 10 '25 14:09 Josh-Cena

This might just be because of const [colorMode, setColorModeState] = useState(defaultMode);. So initially JS believes the page is light and HTML believes the page is dark, and JS only reads the HTML and updates itself in a useEffect. Perhaps useLayoutEffect can help here, but I keep getting SSR errors even when using useIsomorphicLayoutEffect.

Josh-Cena avatar Sep 10 '25 14:09 Josh-Cena

Indeed, this is the problem.

Due to https://github.com/facebook/docusaurus/issues/2891 we have the layout that unmounts/remounts when transitioning from one plugin to another.

And the ColorModeProvider that initializes the color mode context is used on the <LayoutProviders> component, so the color mode context useState() is reset, leading to

Until we have a better place to render our <ColorModeProvider>, a solution is to re-init the state with the correct value:

  const isBrowser = useIsBrowser();

  const [colorMode, setColorModeState] = useState<ColorMode>(
    () =>  {
      return isBrowser ? ColorModeAttribute.get() : defaultMode
    }),
  );

slorber avatar Sep 11 '25 08:09 slorber

Can you please test the following PR and tell me if you see any problem?

https://github.com/facebook/docusaurus/pull/11405

https://deploy-preview-11405--docusaurus-2.netlify.app/

slorber avatar Sep 11 '25 08:09 slorber

LGTM so I'm going to merge, but let me know if you still see this problem on our website

slorber avatar Sep 12 '25 17:09 slorber

Sorry have not seen your message yesterday @slorber : it's ok thanks a lot for your reactivity guys @Josh-Cena !

jhereck avatar Sep 12 '25 17:09 jhereck