mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 04:42:40 +02:00
refactor(docs): theme-common shouldn't depend on docs content (#10316)
This commit is contained in:
parent
d426469608
commit
026a317fc4
51 changed files with 209 additions and 189 deletions
|
@ -1,55 +0,0 @@
|
|||
/**
|
||||
* 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';
|
||||
import {ReactContextError} from '../utils/reactUtils';
|
||||
|
||||
type ContextValue = {
|
||||
/**
|
||||
* The item that the user last opened, `null` when there's none open. On
|
||||
* initial render, it will always be `null`, which doesn't necessarily mean
|
||||
* there's no category open (can have 0, 1, or many being initially open).
|
||||
*/
|
||||
expandedItem: number | null;
|
||||
/**
|
||||
* Set the currently expanded item, when the user opens one. Set the value to
|
||||
* `null` when the user closes an open category.
|
||||
*/
|
||||
setExpandedItem: (a: number | null) => void;
|
||||
};
|
||||
|
||||
const EmptyContext: unique symbol = Symbol('EmptyContext');
|
||||
const Context = React.createContext<ContextValue | typeof EmptyContext>(
|
||||
EmptyContext,
|
||||
);
|
||||
|
||||
/**
|
||||
* Should be used to wrap one sidebar category level. This provider syncs the
|
||||
* expanded states of all sibling categories, and categories can choose to
|
||||
* collapse itself if another one is expanded.
|
||||
*/
|
||||
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(): ContextValue {
|
||||
const value = useContext(Context);
|
||||
if (value === EmptyContext) {
|
||||
throw new ReactContextError('DocSidebarItemsExpandedStateProvider');
|
||||
}
|
||||
return value;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue