-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme-classic): auto-collapse sibling categories in doc sidebar (#…
…3811) Co-authored-by: Josh-Cena <[email protected]>
- Loading branch information
1 parent
c9a6c7b
commit 8ce3cee
Showing
10 changed files
with
104 additions
and
16 deletions.
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