-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(v2): expanded sidebar categories by default #2682
Conversation
Deploy preview for docusaurus-2 ready! Built with commit 4f2e6a4 |
website/docs/docs.md
Outdated
#### Toggling category open by default | ||
|
||
For sites that have collapsible categories, you may want more fine grain control over certain categories. If you want specific categories to be alwasy expanded, you can set `collapsed` to `false`: | ||
|
||
```js title="sidebars.js" | ||
module.exports = { | ||
docs: { | ||
Guides: [ | ||
'creating-pages', | ||
{ | ||
type: 'category', | ||
label: 'Docs', | ||
collapsed: false, | ||
items: ['markdown-features', 'sidebar', 'versioning'], | ||
}, | ||
], | ||
}, | ||
}; | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yangshun I updated the docs - open to suggestions! I wasn't sure if I needed to update any other areas of the docs. Let me know!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine. What's the current default value for the collapsed
field? The default should be true
to preserve backward compatibility.
website/sidebars.js
Outdated
docs: [ | ||
{ | ||
type: 'category', | ||
label: 'Docusaurus', | ||
collapsed: false, | ||
items: ['introduction', 'design-principles', 'contributing'], | ||
}, | ||
{ | ||
type: 'category', | ||
label: 'Getting Started', | ||
collapsed: true, | ||
items: ['installation', 'configuration'], | ||
}, | ||
{ | ||
type: 'category', | ||
label: 'Guides', | ||
collapsed: true, | ||
items: [ | ||
'creating-pages', | ||
'styling-layout', | ||
'static-assets', | ||
{ | ||
Docs: ['docs', 'markdown-features', 'versioning'], | ||
}, | ||
'blog', | ||
'search', | ||
'deployment', | ||
'migrating-from-v1-to-v2', | ||
], | ||
}, | ||
{ | ||
type: 'category', | ||
label: 'Advanced Guides', | ||
collapsed: true, | ||
items: ['using-plugins', 'using-themes', 'presets'], | ||
}, | ||
{ | ||
type: 'category', | ||
label: 'API Reference', | ||
collapsed: true, | ||
items: [ | ||
'cli', | ||
'docusaurus-core', | ||
'docusaurus.config.js', | ||
'lifecycle-apis', | ||
'theme-classic', | ||
], | ||
}, | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lex111 is this what you had in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsjoeio Yes, thanks, this is exactly what I had in mind, however, to achieve this it is necessary to change the sidebar structure (use an array instead of an object), but ok.
Hmm, should we highlight expanded by default category? In my understanding, we need to highlight the category in which we are currently (based on the current path).
For example, I am on the Guides -> Docs page, everything is fine, two related items (Guides and Docs-Introduction) are highlighted in green.
But at the same time, we also see that the top-level item with Docusaurus is also highlighted as activated. I don’t know for sure, but it seems that similar behavior could lead people astray so we don’t need to highlight the category of sidebar, which are expanded by default.
Any objections? cc @yangshun
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should only highlight the active one. Opening/close doesn't result in highlighting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see!
Okay, I assume that's part of this work then - if you want to request changes on this PR, then I get started and ping you when it's ready for re-review @lex111
I made a flow chart to understand the logic behind the category highlighting. I also found the place where I need to modify this logic: |
packages/docusaurus-theme-classic/src/theme/DocSidebar/index.js
Outdated
Show resolved
Hide resolved
@@ -48,7 +68,8 @@ function DocSidebarItem({item, onItemClick, collapsible, ...props}) { | |||
<a | |||
className={classnames('menu__link', { | |||
'menu__link--sublist': collapsible, | |||
'menu__link--active': collapsible && !item.collapsed, | |||
'menu__link--active': | |||
collapsible && !item.collapsed && isCategoryOfActivePage(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I love the function call here, might be better to create a new variable and assign it to a function call and then use it here. Open to other ideas too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be better to make the function a pure one and pass in the require parameters. That makes it easier to test and move around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! I'll refactor that
packages/docusaurus-theme-classic/src/theme/DocSidebar/index.js
Outdated
Show resolved
Hide resolved
packages/docusaurus-theme-classic/src/theme/DocSidebar/index.js
Outdated
Show resolved
Hide resolved
Hmm, we have a little regression behavior compared to the current version. If you open other categories and try to go to any page in them, they will not automatically collapse as earlier (see screenshot) Before: After (it is expected that only the Docusaurus category should always be expanded after go to another category): I don’t know how critical this bug is or not, I want to hear the opinion from @yangshun. Maybe we should adopt a new behavior (when the categories do not collapse after navigating to another category). |
Ah! I didn't consider that. I'll wait to hear from @yangshun before making any changes |
The reason we had the initial behavior is because if users opened a category that's nearer to the bottom of the sidebar, they would have to scroll down before they can access the new one. Collapsing all others categories helps a little bit with the issue of reducing the non-relevant vertical space needed. That said, this is not critical and I think there's no common industry practice. Spark AR docs has our current behavior but [Android docs]. Another issue that we might face here if we decide to stick with the current behavior is what we should do about the categories which are open by default but user decides to click on some other category. Do we close them or keep them open? If we keep them open that logic could be harder to maintain. I'd prefer to stick with the current behavior if possible, assuming we have a feasible solution to the issue I raised above. WDYT? |
I agree with you @yangshun . I prefer to keep the current behavior. Let me investigate to see if I can fix this. |
@jsjoeio nope, we just renamed the document with id "docs" to "docs-introduction" docusaurus/website/sidebars.js Line 17 in d8ebe8b
|
Hmmm.. okay after a few hours of investigating, I don't think it's possible (but happy to be proven otherwise) due to these two facts:
Ideal World
Regression -> Better UX?I could be convinced otherwise, but I feel like the new behavior is fine, if not better for the user? As a user, here's a peek into my thoughts: In this case, I am glad 'Getting Started' didn't automatically collapse because I was peeking around. SummaryLeave it as is and "adopt a new behavior (categories do not collapse after navigating to another category)." WDYT? (no strong opinion here) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried it out, it works great! Thanks @jsjoeio! I removed the setting of collapsed
on every sidebar item since it's redundant and I also wanted to test the default behavior.
Let's go with this new UX and label it as a breaking change. We'll revisit this UX if people complain :)
FWIW nextjs.org docs also doesn't collapse the other categories.
Good call! Glad to hear everything still works as expected!
Good idea! And if they complain, send them my way, and I'm happy to work with you and the team to get something figured it that keeps everyone happy :) Thank you @yangshun (and thanks a ton for the internal feedback! ❤️) and @lex111 for the help along the way! It felt really good working on this. Happy to see it merged! Cheers! 🎉 |
@jsjoeio @yangshun Unfortunately, this feature brings with it an unpleasant effect. During server prerendering, all categories are expanded, this is noticeable when the page loads: the categories are displayed as expanded for a while, and when the page is loaded they are already collapsed (as it should be initially). This is kind of FOUC, as we had with dark mode, but now a similar bug is observed with doc categories. See gif below: As it was before: You can see for yourself if you open these pages and try reloading them. Current behavior - https://v2.docusaurus.io/docs/next/migrating-from-v1-to-v2/ To fix this bug, we need to render the final result upon initial render, and not only after mounting. @jsjoeio could you look at that? |
@slorber do you want to help with this? |
yes, I'll take a look at how to fix this |
Should be fixed by #2867 |
* bug(v2): fix active sidebar item detection logic (#2682 (comment)) * fix sidebar category collapsed normalization to make sure we always have a boolean after normalization * fix sidebarCollapsible option
@slorber is it possible to completely disable and hide the collapse button on the bottom of the side bar? I didn't find the flag in the config |
Motivation
This allows a user to toggle a category open by default.
Have you read the Contributing Guidelines on pull requests?
Yes.
Test Plan
Thanks to the tip from @lex111 I tested this locally on the Docusaurus website by modifying website/versioned_sidebars/version-2.0.0-alpha.40-sidebars.json and the changes worked as expected.
Screenrecording
Updated: only highlight the category if the category belongs to the current page being viewed

Related PRs
Remaining todos