-
-
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
[v2] Option to toggle docs category open by default #2354
Comments
Not sure if this really improves ux. If I'm a new user to the site and by default the references section is open, I'd probably find it confusing because I need to know the introduction docs first before getting into other details. The scenario mentioned in the original post is probably an example but it feels more like content organization issue rather than needing a feature from the ssg tool. Docusaurus also lets you create multiple sidebars that contains different navigation. That could also be an option depending the actual use case. |
I would keep this. I think this option is feasible. It doesn't add much
complexity in terms of Engineering and gives people more flexibility
without causing confusion.
…On Wed, Mar 18, 2020, 3:07 PM Asier Illarramendi ***@***.***> wrote:
Closed #2354 <#2354>.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2354 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAKBCHI4JWTBQBP5FDER7Y3RIBXJLANCNFSM4LA7QE2A>
.
|
WIP ApproachThese are some rough notes on how I'm thinking of approaching this. Note: they are more meant for me so you can ignore them if you're watching this issue.
NotesHard to tell exactly where the code related to categories being open or not lives, but here's the trail I'm following. The actual component is const [collapsed, setCollapsed] = useState(item.collapsed) Now the hard part is walking up the tree to figure out where Random thought: why even be able to toggle them?? Shouldn't they always be open? Not sure. Random thought: what is "swizzling" components? Okay I got it. Maybe I could swizzle the component ResourcesHere are the relevant files:
|
I was able to swizzle Next is how to implement this? Suggested approach:Modify the Sidebar object. Currently, it looks like this: export interface SidebarItemCategory {
type: 'category';
label: string;
items: SidebarItem[];
}
export interface SidebarItemCategoryRaw {
type: 'category';
label: string;
items: SidebarItemRaw[];
} New proposed interfaces: export interface SidebarItemCategory {
type: 'category';
label: string;
items: SidebarItem[];
+ collapsed?: boolean;
}
export interface SidebarItemCategoryRaw {
type: 'category';
label: string;
items: SidebarItemRaw[];
+ collapsed?: boolean;
} This will allow a user to create a module.exports = {
someSidebar: [
{
type: 'category',
label: 'Introduction',
items: ['doc1', 'doc2', 'doc3'],
},
{
type: 'category',
label: 'Reference',
items: ['mdx'],
collapsed: false,
},
],
}; |
Todos
|
Ah, I had to update a few functions:
Snapshot written ✅ |
Additional suggestion: we should link to the |
TIL: you can publish learna to verdaccio like so yarn lerna publish --registry http://localhost:4873 |
Wow...I forgot that before |
Okay this needs to be documented somewhere: Testing your changes locally
If you need to test something like a new docusaurus site,
npm i -g @docusaurus/init --registry http://localhost:4873 This will install from your verdaccio registry @docusaurus/init init my-website classic Update: I don't know if that's right... |
Hmm...now I'm not sure if this is actually the right way to test things locally. Talking out loud here... I made changes to What I should do is create a new docusaurus site that uses the
I think that would work because I would be installing the theme-classic from my local registry? |
To be honest, I'm not sure if that worked...I created a new website with I'm worried that somehow when I run |
Suggestion, the swizzle section of the docs should have an example: yarn swizzle @docusaurus/theme-classic DocSidebar |
Okay I think I'm getting somewhere...I set up a new site, swizzled the module.exports = {
someSidebar: {
"Test": [
{
"type": "category",
"label": "Introduction",
"items": ["doc1"],
"collapsed": false
}
],
"Reference": [
{
"type": "category",
"label": "Powering MDX",
"items": ["doc2"],
"collapsed": false
}
]
}
} And I'm getting the error: Error: Unknown sidebar item keys: collapsed. Item: {"type":"category","label":"Introduction","items":["doc1"],"collapsed":false}
at assertItem (/Users/jprevite/dev/testing/my-website-swizzled/node_modules/@docusaurus/plugin-content-docs/lib/sidebars.js:34:15) This is good! Now, if I try what I tried above (using my local package), and restart, the error should go away. UpdateLooks like it did! The functionality isn't fully working, but at least we know the logic related to adding that new key worked! |
Continuing to test with my changes...one thing I'm noticing is that the |
Follow the trail
|
Ah-HA! I think it is here that it is happening: function normalizeCategoryShorthand(
sidebar: SidebarCategoryShorthandRaw,
): SidebarItemCategoryRaw[] {
return Object.entries(sidebar).map(([label, items]) => ({
type: 'category',
collapsed: true,
label,
items,
}));
} This means |
Okay next step is to figure out how to pass down the correct value from the |
WOW! The issue was right in front of my face. function mutateSidebarCollapsingState(item, path) {
// ...logic
item.collapsed = !anyChildItemsActive
} I'll need to find a workaround in this |
Talking out loud here...we have a function
That means when we load the page, expand the categories on the current page. Cool! I always find it helpful to use concrete examples. Let's do that to understand what we should implement. Example 1I have a site. I have a collapsible sidebar. Here is what I want:
So what does that look like in pseudologic // if (isChildItem for curentPage, or the category.collapsed is false) {
// expand
} |
Let's try this out. Context// sidebars.js
module.exports = {
someSidebar:
[
{
"type": "category",
"label": "Introduction",
"items": ["doc1"],
"collapsed": true
},
{
"type": "category",
"label": "Powering MDX",
"items": ["doc2"],
"collapsed": false
}
]
} "Powering MDX" should not be collapsed because it's set to BeforeThis is what happens before I made any changes. (note: I load the page, "Powering MDX" is collapsed). AfterI made the change and now it loads and is expanded (not collapsed) like I want! And, if I'm on the "Powering MDX", it should be expanded (because it's the current page), which still works as expected. Notice how "Introduction" is not expanded by default because Code changesfunction mutateSidebarCollapsingState(item, path) {
+ // only modify item.collapsed if there are child items active and the category collapsed is set to true
+ // eslint-disable-next-line no-param-reassign
+ if (anyChildItemsActive && item.collapsed) {
+ item.collapsed = !anyChildItemsActive;
+ }
} Next steps
|
yarn lerna publish --registry http://localhost:4873 --no-git-tag-version NOTE: this pushes a new git tag 😅 Which is not desired, you can disable with the flag https://github.com/lerna/lerna/tree/master/commands/version#--no-git-tag-version |
Now, I am unsure if this works as expected...
I mean it worked to test changes to a plugin |
Going a different route: npm set registry http://localhost:4873/ Let's see if this works |
Hmm...this is super ugly but it got the right versions: # set the registry to verdaccio
npm set registry http://localhost:4873/
# install @docusaurus/init globally
npm i -g @docusaurus/init
# run the binary from the location directly
/Users/jprevite/.nvm/versions/node/v10.15.1/lib/node_modules/@docusaurus/init/bin/index.js init test-joe |
Last thing to do is to update the documentation! 🎉 Then I can open a PR |
@lex111 seems like we should discuss what went wrong in the implementation and what new approach we should take moving forward |
@jsjoeio in short, this line of code does not need to be changed. I would add an additional condition under which item must be expanded.
Please see the docs https://v2.docusaurus.io/docs/next/docs#collapsible-categories I recommend checking the changes directly on the v2 website, rather than creating a new one. Other than that, you don’t need to use Verdaccio for such changes at all, it only takes more effort from you. |
Awesome, thanks for the tip!
Ah, didn't think of that. I'll give that a try! I'll see if I can work on this next week and submit a new PR. |
🚀 Feature
Add a new property on the
@docusaurus/plugin-content-docs
SidebarItemCategory
to set the default state as opened. This should affect only to that one category, no all the categories on the docs.Have you read the Contributing Guidelines on issues?
Yes.
Motivation
Improve UX.
Pitch
Imagine docs with just two root elements:
Where
Reference
is a category with few other documents inside. This would allow to have thisReference
category open by default which would be easier for the user to navigate/explore.The text was updated successfully, but these errors were encountered: