mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-10 23:02:56 +02:00
feat(theme-classic): auto-collapse sibling categories in doc sidebar (#3811)
Co-authored-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
parent
c9a6c7b6fb
commit
8ce3cee400
10 changed files with 104 additions and 16 deletions
|
@ -6,6 +6,10 @@
|
|||
*/
|
||||
|
||||
export {useThemeConfig} from './utils/useThemeConfig';
|
||||
export {
|
||||
DocSidebarItemsExpandedStateProvider,
|
||||
useDocSidebarItemsExpandedState,
|
||||
} from './utils/docSidebarItemsExpandedState';
|
||||
|
||||
export type {
|
||||
ThemeConfig,
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -122,6 +122,7 @@ export type ThemeConfig = {
|
|||
prism: PrismConfig;
|
||||
footer?: Footer;
|
||||
hideableSidebar: boolean;
|
||||
autoCollapseSidebarCategories: boolean;
|
||||
image?: string;
|
||||
metadata: Array<Record<string, string>>;
|
||||
sidebarCollapsible: boolean;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue