Glitches on navbar when clicking on links and non default theme mode is used (dark / light)
Have you read the Contributing Guidelines on issues?
- [x] I have read the Contributing Guidelines on issues.
Prerequisites
- [x] 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
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
- Go to Docusaurus official page : https://docusaurus.io/
- Set dark mode if it's not your system default
- Click on links in navbar -> see glitches
- Set light mode
- 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.
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 😅
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 ! :)
Ah, thanks, that should help us troubleshoot the problem!
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:
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.
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.
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
}),
);
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/
LGTM so I'm going to merge, but let me know if you still see this problem on our website
Sorry have not seen your message yesterday @slorber : it's ok thanks a lot for your reactivity guys @Josh-Cena !