-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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(theme-classic): auto-collapse sibling categories in doc sidebar #3811
Merged
slorber
merged 15 commits into
facebook:main
from
softwarecurator:auto-collapse-sidebar-v2
Jan 20, 2022
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
54b0386
Added auto collapsible sidebar
softwarecurator 67c516e
Added new sidebar type
softwarecurator f7fb0be
added documation
softwarecurator 1f3c78d
Updated changes
softwarecurator 2413511
Merged branch master into auto-collapse-sidebar-v2
softwarecurator 8c8edb3
auto collapse changes
softwarecurator 21cb8bb
Merge branch 'main' into auto-collapse-sidebar-v2
Josh-Cena 18a8e31
Fix typing
Josh-Cena b25e2d9
Don't pass autoCollapse as props
Josh-Cena 79f5949
Merge branch 'main' into auto-collapse-sidebar-v2
Josh-Cena 2570f90
work
Josh-Cena 8bc85f8
make it configurable
Josh-Cena 1f480b1
refactor
Josh-Cena 3f99671
docs
Josh-Cena 976dd19
oops
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/docusaurus-theme-common/src/utils/docSidebarItemsExpandedState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {type ReactNode, useMemo, useState, useContext} from 'react'; | ||
|
||
const EmptyContext: unique symbol = Symbol('EmptyContext'); | ||
const Context = React.createContext< | ||
DocSidebarItemsExpandedState | typeof EmptyContext | ||
>(EmptyContext); | ||
type DocSidebarItemsExpandedState = { | ||
expandedItem: number | null; | ||
setExpandedItem: (a: number | null) => void; | ||
}; | ||
|
||
export function DocSidebarItemsExpandedStateProvider({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}): JSX.Element { | ||
const [expandedItem, setExpandedItem] = useState<number | null>(null); | ||
const contextValue = useMemo( | ||
() => ({expandedItem, setExpandedItem}), | ||
[expandedItem], | ||
); | ||
|
||
return <Context.Provider value={contextValue}>{children}</Context.Provider>; | ||
} | ||
|
||
export function useDocSidebarItemsExpandedState(): DocSidebarItemsExpandedState { | ||
const contextValue = useContext(Context); | ||
if (contextValue === EmptyContext) { | ||
throw new Error( | ||
'This hook requires usage of <DocSidebarItemsExpandedStateProvider>', | ||
); | ||
} | ||
return contextValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The only case we have to think twice about UX is when there are multiple sidebar categories expanded by default. In this implementation, the
expandedItem
context is initialized tonull
, and when the user collapses one of those default-expanded categories, the others remain expanded until any category is toggled to be expanded.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’m excited this feature is finally getting some steam
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.
Yes! This is the UX I've been looking forward to, so I did invest some time in this :D
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 haven’t really looked at the code, just a quick glance but I would set the default setting to false, I know not everyone will want this feature on by default, and that was an issue when I first tried to get this in over a year ago 😂
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.
Yes, it's indeed defaulted to false :) Just that it would be cool to be turned on for our website.